def _create_service_instance(self, context, instance_name, network_info): """Creates service vm and sets up networking for it.""" service_image_id = self._get_service_image(context) key_name, key_path = self._get_key(context) if not (self.get_config_option("service_instance_password") or key_name): raise exception.ServiceInstanceException( _('Neither service instance password nor key are available.')) if not key_path: LOG.warning( 'No key path is available. May be non-existent key path is ' 'provided. Check path_to_private_key (current value ' '%(private_path)s) and path_to_public_key (current value ' '%(public_path)s) in manila configuration file.', dict(private_path=self.path_to_private_key, public_path=self.path_to_public_key)) network_data = self.network_helper.setup_network(network_info) fail_safe_data = dict(router_id=network_data.get('router_id'), subnet_id=network_data.get('subnet_id')) if network_data.get('service_port'): fail_safe_data['service_port_id'] = ( network_data['service_port']['id']) if network_data.get('public_port'): fail_safe_data['public_port_id'] = ( network_data['public_port']['id']) if network_data.get('admin_port'): fail_safe_data['admin_port_id'] = ( network_data['admin_port']['id']) try: create_kwargs = self._get_service_instance_create_kwargs() service_instance = self.compute_api.server_create( context, name=instance_name, image=service_image_id, flavor=self.get_config_option("service_instance_flavor_id"), key_name=key_name, nics=network_data['nics'], availability_zone=self.availability_zone, **create_kwargs) fail_safe_data['instance_id'] = service_instance['id'] service_instance = self.wait_for_instance_to_be_active( service_instance['id'], self.max_time_to_build_instance) if self.get_config_option("limit_ssh_access"): try: service_subnet = network_data['service_subnet'] except KeyError: LOG.error( "Unable to limit ssh access to instance id: '%s'!", fail_safe_data['instance_id']) raise exception.ManilaException( "Unable to limit SSH access - " "invalid service subnet details provided") else: service_subnet = False sec_groups = self._get_or_create_security_groups( context, allow_ssh_subnet=service_subnet) for sg in sec_groups: sg_id = sg['id'] LOG.debug("Adding security group '%(sg)s' to server '%(si)s'.", dict(sg=sg_id, si=service_instance["id"])) self.compute_api.add_security_group_to_server( context, service_instance["id"], sg_id) ip = (network_data.get( 'service_port', network_data.get('admin_port'))['fixed_ips']) service_instance['ip'] = ip[0]['ip_address'] public_ip = (network_data.get( 'public_port', network_data.get('service_port'))['fixed_ips']) service_instance['public_address'] = public_ip[0]['ip_address'] except Exception as e: e.detail_data = {'server_details': fail_safe_data} raise service_instance.update(fail_safe_data) service_instance['pk_path'] = key_path for pair in [('router', 'router_id'), ('service_subnet', 'subnet_id')]: if pair[0] in network_data and 'id' in network_data[pair[0]]: service_instance[pair[1]] = network_data[pair[0]]['id'] admin_port = network_data.get('admin_port') if admin_port: try: service_instance['admin_ip'] = ( admin_port['fixed_ips'][0]['ip_address']) except Exception: msg = _("Admin port is being used but Admin IP was not found.") LOG.exception(msg) raise exception.AdminIPNotFound(reason=msg) return service_instance
def _create_service_instance(self, context, instance_name, network_info): """Creates service vm and sets up networking for it.""" service_image_id = self._get_service_image(context) key_name, key_path = self._get_key(context) if not (self.get_config_option("service_instance_password") or key_name): raise exception.ServiceInstanceException( _('Neither service instance password nor key are available.')) if not key_path: LOG.warning( _LW('No key path is available. May be non-existent key path is ' 'provided. Check path_to_private_key (current value ' '%(private_path)s) and path_to_public_key (current value ' '%(public_path)s) in manila configuration file.'), dict(private_path=self.path_to_private_key, public_path=self.path_to_public_key)) network_data = self.network_helper.setup_network(network_info) fail_safe_data = dict(router_id=network_data.get('router_id'), subnet_id=network_data.get('subnet_id')) if network_data.get('service_port'): fail_safe_data['service_port_id'] = ( network_data['service_port']['id']) if network_data.get('public_port'): fail_safe_data['public_port_id'] = ( network_data['public_port']['id']) if network_data.get('admin_port'): fail_safe_data['admin_port_id'] = ( network_data['admin_port']['id']) try: create_kwargs = self._get_service_instance_create_kwargs() service_instance = self.compute_api.server_create( context, name=instance_name, image=service_image_id, flavor=self.get_config_option("service_instance_flavor_id"), key_name=key_name, nics=network_data['nics'], availability_zone=CONF.storage_availability_zone, **create_kwargs) fail_safe_data['instance_id'] = service_instance['id'] service_instance = self.wait_for_instance_to_be_active( service_instance['id'], self.max_time_to_build_instance) security_group = self._get_or_create_security_group(context) if security_group: if self.network_helper.NAME == NOVA_NAME: # NOTE(vponomaryov): Nova-network allows to assign # secgroups only by names. sg_id = security_group.name else: sg_id = security_group.id LOG.debug("Adding security group '%(sg)s' to server '%(si)s'.", dict(sg=sg_id, si=service_instance["id"])) self.compute_api.add_security_group_to_server( context, service_instance["id"], sg_id) if self.network_helper.NAME == NEUTRON_NAME: ip = (network_data.get( 'service_port', network_data.get('admin_port'))['fixed_ips']) service_instance['ip'] = ip[0]['ip_address'] public_ip = (network_data.get( 'public_port', network_data.get('service_port'))['fixed_ips']) service_instance['public_address'] = public_ip[0]['ip_address'] else: net_name = self.network_helper.get_network_name(network_info) service_instance['ip'] = self._get_server_ip( service_instance, net_name) service_instance['public_address'] = service_instance['ip'] except Exception as e: e.detail_data = {'server_details': fail_safe_data} raise service_instance.update(fail_safe_data) service_instance['pk_path'] = key_path for pair in [('router', 'router_id'), ('service_subnet', 'subnet_id')]: if pair[0] in network_data and 'id' in network_data[pair[0]]: service_instance[pair[1]] = network_data[pair[0]]['id'] admin_port = network_data.get('admin_port') if admin_port: try: service_instance['admin_ip'] = ( admin_port['fixed_ips'][0]['ip_address']) except Exception: msg = _("Admin port is being used but Admin IP was not found.") LOG.exception(msg) raise exception.AdminIPNotFound(reason=msg) return service_instance