def POST(self): StratusLabDriver = get_driver('stratuslab') driver = StratusLabDriver('unused-key') locations = driver.list_locations() location = select_id('grnet', locations) images = driver.list_images() image = select_id('L5D9_WONgduc_0vkoF8JmGegUDx', images) sizes = driver.list_sizes() size = select_id('m1.large', sizes) vars = web.input() size.cpu = vars.cpu size.ram = vars.ram #-------------------- Get ssh key ------------------------- home = os.path.expanduser('~') ssh_public_key_path = os.path.join(home, '.ssh', 'id_rsa.pub') ssh_private_key_path = ssh_public_key_path.rstrip('.pub') with open(ssh_public_key_path) as f: pubkey = NodeAuthSSHKey(f.read()) #-----------------Call Create Node ----------------------- node = driver.create_node(size=size, location=location, image=image, auth=pubkey) ws = print_objs(driver.list_nodes()) return ws
def create_instance(self, name, size, image, keypair): """ Create a new instance with the supplied attributes. Return a Node object. """ auth_key = NodeAuthSSHKey(keypair.public_key) try: return self.driver.create_node( name=name, size=size, image=image, auth=auth_key ) except Exception as e: status, error_text = parse_exception(e) if status: self._handle_exception(status, error_text) else: raise e
def test_create_node_with_ssh_keys(self): image = NodeImage(id='01000000-0000-4000-8000-000030060200', name='Ubuntu Server 16.04 LTS (Xenial Xerus)', extra={'type': 'template'}, driver=self.driver) location = NodeLocation(id='fi-hel1', name='Helsinki #1', country='FI', driver=self.driver) size = NodeSize(id='1xCPU-1GB', name='1xCPU-1GB', ram=1024, disk=30, bandwidth=2048, extra={'storage_tier': 'maxiops'}, price=None, driver=self.driver) auth = NodeAuthSSHKey('publikey') node = self.driver.create_node(name='test_server', size=size, image=image, location=location, auth=auth) self.assertTrue( re.match( '^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$', node.id)) self.assertEquals(node.name, 'test_server') self.assertEquals(node.state, NodeState.STARTING) self.assertTrue(len(node.public_ips) > 0) self.assertTrue(len(node.private_ips) > 0) self.assertEquals(node.driver, self.driver)
def create(self, name): """ Create a node """ # id_rsa_path = f"{Path.home()}/.ssh/id_rsa.pub" auth = NodeAuthSSHKey(self.defaults["public_key"]) image = self.provider.get_image(self.defaults["image"]) sizes = self.provider.list_sizes() size = [s for s in sizes if s.id == self.defaults["size"]][0] # Create a network and default subnet if none exists network_name = self.defaults["network"] network, subnet = self._create_network(network_name) # Create a NIC with public IP nic = self._create_create_nic(name, subnet) # Create vm new_vm = self.provider.create_node( name=name, size=size, image=image, auth=auth, ex_use_managed_disks=True, ex_resource_group=self.defaults["resource_group"], ex_storage_account=self.defaults["storage_account"], ex_nic=nic, ex_network=network_name ) return new_vm
def test_create_node_ssh_key_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=NodeAuthSSHKey('foo'))
def _create_machine_linode(conn, key_name, private_key, public_key, machine_name, image, size, location): """Create a machine in Linode. Here there is no checking done, all parameters are expected to be sanitized by create_machine. """ auth = NodeAuthSSHKey(public_key) with get_temp_file(private_key) as tmp_key_path: try: node = conn.create_node( name=machine_name, image=image, size=size, location=location, auth=auth, ssh_key=tmp_key_path, ex_private=True ) except Exception as e: raise MachineCreationError("Linode, got exception %s" % e, e) return node
def libcloud_create_exec(self): self.libcloud_destroy_exec() Driver = get_driver(getattr(Provider, self.provider_id.name)) driver = Driver(self.provider_id.login, self.provider_id.secret_key) image = [i for i in driver.list_images() if i.id == self.libcloud_image][0] size = [s for s in driver.list_sizes() if s.id == self.libcloud_size][0] location = False if self.libcloud_location: location = [l for l in driver.list_locations() if l.id == self.libcloud_location][0] # Create node node = driver.create_node( name=self.name, image=image, size=size, location=location, ssh_username=self.login or 'root', auth=NodeAuthSSHKey(self.public_key)) # Store name in specific field, in case different from name field self.write({'libcloud_name': node.name}) # Wait until running driver.wait_until_running([node]) # Configure node, install Docker, add to Swarm etc... self.configure_exec()
def create_node(self, name=None, image=None, size=None, timeout=360, **kwargs): """ Create a node """ # TODO: move `auth` to libcloud base key_path = self.config["profile"]["key"]["public"] with open(os.path.expanduser(key_path), 'r') as fp: key = fp.read() auth = NodeAuthSSHKey(key) nic = self._get_nic(name) # Create vm new_vm = super().create_node(name=name, size=size, image=image, auth=auth, ex_nic=nic, ex_use_managed_disks=True, ex_resource_group=self.resource_group, ex_storage_account=self.storage_account) return new_vm
def _get_ssh_auth(module): try: ssh_key = module.params.get('ssh_public_key') key = open(ssh_key).read() auth = NodeAuthSSHKey(pubkey=key) return auth.pubkey except Exception as e: module.fail_json( msg="Could not load ssh_public_key for {0}," "Error was: {1}".format(module.params.get('hostname'), str(e)))
def test_create_node_ssh_key_auth(self): # Will exception on failure node = 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=NodeAuthSSHKey("foo"), ) self.assertTrue(isinstance(node, Node))
def vm_create(self, vm_name, vm_type, vm_user, vm_networkassoc, vm_cpuarch, vm_image, vm_mem, vm_cores, vm_storage, customization, vm_keepalive, instance_type, location, job_per_core, vm_keyname, username="", password=""): # will need to use self.driver.deploy_node(...) as this seems to allow for contextualization whereas create_node() does not from libcloud.compute.base import NodeImage, NodeSize, NodeLocation, NodeAuthSSHKey if not username: username = self.username if not password: password = self.password conn = self._get_connection(username, password) if instance_type and instance_type.lower() in self.VM_COMPUTE_SIZE_MAP.keys(): vm_size = self.compute_sizes[self.VM_COMPUTE_SIZE_MAP[instance_type.lower()]] else: log.debug("%s not a valid instance type." % instance_type) return if location.lower() in self.CLOUD_LOCATION_ID_MAP.keys(): try: vm_location = self.locations_dict[self.CLOUD_LOCATION_ID_MAP[location.lower()]] except KeyError: log.debug("Bad Dict Key mapping") return else: log.debug("%s is not a valid location" % location) print 'location invalid' return image = NodeImage(vm_image, '','') # 20035253 image id is a RHL 5.7 that should boot on bronze 32bit and is on markham location instance = None vm_key = NodeAuthSSHKey(vm_keyname) instance = conn.create_node(name=vm_name, image=image, size=vm_size, location=vm_location, auth=vm_key) if instance: new_vm = VM(name = vm_name, id = instance.uuid, vmtype = vm_type, user = vm_user, clusteraddr = self.network_address, cloudtype = self.cloud_type, network = vm_networkassoc, cpuarch = vm_cpuarch, image= vm_image, memory = vm_mem, cpucores = vm_cores, storage = vm_storage, keep_alive = vm_keepalive, job_per_core = job_per_core) try: self.resource_checkout(new_vm) except: log.exception("Unexpected Error checking out resources when creating a VM. Programming error?") self.vm_destroy(new_vm, reason="Failed Resource checkout") return self.ERROR self.vms.append(new_vm) else: log.debug("No return from create_node()") return 0
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 get_auth(vm_): ''' Return either NodeAuthSSHKey or NodeAuthPassword, preferring NodeAuthSSHKey if both are provided. ''' 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.')
def _get_ssh_auth(self): """Figure out the ssh public key for building into the node Returns the public key on success, Calls fail_json on failure """ try: ssh_key = self.module.params.get('ssh_public_key') key = open(ssh_key).read() auth = NodeAuthSSHKey(pubkey=key) return auth.pubkey except Exception as e: self.module.fail_json( msg="Could not load ssh_public_key for {0}," "Error was: {1}".format(self.hostname, str(e)))
def test_create_node_with_ssh_keys(self): node = self.driver.create_node(name='test_server_pubkey', size=self.small_node_size, image=self.centos_8_EU_node_image, location=self.eu_node_location, auth=NodeAuthSSHKey('publickey')) self.assertTrue(len(node.id) > 8) self.assertEqual(node.name, 'my-server') self.assertEqual(node.state, NodeState.RUNNING) self.assertTrue(len(node.public_ips) > 0) self.assertTrue(len(node.private_ips) > 0) self.assertEqual(node.driver, self.driver) self.assertFalse('generated_password' in node.extra)
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 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 test_creating_node_using_ssh_keys(self): auth = NodeAuthSSHKey("sshkey") body = UpcloudCreateNodeRequestBody( name="ts", image=self.image, location=self.location, size=self.size, auth=auth, ) json_body = body.to_json() dict_body = json.loads(json_body) expected_body = { "server": { "title": "ts", "hostname": "localhost", "plan": "1xCPU-1GB", "zone": "fi-hel1", "login_user": { "username": "******", "ssh_keys": { "ssh_key": ["sshkey"] }, }, "storage_devices": { "storage_device": [{ "action": "clone", "size": 30, "title": "Ubuntu Server 16.04 LTS (Xenial Xerus)", "tier": "maxiops", "storage": "01000000-0000-4000-8000-000030060200", }] }, } } self.assertDictEqual(expected_body, dict_body)
def test_creating_node_using_ssh_keys(self): auth = NodeAuthSSHKey('sshkey') body = UpcloudCreateNodeRequestBody(name='ts', image=self.image, location=self.location, size=self.size, auth=auth) json_body = body.to_json() dict_body = json.loads(json_body) expected_body = { 'server': { 'title': 'ts', 'hostname': 'localhost', 'plan': '1xCPU-1GB', 'zone': 'fi-hel1', 'login_user': { 'username': '******', 'ssh_keys': { 'ssh_key': ['sshkey'] }, }, 'storage_devices': { 'storage_device': [{ 'action': 'clone', 'size': 30, 'title': 'Ubuntu Server 16.04 LTS (Xenial Xerus)', 'tier': 'maxiops', 'storage': '01000000-0000-4000-8000-000030060200' }] }, } } self.assertDictEqual(expected_body, dict_body)
def test_create_node_with_ssh_keys(self): image = NodeImage( id="01000000-0000-4000-8000-000030060200", name="Ubuntu Server 16.04 LTS (Xenial Xerus)", extra={"type": "template"}, driver=self.driver, ) location = NodeLocation(id="fi-hel1", name="Helsinki #1", country="FI", driver=self.driver) size = NodeSize( id="1xCPU-1GB", name="1xCPU-1GB", ram=1024, disk=30, bandwidth=2048, extra={"storage_tier": "maxiops"}, price=None, driver=self.driver, ) auth = NodeAuthSSHKey("publikey") node = self.driver.create_node(name="test_server", size=size, image=image, location=location, auth=auth) self.assertTrue( re.match( "^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$", node.id, )) self.assertEqual(node.name, "test_server") self.assertEqual(node.state, NodeState.STARTING) self.assertTrue(len(node.public_ips) > 0) self.assertTrue(len(node.private_ips) > 0) self.assertEqual(node.driver, self.driver)
def _get_create_args(self, data): """Returns the args used to create a server for this adapter.""" driver = self._get_user_driver() app = data['name'].rsplit('-', 1)[0] location = self._find_location(driver, data['region']) size = self._find_size(driver, location, data['size']) image = self._find_image(driver, location, 'Canonical', 'UbuntuServer', '16.04-LTS') ssh_key = NodeAuthSSHKey(data['ssh_key']) if not self._find_resource_group(driver, app): driver.ex_create_resource_group(app, data['region']) network = self._find_network(driver, app) if not network: network = driver.ex_create_network(app, data['region'], app) ipaddr = driver.ex_create_public_ip(data['name'], app, location) subnet = self._find_subnet(driver, network, 'default') nic = driver.ex_create_network_interface(data['name'], subnet, app, location, ipaddr) return { "name": data['name'], "size": size, "image": image, "location": location, "auth": ssh_key, "ex_resource_group": app, "ex_storage_account": None, "ex_user_name": self.server_ssh_user, "ex_nic": nic, "ex_use_managed_disks": True, "ex_storage_account_type": 'Standard_LRS' }
def create(self, vmdiracInstanceID=''): """ This creates a new virtual machine instance based on the appliance identifier and cloud identifier defined when this object was created. Successful creation returns a tuple with the node object returned from the StratusLab Libcloud driver and the public IP address of the instance. NOTE: The node object should be treated as an opaque identifier by the called and returned unmodified when calling the other methods of this class. :return: S_OK( ( node, publicIP ) ) | S_ERROR """ # Get ssh key. home = os.path.expanduser('~') ssh_public_key_path = os.path.join(home, '.ssh', 'id_dsa.pub') with open(ssh_public_key_path) as f: pubkey = NodeAuthSSHKey(f.read()) # Create the new instance, called a 'node' for Libcloud. try: node = self._driver.create_node(name=vmdiracInstanceID, size=self.size, location=self.location, image=self.image, auth=pubkey) public_ips = node.public_ips if len(public_ips) > 0: public_ip = public_ips[0] else: public_ip = None return S_OK((node, public_ip)) except Exception, e: return S_ERROR(e)
def _create_machine_hostvirtual(conn, public_key, machine_name, image, size, location): """Create a machine in HostVirtual. Here there is no checking done, all parameters are expected to be sanitized by create_machine. """ key = public_key.replace('\n', '') auth = NodeAuthSSHKey(pubkey=key) try: node = conn.create_node( name=machine_name, image=image, size=size, auth=auth, location=location ) except Exception as e: raise MachineCreationError("HostVirtual, got exception %s" % e, e) return node
def create_node(name, **kwargs): """ Create a node server """ PROVIDER = get_provider_dict() keyname = kwargs.get('keyname', None) image = kwargs.get('image', get_node_image(PROVIDER['image'])) size = kwargs.get('size', '') location = kwargs.get('location', get_node_location(PROVIDER['location'])) if 'ec2' in fabric.api.env.conf['PROVIDER']: node = _get_connection().create_node(name=name, ex_keyname=keyname, image=image, size=size, location=location) # TODO: This does not work until libcloud 0.4.3 tags = { 'name': name, } _get_connection().ex_create_tags(node, tags) else: if keyname: pubkey = open(keyname, 'r').read() from libcloud.compute.base import NodeAuthSSHKey key = NodeAuthSSHKey(pubkey) else: key = None node = _get_connection().create_node(name=name, auth=key, image=image, size=size, location=location) print fabric.colors.green('Node %s successfully created' % name) return node
def create_instance(self, name, size_id, image_id, keypair): """ Create a new instance with the supplied attributes. Return a Node object. """ try: image = self.get_image(image_id) if image is None: raise ItemNotFound("Image not found") size = self.driver.ex_get_size(size_id) args = { "name": name, "size": size, "image": image, } if keypair is not None: auth_key = NodeAuthSSHKey(keypair.public_key) args["auth"] = auth_key args["ex_keyname"] = name return self.driver.create_node(**args) except Exception as e: status, error_text = parse_exception(e) if status: self._handle_exception(status, error_text) else: log.error( "create_instance method raised an exception: {}".format(e)) log.error('image id {}'.format(image_id))
def create_node(self, name, **kwargs): """ Create a node """ key_path = self.config["profile"]["key"]["public"] with open(os.path.expanduser(key_path), 'r') as fp: key = fp.read() auth = NodeAuthSSHKey(key) image = self.get_image(self.defaults["image"]) sizes = self.list_sizes() size = [s for s in sizes if s.id == self.defaults["size"]][0] # Create a network and default subnet if none exists network_name = self.defaults["network"] network, subnet = self._create_network(network_name) # Create a NIC with public IP nic = self._create_nic(name, subnet) # Create vm new_vm = super().create_node( name=name, size=size, image=image, auth=auth, ex_use_managed_disks=True, ex_resource_group=self.defaults["resource_group"], ex_storage_account=self.defaults["storage_account"], ex_nic=nic, ex_network=network_name ) return new_vm
def create(self, name=None, image=None, size=None, location=None, timeout=360, **kwargs): """ creates a named node :param name: the name of the node :param image: the image used :param size: the size of the image :param location: the location of the image :param timeout: a timeout in seconds that is invoked in case the image does not boot. The default is set to 3 minutes. :param kwargs: additional arguments HEADING(c=".")ed along at time of boot :return: """ image_use = None flavor_use = None # keyname = Config()["cloudmesh"]["profile"]["user"] # ex_keyname has to be the registered keypair name in cloud if self.cloudtype in ["openstack", "aws", "googlelibcloud"]: images = self.images(raw=True) for _image in images: if _image.name == image: image_use = _image break elif self.cloudtype == 'azure_arm': image_use = self.cloudman.get_image(image) flavors = self.flavors(raw=True) for _flavor in flavors: if _flavor.name == size: flavor_use = _flavor break if self.cloudtype == "openstack": if "ex_security_groups" in kwargs: secgroupsobj = [] # # this gives existing secgroups in obj form secgroups = self.list_secgroups(raw=True) for secgroup in kwargs["ex_security_groups"]: for _secgroup in secgroups: if _secgroup.name == secgroup: secgroupsobj.append(_secgroup) # now secgroup name is converted to object which # is required by the libcloud api call kwargs["ex_security_groups"] = secgroupsobj if self.cloudtype in ["openstack", "aws"]: node = self.cloudman.create_node(name=name, image=image_use, size=flavor_use, **kwargs) elif self.cloudtype == 'azure_arm': auth = None if "sshpubkey" in kwargs: auth = NodeAuthSSHKey(kwargs["sshpubkey"]) pubip = self.cloudman.ex_create_public_ip( name='{nodename}-ip'.format(nodename=name), resource_group=kwargs["resource_group"]) networks = self.cloudman.ex_list_networks() network_use = None for network in networks: if network.name == kwargs["network"]: network_use = network pprint(network_use) break subnets = self.cloudman.ex_list_subnets(network_use) subnet_use = None for subnet in subnets: if subnet.name == kwargs["subnet"]: subnet_use = subnet break nic_use = self.cloudman.ex_create_network_interface( name='{nodename}-nic'.format(nodename=name), subnet=subnet_use, resource_group=kwargs["resource_group"], public_ip=pubip) node = self.cloudman.create_node( name=name, image=image_use, size=flavor_use, auth=auth, # the following three were created in azure portal ex_resource_group=kwargs["resource_group"], # for storage account, use the default v2 setting ex_storage_account=kwargs["storage_account"], # under the storage account, blobs services, # create 'vhds' container ex_blob_container=kwargs["blob_container"], ex_nic=nic_use) elif self.cloudtype == 'googlelibcloud': location_use = self.spec["credentials"]["datacenter"] metadata = { "items": [{ "value": self.user + ":" + self.key_val, "key": "ssh-keys" }] } node = self.cloudman.create_node(name=name, image=image_use, size=flavor_use, location=location_use, ex_metadata=metadata, **kwargs) else: sys.exit("this cloud is not yet supported") return self.update_dict(node, kind="node")[0]
def test_get_auth_ssh(self): n = NodeDriver('foo') n.features = {'create_node': ['ssh_key']} auth = NodeAuthSSHKey('pubkey...') self.assertEqual(auth, n._get_and_check_auth(auth))
def test_get_auth_password_but_given_ssh_key(self): n = NodeDriver('foo') n.features = {'create_node': ['password']} auth = NodeAuthSSHKey('publickey') self.assertRaises(LibcloudError, n._get_and_check_auth, auth)
cleanup_and_exit(node) print("Spinning up REMnux Instance, this will take a minute or two. . .") nodeName = datetime.datetime.utcnow().strftime("REMnux-%Y-%m-%dt%H-%M-%S-%f") sizes = driver.list_sizes() images = driver.list_images(location=myregion, ex_image_ids=[IMAGE_ID]) size = [s for s in sizes if s.id == SIZE_ID][0] image = [i for i in images if i.id == IMAGE_ID][0] #read in public key in order to deploy to new node with file(public_key_file) as f: pub_key = f.read() pk = NodeAuthSSHKey(pub_key) # create this ssh key in the cloud driver.import_key_pair_from_string(nodeName, pub_key) # create the security group, only allowing ssh at first sg = driver.ex_create_security_group(nodeName, "allows ssh and rdp from anywhere", vpc_id=None) sg = driver.ex_authorize_security_group_ingress(sg["group_id"], 22, 22, cidr_ips=['0.0.0.0/0'], group_pairs=None, protocol='tcp')