def delete_vminstance(self, vm_id, created_items=None): """ Removes a VM instance from VIM and its associated elements :param vm_id: VIM identifier of the VM, provided by method new_vminstance :param created_items: dictionary with extra items to be deleted. provided by method new_vminstance and/or method action_vminstance :return: None or the same vm_id. Raises an exception on fail """ try: one = self._new_one_connection() one.vm.recover(int(vm_id), 3) vm = None while True: if vm is not None and vm.LCM_STATE == 0: break else: vm = one.vm.info(int(vm_id)) except pyone.OneNoExistsException: self.logger.info("The vm " + str(vm_id) + " does not exist or is already deleted") raise vimconn.VimConnNotFoundException( "The vm {} does not exist or is already deleted".format(vm_id)) except Exception as e: self.logger.error("Delete vm instance " + str(vm_id) + " error: " + str(e)) raise vimconn.VimConnException(e)
def get_network(self, net_id): """Obtain network details from the 'net_id' VIM network Return a dict that contains: 'id': (mandatory) VIM network id, that is, net_id 'name': (mandatory) VIM network name 'status': (mandatory) can be 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER' 'error_msg': (optional) text that explains the ERROR status other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param Raises an exception upon error or when network is not found """ try: one = self._new_one_connection() net_pool = one.vnpool.info(-2, -1, -1).VNET net = {} for network in net_pool: if str(network.ID) == str(net_id): net['id'] = network.ID net['name'] = network.NAME net['status'] = "ACTIVE" break if net: return net else: raise vimconn.VimConnNotFoundException( "Network {} not found".format(net_id)) except Exception as e: self.logger.error("Get network " + str(net_id) + " error): " + str(e)) raise vimconn.VimConnException(e)
def new_tenant(self, tenant_name, tenant_description): # '''Adds a new tenant to VIM with this name and description, returns the tenant identifier''' try: client = oca.Client(self.user + ':' + self.passwd, self.url) group_list = oca.GroupPool(client) user_list = oca.UserPool(client) group_list.info() user_list.info() create_primarygroup = 1 # create group-tenant for group in group_list: if str(group.name) == str(tenant_name): create_primarygroup = 0 break if create_primarygroup == 1: oca.Group.allocate(client, tenant_name) group_list.info() # set to primary_group the tenant_group and oneadmin to secondary_group for group in group_list: if str(group.name) == str(tenant_name): for user in user_list: if str(user.name) == str(self.user): if user.name == "oneadmin": return str(0) else: self._add_secondarygroup(user.id, group.id) user.chgrp(group.id) return str(group.id) except Exception as e: self.logger.error("Create new tenant error: " + str(e)) raise vimconn.VimConnException(e)
def get_image_list(self, filter_dict={}): """Obtain tenant images from VIM Filter_dict can be: name: image name id: image uuid checksum: image checksum location: image path Returns the image list of dictionaries: [{<the fields at Filter_dict plus some VIM specific>}, ...] List can be empty """ try: one = self._new_one_connection() image_pool = one.imagepool.info(-2, -1, -1).IMAGE images = [] if "name" in filter_dict: image_name_filter = filter_dict["name"] else: image_name_filter = None if "id" in filter_dict: image_id_filter = filter_dict["id"] else: image_id_filter = None for image in image_pool: if str(image_name_filter) == str(image.NAME) or str( image.ID) == str(image_id_filter): images_dict = {"name": image.NAME, "id": str(image.ID)} images.append(images_dict) return images except Exception as e: self.logger.error("Get image list error: " + str(e)) raise vimconn.VimConnException(e)
def new_network(self, net_name, net_type, ip_profile=None, shared=False, provider_network_profile=None): """Adds a tenant network to VIM Params: 'net_name': name of the network 'net_type': one of: 'bridge': overlay isolated network 'data': underlay E-LAN network for Passthrough and SRIOV interfaces 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces. 'ip_profile': is a dict containing the IP parameters of the network 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented) 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y 'gateway_address': (Optional) ip_schema, that is X.X.X.X 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X] 'dhcp_enabled': True or False 'dhcp_start_address': ip_schema, first IP to grant 'dhcp_count': number of IPs to grant. 'shared': if this network can be seen/use by other tenants/organization Returns the network identifier on success or raises and exception on failure """ self.logger.debug('new_network: {}'.format(locals())) if net_type in ['data', 'ptp']: raise vimconn.VimConnNotImplemented('{} type of network not supported'.format(net_type)) net_uuid = '{}'.format(uuid.uuid4()) desc = { 'uuid': net_uuid, 'name': net_name, 'net_type': 'ELAN', 'is_mgmt': False } if ip_profile is not None: ip = {} if ip_profile.get('ip_version') == 'IPv4': ip_info = {} ip_range = self.__get_ip_range(ip_profile.get('dhcp_start_address'), ip_profile.get('dhcp_count')) dhcp_range = '{},{}'.format(ip_range[0], ip_range[1]) ip['subnet'] = ip_profile.get('subnet_address') ip['dns'] = ip_profile.get('dns', None) ip['dhcp_enable'] = ip_profile.get('dhcp_enabled', False) ip['dhcp_range'] = dhcp_range ip['gateway'] = ip_profile.get('gateway_address', None) desc['ip_configuration'] = ip_info else: raise vimconn.VimConnNotImplemented('IPV6 network is not implemented at VIM') desc['ip_configuration'] = ip self.logger.debug('VIM new_network args: {} - Generated Eclipse fog05 Descriptor {}'.format(locals(), desc)) try: self.fos_api.network.add_network(desc) except fimapi.FIMAResouceExistingException as free: raise vimconn.VimConnConflictException("Network already exists at VIM. Error {}".format(free)) except Exception as e: raise vimconn.VimConnException("Unable to create network {}. Error {}".format(net_name, e)) # No way from the current rest service to get the actual error, most likely it will be an already # existing error return net_uuid, {}
def get_vminstance(self, vm_id): """Returns the VM instance information from VIM""" try: one = self._new_one_connection() vm = one.vm.info(int(vm_id)) return vm except Exception as e: self.logger.error("Getting vm instance error: " + str(e) + ": VM Instance not found") raise vimconn.VimConnException(e)
def delete_flavor(self, flavor_id): """ Deletes a tenant flavor from VIM Returns the old flavor_id """ try: one = self._new_one_connection() one.template.delete(int(flavor_id), False) return flavor_id except Exception as e: self.logger.error("Error deleting flavor " + str(flavor_id) + ". Flavor not found") raise vimconn.VimConnException(e)
def delete_network(self, net_id, created_items=None): """Deletes a tenant network from VIM Returns the network identifier or raises an exception upon error or when network is not found """ self.logger.debug('delete_network: {}'.format(net_id)) try: self.fos_api.network.remove_network(net_id) except fimapi.FIMNotFoundException as fnfe: raise vimconn.VimConnNotFoundException( "Network {} not found at VIM (already deleted?). Error {}".format(net_id, fnfe)) except Exception as e: raise vimconn.VimConnException("Cannot delete network {} from VIM. Error {}".format(net_id, e)) return net_id
def new_flavor(self, flavor_data): """Adds a tenant flavor to VIM flavor_data contains a dictionary with information, keys: name: flavor name ram: memory (cloud type) in MBytes vpcus: cpus (cloud type) extended: EPA parameters - numas: #items requested in same NUMA memory: number of 1G huge pages memory paired-threads|cores|threads: number of paired hyperthreads, complete cores OR individual threads interfaces: # passthrough(PT) or SRIOV interfaces attached to this numa - name: interface name dedicated: yes|no|yes:sriov; for PT, SRIOV or only one SRIOV for the physical NIC bandwidth: X Gbps; requested guarantee bandwidth vpci: requested virtual PCI address disk: disk size is_public: #TODO to concrete Returns the flavor identifier""" disk_size = str(int(flavor_data["disk"]) * 1024) try: one = self._new_one_connection() template_id = one.template.allocate({ 'TEMPLATE': { 'NAME': flavor_data["name"], 'CPU': flavor_data["vcpus"], 'VCPU': flavor_data["vcpus"], 'MEMORY': flavor_data["ram"], 'DISK': { 'SIZE': disk_size }, 'CONTEXT': { 'NETWORK': "YES", 'SSH_PUBLIC_KEY': '$USER[SSH_PUBLIC_KEY]' }, 'GRAPHICS': { 'LISTEN': '0.0.0.0', 'TYPE': 'VNC' }, 'CLUSTER_ID': self.config["cluster"]["id"] } }) return template_id except Exception as e: self.logger.error("Create new flavor error: " + str(e)) raise vimconn.VimConnException(e)
def delete_network(self, net_id, created_items=None): """ Removes a tenant network from VIM and its associated elements :param net_id: VIM identifier of the network, provided by method new_network :param created_items: dictionary with extra items to be deleted. provided by method new_network Returns the network identifier or raises an exception upon error or when network is not found """ try: one = self._new_one_connection() one.vn.delete(int(net_id)) return net_id except Exception as e: self.logger.error("Delete network " + str(net_id) + "error: network not found" + str(e)) raise vimconn.VimConnException(e)
def get_flavor(self, flavor_id): # Esta correcto """Obtain flavor details from the VIM Returns the flavor dict details {'id':<>, 'name':<>, other vim specific } Raises an exception upon error or if not found """ try: one = self._new_one_connection() template = one.template.info(int(flavor_id)) if template is not None: return {'id': template.ID, 'name': template.NAME} raise vimconn.VimConnNotFoundException( "Flavor {} not found".format(flavor_id)) except Exception as e: self.logger.error("get flavor " + str(flavor_id) + " error: " + str(e)) raise vimconn.VimConnException(e)
def get_network_list(self, filter_dict={}): """Obtain tenant networks of VIM :params filter_dict: (optional) contains entries to return only networks that matches ALL entries: name: string => returns only networks with this name id: string => returns networks with this VIM id, this imply returns one network at most shared: boolean >= returns only networks that are (or are not) shared tenant_id: sting => returns only networks that belong to this tenant/project (not used yet) admin_state_up: boolean => returns only networks that are (or are not) in admin state active (not used yet) status: 'ACTIVE','ERROR',... => filter networks that are on this status Returns the network list of dictionaries. each dictionary contains: 'id': (mandatory) VIM network id 'name': (mandatory) VIM network name 'status': (mandatory) can be 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER' 'network_type': (optional) can be 'vxlan', 'vlan' or 'flat' 'segmentation_id': (optional) in case network_type is vlan or vxlan this field contains the segmentation id 'error_msg': (optional) text that explains the ERROR status other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param List can be empty if no network map the filter_dict. Raise an exception only upon VIM connectivity, authorization, or some other unspecific error """ try: one = self._new_one_connection() net_pool = one.vnpool.info(-2, -1, -1).VNET response = [] if "name" in filter_dict: network_name_filter = filter_dict["name"] else: network_name_filter = None if "id" in filter_dict: network_id_filter = filter_dict["id"] else: network_id_filter = None for network in net_pool: if network.NAME == network_name_filter or str( network.ID) == str(network_id_filter): net_dict = { "name": network.NAME, "id": str(network.ID), "status": "ACTIVE" } response.append(net_dict) return response except Exception as e: self.logger.error("Get network list error: " + str(e)) raise vimconn.VimConnException(e)
def delete_tenant(self, tenant_id): """Delete a tenant from VIM. Returns the old tenant identifier""" try: client = oca.Client(self.user + ':' + self.passwd, self.url) group_list = oca.GroupPool(client) user_list = oca.UserPool(client) group_list.info() user_list.info() for group in group_list: if str(group.id) == str(tenant_id): for user in user_list: if str(user.name) == str(self.user): self._delete_secondarygroup(user.id, group.id) group.delete(client) return None raise vimconn.VimConnNotFoundException( "Group {} not found".format(tenant_id)) except Exception as e: self.logger.error("Delete tenant " + str(tenant_id) + " error: " + str(e)) raise vimconn.VimConnException(e)
def delete_vminstance(self, vm_id, created_items=None): """ Removes a VM instance from VIM and each associate elements :param vm_id: VIM identifier of the VM, provided by method new_vminstance :param created_items: dictionary with extra items to be deleted. provided by method new_vminstance and/or method action_vminstance :return: None or the same vm_id. Raises an exception on fail """ self.logger.debug('FOS delete_vminstance with args: {}'.format(locals())) fduid = created_items.get('fdu_id') try: instance = self.fos_api.fdu.instance_info(vm_id) instance_list = self.fos_api.fdu.instance_list(instance.fdu_id) selected_node = '' for n in instance_list: instances = instance_list[n] if instance.uuid in instances: selected_node = n if selected_node == '': raise ValueError("Unable to find node for the given Instance") self.fos_api.fdu.stop(vm_id) for cp in instance.to_json()['connection_points']: nets = self.fos_api.network.list() for net in nets: if net.get('uuid') == cp['vld_ref']: self.fos_api.network.remove_network_from_node(net.get('uuid'), selected_node) self.fos_api.fdu.clean(vm_id) self.fos_api.fdu.undefine(vm_id) self.fos_api.fdu.offload(fduid) except Exception as e: raise vimconn.VimConnException("Error on deleting VM with id {}. Error {}".format(vm_id, e)) return vm_id
def __init__(self, error_msg): self.error_msg = error_msg for method in dir(vimconn.VimConnector): if method[0] != "_": setattr(self, method, Mock(side_effect=vimconn.VimConnException(error_msg)))
def new_vminstance(self, name, description, start, image_id, flavor_id, net_list, cloud_config=None, disk_list=None, availability_zone_index=None, availability_zone_list=None): """Adds a VM instance to VIM :param start: (boolean) indicates if VM must start or created in pause mode. :param image_id: :param flavor_id: image and flavor VIM id to use for the VM :param net_list: list of interfaces, each one is a dictionary with: 'name': (optional) name for the interface. 'net_id': VIM network id where this interface must be connect to. Mandatory for type==virtual 'vpci': (optional) virtual vPCI address to assign at the VM. Can be ignored depending on VIM capabilities 'model': (optional and only have sense for type==virtual) interface model: virtio, e1000, ... 'mac_address': (optional) mac address to assign to this interface 'ip_address': (optional) IP address to assign to this interface #TODO: CHECK if an optional 'vlan' parameter is needed for VIMs when type if VF and net_id is not provided, the VLAN tag to be used. In case net_id is provided, the internal network vlan is used for tagging VF 'type': (mandatory) can be one of: 'virtual', in this case always connected to a network of type 'net_type=bridge' 'PCI-PASSTHROUGH' or 'PF' (passthrough): depending on VIM capabilities it can be connected to a data/ptp network ot it can created unconnected 'SR-IOV' or 'VF' (SRIOV with VLAN tag): same as PF for network connectivity. 'VFnotShared'(SRIOV without VLAN tag) same as PF for network connectivity. VF where no other VFs are allocated on the same physical NIC 'bw': (optional) only for PF/VF/VFnotShared. Minimal Bandwidth required for the interface in GBPS 'port_security': (optional) If False it must avoid any traffic filtering at this interface. If missing or True, it must apply the default VIM behaviour After execution the method will add the key: 'vim_id': must be filled/added by this method with the VIM identifier generated by the VIM for this interface. 'net_list' is modified :param cloud_config: (optional) dictionary with: 'key-pairs': (optional) list of strings with the public key to be inserted to the default user 'users': (optional) list of users to be inserted, each item is a dict with: 'name': (mandatory) user name, 'key-pairs': (optional) list of strings with the public key to be inserted to the user 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init, or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file 'config-files': (optional). List of files to be transferred. Each item is a dict with: 'dest': (mandatory) string with the destination absolute path 'encoding': (optional, by default text). Can be one of: 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64' 'content' (mandatory): string with the content of the file 'permissions': (optional) string with file permissions, typically octal notation '0644' 'owner': (optional) file owner, string with the format 'owner:group' 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk) :param disk_list: (optional) list with additional disks to the VM. Each item is a dict with: 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted 'size': (mandatory) string with the size of the disk in GB :param availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required :param availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if availability_zone_index is None Returns a tuple with the instance identifier and created_items or raises an exception on error created_items can be None or a dictionary where this method can include key-values that will be passed to the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc. Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same as not present. """ self.logger.debug('new_vminstance with args: {}'.format(locals())) fdu_uuid = '{}'.format(uuid.uuid4()) flv = self.fos_api.flavor.get(flavor_id) img = self.fos_api.image.get(image_id) if flv is None: raise vimconn.VimConnNotFoundException("Flavor {} not found at VIM".format(flavor_id)) if img is None: raise vimconn.VimConnNotFoundException("Image {} not found at VIM".format(image_id)) created_items = { 'fdu_id': '', 'node_id': '', 'connection_points': [] } fdu_desc = { 'name': name, 'id': fdu_uuid, 'uuid': fdu_uuid, 'computation_requirements': flv, 'image': img, 'hypervisor': self.hv, 'migration_kind': 'LIVE', 'interfaces': [], 'io_ports': [], 'connection_points': [], 'depends_on': [], 'storage': [] } nets = [] cps = [] intf_id = 0 for n in net_list: cp_id = '{}'.format(uuid.uuid4()) n['vim_id'] = cp_id pair_id = n.get('net_id') cp_d = { 'id': cp_id, 'name': cp_id, 'vld_ref': pair_id } intf_d = { 'name': n.get('name', 'eth{}'.format(intf_id)), 'is_mgmt': False, 'if_type': 'INTERNAL', 'virtual_interface': { 'intf_type': n.get('model', 'VIRTIO'), 'vpci': n.get('vpci', '0:0:0'), 'bandwidth': int(n.get('bw', 100)) }, 'cp_id': cp_id } if n.get('mac_address', None) is not None: intf_d['mac_address'] = n['mac_address'] created_items['connection_points'].append(cp_id) fdu_desc['connection_points'].append(cp_d) fdu_desc['interfaces'].append(intf_d) intf_id = intf_id + 1 if cloud_config is not None: configuration = {'conf_type': 'CLOUD_INIT'} if cloud_config.get('user-data') is not None: configuration['script'] = cloud_config.get('user-data') if cloud_config.get('key-pairs') is not None: configuration['ssh_keys'] = cloud_config.get('key-pairs') if 'script' in configuration: fdu_desc['configuration'] = configuration self.logger.debug('Eclipse fog05 FDU Descriptor: {}'.format(fdu_desc)) fdu = FDU(fdu_desc) try: self.fos_api.fdu.onboard(fdu) instance = self.fos_api.fdu.define(fdu_uuid) instance_list = self.fos_api.fdu.instance_list(fdu_uuid) selected_node = '' for n in instance_list: instances = instance_list[n] if instance.uuid in instances: selected_node = n if selected_node == '': raise ValueError("Unable to find node for network creation") self.logger.debug('Selected node by VIM: {}'.format(selected_node)) created_items['fdu_id'] = fdu_uuid created_items['node_id'] = selected_node for cp in fdu_desc['connection_points']: nets = self.fos_api.network.list() for net in nets: if net.get('uuid') == cp['vld_ref']: self.fos_api.network.add_network_to_node(net, selected_node) self.fos_api.fdu.configure(instance.uuid) self.fos_api.fdu.start(instance.uuid) self.logger.debug('Eclipse fog05 FDU Started {}'.format(instance.uuid)) created_items['instance_id'] = str(instance.uuid) self.fdu_node_map[instance.uuid] = selected_node self.logger.debug('new_vminstance returns: {} {}'.format(instance.uuid, created_items)) return str(instance.uuid), created_items except fimapi.FIMAResouceExistingException as free: raise vimconn.VimConnConflictException("VM already exists at VIM. Error {}".format(free)) except Exception as e: raise vimconn.VimConnException("Error while instantiating VM {}. Error {}".format(name, e))
def new_network(self, net_name, net_type, ip_profile=None, shared=False, provider_network_profile=None): """Adds a tenant network to VIM Params: 'net_name': name of the network 'net_type': one of: 'bridge': overlay isolated network 'data': underlay E-LAN network for Passthrough and SRIOV interfaces 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces. 'ip_profile': is a dict containing the IP parameters of the network 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented) 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y 'gateway_address': (Optional) ip_schema, that is X.X.X.X 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X] 'dhcp_enabled': True or False 'dhcp_start_address': ip_schema, first IP to grant 'dhcp_count': number of IPs to grant. 'shared': if this network can be seen/use by other tenants/organization 'provider_network_profile': (optional) contains {segmentation-id: vlan, provider-network: vim_netowrk} Returns a tuple with the network identifier and created_items, or raises an exception on error created_items can be None or a dictionary where this method can include key-values that will be passed to the method delete_network. Can be used to store created segments, created l2gw connections, etc. Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same as not present. """ # oca library method cannot be used in this case (problem with cluster parameters) try: vlan = None if provider_network_profile: vlan = provider_network_profile.get("segmentation-id") created_items = {} one = self._new_one_connection() size = "254" if ip_profile is None: subnet_rand = random.randint(0, 255) ip_start = "192.168.{}.1".format(subnet_rand) else: index = ip_profile["subnet_address"].find("/") ip_start = ip_profile["subnet_address"][:index] if "dhcp_count" in ip_profile and ip_profile[ "dhcp_count"] is not None: size = str(ip_profile["dhcp_count"]) elif "dhcp_count" not in ip_profile and ip_profile[ "ip_version"] == "IPv4": prefix = ip_profile["subnet_address"][index + 1:] size = int(math.pow(2, 32 - prefix)) if "dhcp_start_address" in ip_profile and ip_profile[ "dhcp_start_address"] is not None: ip_start = str(ip_profile["dhcp_start_address"]) # if ip_profile["ip_version"] == "IPv6": # ip_prefix_type = "GLOBAL_PREFIX" if vlan is not None: vlan_id = vlan else: vlan_id = str(random.randint(100, 4095)) # if "internal" in net_name: # OpenNebula not support two networks with same name random_net_name = str(random.randint(1, 1000000)) net_name = net_name + random_net_name net_id = one.vn.allocate( { 'NAME': net_name, 'VN_MAD': '802.1Q', 'PHYDEV': self.config["network"]["phydev"], 'VLAN_ID': vlan_id }, self.config["cluster"]["id"]) arpool = { 'AR_POOL': { 'AR': { 'TYPE': 'IP4', 'IP': ip_start, 'SIZE': size } } } one.vn.add_ar(net_id, arpool) return net_id, created_items except Exception as e: self.logger.error("Create new network error: " + str(e)) raise vimconn.VimConnException(e)
def __init__(self, uuid, name, tenant_id, tenant_name, url, url_admin=None, user=None, passwd=None, log_level=None, config={}, persistent_info={}): """ Params: uuid - id asigned to this VIM name - name assigned to this VIM, can be used for logging tenant_id - ID to be used for tenant tenant_name - name of tenant to be used VIM tenant to be used url_admin - optional, url used for administrative tasks user - credentials of the VIM user passwd - credentials of the VIM user log_level - if must use a different log_level than the general one config - dictionary with misc VIM information region_name - name of region to deploy the instances vpc_cidr_block - default CIDR block for VPC security_groups - default security group to specify this instance persistent_info - dict where the class can store information that will be available among class destroy/creation cycles. This info is unique per VIM/credential. At first call it will contain an empty dict. Useful to store login/tokens information for speed up communication """ vimconn.VimConnector.__init__(self, uuid, name, tenant_id, tenant_name, url, url_admin, user, passwd, log_level, config, persistent_info) self.persistent_info = persistent_info self.a_creds = {} if user: self.a_creds['aws_access_key_id'] = user else: raise vimconn.VimConnAuthException("Username is not specified") if passwd: self.a_creds['aws_secret_access_key'] = passwd else: raise vimconn.VimConnAuthException("Password is not specified") if 'region_name' in config: self.region = config.get('region_name') else: raise vimconn.VimConnException( "AWS region_name is not specified at config") self.vpc_data = {} self.subnet_data = {} self.conn = None self.conn_vpc = None self.account_id = None self.vpc_id = self.get_tenant_list()[0]['id'] # we take VPC CIDR block if specified, otherwise we use the default CIDR # block suggested by AWS while creating instance self.vpc_cidr_block = '10.0.0.0/24' if tenant_id: self.vpc_id = tenant_id if 'vpc_cidr_block' in config: self.vpc_cidr_block = config['vpc_cidr_block'] self.security_groups = None if 'security_groups' in config: self.security_groups = config['security_groups'] self.key_pair = None if 'key_pair' in config: self.key_pair = config['key_pair'] self.flavor_info = None if 'flavor_info' in config: flavor_data = config.get('flavor_info') if isinstance(flavor_data, str): try: if flavor_data[0] == "@": # read from a file with open(flavor_data[1:], 'r') as stream: self.flavor_info = yaml.load(stream, Loader=yaml.Loader) else: self.flavor_info = yaml.load(flavor_data, Loader=yaml.Loader) except yaml.YAMLError as e: self.flavor_info = None raise vimconn.VimConnException( "Bad format at file '{}': {}".format( flavor_data[1:], e)) except IOError as e: raise vimconn.VimConnException( "Error reading file '{}': {}".format( flavor_data[1:], e)) elif isinstance(flavor_data, dict): self.flavor_info = flavor_data self.logger = logging.getLogger('openmano.vim.aws') if log_level: self.logger.setLevel(getattr(logging, log_level))
def new_vminstance(self, name, description, start, image_id, flavor_id, net_list, cloud_config=None, disk_list=None, availability_zone_index=None, availability_zone_list=None): """ Adds a VM instance to VIM :param name: :param description: :param start: (boolean) indicates if VM must start or created in pause mode. :param image_id: image VIM id to use for the VM :param flavor_id: flavor VIM id to use for the VM :param net_list: list of interfaces, each one is a dictionary with: 'name': (optional) name for the interface. 'net_id': VIM network id where this interface must be connect to. Mandatory for type==virtual 'vpci': (optional) virtual vPCI address to assign at the VM. Can be ignored depending on VIM capabilities 'model': (optional and only have sense for type==virtual) interface model: virtio, e1000, ... 'mac_address': (optional) mac address to assign to this interface 'ip_address': (optional) IP address to assign to this interface #TODO: CHECK if an optional 'vlan' parameter is needed for VIMs when type if VF and net_id is not provided, the VLAN tag to be used. In case net_id is provided, the internal network vlan is used for tagging VF 'type': (mandatory) can be one of: 'virtual', in this case always connected to a network of type 'net_type=bridge' 'PCI-PASSTHROUGH' or 'PF' (passthrough): depending on VIM capabilities it can be connected to a data/ptp network ot itcan created unconnected 'SR-IOV' or 'VF' (SRIOV with VLAN tag): same as PF for network connectivity. 'VFnotShared'(SRIOV without VLAN tag) same as PF for network connectivity. VF where no other VFs are allocated on the same physical NIC 'bw': (optional) only for PF/VF/VFnotShared. Minimal Bandwidth required for the interface in GBPS 'port_security': (optional) If False it must avoid any traffic filtering at this interface. If missing or True, it must apply the default VIM behaviour After execution the method will add the key: 'vim_id': must be filled/added by this method with the VIM identifier generated by the VIM for this interface. 'net_list' is modified :param cloud_config: (optional) dictionary with: 'key-pairs': (optional) list of strings with the public key to be inserted to the default user 'users': (optional) list of users to be inserted, each item is a dict with: 'name': (mandatory) user name, 'key-pairs': (optional) list of strings with the public key to be inserted to the user 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init, or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file 'config-files': (optional). List of files to be transferred. Each item is a dict with: 'dest': (mandatory) string with the destination absolute path 'encoding': (optional, by default text). Can be one of: 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64' 'content' (mandatory): string with the content of the file 'permissions': (optional) string with file permissions, typically octal notation '0644' 'owner': (optional) file owner, string with the format 'owner:group' 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk) :param disk_list: (optional) list with additional disks to the VM. Each item is a dict with: 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted 'size': (mandatory) string with the size of the disk in GB :param availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required :param availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if availability_zone_index is None :return: a tuple with the instance identifier and created_items or raises an exception on error created_items can be None or a dictionary where this method can include key-values that will be passed to the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc. Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same as not present. """ self.logger.debug( "new_vminstance input: image='{}' flavor='{}' nics='{}'".format( image_id, flavor_id, str(net_list))) try: one = self._new_one_connection() template_vim = one.template.info(int(flavor_id), True) disk_size = str(template_vim.TEMPLATE["DISK"]["SIZE"]) one = self._new_one_connection() template_updated = "" for net in net_list: net_in_vim = one.vn.info(int(net["net_id"])) net["vim_id"] = str(net_in_vim.ID) network = 'NIC = [NETWORK = "{}",NETWORK_UNAME = "{}" ]'.format( net_in_vim.NAME, net_in_vim.UNAME) template_updated += network template_updated += "DISK = [ IMAGE_ID = {},\n SIZE = {}]".format( image_id, disk_size) if isinstance(cloud_config, dict): if cloud_config.get("key-pairs"): context = 'CONTEXT = [\n NETWORK = "YES",\n SSH_PUBLIC_KEY = "' for key in cloud_config["key-pairs"]: context += key + '\n' # if False: # context += '"\n USERNAME = '******'"]' template_updated += context vm_instance_id = one.template.instantiate(int(flavor_id), name, False, template_updated) self.logger.info( "Instanciating in OpenNebula a new VM name:{} id:{}".format( name, flavor_id)) return str(vm_instance_id), None except pyone.OneNoExistsException as e: self.logger.error("Network with id " + str(e) + " not found: " + str(e)) raise vimconn.VimConnNotFoundException(e) except Exception as e: self.logger.error("Create new vm instance error: " + str(e)) raise vimconn.VimConnException(e)