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')
from oneandone.client import OneAndOneService client = OneAndOneService('API-TOKEN') snapshots = client.list_server_snapshots(server_id='') # 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')
def _get_server(vm_): ''' Construct server instance from cloud profile config ''' description = config.get_cloud_config_value('description', vm_, __opts__, default=None, search_global=False) ssh_key = load_public_key(vm_) server_type = config.get_cloud_config_value('server_type', vm_, __opts__, default='cloud', search_global=False) vcore = None cores_per_processor = None ram = None fixed_instance_size_id = None baremetal_model_id = None if 'fixed_instance_size' in vm_: fixed_instance_size = get_size(vm_) fixed_instance_size_id = fixed_instance_size['id'] elif 'vm_core' in vm_ and 'cores_per_processor' in vm_ and 'ram' in vm_ and 'hdds' in vm_: vcore = config.get_cloud_config_value('vcore', vm_, __opts__, default=None, search_global=False) cores_per_processor = config.get_cloud_config_value( 'cores_per_processor', vm_, __opts__, default=None, search_global=False) ram = config.get_cloud_config_value('ram', vm_, __opts__, default=None, search_global=False) elif 'baremetal_model_id' in vm_ and server_type == 'baremetal': baremetal_model_id = config.get_cloud_config_value( 'baremetal_model_id', vm_, __opts__, default=None, search_global=False) else: raise SaltCloudConfigError( "'fixed_instance_size' or 'vcore', " "'cores_per_processor', 'ram', and 'hdds' " "must be provided for 'cloud' server. " "For 'baremetal' server, 'baremetal_model_id'" "must be provided.") appliance_id = config.get_cloud_config_value('appliance_id', vm_, __opts__, default=None, search_global=False) password = config.get_cloud_config_value('password', vm_, __opts__, default=None, search_global=False) firewall_policy_id = config.get_cloud_config_value('firewall_policy_id', vm_, __opts__, default=None, search_global=False) ip_id = config.get_cloud_config_value('ip_id', vm_, __opts__, default=None, search_global=False) load_balancer_id = config.get_cloud_config_value('load_balancer_id', vm_, __opts__, default=None, search_global=False) monitoring_policy_id = config.get_cloud_config_value( 'monitoring_policy_id', vm_, __opts__, default=None, search_global=False) datacenter_id = config.get_cloud_config_value('datacenter_id', vm_, __opts__, default=None, search_global=False) private_network_id = config.get_cloud_config_value('private_network_id', vm_, __opts__, default=None, search_global=False) power_on = config.get_cloud_config_value('power_on', vm_, __opts__, default=True, search_global=False) public_key = config.get_cloud_config_value('public_key_ids', vm_, __opts__, default=None, search_global=False) # Contruct server object return Server(name=vm_['name'], description=description, fixed_instance_size_id=fixed_instance_size_id, vcore=vcore, cores_per_processor=cores_per_processor, ram=ram, appliance_id=appliance_id, password=password, power_on=power_on, firewall_policy_id=firewall_policy_id, ip_id=ip_id, load_balancer_id=load_balancer_id, monitoring_policy_id=monitoring_policy_id, datacenter_id=datacenter_id, rsa_key=ssh_key, private_network_id=private_network_id, public_key=public_key, server_type=server_type, baremetal_model_id=baremetal_model_id)
def _get_server(vm_): """ Construct server instance from cloud profile config """ description = config.get_cloud_config_value( "description", vm_, __opts__, default=None, search_global=False ) ssh_key = load_public_key(vm_) vcore = None cores_per_processor = None ram = None fixed_instance_size_id = None if "fixed_instance_size" in vm_: fixed_instance_size = get_size(vm_) fixed_instance_size_id = fixed_instance_size["id"] elif vm_["vcore"] and vm_["cores_per_processor"] and vm_["ram"] and vm_["hdds"]: vcore = config.get_cloud_config_value( "vcore", vm_, __opts__, default=None, search_global=False ) cores_per_processor = config.get_cloud_config_value( "cores_per_processor", vm_, __opts__, default=None, search_global=False ) ram = config.get_cloud_config_value( "ram", vm_, __opts__, default=None, search_global=False ) else: raise SaltCloudConfigError( "'fixed_instance_size' or 'vcore'," "'cores_per_processor', 'ram', and 'hdds'" "must be provided." ) appliance_id = config.get_cloud_config_value( "appliance_id", vm_, __opts__, default=None, search_global=False ) password = config.get_cloud_config_value( "password", vm_, __opts__, default=None, search_global=False ) firewall_policy_id = config.get_cloud_config_value( "firewall_policy_id", vm_, __opts__, default=None, search_global=False ) ip_id = config.get_cloud_config_value( "ip_id", vm_, __opts__, default=None, search_global=False ) load_balancer_id = config.get_cloud_config_value( "load_balancer_id", vm_, __opts__, default=None, search_global=False ) monitoring_policy_id = config.get_cloud_config_value( "monitoring_policy_id", vm_, __opts__, default=None, search_global=False ) datacenter_id = config.get_cloud_config_value( "datacenter_id", vm_, __opts__, default=None, search_global=False ) private_network_id = config.get_cloud_config_value( "private_network_id", vm_, __opts__, default=None, search_global=False ) power_on = config.get_cloud_config_value( "power_on", vm_, __opts__, default=True, search_global=False ) public_key = config.get_cloud_config_value( "public_key_ids", vm_, __opts__, default=True, search_global=False ) # Contruct server object return Server( name=vm_["name"], description=description, fixed_instance_size_id=fixed_instance_size_id, vcore=vcore, cores_per_processor=cores_per_processor, ram=ram, appliance_id=appliance_id, password=password, power_on=power_on, firewall_policy_id=firewall_policy_id, ip_id=ip_id, load_balancer_id=load_balancer_id, monitoring_policy_id=monitoring_policy_id, datacenter_id=datacenter_id, rsa_key=ssh_key, private_network_id=private_network_id, public_key=public_key, )
from oneandone.client import OneAndOneService from oneandone.client import Server, Hdd token = os.getenv('ONEANDONE_TOKEN') 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"
rule1 = FirewallPolicyRule(protocol='TCP', port_from=80, port_to=80, source='0.0.0.0') rules = [rule1] new_firewall = client.create_firewall_policy(firewall_policy=fp1, firewall_policy_rules=rules) ## Wait for Firewall Policy to go live print 'Creating firewall policy...' print fp1.wait_for() # Create Server server1 = Server(name='Example App Server', fixed_instance_size_id='65929629F35BBFBA63022008F773F3EB', appliance_id='6C902E5899CC6F7ED18595EBEB542EE1', datacenter_id='5091F6D8CBFEF9C26ACE957C652D5D49') new_server = client.create_server(server=server1) ## Wait for the Server to go live print 'Creating new server...' print server1.wait_for() # Add Load Balancer to New Server lb_response = client.add_load_balancer(server_id=new_server['id'], ip_id=server1.first_ip['id'], load_balancer_id=new_load_balancer['id']) ## Wait for Load Balancer to be added print 'Adding load balancer to Server...'
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
from oneandone.client import Server, Hdd token = os.getenv('ONEANDONE_TOKEN') client = OneAndOneService(token) server_appliances = client.list_appliances(q='ubuntu') for appliance in server_appliances: if appliance['type'] == "IMAGE": break # Create a Server server1 = Server(name='Test Server12', description='Server Description', vcore=1, cores_per_processor=1, ram=2, appliance_id=appliance['id'], server_type='cloud') hdd1 = Hdd(size=120, is_main=True) hdds = [hdd1] new_server = client.create_server(server=server1, hdds=hdds) print server1.wait_for() serverResp = client.get_server(server_id=new_server['id']) # Create a firewall fp1 = FirewallPolicy(name='Python Test Firewall Policy12',
## Wait for Firewall Policy to go live print 'Creating firewall policy...' print fp1.wait_for() # Preapare hdds hdds = [] hdd1 = Hdd(size=40, is_main=True) hdd2 = Hdd(size=20, is_main=False) hdds.append(hdd1) hdds.append(hdd2) # Create Server server1 = Server(name='Example App Server', vcore=2, cores_per_processor=1, ram=2, appliance_id='C5A349786169F140BCBC335675014C08', datacenter_id='5091F6D8CBFEF9C26ACE957C652D5D49') new_server = client.create_server(server=server1, hdds=hdds) ## Wait for the Server to go live print 'Creating new server...' print server1.wait_for() # Add Load Balancer to New Server lb_response = client.add_load_balancer( server_id=new_server['id'], ip_id=server1.first_ip['id'], load_balancer_id=new_load_balancer['id'])