def _get_auth(self): username = self.params.user.as_string(default=getpass.getuser()) if 'password' in self.driver.features['create_node']: password = self.params.password.as_string(default=None) if password is not None: auth = NodeAuthPassword(password) auth.username = username return auth if 'ssh_key' in self.driver.features['create_node']: pubkey = self.params.public_key.as_string(default=None) if pubkey is not None: fp = self.root.openers.open(os.path.expanduser(pubkey)) auth = NodeAuthSSHKey(fp.read()) auth.username = username return auth
def test_ex_provision_node(self): node = self.driver.list_nodes()[0] auth = NodeAuthPassword('vr!@#hosted#@!') self.assertTrue(self.driver.ex_provision_node( node=node, auth=auth ))
def test_create_node_and_deployment_one_node(self): kwargs = { "ex_storage_service_name": "mtlytics", "ex_deployment_name": "dcoddkinztest02", "ex_deployment_slot": "Production", "ex_admin_user_id": "azurecoder", } auth = NodeAuthPassword("Pa55w0rd", False) kwargs["auth"] = auth kwargs["name"] = "dcoddkinztest03" kwargs["size"] = NodeSize( id="ExtraSmall", name="ExtraSmall", ram=1024, disk="30gb", bandwidth=0, price=0, driver=self.driver, ) kwargs["image"] = NodeImage( id="5112500ae3b842c8b9c604889f8753c3__OpenLogic-CentOS-65-20140415", name="FakeImage", driver=self.driver, extra={"vm_image": False}, ) result = self.driver.create_node(ex_cloud_service_name="testdcabc", **kwargs) self.assertIsNotNone(result)
def get_auth(vm_): ''' Return either NodeAuthSSHKey or NodeAuthPassword, preferring NodeAuthSSHKey if both are provided. ''' if HAS_LINODEPY: if get_pubkey(vm_) is not None: return NodeAuthSSHKey(get_pubkey(vm_)) elif get_password(vm_) is not None: return NodeAuthPassword(get_password(vm_)) else: raise SaltCloudConfigError( 'The Linode driver requires either a password or ssh_pubkey with ' 'corresponding ssh_private_key.') if HAS_LIBCLOUD: return NodeAuthPassword(get_password(vm_))
def test_create_node(self): ECSMockHttp.type = "create_node" name = "test_create_node" node = self.driver.create_node( name=name, image=self.fake_image, size=self.fake_size, ex_security_group_id="sg-28ou0f3xa", ex_description="description", ex_internet_charge_type="PayByTraffic", ex_internet_max_bandwidth_out=1, ex_internet_max_bandwidth_in=200, ex_hostname="hostname", auth=NodeAuthPassword("password"), ex_io_optimized=True, ex_system_disk={ "category": "cloud", "disk_name": "root", "description": "sys", }, ex_vswitch_id="vswitch-id1", ex_private_ip_address="1.1.1.2", ex_client_token="client_token", ) self.assertIsNotNone(node)
def run(self, region, location, network_id, image_name, name, description, is_started, password, memory_gb, cpu_count, cpu_speed, cores_per_socket): driver = self._get_compute_driver(region) root_pw = NodeAuthPassword(password) location = driver.ex_get_location_by_id(location) images = driver.list_images(location=location) image = list(filter(lambda x: x.name == image_name, images))[0] networks = driver.list_networks(location) network = list(filter(lambda x: x.id == network_id, networks))[0] cpu = None if cpu_count is not None: cpu = DimensionDataServerCpuSpecification( cpu_count=cpu_count, cores_per_socket=cores_per_socket, performance=cpu_speed ) node = driver.create_node(name=name, image=image, auth=root_pw, ex_description=description, ex_network=network, ex_cpu_specification=cpu, ex_memory_gb=memory_gb, ex_is_started=is_started) return self.resultsets.formatter(node)
def create(vm_): ''' Create a single vm from a data dict ''' print('Creating Cloud VM {0}'.format(vm_['name'])) conn = get_conn() kwargs = {} kwargs['name'] = vm_['name'] kwargs['deploy'] = script(vm_) kwargs['image'] = get_image(conn, vm_) kwargs['size'] = get_size(conn, vm_) kwargs['location'] = get_location(conn, vm_) kwargs['auth'] = NodeAuthPassword(get_password(vm_)) try: data = conn.deploy_node(**kwargs) except Exception as exc: err = ('Error creating {0} on LINODE\n\n' 'The following exception was thrown by libcloud when trying to ' 'run the initial deployment: \n{1}').format( vm_['name'], exc.message) sys.stderr.write(err) return False print('Created Cloud VM {0} with the following values:'.format( vm_['name'])) for key, val in data.__dict__.items(): print(' {0}: {1}'.format(key, val))
def test_create_node_password_auth(self): # Will exception on failure self.driver.create_node(name="Test", location=self.driver.list_locations()[0], size=self.driver.list_sizes()[0], image=self.driver.list_images()[6], auth=NodeAuthPassword("test123"))
def test_create_node_and_deployment_second_node_307_response(self): kwargs = { "ex_storage_service_name": "mtlytics", "ex_deployment_name": "dcoddkinztest04", "ex_deployment_slot": "Production", "ex_admin_user_id": "azurecoder" } auth = NodeAuthPassword("Pa55w0rd", False) kwargs["auth"] = auth kwargs["size"] = NodeSize(id="ExtraSmall", name="ExtraSmall", ram=1024, disk="30gb", bandwidth=0, price=0, driver=self.driver) kwargs["image"] = NodeImage( id="5112500ae3b842c8b9c604889f8753c3__OpenLogic-CentOS-65-20140415", name="FakeImage", driver=self.driver, extra={'vm_image': False}) kwargs["name"] = "dcoddkinztest04" with self.assertRaises(LibcloudError): self.driver.create_node(ex_cloud_service_name="testdcabc3", **kwargs)
def boot_vm(self, name, image=None, flavor=None, cloud=None, key=None, secgroup=None, meta=None, nics=None, **kwargs): """ Spawns a VM instance on the cloud. If image and flavor passed as none, it would consider the defaults specified in cloudmesh.yaml. :param name: Name of the instance to be started :param image: Image id to be used for the instance :param flavor: Flavor to be used for the instance :param cloud: Cloud on which to spawn the machine. Defaults to 'India'. :param key: Key to be used for the instance :param secgroup: Security group for the instance :param nics: TODO: fixme :param meta: A dict of arbitrary key/value metadata to store for this server """ pprint("BOOTING UP THE VM") if cloud is None: Console.error("Cloud is not specified") return auth = NodeAuthPassword('mysecretpassword') # self.provider.create_node("test_node", auth=auth) if image is not None: image = self.get_image_by_id(image) pprint("Image Id") pprint(image) else: Console.error("Image Id not found") if flavor is not None: flavor = self.get_size_by_id(flavor) pprint("FLAVOR::") pprint(flavor) else: Console.error("valid Flavor Id not found") # flavor = self.provider.list_sizes()[2] # location = self.provider.list_locations()[0] # pprint(self.provider.features['create_node']) # create_args = dict() # create_args['image'] = image # Console.info("Demo start a VM:") # Console.info("Image selected :"+image.name) # Console.info("Flavor selected :"+flavor.name) # Console.info("Key :") # pprint(key) self.provider.create_node(name=name, image=image, size=flavor, ex_keyname=key) Console.info("VM boot up success.ok.")
def test_create_node(self): node = self.driver.create_node(name='foo', size=self.driver.list_sizes()[0], image=self.driver.list_images()[0], auth=NodeAuthPassword("test123")) self.assertTrue(isinstance(node, Node)) self.assertEqual(node.state, NodeState.PENDING) self.assertEqual(node.name, 'foo.apitest.blueboxgrid.com')
def test_create_node_response(self): rootPw = NodeAuthPassword('pass123') image = self.driver.list_images()[0] network = self.driver.ex_list_networks()[0] node = self.driver.create_node(name='test2', image=image, auth=rootPw, ex_description='test2 node', ex_network=network, ex_isStarted=False) self.assertEqual(node.id, 'e75ead52-692f-4314-8725-c8a4f4d13a87') self.assertEqual(node.extra['status'].action, 'DEPLOY_SERVER')
def test_create_node(self): node = self.driver.create_node( name="name", image=NodeImage(id="image_id", name=None, driver=self.driver), ex_fixed_instance_size_id="instance_id", location=NodeLocation("datacenter_id", name="name", country="GB", driver=self.driver), auth=NodeAuthPassword("password"), ex_ip="1.1.1.1", ex_monitoring_policy_id="mp_id", ex_firewall_policy_id="fw_id", ex_loadbalancer_id="lb_id", ex_description="description", ex_power_on="SHUTDOWN", ) self.assertEqual(node.id, "E7D36EC025C73796035BF4F171379025") self.assertEqual(node.name, "Docs Content Test Server: CentOS 7-1") self.assertEqual(node.extra["description"], "My server description") self.assertIsNone(node.extra["status"]["percent"]) self.assertEqual(node.extra["status"]["state"], "POWERED_ON") self.assertEqual(node.extra["image"]["id"], "B5F778B85C041347BCDCFC3172AB3F3C") self.assertEqual(node.extra["image"]["name"], "centos7-64std") self.assertEqual(node.extra["datacenter"]["id"], "908DC2072407C94C8054610AD5A53B8C") self.assertEqual(node.extra["datacenter"]["country_code"], "US") self.assertEqual(node.extra["datacenter"]["location"], "United States of America") self.assertEqual( node.extra["hardware"]["fixed_instance_size_id"], "65929629F35BBFBA63022008F773F3EB", ) self.assertEqual(node.extra["hardware"]["vcore"], 1) self.assertEqual(node.extra["hardware"]["hdds"][0]["id"], "CDB278D95A92CB4C379A9CAAD6759F02") self.assertEqual(node.extra["hardware"]["hdds"][0]["size"], 40) self.assertEqual(node.extra["hardware"]["hdds"][0]["is_main"], True) self.assertEqual(node.extra["hardware"]["cores_per_processor"], 1) self.assertEqual(node.extra["hardware"]["vcore"], 1) self.assertEqual(node.extra["hardware"]["ram"], 1) self.assertEqual(node.extra["ips"][0]["id"], "FDBE99EDD57F8596CBF71B6B64BD0A92") self.assertEqual(node.extra["ips"][0]["ip"], "62.151.179.99") self.assertIsNone(node.extra["monitoring_policy"]) self.assertEqual(node.extra["alerts"], []) self.assertIsNone(node.extra["snapshot"]) self.assertIsNone(node.extra["dvd"]) self.assertIsNone(node.extra["private_networks"])
def test_create_node_no_network(self): rootPw = NodeAuthPassword('pass123') image = self.driver.list_images()[0] try: self.driver.create_node(name='test2', image=image, auth=rootPw, ex_description='test2 node', ex_network=None, ex_isStarted=False) except ValueError: pass
def test_create_node_response(self): # should return a node object node = self.driver.create_node(name="node-name", location=self.driver.list_locations()[ 0], size=self.driver.list_sizes()[0], image=self.driver.list_images()[0], auth=NodeAuthPassword("foobar")) self.assertTrue(isinstance(node, Node))
def test_create_node(self): auth = NodeAuthPassword('vr!@#hosted#@!') size = self.driver.list_sizes()[0] image = self.driver.list_images()[0] node = self.driver.create_node(name='test.com', image=image, size=size, auth=auth) self.assertEqual('62291', node.id) self.assertEqual('server1.vr-cluster.org', node.name)
def test_create_node(self): auth = NodeAuthPassword("vr!@#hosted#@!") size = self.driver.list_sizes()[0] image = self.driver.list_images()[0] node = self.driver.create_node(name="test.com", image=image, size=size, auth=auth) self.assertEqual("62291", node.id) self.assertEqual("server1.vr-cluster.org", node.name)
def create_node(self, name, image, size, provider=None, location=None, ex_keyname=None, ex_security_groups=None): if provider is None: raise RuntimeError( "Node must be created with a specified provider using this function; use create_managed_node to create a node using SLOs" ) return None if not provider in self.providerDrivers.keys(): raise RuntimeError( "No defined implementation for the provider specified") node = None if provider == Provider.EC2: if ex_keyname is None: keypairs = self.providerDrivers[Provider.EC2].list_key_pairs() for k in keypairs: if k.name in self.sshKey: ex_keyname = k.name break if ex_keyname is None: raise RuntimeError( "Could not find a ssh keypair that is both loaded in this driver and registered with Amazon" ) if ex_security_groups is None: ex_security_groups = ["default"] node = self.providerDrivers[provider].create_node( name=name, image=image, size=size, ex_keyname=ex_keyname, ex_security_groups=ex_security_groups) elif provider == Provider.GCE: if location is None: location = 'us-central1-a' node = self.providerDrivers[provider].create_node( name=name, image=image, size=size, location=location) elif provider == Provider.AZURE_ARM: node = self.providerDrivers[provider].create_node( name=name, image=image, size=size, auth=NodeAuthPassword('mysecretpassword'), ex_resource_group=self.providerKeys["AZURE_ARM"] ["resource_group"], ex_storage_account=self.providerKeys["AZURE_ARM"] ["storage_account"], ex_network=keys["AZURE_ARM"]["virtual_network"]) node.size = size return node
def test_create_node_in_location(self): auth = NodeAuthPassword('vr!@#hosted#@!') size = self.driver.list_sizes()[0] image = self.driver.list_images()[0] location = self.driver.list_locations()[1] node = self.driver.create_node(name='test.com', image=image, size=size, auth=auth, location=location) self.assertEqual('76070', node.id) self.assertEqual('test.com', node.name)
def start_instance(self, key_name, public_key_path, private_key_path, security_group, flavor, image_id, image_userdata, cluster_name, username=None, node_name=None, **options): self.__prepare_key_pair(key_name, private_key_path, public_key_path, options.get('image_user_password')) options['name'] = node_name options['size'] = self._get_flavor_by_name(flavor) options['image'] = self.driver.get_image(image_id) if security_group: options['security_groups'] = security_group options['ex_userdata'] = image_userdata options['username'] = username network_ids = [ netid.strip() for netid in options.pop('network_ids', '').split(',') if netid.strip() != '' ] if network_ids: try: options['networks'] = [ net for net in self.driver.ex_list_networks() if net.id in network_ids ] except AttributeError: raise UnsupportedError( "Cluster specifies `network_ids`" " but the cloud provider does not implement" " the `ex_list_networks()` call.") if self.driver.get_key_pair(key_name): options['auth'] = NodeAuthSSHKey( self.driver.get_key_pair(key_name).public_key) options['ex_keyname'] = key_name else: options['auth'] = NodeAuthPassword( options.get('image_user_password')) node = self.driver.create_node(**options) if node: return {'instance_id': node.id} else: raise InstanceError("Error creating node `{0}`".format(node_name))
def test_create_node_response_network_domain(self): rootPw = NodeAuthPassword('pass123') location = self.driver.ex_get_location_by_id('NA9') image = self.driver.list_images(location=location)[0] network_domain = self.driver.ex_list_network_domains(location=location)[0] vlan = self.driver.ex_list_vlans(location=location)[0] node = self.driver.create_node(name='test2', image=image, auth=rootPw, ex_description='test2 node', ex_network_domain=network_domain, ex_vlan=vlan, ex_is_started=False) self.assertEqual(node.id, 'e75ead52-692f-4314-8725-c8a4f4d13a87') self.assertEqual(node.extra['status'].action, 'DEPLOY_SERVER')
def boot_vm(self, name, image=None, flavor=None, cloud="kilo", key=None, secgroup=None, meta=None, nics=None, **kwargs): """ Spawns a VM instance on the cloud. If image and flavor passed as none, it would consider the defaults specified in cloudmesh.yaml. :param name: Name of the instance to be started :param image: Image id to be used for the instance :param flavor: Flavor to be used for the instance :param cloud: Cloud on which to spawn the machine. Defaults to 'India'. :param key: Key to be used for the instance :param secgroup: Security group for the instance :param nics: TODO: fixme :param meta: A dict of arbitrary key/value metadata to store for this server """ pprint("BOOTING UP THE VM") auth = NodeAuthPassword('mysecretpassword') # self.provider.create_node("test_node", auth=auth) if image: pprint("Image Id") pprint(image) image = self.get_image_by_id(image) else: pprint("valid Image Id not found") if flavor: flavor = self.get_size_by_id(flavor) if flavor: pprint("FLAVOR::") pprint(flavor) else: pprint("valid Flavor Id not found") # flavor = self.provider.list_sizes()[2] # location = self.provider.list_locations()[0] # pprint(self.provider.features['create_node']) # create_args = dict() # create_args['image'] = image self.provider.create_node(name=name, ex_iamprofile=name, image=image, size=flavor)
def start_instance(self, key_name, public_key_path, private_key_path, security_group, flavor, image_id, image_userdata, username=None, node_name=None, **options): self.__prepare_key_pair(key_name, private_key_path, public_key_path, options.get('image_user_password')) options['name'] = node_name options['size'] = flavor options['image'] = image_id if security_group: options['security_groups'] = security_group options['ex_userdata'] = image_userdata options['username'] = username options['networks'] = options.pop('network_ids', None) if self.driver.get_key_pair(key_name): options['auth'] = NodeAuthSSHKey( self.driver.get_key_pair(key_name).public_key) options['ex_keyname'] = key_name else: options['auth'] = NodeAuthPassword( options.get('image_user_password')) for key in options.keys(): try: list_fn = self.__get_function_by_pattern( 'list_{0}'.format(key)) except AttributeError: # skip non-existing continue populated_list = self.__get_name_or_id(options[key], list_fn()) if populated_list: if key.endswith('s'): options[key] = populated_list else: options[key] = populated_list[0] node = self.driver.create_node(**options) if node: return node.id return None
def create_node(self, instance, image_meta, *args, **kwargs): config = self.load_config() # Get info image_id = getattr(image_meta.properties, 'os_distro') flavor_name = instance.flavor['name'] node_config = {'name': instance.uuid} # Find image for image in self.driver.list_images(): if image.id == image_id: node_config['image'] = image break else: Exception('Image with id "{}" not found'.format(image_id)) # Find size for size in self.driver.list_sizes(): if size.id == flavor_name: node_config['size'] = size break else: Exception('Flavor with id "{}" not found'.format(flavor_name)) # Find location for location in self.driver.list_locations(): if location.id == config['location']: node_config['location'] = location break else: Exception('Location with id "{}" not found'.format( config['location'])) # Root password try: if config.get('root_password'): node_config['auth'] = NodeAuthPassword( config.get('root_password')) except cfg.NoSuchOptError: pass instance = self.driver.create_node(**node_config) return instance
def provision_vm(self, vm, backend_image_id=None, backend_size_id=None): try: backend_vm = self.manager.create_node( name=vm.name, size=self.get_size(backend_size_id), image=self.get_image(backend_image_id), ex_cloud_service_name=self.cloud_service_name, ex_storage_service_name=self.get_storage_name(), ex_deployment_slot=self.deployment, ex_custom_data=vm.user_data, ex_admin_user_id=vm.user_username, auth=NodeAuthPassword(vm.user_password)) except LibcloudError as e: logger.exception('Failed to provision virtual machine %s', vm.name) reraise(e) vm.backend_id = backend_vm.id vm.runtime_state = backend_vm.state vm.save(update_fields=['backend_id', 'runtime_state'])
def _get_create_args(self, data): """Returns the args used to create a server for this adapter.""" driver = self._get_user_driver() size = self._find_size(driver, data['size']) image = self._find_image(driver, 'Ubuntu Server 16.04 LTS') try: driver.ex_create_storage_service( # name = data['name'], name='nanobox', location=data['region']) except libcloud.common.types.LibcloudError: pass try: driver.ex_create_cloud_service(name=data['name'], location=data['region']) except libcloud.common.types.LibcloudError: pass cs_list = [] while len(cs_list) < 1: try: cs_list = [ serv for serv in driver.ex_list_cloud_services() if serv.service_name == data['name'] and serv.hosted_service_properties.status == 'Created' ] except AttributeError: pass return { "name": data['name'], "size": size, "image": image, "auth": NodeAuthPassword(self._get_password(data['name'])), "ex_new_deployment": True, "ex_admin_user_id": 'nanobox', # "ex_storage_service_name": data['name'], "ex_storage_service_name": 'nanobox', "ex_cloud_service_name": data['name'] }
def test_create_node(self): ECSMockHttp.type = 'create_node' name = 'test_create_node' node = self.driver.create_node(name=name, image=self.fake_image, size=self.fake_size, ex_security_group_id='sg-28ou0f3xa', ex_description='description', ex_internet_charge_type='PayByTraffic', ex_internet_max_bandwidth_out=1, ex_internet_max_bandwidth_in=200, ex_hostname='hostname', auth=NodeAuthPassword('password'), ex_io_optimized=True, ex_system_disk={'category': 'cloud', 'disk_name': 'root', 'description': 'sys'}, ex_vswitch_id='vswitch-id1', ex_private_ip_address='1.1.1.2', ex_client_token='client_token') self.assertIsNotNone(node)
def run(self, region, location, network_domain_id, name, vlan_id, image_name, description, is_started, password, memory_gb, cpu_count, cpu_speed, cores_per_socket): driver = self._get_compute_driver(region) root_pw = NodeAuthPassword(password) location = driver.ex_get_location_by_id(location) images = driver.list_images(location=location) image = list(filter(lambda x: x.name == image_name, images))[0] network_domain = driver.ex_get_network_domain(network_domain_id) vlan = driver.ex_get_vlan(vlan_id) cpu = None if cpu_count is not None: cpu = DimensionDataServerCpuSpecification( cpu_count=cpu_count, cores_per_socket=cores_per_socket, performance=cpu_speed ) node = driver.create_node(name=name, image=image, auth=root_pw, ex_description=description, ex_network_domain=network_domain, ex_vlan=vlan, ex_cpu_specification=cpu, ex_memory_gb=memory_gb, ex_is_started=is_started) timeout = 1200 # 20 minutes poll_interval = 5 cnt = 0 while cnt < timeout / poll_interval: result = driver.ex_get_node_by_id(node.id) if result.state is NodeState.RUNNING: return self.resultsets.formatter(result) sleep(poll_interval) cnt += 1 raise Exception("Timed out creating server")
def _get_and_check_auth(self, auth): """ Helper function for providers supporting L{NodeAuthPassword} or L{NodeAuthSSHKey} Validates that only a supported object type is passed to the auth parameter and raises an exception if it is not. If no L{NodeAuthPassword} object is provided but one is expected then a password is automatically generated. """ if isinstance(auth, NodeAuthPassword): if 'password' in self.features['create_node']: return auth raise LibcloudError( 'Password provided as authentication information, but password' 'not supported', driver=self) if isinstance(auth, NodeAuthSSHKey): if 'ssh_key' in self.features['create_node']: return auth raise LibcloudError( 'SSH Key provided as authentication information, but SSH Key' 'not supported', driver=self) if 'password' in self.features['create_node']: value = os.urandom(16) value = binascii.hexlify(value).decode('ascii') return NodeAuthPassword(value, generated=True) if auth: raise LibcloudError( '"auth" argument provided, but it was not a NodeAuthPassword' 'or NodeAuthSSHKey object', driver=self)
def create(vm_): ''' Create a single vm from a data dict ''' print('Creating Cloud VM {0}'.format(vm_['name'])) conn = get_conn() deploy_script = script(vm_) kwargs = {} kwargs['name'] = vm_['name'] kwargs['image'] = get_image(conn, vm_) kwargs['size'] = get_size(conn, vm_) kwargs['location'] = get_location(conn, vm_) kwargs['auth'] = NodeAuthPassword(get_password(vm_)) try: data = conn.create_node(**kwargs) except Exception as exc: err = ('Error creating {0} on LINODE\n\n' 'The following exception was thrown by libcloud when trying to ' 'run the initial deployment: \n{1}').format( vm_['name'], exc.message) sys.stderr.write(err) return False deployed = saltcloud.utils.deploy_script( host=data.public_ips[0], username='******', password=__opts__['LINODE.password'], script=deploy_script.script) if deployed: print('Salt installed on {0}'.format(vm_['name'])) else: print('Failed to start Salt on Cloud VM {0}'.format(vm_['name'])) print('Created Cloud VM {0} with the following values:'.format( vm_['name'])) for key, val in data.__dict__.items(): print(' {0}: {1}'.format(key, val))