def resize_flavor(self, new_flavor_id): self.validate_can_perform_action() LOG.debug("resizing instance %s flavor to %s" % (self.id, new_flavor_id)) # Validate that the flavor can be found and that it isn't the same size # as the current one. client = create_nova_client(self.context) try: new_flavor = client.flavors.get(new_flavor_id) except nova_exceptions.NotFound: raise exception.FlavorNotFound(uuid=new_flavor_id) old_flavor = client.flavors.get(self.flavor_id) new_flavor_size = new_flavor.ram old_flavor_size = old_flavor.ram if CONF.reddwarf_volume_support: if new_flavor.ephemeral != 0: raise exception.LocalStorageNotSupported() if new_flavor_size == old_flavor_size: raise exception.CannotResizeToSameSize() elif CONF.device_path is not None: # ephemeral support enabled if new_flavor.ephemeral == 0: raise exception.LocalStorageNotSpecified(flavor=new_flavor_id) if (new_flavor_size == old_flavor_size and new_flavor.ephemeral == new_flavor.ephemeral): raise exception.CannotResizeToSameSize() # Set the task to RESIZING and begin the async call before returning. self.update_db(task_status=InstanceTasks.RESIZING) LOG.debug("Instance %s set to RESIZING." % self.id) task_api.API(self.context).resize_flavor(self.id, new_flavor_id, old_flavor_size, new_flavor_size)
def create(cls, context, name, flavor_id, image_id, databases, users, service_type, volume_size): client = create_nova_client(context) try: flavor = client.flavors.get(flavor_id) except nova_exceptions.NotFound: raise exception.FlavorNotFound(uuid=flavor_id) db_info = DBInstance.create(name=name, flavor_id=flavor_id, tenant_id=context.tenant, volume_size=volume_size, task_status=InstanceTasks.BUILDING) LOG.debug(_("Tenant %s created new Reddwarf instance %s...") % (context.tenant, db_info.id)) service_status = InstanceServiceStatus.create( instance_id=db_info.id, status=ServiceStatuses.NEW) dns_support = config.Config.get("reddwarf_dns_support", 'False') if utils.bool_from_string(dns_support): dns_client = create_dns_client(context) hostname = dns_client.determine_hostname(db_info.id) db_info.hostname = hostname db_info.save() task_api.API(context).create_instance(db_info.id, name, flavor_id, flavor.ram, image_id, databases, users, service_type, volume_size) return SimpleInstance(context, db_info, service_status)
def create(cls, context, name, flavor_ref, image_id, databases, service_type, volume_size): db_info = DBInstance.create(name=name, task_status=InstanceTasks.NONE) LOG.debug(_("Created new Reddwarf instance %s...") % db_info.id) volume, volume_info = cls._create_volume(context, db_info, volume_size) client = create_nova_client(context) files = {"/etc/guest_info": "guest_id=%s\nservice_type=%s\n" % (db_info.id, service_type)} server = client.servers.create(name, image_id, flavor_ref, files=files, block_device_mapping=volume_info['block_device']) LOG.debug(_("Created new compute instance %s.") % server.id) db_info.compute_instance_id = server.id db_info.save() service_status = InstanceServiceStatus.create(instance_id=db_info.id, status=ServiceStatuses.NEW) # Now wait for the response from the create to do additional work guest = create_guest_client(context, db_info.id) # populate the databases model_schemas = populate_databases(databases) guest.prepare(512, model_schemas, users=[], device_path=volume_info['device_path'], mount_point=volume_info['mount_point']) return Instance(context, db_info, server, service_status, volume)
def load(context): def load_simple_instance(context, db, status): return SimpleInstance(context, db, status) if context is None: raise TypeError("Argument context not defined.") client = create_nova_client(context) servers = client.servers.list() db_infos = DBInstance.find_all(tenant_id=context.tenant, deleted=False) limit = int(context.limit or Instances.DEFAULT_LIMIT) if limit > Instances.DEFAULT_LIMIT: limit = Instances.DEFAULT_LIMIT data_view = DBInstance.find_by_pagination('instances', db_infos, "foo", limit=limit, marker=context.marker) next_marker = data_view.next_page_marker find_server = create_server_list_matcher(servers) for db in db_infos: LOG.debug("checking for db [id=%s, compute_instance_id=%s]" % (db.id, db.compute_instance_id)) ret = Instances._load_servers_status(load_simple_instance, context, data_view.collection, find_server) return ret, next_marker
def _create_dns_entry(self): LOG.debug("%s: Creating dns entry for instance: %s" % (greenthread.getcurrent(), self.id)) dns_client = create_dns_client(self.context) dns_support = config.Config.get("reddwarf_dns_support", 'False') LOG.debug(_("reddwarf dns support = %s") % dns_support) nova_client = create_nova_client(self.context) if utils.bool_from_string(dns_support): def get_server(): c_id = self.db_info.compute_instance_id return nova_client.servers.get(c_id) def ip_is_available(server): LOG.info("Polling for ip addresses: $%s " % server.addresses) if server.addresses != {}: return True elif server.addresses == {} and\ server.status != InstanceStatus.ERROR: return False elif server.addresses == {} and\ server.status == InstanceStatus.ERROR: LOG.error(_("Instance IP not available, instance (%s): " "server had status (%s).") % (self.id, server.status)) raise ReddwarfError(status=server.status) poll_until(get_server, ip_is_available, sleep_time=1, time_out=60 * 2) server = nova_client.servers.get(self.db_info.compute_instance_id) LOG.info("Creating dns entry...") dns_client.create_instance_entry(self.id, get_ip_address(server.addresses))
def create(cls, context, name, flavor_id, image_id, databases, users, service_type, volume_size): client = create_nova_client(context) try: flavor = client.flavors.get(flavor_id) except nova_exceptions.NotFound: raise exception.FlavorNotFound(uuid=flavor_id) db_info = DBInstance.create(name=name, flavor_id=flavor_id, tenant_id=context.tenant, volume_size=volume_size, task_status=InstanceTasks.BUILDING) LOG.debug( _("Tenant %s created new Reddwarf instance %s...") % (context.tenant, db_info.id)) service_status = InstanceServiceStatus.create( instance_id=db_info.id, status=ServiceStatuses.NEW) dns_support = config.Config.get("reddwarf_dns_support", 'False') if utils.bool_from_string(dns_support): dns_client = create_dns_client(context) hostname = dns_client.determine_hostname(db_info.id) db_info.hostname = hostname db_info.save() task_api.API(context).create_instance(db_info.id, name, flavor_id, flavor.ram, image_id, databases, users, service_type, volume_size) return SimpleInstance(context, db_info, service_status)
def _create_server_volume(self, flavor_id, image_id, service_type, volume_size): server = None try: nova_client = create_nova_client(self.context) files = {"/etc/guest_info": "--guest_id=%s\n--service_type=%s\n" % (self.id, service_type)} name = self.hostname or self.name volume_desc = ("mysql volume for %s" % self.id) volume_name = ("mysql-%s" % self.id) volume_ref = {'size': volume_size, 'name': volume_name, 'description': volume_desc} server = nova_client.servers.create(name, image_id, flavor_id, files=files, volume=volume_ref) LOG.debug(_("Created new compute instance %s.") % server.id) server_dict = server._info LOG.debug("Server response: %s" % server_dict) volume_id = None for volume in server_dict.get('os:volumes', []): volume_id = volume.get('id') # Record the server ID and volume ID in case something goes wrong. self.update_db(compute_instance_id=server.id, volume_id=volume_id) except Exception as e: msg = "Error creating server and volume for instance." err = inst_models.InstanceTasks.BUILDING_ERROR_SERVER self._log_and_raise(e, msg, err) device_path = config.Config.get('device_path', '/dev/vdb') mount_point = config.Config.get('mount_point', '/var/lib/mysql') volume_info = {'device_path': device_path, 'mount_point': mount_point} return server, volume_info
def resize_flavor(self, new_flavor_id): self.validate_can_perform_action() LOG.debug("resizing instance %s flavor to %s" % (self.id, new_flavor_id)) # Validate that the flavor can be found and that it isn't the same size # as the current one. client = create_nova_client(self.context) try: new_flavor = client.flavors.get(new_flavor_id) except nova_exceptions.NotFound: raise exception.FlavorNotFound(uuid=new_flavor_id) old_flavor = client.flavors.get(self.flavor_id) new_flavor_size = new_flavor.ram old_flavor_size = old_flavor.ram if CONF.reddwarf_volume_support: if new_flavor.ephemeral != 0: raise exception.LocalStorageNotSupported() if new_flavor_size == old_flavor_size: raise exception.CannotResizeToSameSize() elif CONF.device_path is not None: # ephemeral support enabled if new_flavor.ephemeral == 0: raise exception.LocalStorageNotSpecified(flavor=new_flavor_id) if (new_flavor_size == old_flavor_size and new_flavor.ephemeral == new_flavor.ephemeral): raise exception.CannotResizeToSameSize() # Set the task to RESIZING and begin the async call before returning. self.update_db(task_status=InstanceTasks.RESIZING) LOG.debug("Instance %s set to RESIZING." % self.id) task_api.API(self.context).resize_flavor(self.id, new_flavor_id, old_flavor_size, new_flavor_size)
def load(context): def load_simple_instance(context, db, status): return SimpleInstance(context, db, status) if context is None: raise TypeError("Argument context not defined.") client = create_nova_client(context) servers = client.servers.list() db_infos = DBInstance.find_all(tenant_id=context.tenant, deleted=False) limit = int(context.limit or Instances.DEFAULT_LIMIT) if limit > Instances.DEFAULT_LIMIT: limit = Instances.DEFAULT_LIMIT data_view = DBInstance.find_by_pagination('instances', db_infos, "foo", limit=limit, marker=context.marker) next_marker = data_view.next_page_marker find_server = create_server_list_matcher(servers) for db in db_infos: LOG.debug("checking for db [id=%s, compute_instance_id=%s]" % (db.id, db.compute_instance_id)) ret = Instances._load_servers_status(load_simple_instance, context, data_view.collection, find_server) return ret, next_marker
def load(context, id): client = create_nova_client(context) account = client.accounts.get_instances(id) db_infos = DBInstance.find_all(tenant_id=id, deleted=False) servers = [Server(server) for server in account.servers] instances = MgmtInstances.load_status_from_existing(context, db_infos, servers) return Account(id, instances)
def load(context, id): client = create_nova_client(context) account = client.accounts.get_instances(id) db_infos = DBInstance.find_all(tenant_id=id, deleted=False) servers = [Server(server) for server in account.servers] instances = MgmtInstances.load_status_from_existing( context, db_infos, servers) return Account(id, instances)
def _create_server(self, flavor_id, image_id, service_type, block_device_mapping): nova_client = create_nova_client(self.context) files = {"/etc/guest_info": "guest_id=%s\nservice_type=%s\n" % (self.id, service_type)} name = self.hostname or self.name bdmap = block_device_mapping server = nova_client.servers.create(name, image_id, flavor_id, files=files, block_device_mapping=bdmap) LOG.debug(_("Created new compute instance %s.") % server.id) return server
def load(context): if context is None: raise TypeError("Argument context not defined.") client = create_nova_client(context) servers = client.servers.list() volume_client = create_nova_volume_client(context) try: volumes = volume_client.volumes.list(detailed=False) except nova_exceptions.NotFound: volumes = [] db_infos = DBInstance.find_all() limit = int(context.limit or Instances.DEFAULT_LIMIT) if limit > Instances.DEFAULT_LIMIT: limit = Instances.DEFAULT_LIMIT data_view = DBInstance.find_by_pagination('instances', db_infos, "foo", limit=limit, marker=context.marker) next_marker = data_view.next_page_marker ret = [] find_server = create_server_list_matcher(servers) find_volumes = create_volumes_list_matcher(volumes) for db in db_infos: LOG.debug("checking for db [id=%s, compute_instance_id=%s]" % (db.id, db.compute_instance_id)) for db in data_view.collection: try: # TODO(hub-cap): Figure out if this is actually correct. # We are not sure if we should be doing some validation. # Basically if the server find returns nothing, but we # have something, there is a mismatch between what the # nova db has compared to what we have. We should have # a way to handle this. server = find_server(db.id, db.compute_instance_id) volumes = find_volumes(server.id) status = InstanceServiceStatus.find_by(instance_id=db.id) LOG.info(_("Server api_status(%s)") % (status.status.api_status)) if not status.status: LOG.info(_("Server status could not be read for " "instance id(%s)") % (db.compute_instance_id)) continue if status.status.api_status in ['SHUTDOWN']: LOG.info(_("Server was shutdown id(%s)") % (db.compute_instance_id)) continue except rd_exceptions.ComputeInstanceNotFound: LOG.info(_("Could not find server %s") % db.compute_instance_id) continue except ModelNotFoundError: LOG.info(_("Status entry not found either failed to start " "or instance was deleted")) continue ret.append(Instance(context, db, server, status, volumes)) return ret, next_marker
def load_all(context): client = create_nova_client(context) LOG.debug("Client.rdhosts=" + str(client.rdhosts)) rdhosts = client.rdhosts.list() LOG.debug("RDHOSTS=" + str(rdhosts)) for rdhost in rdhosts: LOG.debug("rdhost=" + str(rdhost)) return [SimpleHost(rdhost.name, rdhost.instanceCount) for rdhost in rdhosts]
def load_mgmt_instances(context, deleted=None): client = create_nova_client(context) mgmt_servers = client.rdservers.list() db_infos = None if deleted is not None: db_infos = instance_models.DBInstance.find_all(deleted=deleted) else: db_infos = instance_models.DBInstance.find_all() instances = MgmtInstances.load_status_from_existing(context, db_infos, mgmt_servers) return instances
def load_all(context): client = create_nova_client(context) LOG.debug("Client.rdhosts=" + str(client.rdhosts)) rdhosts = client.rdhosts.list() LOG.debug("RDHOSTS=" + str(rdhosts)) for rdhost in rdhosts: LOG.debug("rdhost=" + str(rdhost)) return [ SimpleHost(rdhost.name, rdhost.instanceCount) for rdhost in rdhosts ]
def load_server(context, instance_id, server_id): """Loads a server or raises an exception.""" client = create_nova_client(context) try: server = client.servers.get(server_id) except nova_exceptions.NotFound as e: LOG.debug("Could not find nova server_id(%s)" % server_id) raise exception.ComputeInstanceNotFound(instance_id=instance_id, server_id=server_id) except nova_exceptions.ClientException, e: raise exception.ReddwarfError(str(e))
def load_server(context, instance_id, server_id): """Loads a server or raises an exception.""" client = create_nova_client(context) try: server = client.servers.get(server_id) volumes = load_volumes(context, server_id, client=client) except nova_exceptions.NotFound, e: LOG.debug("Could not find nova server_id(%s)" % server_id) raise rd_exceptions.ComputeInstanceNotFound(instance_id=instance_id, server_id=server_id)
def load_mgmt_instances(context, deleted=None): client = create_nova_client(context) mgmt_servers = client.rdservers.list() db_infos = None if deleted is not None: db_infos = instance_models.DBInstance.find_all(deleted=deleted) else: db_infos = instance_models.DBInstance.find_all() instances = MgmtInstances.load_status_from_existing( context, db_infos, mgmt_servers) return instances
def load_server(context, instance_id, server_id): """Loads a server or raises an exception.""" client = create_nova_client(context) try: server = client.servers.get(server_id) except nova_exceptions.NotFound as e: LOG.debug("Could not find nova server_id(%s)" % server_id) raise exception.ComputeInstanceNotFound(instance_id=instance_id, server_id=server_id) except nova_exceptions.ClientException, e: raise exception.ReddwarfError(str(e))
def __init__(self, flavor=None, context=None, flavor_id=None): if flavor: self.flavor = flavor return if flavor_id and context: try: client = create_nova_client(context) self.flavor = client.flavors.get(flavor_id) except nova_exceptions.NotFound, e: raise exception.NotFound(uuid=flavor_id) except nova_exceptions.ClientException, e: raise exception.ReddwarfError(str(e))
def __init__(self, flavor=None, context=None, flavor_id=None): if flavor: self.flavor = flavor return if flavor_id and context: try: client = create_nova_client(context) self.flavor = client.flavors.get(flavor_id) except nova_exceptions.NotFound, e: raise exception.NotFound(uuid=flavor_id) except nova_exceptions.ClientException, e: raise exception.ReddwarfError(str(e))
def load_simple_instance_server_status(context, db_info): """Loads a server or raises an exception.""" if 'BUILDING' == db_info.task_status.action: db_info.server_status = "BUILD" db_info.addresses = {} else: client = create_nova_client(context) try: server = client.servers.get(db_info.compute_instance_id) db_info.server_status = server.status db_info.addresses = server.addresses except nova_exceptions.NotFound, e: db_info.server_status = "SHUTDOWN" db_info.addresses = {}
def load_mgmt_instance(cls, context, id): try: instance = load_instance(cls, context, id, needs_server=True) client = create_nova_client(context) server = client.rdservers.get(instance.server_id) instance.server.host = server.host instance.server.deleted = server.deleted instance.server.deleted_at = server.deleted_at instance.server.local_id = server.local_id assert instance.server is not None except Exception as e: LOG.error(e) instance = load_instance(cls, context, id, needs_server=False) return instance
def load_mgmt_instance(cls, context, id): try: instance = load_instance(cls, context, id, needs_server=True) client = create_nova_client(context) server = client.rdservers.get(instance.server_id) instance.server.host = server.host instance.server.deleted = server.deleted instance.server.deleted_at = server.deleted_at instance.server.local_id = server.local_id assert instance.server is not None except Exception as e: LOG.error(e) instance = load_instance(cls, context, id, needs_server=False) return instance
def load_simple_instance_server_status(context, db_info): """Loads a server or raises an exception.""" if 'BUILDING' == db_info.task_status.action: db_info.server_status = "BUILD" db_info.addresses = {} else: client = create_nova_client(context) try: server = client.servers.get(db_info.compute_instance_id) db_info.server_status = server.status db_info.addresses = server.addresses except nova_exceptions.NotFound, e: db_info.server_status = "SHUTDOWN" db_info.addresses = {}
def _create_resources(): client = create_nova_client(context) security_groups = None try: flavor = client.flavors.get(flavor_id) except nova_exceptions.NotFound: raise exception.FlavorNotFound(uuid=flavor_id) if backup_id is not None: backup_info = Backup.get_by_id(backup_id) if backup_info.is_running: raise exception.BackupNotCompleteError(backup_id=backup_id) location = backup_info.location LOG.info(_("Checking if backup exist in '%s'") % location) if not Backup.check_object_exist(context, location): raise exception.BackupFileNotFound(location=location) db_info = DBInstance.create(name=name, flavor_id=flavor_id, tenant_id=context.tenant, volume_size=volume_size, task_status=InstanceTasks.BUILDING) LOG.debug( _("Tenant %s created new Reddwarf instance %s...") % (context.tenant, db_info.id)) service_status = InstanceServiceStatus.create( instance_id=db_info.id, status=ServiceStatuses.NEW) if CONF.reddwarf_dns_support: dns_client = create_dns_client(context) hostname = dns_client.determine_hostname(db_info.id) db_info.hostname = hostname db_info.save() if CONF.reddwarf_security_groups_support: security_group = SecurityGroup.create_for_instance( db_info.id, context) security_groups = [security_group["name"]] task_api.API(context).create_instance(db_info.id, name, flavor_id, flavor.ram, image_id, databases, users, service_type, volume_size, security_groups, backup_id) return SimpleInstance(context, db_info, service_status)
def _create_resources(): client = create_nova_client(context) security_groups = None try: flavor = client.flavors.get(flavor_id) except nova_exceptions.NotFound: raise exception.FlavorNotFound(uuid=flavor_id) if backup_id is not None: backup_info = Backup.get_by_id(backup_id) if backup_info.is_running: raise exception.BackupNotCompleteError(backup_id=backup_id) location = backup_info.location LOG.info(_("Checking if backup exist in '%s'") % location) if not Backup.check_object_exist(context, location): raise exception.BackupFileNotFound(location=location) db_info = DBInstance.create(name=name, flavor_id=flavor_id, tenant_id=context.tenant, volume_size=volume_size, task_status=InstanceTasks.BUILDING) LOG.debug(_("Tenant %s created new Reddwarf instance %s...") % (context.tenant, db_info.id)) service_status = InstanceServiceStatus.create( instance_id=db_info.id, status=ServiceStatuses.NEW) if CONF.reddwarf_dns_support: dns_client = create_dns_client(context) hostname = dns_client.determine_hostname(db_info.id) db_info.hostname = hostname db_info.save() if CONF.reddwarf_security_groups_support: security_group = SecurityGroup.create_for_instance( db_info.id, context) security_groups = [security_group["name"]] task_api.API(context).create_instance(db_info.id, name, flavor_id, flavor.ram, image_id, databases, users, service_type, volume_size, security_groups, backup_id) return SimpleInstance(context, db_info, service_status)
def _create_server(self, flavor_id, image_id, service_type, block_device_mapping): nova_client = create_nova_client(self.context) files = { "/etc/guest_info": "guest_id=%s\nservice_type=%s\n" % (self.id, service_type) } name = self.hostname or self.name bdmap = block_device_mapping server = nova_client.servers.create(name, image_id, flavor_id, files=files, block_device_mapping=bdmap) LOG.debug(_("Created new compute instance %s.") % server.id) return server
def load_mgmt_instances(context, deleted=None, client=None): if not client: client = remote.create_nova_client(context) try: mgmt_servers = client.rdservers.list() except AttributeError: mgmt_servers = client.servers.list(search_opts={'all_tenants': 1}) LOG.info("Found %d servers in Nova" % len(mgmt_servers if mgmt_servers else [])) if deleted is not None: db_infos = instance_models.DBInstance.find_all(deleted=deleted) else: db_infos = instance_models.DBInstance.find_all() instances = MgmtInstances.load_status_from_existing(context, db_infos, mgmt_servers) return instances
def _create_server_volume(self, flavor_id, image_id, security_groups, service_type, volume_size): server = None try: nova_client = create_nova_client(self.context) files = { "/etc/guest_info": ("[DEFAULT]\n--guest_id=" "%s\n--service_type=%s\n" % (self.id, service_type)) } name = self.hostname or self.name volume_desc = ("mysql volume for %s" % self.id) volume_name = ("mysql-%s" % self.id) volume_ref = { 'size': volume_size, 'name': volume_name, 'description': volume_desc } server = nova_client.servers.create( name, image_id, flavor_id, files=files, volume=volume_ref, security_groups=security_groups) LOG.debug(_("Created new compute instance %s.") % server.id) server_dict = server._info LOG.debug("Server response: %s" % server_dict) volume_id = None for volume in server_dict.get('os:volumes', []): volume_id = volume.get('id') # Record the server ID and volume ID in case something goes wrong. self.update_db(compute_instance_id=server.id, volume_id=volume_id) except Exception as e: msg = "Error creating server and volume for instance." err = inst_models.InstanceTasks.BUILDING_ERROR_SERVER self._log_and_raise(e, msg, err) device_path = CONF.device_path mount_point = CONF.mount_point volume_info = {'device_path': device_path, 'mount_point': mount_point} return server, volume_info
def load(context, id): if context is None: raise TypeError("Argument context not defined.") elif id is None: raise TypeError("Argument id not defined.") try: db_info = inst_models.DBInstance.find_by(id=id) except exception.NotFound: raise exception.NotFound(uuid=id) server, volumes = inst_models.load_server_with_volumes(context, db_info.id, db_info.compute_instance_id) nova_client = remote.create_nova_client(context) volume_client = remote.create_nova_volume_client(context) guest = remote.create_guest_client(context, id) return InstanceTasks(context, db_info, server, volumes, nova_client=nova_client, volume_client=volume_client, guest=guest)
def resize_flavor(self, new_flavor_id): self._validate_can_perform_action() LOG.debug("resizing instance %s flavor to %s" % (self.id, new_flavor_id)) # Validate that the flavor can be found and that it isn't the same size # as the current one. client = create_nova_client(self.context) try: new_flavor = client.flavors.get(new_flavor_id) except nova_exceptions.NotFound: raise exception.FlavorNotFound(uuid=new_flavor_id) old_flavor = client.flavors.get(self.flavor_id) new_flavor_size = new_flavor.ram old_flavor_size = old_flavor.ram if new_flavor_size == old_flavor_size: raise exception.CannotResizeToSameSize() # Set the task to RESIZING and begin the async call before returning. self.update_db(task_status=InstanceTasks.RESIZING) LOG.debug("Instance %s set to RESIZING." % self.id) task_api.API(self.context).resize_flavor(self.id, new_flavor_id, old_flavor_size, new_flavor_size)
def load_volumes(context, server_id, client=None): volume_support = config.Config.get("reddwarf_volume_support", 'False') if utils.bool_from_string(volume_support): if client is None: client = create_nova_client(context) volume_client = create_nova_volume_client(context) try: volumes = [] volumes_info = client.volumes.get_server_volumes(server_id) volume_ids = [attachments.volumeId for attachments in volumes_info] for volume_id in volume_ids: volume_info = volume_client.volumes.get(volume_id) volume = {'id': volume_info.id, 'size': volume_info.size} volumes.append(volume) except nova_exceptions.NotFound, e: LOG.debug("Could not find nova server_id(%s)" % server_id) raise rd_exceptions.VolumeAttachmentsNotFound(server_id=server_id) except nova_exceptions.ClientException, e: raise rd_exceptions.ReddwarfError(str(e))
def _create_dns_entry(self): LOG.debug("%s: Creating dns entry for instance: %s" % (greenthread.getcurrent(), self.id)) dns_support = CONF.reddwarf_dns_support LOG.debug(_("reddwarf dns support = %s") % dns_support) if dns_support: nova_client = create_nova_client(self.context) dns_client = create_dns_client(self.context) def get_server(): c_id = self.db_info.compute_instance_id return nova_client.servers.get(c_id) def ip_is_available(server): LOG.info("Polling for ip addresses: $%s " % server.addresses) if server.addresses != {}: return True elif (server.addresses == {} and server.status != InstanceStatus.ERROR): return False elif (server.addresses == {} and server.status == InstanceStatus.ERROR): msg = _("Instance IP not available, instance (%s): " "server had status (%s).") LOG.error(msg % (self.id, server.status)) raise ReddwarfError(status=server.status) poll_until(get_server, ip_is_available, sleep_time=1, time_out=DNS_TIME_OUT) server = nova_client.servers.get(self.db_info.compute_instance_id) LOG.info("Creating dns entry...") dns_client.create_instance_entry(self.id, get_ip_address(server.addresses)) else: LOG.debug("%s: DNS not enabled for instance: %s" % (greenthread.getcurrent(), self.id))
def _create_resources(): client = create_nova_client(context) security_groups = None try: flavor = client.flavors.get(flavor_id) except nova_exceptions.NotFound: raise exception.FlavorNotFound(uuid=flavor_id) db_info = DBInstance.create(name=name, flavor_id=flavor_id, tenant_id=context.tenant, volume_size=volume_size, task_status=InstanceTasks.BUILDING) LOG.debug( _("Tenant %s created new Reddwarf instance %s...") % (context.tenant, db_info.id)) service_status = InstanceServiceStatus.create( instance_id=db_info.id, status=ServiceStatuses.NEW) if CONF.reddwarf_dns_support: dns_client = create_dns_client(context) hostname = dns_client.determine_hostname(db_info.id) db_info.hostname = hostname db_info.save() if CONF.reddwarf_security_groups_support: security_group = SecurityGroup.create_for_instance( db_info.id, context) security_groups = [security_group["name"]] task_api.API(context).create_instance(db_info.id, name, flavor_id, flavor.ram, image_id, databases, users, service_type, volume_size, security_groups) return SimpleInstance(context, db_info, service_status)
def _create_resources(): client = create_nova_client(context) security_groups = None try: flavor = client.flavors.get(flavor_id) except nova_exceptions.NotFound: raise exception.FlavorNotFound(uuid=flavor_id) db_info = DBInstance.create(name=name, flavor_id=flavor_id, tenant_id=context.tenant, volume_size=volume_size, task_status=InstanceTasks.BUILDING) LOG.debug(_("Tenant %s created new Reddwarf instance %s...") % (context.tenant, db_info.id)) service_status = InstanceServiceStatus.create( instance_id=db_info.id, status=ServiceStatuses.NEW) if CONF.reddwarf_dns_support: dns_client = create_dns_client(context) hostname = dns_client.determine_hostname(db_info.id) db_info.hostname = hostname db_info.save() if CONF.reddwarf_security_groups_support: security_group = SecurityGroup.create_for_instance( db_info.id, context) security_groups = [security_group["name"]] task_api.API(context).create_instance(db_info.id, name, flavor_id, flavor.ram, image_id, databases, users, service_type, volume_size, security_groups) return SimpleInstance(context, db_info, service_status)
def resize_flavor(self, new_flavor_id): self.validate_can_perform_resize() LOG.debug("resizing instance %s flavor to %s" % (self.id, new_flavor_id)) # Validate that the flavor can be found and that it isn't the same size # as the current one. client = create_nova_client(self.context) try: new_flavor = client.flavors.get(new_flavor_id) except nova_exceptions.NotFound: raise rd_exceptions.FlavorNotFound(uuid=new_flavor_id) old_flavor = client.flavors.get(self.server.flavor['id']) new_flavor_size = new_flavor.ram old_flavor_size = old_flavor.ram if new_flavor_size == old_flavor_size: raise rd_exceptions.CannotResizeToSameSize() # Set the task to RESIZING and begin the async call before returning. self.db_info.task_status = InstanceTasks.RESIZING self.db_info.save() LOG.debug("Instance %s set to RESIZING." % self.id) self.call_async(self.resize_flavor_async, new_flavor_id, old_flavor_size, new_flavor_size)
def resize_flavor(self, new_flavor_id): self._validate_can_perform_action() LOG.debug("resizing instance %s flavor to %s" % (self.id, new_flavor_id)) # Validate that the flavor can be found and that it isn't the same size # as the current one. client = create_nova_client(self.context) try: new_flavor = client.flavors.get(new_flavor_id) except nova_exceptions.NotFound: raise exception.FlavorNotFound(uuid=new_flavor_id) old_flavor = client.flavors.get(self.flavor_id) new_flavor_size = new_flavor.ram old_flavor_size = old_flavor.ram if new_flavor_size == old_flavor_size: raise exception.CannotResizeToSameSize() # Set the task to RESIZING and begin the async call before returning. self.update_db(task_status=InstanceTasks.RESIZING) LOG.debug("Instance %s set to RESIZING." % self.id) task_api.API(self.context).resize_flavor(self.id, new_flavor_id, old_flavor_size, new_flavor_size)
def _create_dns_entry(self): LOG.debug("%s: Creating dns entry for instance: %s" % (greenthread.getcurrent(), self.id)) dns_support = CONF.reddwarf_dns_support LOG.debug(_("reddwarf dns support = %s") % dns_support) if dns_support: nova_client = create_nova_client(self.context) dns_client = create_dns_client(self.context) def get_server(): c_id = self.db_info.compute_instance_id return nova_client.servers.get(c_id) def ip_is_available(server): LOG.info("Polling for ip addresses: $%s " % server.addresses) if server.addresses != {}: return True elif (server.addresses == {} and server.status != InstanceStatus.ERROR): return False elif (server.addresses == {} and server.status == InstanceStatus.ERROR): msg = _("Instance IP not available, instance (%s): " "server had status (%s).") LOG.error(msg % (self.id, server.status)) raise ReddwarfError(status=server.status) poll_until(get_server, ip_is_available, sleep_time=1, time_out=DNS_TIME_OUT) server = nova_client.servers.get(self.db_info.compute_instance_id) LOG.info("Creating dns entry...") dns_client.create_instance_entry(self.id, get_ip_address(server.addresses)) else: LOG.debug("%s: DNS not enabled for instance: %s" % (greenthread.getcurrent(), self.id))
def create(cls, context, name, flavor_id, image_id, databases, users, service_type, volume_size, backup_id): client = create_nova_client(context) try: flavor = client.flavors.get(flavor_id) except nova_exceptions.NotFound: raise exception.FlavorNotFound(uuid=flavor_id) deltas = {'instances': 1} if CONF.reddwarf_volume_support: validate_volume_size(volume_size) deltas['volumes'] = volume_size else: if volume_size is not None: raise exception.VolumeNotSupported() ephemeral_support = CONF.device_path if ephemeral_support and flavor.ephemeral == 0: raise exception.LocalStorageNotSpecified(flavor=flavor_id) def _create_resources(): security_groups = None if backup_id is not None: backup_info = Backup.get_by_id(backup_id) if backup_info.is_running: raise exception.BackupNotCompleteError(backup_id=backup_id) location = backup_info.location LOG.info(_("Checking if backup exist in '%s'") % location) if not Backup.check_object_exist(context, location): raise exception.BackupFileNotFound(location=location) db_info = DBInstance.create(name=name, flavor_id=flavor_id, tenant_id=context.tenant, volume_size=volume_size, task_status=InstanceTasks.BUILDING) LOG.debug(_("Tenant %s created new Reddwarf instance %s...") % (context.tenant, db_info.id)) service_status = InstanceServiceStatus.create( instance_id=db_info.id, status=ServiceStatuses.NEW) if CONF.reddwarf_dns_support: dns_client = create_dns_client(context) hostname = dns_client.determine_hostname(db_info.id) db_info.hostname = hostname db_info.save() if CONF.reddwarf_security_groups_support: security_group = SecurityGroup.create_for_instance( db_info.id, context) security_groups = [security_group["name"]] task_api.API(context).create_instance(db_info.id, name, flavor_id, flavor.ram, image_id, databases, users, service_type, volume_size, security_groups, backup_id) return SimpleInstance(context, db_info, service_status) return run_with_quotas(context.tenant, deltas, _create_resources)
def create(cls, context, name, flavor_id, image_id, databases, users, service_type, volume_size, backup_id): client = create_nova_client(context) try: flavor = client.flavors.get(flavor_id) except nova_exceptions.NotFound: raise exception.FlavorNotFound(uuid=flavor_id) deltas = {'instances': 1} if CONF.reddwarf_volume_support: validate_volume_size(volume_size) deltas['volumes'] = volume_size else: if volume_size is not None: raise exception.VolumeNotSupported() ephemeral_support = CONF.device_path if ephemeral_support and flavor.ephemeral == 0: raise exception.LocalStorageNotSpecified(flavor=flavor_id) def _create_resources(): security_groups = None if backup_id is not None: backup_info = Backup.get_by_id(backup_id) if backup_info.is_running: raise exception.BackupNotCompleteError(backup_id=backup_id) location = backup_info.location LOG.info(_("Checking if backup exist in '%s'") % location) if not Backup.check_object_exist(context, location): raise exception.BackupFileNotFound(location=location) db_info = DBInstance.create(name=name, flavor_id=flavor_id, tenant_id=context.tenant, volume_size=volume_size, task_status=InstanceTasks.BUILDING) LOG.debug( _("Tenant %s created new Reddwarf instance %s...") % (context.tenant, db_info.id)) service_status = InstanceServiceStatus.create( instance_id=db_info.id, status=ServiceStatuses.NEW) if CONF.reddwarf_dns_support: dns_client = create_dns_client(context) hostname = dns_client.determine_hostname(db_info.id) db_info.hostname = hostname db_info.save() if CONF.reddwarf_security_groups_support: security_group = SecurityGroup.create_for_instance( db_info.id, context) security_groups = [security_group["name"]] task_api.API(context).create_instance(db_info.id, name, flavor_id, flavor.ram, image_id, databases, users, service_type, volume_size, security_groups, backup_id) return SimpleInstance(context, db_info, service_status) return run_with_quotas(context.tenant, deltas, _create_resources)
def __init__(self, context): nova_flavors = create_nova_client(context).flavors.list() self.flavors = [Flavor(flavor=item) for item in nova_flavors]
def create(cls, context, name, flavor_ref, image_id, databases, service_type, volume_size): db_info = DBInstance.create(name=name, task_status=InstanceTasks.NONE) LOG.debug(_("Created new Reddwarf instance %s...") % db_info.id) if volume_size: volume_info = cls._create_volume(context, db_info, volume_size) block_device_mapping = volume_info['block_device'] device_path = volume_info['device_path'] mount_point = volume_info['mount_point'] volumes = volume_info['volumes'] else: block_device_mapping = None device_path = None mount_point = None volumes = [] client = create_nova_client(context) files = {"/etc/guest_info": "guest_id=%s\nservice_type=%s\n" % (db_info.id, service_type)} server = client.servers.create(name, image_id, flavor_ref, files=files, block_device_mapping=block_device_mapping) LOG.debug(_("Created new compute instance %s.") % server.id) db_info.compute_instance_id = server.id db_info.save() service_status = InstanceServiceStatus.create(instance_id=db_info.id, status=ServiceStatuses.NEW) # Now wait for the response from the create to do additional work guest = create_guest_client(context, db_info.id) # populate the databases model_schemas = populate_databases(databases) guest.prepare(512, model_schemas, users=[], device_path=device_path, mount_point=mount_point) dns_support = config.Config.get("reddwarf_dns_support", 'False') LOG.debug(_("reddwarf dns support = %s") % dns_support) dns_client = create_dns_client(context) # Default the hostname to instance name if no dns support dns_client.update_hostname(db_info) if utils.bool_from_string(dns_support): def get_server(): return client.servers.get(server.id) def ip_is_available(server): if server.addresses != {}: return True elif server.addresses == {} and\ server.status != InstanceStatus.ERROR: return False elif server.addresses == {} and\ server.status == InstanceStatus.ERROR: LOG.error(_("Instance IP not available, instance (%s): server had " " status (%s).") % (db_info['id'], server.status)) raise rd_exceptions.ReddwarfError( status=server.status) poll_until(get_server, ip_is_available, sleep_time=1, time_out=60*2) dns_client.create_instance_entry(db_info['id'], get_ip_address(server.addresses)) return Instance(context, db_info, server, service_status, volumes)
def load(context, name): client = create_nova_client(context) try: return DetailedHost(client.rdhosts.get(name)) except nova_exceptions.NotFound: raise exception.NotFound(uuid=name)
def nova_client(self): if not self._nova_client: self._nova_client = create_nova_client(self.context) return self._nova_client
def __init__(self, context): nova_flavors = create_nova_client(context).flavors.list() self.flavors = [Flavor(flavor=item) for item in nova_flavors]
def load(context, name): client = create_nova_client(context) try: return DetailedHost(client.rdhosts.get(name)) except nova_exceptions.NotFound: raise exception.NotFound(uuid=name)
def nova_client(self): if not self._nova_client: self._nova_client = create_nova_client(self.context) return self._nova_client
def get_client(cls, context): return remote.create_nova_client(context)