Exemple #1
0
def _get_hdds(vm_):
    """
    Construct VM hdds from cloud profile config
    """
    _hdds = config.get_cloud_config_value(
        "hdds", vm_, __opts__, default=None, search_global=False
    )

    hdds = []

    for hdd in _hdds:
        hdds.append(Hdd(size=hdd["size"], is_main=hdd["is_main"]))

    return hdds
Exemple #2
0
def _get_hdds(vm_):
    '''
    Construct VM hdds from cloud profile config
    '''
    _hdds = config.get_cloud_config_value('hdds',
                                          vm_,
                                          __opts__,
                                          default=None,
                                          search_global=False)

    hdds = []

    for hdd in _hdds:
        hdds.append(Hdd(size=hdd['size'], is_main=hdd['is_main']))

    return hdds
    def test_add_hdd(self):
        with open('mock-api/add-hdd.json') as f:
            data = json.load(f)

        server_id = data['id']
        hdd1 = Hdd(size=40, is_main=False)
        hdds = [hdd1]

        responses.add(responses.POST,
                      'https://cloudpanel-api.1and1.com/v1/servers/%s/hardware/hdds' % server_id,
                      body=json.dumps(data), status=202,
                      content_type="application/json")

        r = self.client.add_hdd(server_id=server_id, hdds=hdds)

        self.assertEqual(r['status']['state'], 'CONFIGURING')
    def test_create_server(self):
        with open('mock-api/create-server.json') as f:
            data = json.load(f)

        server1 = Server(name=data['name'],
                         description=data['description'],
                         vcore=data['hardware']['vcore'],
                         cores_per_processor=data['hardware'][
                             'cores_per_processor'],
                         ram=data['hardware']['ram'],
                         appliance_id='APPLIANCE_ID',
                         server_type='cloud')
        hdd1 = Hdd(size=40, is_main=True)
        hdds = [hdd1]

        responses.add(responses.POST,
                      'https://cloudpanel-api.1and1.com/v1/servers',
                      body=json.dumps(data), status=202,
                      content_type="application/json")

        r = self.client.create_server(server=server1, hdds=hdds)

        self.assertEqual(r['name'], server1.specs['name'])
        self.assertEqual(r['status']['state'], 'DEPLOYING')
Exemple #5
0
# Create a cloud server
from oneandone.client import OneAndOneService
from oneandone.client import Server, Hdd

client = OneAndOneService('API-TOKEN')

server1 = Server(name='Test Server',
                 description='Server Description',
                 vcore=1,
                 cores_per_processor=1,
                 ram=2,
                 appliance_id='',
                 server_type='cloud')

hdd1 = Hdd(size=120, is_main=True)
hdds = [hdd1]

new_server = client.create_server(server=server1, hdds=hdds)

# Create a baremetal server
from oneandone.client import OneAndOneService
from oneandone.client import Server, Hdd

client = OneAndOneService('API-TOKEN')

server1 = Server(name='Test Server',
                 description='Server Description',
                 vcore=1,
                 cores_per_processor=1,
                 ram=2,
client = OneAndOneService(token)
print "creating bare metal server"
server_appliances = client.list_appliances(q='baremetal')

baremetal_model = client.list_baremetal_models(q='BMC_L')


# Create a server
server1 = Server(name='Python baremetal Server',
                 description='Server Description',
                 server_type='baremetal',
                 baremetal_model_id=baremetal_model[0]['id'],
                 appliance_id=server_appliances[0]['id']
                 )

hdd1 = Hdd(size=120, is_main=True)
hdds = [hdd1]
new_server = client.create_server(server=server1)
print server1.wait_for()

# Add a new IP to a server.
print "add a new IP to the server"
response = client.add_new_ip(server_id=new_server['id'], ip_type='IPV4')
print server1.wait_for()

# Modify a server
print "Rename server"
response = client.modify_server(server_id=new_server['id'], name='New baremetal Name',
                                description='New Description')
print server1.wait_for()
Exemple #7
0
    def spawnMachines(self, machineType, requested):
        """
        spawn VMs for 1and1 cloud service
        :param machineType:
        :param requested:
        :return: count
        """
        # check if machine type is requested machine type
        if not machineType == list(self.getConfig(self.configMachines).keys())[0]:
            return

        # check if spawning is allowed
        start_time = datetime.datetime.strptime(self.getConfig(self.configTimeStart), "%H:%M").time()
        drain_time = datetime.datetime.strptime(self.getConfig(self.configTimeDrain), "%H:%M").time()
        current_time = datetime.datetime.now().time()
        # if current_time < start_time and current_time > drain_time:
        if drain_time < current_time < start_time:
            self.logger.info("Request not permitted due to limitations: Booting VMs begins at %s" % start_time)
            return

        # check if requested number of VMs is higher than the allowed number of machines per cycle
        # and if so, limit it to the allowed number
        if requested > self.getConfig(self.configMaxMachinesPerCycle):
            self.logger.info("Request exceeds maximum number of allowed machines per cycle on this site (%d>%d)!" %
                             (requested, self.getConfig(self.configMaxMachinesPerCycle)))
            requested = self.getConfig(self.configMaxMachinesPerCycle)
            self.logger.info("Will spawn %d machines." % requested)

        # get list of all shut down machines
        # to start the shutdown servers at first
        machines_down = self.mr.getMachines(status=self.mr.statusDown)
        if requested > len(machines_down) > 0:
            requested = len(machines_down)
        for mid in list(machines_down.keys())[0:requested]:
            # start the machine
            try:
                vm = machines_down[mid]
                self.modifyMachineStatus(mid, self.command_power_on)
                self.mr.updateMachineStatus(mid, self.mr.statusBooting)
                vm[self.reg_site_server_status] = self.state_powered_off
            except Exception as excpetion:
                self.logger.info(excpetion)
                return
            requested -= 1

        # check if any other machines are needed
        if requested == 0:
            return

        # if so check if the the amount of requested machines would exceed the number of allowed machines
        # and limit if necessary
        if (requested + self.cloudOccupyingMachinesCount) > self.getConfig(self.configMaxMachines):
            self.logger.info(
                "Number of requested machines would exceed number of allowed machines (%d>%d). Will request %d machines" % requested + self.cloudOccupyingMachinesCount,
                self.getConfig(self.configMaxMachines),
                self.getConfig(self.configMaxMachines) - self.cloudOccupyingMachinesCount)
            requested = self.getconfig(self.configMaxMachines) - self.cloudOccupyingMachinesCount

        # get a list of unused indices
        try:
            oao_machines = self.getOneAndOneMachines()
        except Exception as exception:
            return
        free_index = self.getFreeIndex(oao_machines, requested)

        # request new machines
        for count in range(requested):
            # assign vm name based on the generated free index list
            vm_name = self.getConfig(self.configPrefix) + free_index.pop(0)
            # get the IDs
            appliance_id, datacenter_id, network_id = self.getIDs()
            # initialize the machine
            server = Server(name=vm_name,
                            appliance_id=appliance_id,
                            datacenter_id=datacenter_id,
                            vcore=self.getConfig(self.configVcores),
                            cores_per_processor=self.getConfig(self.configCoresPerProcessor),
                            ram=self.getConfig(self.configRam),
                            firewall_policy_id=self.getConfig(self.configFirewallPolicyID),
                            monitoring_policy_id=self.getConfig(self.configMonitoringPolicyID),
                            power_on=False
                            )
            # init Hdd
            hdd = Hdd(size=self.getConfig(self.configHddSize), is_main=True)
            hdds = [hdd]
            # request machine at 1and1
            try:
                client = self.getOneAndOneClient()
                vm = client.create_server(server=server, hdds=hdds)
            except Exception as exception:
                self.logger.warning("Could not start server on OneAndOne Cloud Service:\n%s\nTrying again next cycle."
                                    % exception)
                return

            # create new machine in machine registry
            mid = self.mr.newMachine()

            # set some machine specific entries in machine registry
            self.mr.machines[mid][self.mr.regSite] = self.siteName
            self.mr.machines[mid][self.mr.regSiteType] = self.siteType
            self.mr.machines[mid][self.mr.regMachineType] = machineType
            self.mr.machines[mid][self.reg_site_server_name] = vm[self.name]
            self.mr.machines[mid][self.reg_site_server_id] = vm[self.id]
            self.mr.machines[mid][self.reg_site_server_status] = vm[self.status][self.state]
            self.mr.machines[mid][self.reg_site_server_datacenter] = datacenter_id
            self.mr.machines[mid][self.reg_site_server_network] = network_id
            self.mr.machines[mid][self.reg_site_server_condor_name] = str()

            # update machine status
            self.mr.updateMachineStatus(mid, self.mr.statusBooting)

        return