def load_mgmt_instances(context, deleted=None, client=None, include_clustered=None, project_id=None): if not client: client = clients.create_nova_client( context, CONF.service_credentials.region_name) search_opts = {'all_tenants': False} mgmt_servers = client.servers.list(search_opts=search_opts, limit=-1) LOG.info("Found %d servers in Nova", len(mgmt_servers if mgmt_servers else [])) args = {} if deleted is not None: args['deleted'] = deleted if not include_clustered: args['cluster_id'] = None if project_id: args['tenant_id'] = project_id db_infos = instance_models.DBInstance.find_all(**args) instances = MgmtInstances.load_status_from_existing( context, db_infos, mgmt_servers) return instances
def load_mgmt_instance(cls, context, id, include_deleted): try: instance = instance_models.load_instance( cls, context, id, needs_server=True, include_deleted=include_deleted) client = clients.create_nova_client( context, CONF.service_credentials.region_name) try: server = client.rdservers.get(instance.server_id) except AttributeError: server = client.servers.get(instance.server_id) if hasattr(server, 'host'): instance.server.host = server.host elif hasattr(server, 'hostId'): instance.server.host = server.hostId if hasattr(server, 'deleted'): instance.server.deleted = server.deleted if hasattr(server, 'deleted_at'): instance.server.deleted_at = server.deleted_at if hasattr(server, 'local_id'): instance.server.local_id = server.local_id assert instance.server is not None except Exception as e: LOG.error(e) instance = instance_models.load_instance( cls, context, id, needs_server=False, include_deleted=include_deleted) return instance
def _check_instances(context, instances, datastore_version, allowed_instance_count=None): instance_count = len(instances) if allowed_instance_count: if instance_count not in allowed_instance_count: raise exception.ClusterNumInstancesNotSupported( num_instances=allowed_instance_count ) flavor_ids = [instance['flavor_id'] for instance in instances] if len(set(flavor_ids)) != 1: raise exception.ClusterFlavorsNotEqual() flavor_id = flavor_ids[0] nova_client = clients.create_nova_client(context) try: flavor = nova_client.flavors.get(flavor_id) except nova_exceptions.NotFound: raise exception.FlavorNotFound(uuid=flavor_id) mongo_conf = CONF.get(datastore_version.manager) volume_sizes = [instance['volume_size'] for instance in instances if instance.get('volume_size', None)] if mongo_conf.volume_support: if len(volume_sizes) != instance_count: raise exception.ClusterVolumeSizeRequired() if len(set(volume_sizes)) != 1: raise exception.ClusterVolumeSizesNotEqual() volume_size = volume_sizes[0] models.validate_volume_size(volume_size) else: # TODO(amcreynolds): is ephemeral possible for mongodb clusters? if len(volume_sizes) > 0: raise exception.VolumeNotSupported() ephemeral_support = mongo_conf.device_path if ephemeral_support and flavor.ephemeral == 0: raise exception.LocalStorageNotSpecified(flavor=flavor_id)
def list_datastore_version_flavor_associations(cls, context, datastore_version_id): """Get allowed flavors for a given datastore version. All nova flavors are permitted for a datastore_version unless one or more entries are found in datastore_version_metadata, in which case only those are permitted. """ nova_flavors = create_nova_client(context).flavors.list() bound_flavors = DBDatastoreVersionMetadata.find_all( datastore_version_id=datastore_version_id, key='flavor', deleted=False ) if (bound_flavors.count() != 0): bound_flavors = tuple(f.value for f in bound_flavors) # Generate a filtered list of nova flavors ds_nova_flavors = (f for f in nova_flavors if f.id in bound_flavors) associated_flavors = tuple(flavor_model(flavor=item) for item in ds_nova_flavors) else: # Return all nova flavors if no flavor metadata found # for datastore_version. associated_flavors = tuple(flavor_model(flavor=item) for item in nova_flavors) return associated_flavors
def list_datastore_version_flavor_associations(cls, context, datastore_type, datastore_version_id): if datastore_type and datastore_version_id: """ All nova flavors are permitted for a datastore_version unless one or more entries are found in datastore_version_metadata, in which case only those are permitted. """ (datastore, datastore_version) = get_datastore_version( type=datastore_type, version=datastore_version_id) # If datastore_version_id and flavor key exists in the # metadata table return all the associated flavors for # that datastore version. nova_flavors = create_nova_client(context).flavors.list() bound_flavors = DBDatastoreVersionMetadata.find_all( datastore_version_id=datastore_version.id, key='flavor', deleted=False) if (bound_flavors.count() != 0): bound_flavors = tuple(f.value for f in bound_flavors) # Generate a filtered list of nova flavors ds_nova_flavors = (f for f in nova_flavors if f.id in bound_flavors) associated_flavors = tuple( flavor_model(flavor=item) for item in ds_nova_flavors) else: # Return all nova flavors if no flavor metadata found # for datastore_version. associated_flavors = tuple( flavor_model(flavor=item) for item in nova_flavors) return associated_flavors else: msg = _("Specify both the datastore and datastore_version_id.") raise exception.BadRequest(msg)
def create(cls, context, locality, name_suffix): client = create_nova_client(context) server_group_name = "%s_%s" % ('locality', name_suffix) server_group = client.server_groups.create(name=server_group_name, policies=[locality]) LOG.debug( "Created '%(locality)s' server group called %(group_name)s " "(id: %(group_id)s).", { 'locality': locality, 'group_name': server_group_name, 'group_id': server_group.id }) return server_group
def delete(cls, context, server_group, force=False): # Only delete the server group if we're the last member in it, or if # it has no members if server_group: if force or len(server_group.members) <= 1: LOG.info("Deleting server group %s", server_group.id) client = create_nova_client(context) client.server_groups.delete(server_group.id) else: LOG.debug( "Skipping delete of server group %(id)s " "(members: %(members)s).", { 'id': server_group.id, 'members': server_group.members })
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: raise exception.NotFound(uuid=flavor_id) except nova_exceptions.ClientException as e: raise exception.TroveError(str(e)) return msg = ("Flavor is not defined, and" " context and flavor_id were not specified.") raise exception.InvalidModelError(errors=msg)
def load(cls, context, instance_id): client = create_nova_client(context) server_group = None expected_name = "locality_%s" % instance_id try: for sg in client.server_groups.list(): if sg.name == expected_name: server_group = sg except Exception: LOG.exception("Could not load server group for instance %s", instance_id) if not server_group: LOG.info('No server group found for instance %s', instance_id) return server_group
def _render_cluster_config(self, context, instance, cluster_ips, cluster_name, replication_user): client = create_nova_client(context) flavor = client.flavors.get(instance.flavor_id) instance_ip = self.get_ip(instance) config = ClusterConfigTemplate(self.datastore_version, flavor, instance.id) replication_user_pass = "******" % replication_user config_rendered = config.render( replication_user_pass=replication_user_pass, cluster_ips=cluster_ips, cluster_name=cluster_name, instance_ip=instance_ip, instance_name=instance.name, ) return config_rendered
def validate_instance_flavors(context, instances, volume_enabled, ephemeral_enabled): """Validate flavors for given instance definitions.""" nova_cli_cache = dict() for instance in instances: region_name = instance.get('region_name') flavor_id = instance['flavor_id'] try: if region_name in nova_cli_cache: nova_client = nova_cli_cache[region_name] else: nova_client = clients.create_nova_client(context, region_name) nova_cli_cache[region_name] = nova_client flavor = nova_client.flavors.get(flavor_id) if (not volume_enabled and (ephemeral_enabled and flavor.ephemeral == 0)): raise exception.LocalStorageNotSpecified(flavor=flavor_id) except nova_exceptions.NotFound: raise exception.FlavorNotFound(uuid=flavor_id)
def __init__(self, context): nova_flavors = create_nova_client(context).flavors.list() self.flavors = [Flavor(flavor=item) for item in nova_flavors]
def publish_quota_notifications(self, context): nova_client = clients.create_nova_client(self.admin_context) for tenant in nova_client.tenants.list(): for quota in QUOTAS.get_all_quotas_by_tenant(tenant.id): usage = QUOTAS.get_quota_usage(quota) DBaaSQuotas(self.admin_context, quota, usage).notify()
def get_client(cls, context, region_name): return clients.create_nova_client(context, region_name)
def __init__(self, context, region_name): try: self.client = clients.create_nova_client(context, region_name) except nova_exceptions.ClientException as e: raise exception.TroveError(str(e))