X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=snaps%2Fopenstack%2Fcreate_instance.py;h=b09e8792025daf2992117dd105389d34335d55e1;hb=49aaa5d61e87e11c5d5b9ce7dd2fa598f16b82a7;hp=189ea5756db026fe84b7579bd5a17103cf8f0dbf;hpb=0050ed771d89f6a758a387a930feeb616dceecec;p=snaps.git diff --git a/snaps/openstack/create_instance.py b/snaps/openstack/create_instance.py index 189ea57..b09e879 100644 --- a/snaps/openstack/create_instance.py +++ b/snaps/openstack/create_instance.py @@ -57,9 +57,6 @@ class OpenStackVmInstance: self.image_settings = image_settings self.keypair_settings = keypair_settings - # TODO - get rid of FIP list and only use the dict(). Need to fix - # populating this object when already exists - self.__floating_ips = list() self.__floating_ip_dict = dict() # Instantiated in self.create() @@ -94,21 +91,22 @@ class OpenStackVmInstance: VM with the same name already exists within the project """ - servers = nova_utils.get_servers_by_name(self.__nova, - self.instance_settings.name) - for server in servers: + server = nova_utils.get_server( + self.__nova, vm_inst_settings=self.instance_settings) + if server: if server.name == self.instance_settings.name: self.__vm = server logger.info( 'Found existing machine with name - %s', self.instance_settings.name) - fips = neutron_utils.get_floating_ips(self.__neutron) - for fip in fips: - for subnet_name, ips in server.networks.items(): - if fip.ip in ips: - self.__floating_ips.append(fip) - # TODO - Determine a means to associate to the FIP - # configuration and add to FIP map + + fips = neutron_utils.get_floating_ips(self.__neutron, + self.__ports) + for port_name, fip in fips: + settings = self.instance_settings.floating_ip_settings + for fip_setting in settings: + if port_name == fip_setting.port_name: + self.__floating_ip_dict[fip_setting.name] = fip def __create_vm(self, block=False): """ @@ -126,7 +124,7 @@ class OpenStackVmInstance: if block: if not self.vm_active(block=True): - raise Exception( + raise VmInstanceCreationError( 'Fatal error, VM did not become ACTIVE within the alloted ' 'time') @@ -136,7 +134,7 @@ class OpenStackVmInstance: nova_utils.add_security_group(self.__nova, self.__vm, sec_grp_name) else: - raise Exception( + raise VmInstanceCreationError( 'Cannot applying security group with name ' + sec_grp_name + ' to VM that did not activate with name - ' + @@ -157,7 +155,7 @@ class OpenStackVmInstance: port = port_dict.get(floating_ip_setting.port_name) if not port: - raise Exception( + raise VmInstanceCreationError( 'Cannot find port object with name - ' + floating_ip_setting.port_name) @@ -166,11 +164,11 @@ class OpenStackVmInstance: ext_gateway = self.__ext_gateway_by_router( floating_ip_setting.router_name) if ext_gateway: - subnet = neutron_utils.get_subnet_by_name( - self.__neutron, floating_ip_setting.subnet_name) + subnet = neutron_utils.get_subnet( + self.__neutron, + subnet_name=floating_ip_setting.subnet_name) floating_ip = neutron_utils.create_floating_ip( self.__neutron, ext_gateway) - self.__floating_ips.append(floating_ip) self.__floating_ip_dict[floating_ip_setting.name] = floating_ip logger.info( @@ -178,9 +176,9 @@ class OpenStackVmInstance: floating_ip_setting.router_name) self.__add_floating_ip(floating_ip, port, subnet) else: - raise Exception('Unable to add floating IP to port,' - ' cannot locate router with an external ' - 'gateway ') + raise VmInstanceCreationError( + 'Unable to add floating IP to port, cannot locate router ' + 'with an external gateway ') def __ext_gateway_by_router(self, router_name): """ @@ -189,7 +187,8 @@ class OpenStackVmInstance: :param router_name: The name of the router to lookup :return: the external network name or None """ - router = neutron_utils.get_router_by_name(self.__neutron, router_name) + router = neutron_utils.get_router( + self.__neutron, router_name=router_name) if router and router.external_gateway_info: network = neutron_utils.get_network_by_id( self.__neutron, @@ -204,13 +203,12 @@ class OpenStackVmInstance: """ # Cleanup floating IPs - for floating_ip in self.__floating_ips: + for name, floating_ip in self.__floating_ip_dict.items(): try: logger.info('Deleting Floating IP - ' + floating_ip.ip) neutron_utils.delete_floating_ip(self.__neutron, floating_ip) except Exception as e: logger.error('Error deleting Floating IP - ' + str(e)) - self.__floating_ips = list() self.__floating_ip_dict = dict() # Cleanup ports @@ -263,10 +261,10 @@ class OpenStackVmInstance: ports = list() for port_setting in port_settings: - port = neutron_utils.get_port_by_name(self.__neutron, - port_setting.name) + port = neutron_utils.get_port( + self.__neutron, port_settings=port_setting) if port: - ports.append((port_setting.name, {'port': port})) + ports.append((port_setting.name, port)) elif not cleanup: # Exception will be raised when port with same name already # exists @@ -287,8 +285,8 @@ class OpenStackVmInstance: if subnet: # Take IP of subnet if there is one configured on which to place # the floating IP - for fixed_ip in port.fixed_ips: - if fixed_ip['subnet_id'] == subnet['subnet']['id']: + for fixed_ip in port.ips: + if fixed_ip['subnet_id'] == subnet.id: ip = fixed_ip['ip_address'] break else: @@ -314,11 +312,12 @@ class OpenStackVmInstance: count -= 1 pass else: - raise Exception( + raise VmInstanceCreationError( 'Unable find IP address on which to place the floating IP') logger.error('Timeout attempting to add the floating IP to instance.') - raise Exception('Timeout while attempting add floating IP to instance') + raise VmInstanceCreationError( + 'Timeout while attempting add floating IP to instance') def get_os_creds(self): """ @@ -353,8 +352,8 @@ class OpenStackVmInstance: port = self.get_port_by_name(port_name) if port: if subnet_name: - subnet = neutron_utils.get_subnet_by_name(self.__neutron, - subnet_name) + subnet = neutron_utils.get_subnet( + self.__neutron, subnet_name=subnet_name) if not subnet: logger.warning('Cannot retrieve port IP as subnet could ' 'not be located with name - %s', @@ -405,19 +404,20 @@ class OpenStackVmInstance: """ Responsible for configuring NICs on RPM systems where the instance has more than one configured port - :return: None + :return: the value returned by ansible_utils.apply_ansible_playbook() """ - if len(self.__ports) > 1 and len(self.__floating_ips) > 0: + if len(self.__ports) > 1 and len(self.__floating_ip_dict) > 0: if self.vm_active(block=True) and self.vm_ssh_active(block=True): for key, port in self.__ports: port_index = self.__ports.index((key, port)) if port_index > 0: nic_name = 'eth' + repr(port_index) - self.__config_nic( + retval = self.__config_nic( nic_name, port, self.__get_first_provisioning_floating_ip().ip) logger.info('Configured NIC - %s on VM - %s', nic_name, self.instance_settings.name) + return retval def __get_first_provisioning_floating_ip(self): """ @@ -430,8 +430,9 @@ class OpenStackVmInstance: fip = self.__floating_ip_dict.get(floating_ip_setting.name) if fip: return fip - elif len(self.__floating_ips) > 0: - return self.__floating_ips[0] + elif len(self.__floating_ip_dict) > 0: + for key, fip in self.__floating_ip_dict.items(): + return fip def __config_nic(self, nic_name, port, ip): """ @@ -567,7 +568,8 @@ class OpenStackVmInstance: return False if status == 'ERROR': - raise Exception('Instance had an error during deployment') + raise VmInstanceCreationError( + 'Instance had an error during deployment') logger.debug( 'Instance status [%s] is - %s', self.instance_settings.name, status) @@ -612,9 +614,10 @@ class OpenStackVmInstance: Returns True when can create a SSH session else False :return: T/F """ - if len(self.__floating_ips) > 0: + if len(self.__floating_ip_dict) > 0: ssh = self.ssh_client() if ssh: + ssh.close() return True return False @@ -628,9 +631,8 @@ class OpenStackVmInstance: fip = None if fip_name and self.__floating_ip_dict.get(fip_name): return self.__floating_ip_dict.get(fip_name) - if not fip and len(self.__floating_ips) > 0: - return self.__floating_ips[0] - return None + if not fip: + return self.__get_first_provisioning_floating_ip() def ssh_client(self, fip_name=None): """ @@ -642,7 +644,8 @@ class OpenStackVmInstance: fip = self.get_floating_ip(fip_name) if fip: return ansible_utils.ssh_client( - self.__floating_ips[0].ip, self.get_image_user(), + self.__get_first_provisioning_floating_ip().ip, + self.get_image_user(), self.keypair_settings.private_filepath, proxy_settings=self.__os_creds.proxy_settings) else: @@ -742,7 +745,7 @@ class VmInstanceSettings: elif isinstance(kwargs['security_group_names'], str): self.security_group_names = [kwargs['security_group_names']] else: - raise Exception( + raise VmInstanceSettingsError( 'Invalid data type for security_group_names attribute') else: self.security_group_names = set() @@ -780,11 +783,11 @@ class VmInstanceSettings: self.availability_zone = None if not self.name or not self.flavor: - raise Exception( + raise VmInstanceSettingsError( 'Instance configuration requires the attributes: name, flavor') if len(self.port_settings) == 0: - raise Exception( + raise VmInstanceSettingsError( 'Instance configuration requires port settings (aka. NICS)') @@ -821,6 +824,24 @@ class FloatingIpSettings: self.provisioning = True if not self.name or not self.port_name or not self.router_name: - raise Exception( + raise FloatingIpSettingsError( 'The attributes name, port_name and router_name are required ' 'for FloatingIPSettings') + + +class VmInstanceSettingsError(Exception): + """ + Exception to be thrown when an VM instance settings are incorrect + """ + + +class FloatingIpSettingsError(Exception): + """ + Exception to be thrown when an VM instance settings are incorrect + """ + + +class VmInstanceCreationError(Exception): + """ + Exception to be thrown when an VM instance cannot be created + """