def validate_params(cls, params, capabilities=None): con_dict = {} region = params.get('region') if not region: msg = (_("Param region is missing.")) LOG.warning(msg) raise exception.EC2Exception(msg) con_dict['region'] = region aws_access_key_id = params.get('aws_access_key_id') if not aws_access_key_id: msg = (_("Param aws_access_key_id is missing.")) LOG.warning(msg) raise exception.EC2Exception(msg) con_dict['aws_access_key_id'] = aws_access_key_id aws_secret_access_key = params.get('aws_secret_access_key') if not aws_secret_access_key: msg = (_("Param aws_secret_access_key is missing.")) LOG.warning(msg) raise exception.EC2Exception(msg) con_dict['aws_secret_access_key'] = aws_secret_access_key bucket_name = params.get('bucket_name') if not bucket_name: msg = (_("Param bucket_name is missing.")) LOG.warning(msg) raise exception.EC2Exception(msg) con_dict['bucket_name'] = bucket_name return con_dict
def validate_params(cls, params, capabilities=None): con_dict = {} host = params.get('host') if not host: msg = (_("Param host is missing.")) LOG.warning(msg) raise exception.VSphereException(msg) con_dict['host'] = host username = params.get('username') if not username: msg = (_("Param username is missing.")) LOG.warning(msg) raise exception.VSphereException(msg) con_dict['username'] = username password = params.get('password') if not password: msg = (_("Param password is missing.")) LOG.warning(msg) raise exception.VSphereException(msg) con_dict['password'] = password con_dict['port'] = params.get('port') or cls.OPTIONAL_PARAMS['port'] return con_dict
def update(self, req, id, body): """Updates a Cloud.""" context = req.environ['guts.context'] if not body: msg = _("Missing request body") raise exc.HTTPBadRequest(explanation=msg) if 'cloud' not in body: msg = _("Missing required element '%s' in request body") % 'cloud' raise exc.HTTPBadRequest(explanation=msg) cloud = body['cloud'] self.validate_name_and_description(cloud) valid_update_keys = ( 'name', 'driver', 'capabilities', 'params', 'enabled' ) updates = {} for key in valid_update_keys: if key in cloud: updates[key] = cloud[key] if not updates: msg = _("Nothing to update") raise exc.HTTPBadRequest(explanation=msg) cloud = self.migration_api.update_cloud(context, id, **updates) return self._view_builder.detail(req, cloud)
def validate_integer(value, name, min_value=None, max_value=None): """Make sure that value is a valid integer, potentially within range. :param value: the value of the integer :param name: the name of the integer :param min_length: the min_length of the integer :param max_length: the max_length of the integer :returns: integer """ try: value = int(value) except (TypeError, ValueError, UnicodeEncodeError): raise webob.exc.HTTPBadRequest(explanation=( _('%s must be an integer.') % name)) if min_value is not None and value < min_value: raise webob.exc.HTTPBadRequest( explanation=(_('%(value_name)s must be >= %(min_value)d') % {'value_name': name, 'min_value': min_value})) if max_value is not None and value > max_value: raise webob.exc.HTTPBadRequest( explanation=(_('%(value_name)s must be <= %(max_value)d') % {'value_name': name, 'max_value': max_value})) return value
def update(self, req, id, body): """Enable/Disable a service.""" context = req.environ['guts.context'] authorize(context, action='update') ext_loaded = self.ext_mgr.is_loaded('os-extended-services') ret_val = {} if id == "enable": disabled = False status = "enabled" if ext_loaded: ret_val['disabled_reason'] = None elif (id == "disable" or (id == "disable-log-reason" and ext_loaded)): disabled = True status = "disabled" else: raise webob.exc.HTTPNotFound(explanation=_("Unknown action")) try: host = body['host'] except (TypeError, KeyError): msg = _("Missing required element 'host' in request body.") raise webob.exc.HTTPBadRequest(explanation=msg) ret_val['disabled'] = disabled if id == "disable-log-reason" and ext_loaded: reason = body.get('disabled_reason') if not self._is_valid_as_reason(reason): msg = _('Disabled reason contains invalid characters ' 'or is too long') raise webob.exc.HTTPBadRequest(explanation=msg) ret_val['disabled_reason'] = reason service = body.get('service', '') binary = body.get('binary', '') binary_key = binary or service if not binary_key: raise webob.exc.HTTPBadRequest() try: svc = objects.Service.get_by_args(context, host, binary_key) if not svc: raise webob.exc.HTTPNotFound(explanation=_('Unknown service')) svc.disabled = ret_val['disabled'] if 'disabled_reason' in ret_val: svc.disabled_reason = ret_val['disabled_reason'] svc.save() except exception.ServiceNotFound: raise webob.exc.HTTPNotFound(explanation=_("service not found")) ret_val.update({'host': host, 'service': service, 'binary': binary, 'status': status}) return ret_val
def update(self, req, id, body): """Enable/Disable a service.""" context = req.environ["guts.context"] authorize(context, action="update") ext_loaded = self.ext_mgr.is_loaded("os-extended-services") ret_val = {} if id == "enable": disabled = False status = "enabled" if ext_loaded: ret_val["disabled_reason"] = None elif id == "disable" or (id == "disable-log-reason" and ext_loaded): disabled = True status = "disabled" else: raise webob.exc.HTTPNotFound(explanation=_("Unknown action")) try: host = body["host"] except (TypeError, KeyError): msg = _("Missing required element 'host' in request body.") raise webob.exc.HTTPBadRequest(explanation=msg) ret_val["disabled"] = disabled if id == "disable-log-reason" and ext_loaded: reason = body.get("disabled_reason") if not self._is_valid_as_reason(reason): msg = _("Disabled reason contains invalid characters " "or is too long") raise webob.exc.HTTPBadRequest(explanation=msg) ret_val["disabled_reason"] = reason service = body.get("service", "") binary = body.get("binary", "") binary_key = binary or service if not binary_key: raise webob.exc.HTTPBadRequest() try: svc = objects.Service.get_by_args(context, host, binary_key) if not svc: raise webob.exc.HTTPNotFound(explanation=_("Unknown service")) svc.disabled = ret_val["disabled"] if "disabled_reason" in ret_val: svc.disabled_reason = ret_val["disabled_reason"] svc.save() except exception.ServiceNotFound: raise webob.exc.HTTPNotFound(explanation=_("service not found")) ret_val.update({"host": host, "service": service, "binary": binary, "status": status}) return ret_val
def _get_offset_param(params): """Extract offset id from request's dictionary (defaults to 0) or fail.""" try: offset = int(params.pop("offset", 0)) except ValueError: msg = _("offset param must be an integer") raise webob.exc.HTTPBadRequest(explanation=msg) if offset < 0: msg = _("offset param must be positive") raise webob.exc.HTTPBadRequest(explanation=msg) return offset
def _get_offset_param(params): """Extract offset id from request's dictionary (defaults to 0) or fail.""" try: offset = int(params.pop('offset', 0)) except ValueError: msg = _('offset param must be an integer') raise webob.exc.HTTPBadRequest(explanation=msg) if offset < 0: msg = _('offset param must be positive') raise webob.exc.HTTPBadRequest(explanation=msg) return offset
def bool_from_string(subject, strict=False, default=False): """Interpret a string as a boolean. A case-insensitive match is performed such that strings matching 't', 'true', 'on', 'y', 'yes', or '1' are considered True and, when `strict=False`, anything else returns the value specified by 'default'. Useful for JSON-decoded stuff and config file parsing. If `strict=True`, unrecognized values, including None, will raise a ValueError which is useful when parsing values passed in from an API call. Strings yielding False are 'f', 'false', 'off', 'n', 'no', or '0'. """ if not isinstance(subject, six.string_types): subject = six.text_type(subject) lowered = subject.strip().lower() if lowered in TRUE_STRINGS: return True elif lowered in FALSE_STRINGS: return False elif strict: acceptable = ', '.join( "'%s'" % s for s in sorted(TRUE_STRINGS + FALSE_STRINGS)) msg = _("Unrecognized value '%(val)s', acceptable values are:" " %(acceptable)s") % {'val': subject, 'acceptable': acceptable} raise ValueError(msg) else: return default
def __call__(self, environ, start_response): r"""Subclasses will probably want to implement __call__ like this: @webob.dec.wsgify(RequestClass=Request) def __call__(self, req): # Any of the following objects work as responses: # Option 1: simple string res = 'message\n' # Option 2: a nicely formatted HTTP exception page res = exc.HTTPForbidden(explanation='Nice try') # Option 3: a webob Response object (in case you need to play with # headers, or you want to be treated like an iterable) res = Response(); res.app_iter = open('somefile') # Option 4: any wsgi app to be run next res = self.application # Option 5: you can get a Response object for a wsgi app, too, to # play with headers etc res = req.get_response(self.application) # You can then just return your response... return res # ... or set req.response and return None. req.response = res See the end of http://pythonpaste.org/webob/modules/dec.html for more info. """ raise NotImplementedError(_("You must implement __call__"))
def get_migration(ctxt, id): """Retrieves single source by ID.""" if id is None: msg = _("ID cannot be None") raise exception.InvalidSource(reason=msg) check_policy(ctxt, 'get_migration') return db.migration_get(ctxt, id)
def __init__(self, name, loader=None): """Initialize, but do not start the WSGI server. :param name: The name of the WSGI server given to the loader. :param loader: Loads the WSGI application using the given name. :returns: None """ self.name = name self.manager = self._get_manager() self.loader = loader or wsgi_common.Loader() self.app = self.loader.load_app(name) self.host = getattr(CONF, '%s_listen' % name, "0.0.0.0") self.port = getattr(CONF, '%s_listen_port' % name, 0) self.workers = (getattr(CONF, '%s_workers' % name, None) or processutils.get_worker_count()) if self.workers and self.workers < 1: worker_name = '%s_workers' % name msg = (_("%(worker_name)s value of %(workers)d is invalid, " "must be greater than 0.") % {'worker_name': worker_name, 'workers': self.workers}) raise exception.InvalidInput(reason=msg) setup_profiler(name, self.host) self.server = wsgi.Server(name, self.app, host=self.host, port=self.port)
def get_migration_by_name(ctxt, name): """Retrieves single source by name.""" if name is None: msg = _("Source name cannot be None") raise exception.InvalidSource(reason=msg) return db.migration_get_by_name(ctxt, name)
def attach(self, *slaves): """Attach one or more slave templates. Attaches one or more slave templates to the master template. Slave templates must have a root element with the same tag as the master template. The slave template's apply() method will be called to determine if the slave should be applied to this master; if it returns False, that slave will be skipped. (This allows filtering of slaves based on the version of the master template.) """ slave_list = [] for slave in slaves: slave = slave.wrap() # Make sure we have a tree match if slave.root.tag != self.root.tag: msg = (_("Template tree mismatch; adding slave %(slavetag)s " "to master %(mastertag)s") % {'slavetag': slave.root.tag, 'mastertag': self.root.tag}) raise ValueError(msg) # Make sure slave applies to this template if not slave.apply(self): continue slave_list.append(slave) # Add the slaves self.slaves.extend(slave_list)
def get_source(context, id): """Retrieves single source by ID.""" if id is None: msg = _("ID cannot be None") raise exception.InvalidSource(reason=msg) return db.source_get(context, id)
def model_query(context, *args, **kwargs): """Query helper that accounts for context's `read_deleted` field. :param context: context to query under :param session: if present, the session to use :param read_deleted: if present, overrides context's read_deleted field. :param project_only: if present and context is user-type, then restrict query to match the context's project_id. """ session = kwargs.get('session') or get_session() read_deleted = kwargs.get('read_deleted') or context.read_deleted project_only = kwargs.get('project_only') query = session.query(*args) if read_deleted == 'no': query = query.filter_by(deleted=False) elif read_deleted == 'yes': pass # omit the filter to include deleted and active elif read_deleted == 'only': query = query.filter_by(deleted=True) else: raise Exception( _("Unrecognized read_deleted value '%s'") % read_deleted) if project_only and is_user_context(context): query = query.filter_by(project_id=context.project_id) return query
def create(self): if self.obj_attr_is_set('id'): raise exception.ObjectActionError(action='create', reason=_('already created')) updates = self.guts_obj_get_changes() db_migration = db.migration_create(self._context, updates) self._from_db_object(self._context, self, db_migration)
def create(self): if self.obj_attr_is_set('id'): raise exception.ObjectActionError(action='create', reason=_('already created')) updates = self.guts_obj_get_changes() db_service = db.service_create(self._context, updates) self._from_db_object(self._context, self, db_service)
def get_vm_by_name(context, name): """Retrieves single source by name.""" if name is None: msg = _("VM name cannot be None") raise exception.InvalidSource(reason=msg) return db.vm_get_by_name(context, name)
def wrapper(*args, **kwargs): try: return f(*args, **kwargs) except db_exc.DBDataError: msg = _('Error writing field to database') LOG.exception(msg) raise exception.Invalid(msg)
def get_source_type_by_name(context, name): """Retrieves single source type by name.""" if name is None: msg = _("Source type name cannot be None") raise exception.InvalidSourceType(reason=msg) return db.source_type_get_by_name(context, name)
def _max_attempts(self): max_attempts = CONF.scheduler_max_attempts if max_attempts < 1: raise exception.InvalidParameterValue( err=_("Invalid value for 'scheduler_max_attempts', " "must be >=1")) return max_attempts
def action_peek_json(body): """Determine action to invoke.""" try: decoded = jsonutils.loads(body) except ValueError: msg = _("cannot understand JSON") raise exception.MalformedRequestBody(reason=msg) # Make sure there's exactly one key... if len(decoded) != 1: msg = _("too many body keys") raise exception.MalformedRequestBody(reason=msg) # Return the action and the decoded body... return list(decoded.keys())[0]
def do_setup(self, context): """Any initialization the destination driver does while starting.""" super(OpenStackDestinationDriver, self).do_setup(context) auth_url = self.configuration.auth_url if auth_url is None: raise ValueError(_("Cannot authenticate without an auth_url")) username = self.configuration.username password = self.configuration.password tenant_name = self.configuration.tenant_name project_id = self.configuration.project_id user_domain_name = self.configuration.user_domain_name nova_api_version = self.configuration.nova_api_version cinder_api_version = self.configuration.cinder_api_version glance_api_version = self.configuration.glance_api_version keystone_version = self.configuration.keystone_version if keystone_version == 'v3': auth = v3.Password(auth_url=auth_url, username=username, password=password, project_id=project_id, user_domain_name=user_domain_name) sess = v3_session.Session(auth=auth) elif keystone_version == 'v2': auth = v2.Password(auth_url, username=username, password=password, tenant_name=tenant_name) sess = v2_session.Session(auth=auth) self.nova = nova_client.Client(nova_api_version, session=sess) self.cinder = cinder_client.Client(cinder_api_version, session=sess) self.glance = glance_client.Client(glance_api_version, session=sess) self._initialized = True
def __init__(self, name, loader=None): """Initialize, but do not start the WSGI server. :param name: The name of the WSGI server given to the loader. :param loader: Loads the WSGI application using the given name. :returns: None """ self.name = name self.manager = self._get_manager() self.loader = loader or wsgi_common.Loader() self.app = self.loader.load_app(name) self.host = getattr(CONF, '%s_listen' % name, "0.0.0.0") self.port = getattr(CONF, '%s_listen_port' % name, 0) self.workers = (getattr(CONF, '%s_workers' % name, None) or processutils.get_worker_count()) if self.workers and self.workers < 1: worker_name = '%s_workers' % name msg = (_("%(worker_name)s value of %(workers)d is invalid, " "must be greater than 0.") % { 'worker_name': worker_name, 'workers': self.workers }) raise exception.InvalidInput(reason=msg) setup_profiler(name, self.host) self.server = wsgi.Server(name, self.app, host=self.host, port=self.port)
class GutsException(Exception): """Base Guts Exception To correctly use this class, inherit from it and define a 'message' property. That message will get printf'd with the keyword arguments provided to the constructor. """ message = _("An unknown exception occurred.") code = 500 headers = {} safe = False def __init__(self, message=None, **kwargs): self.kwargs = kwargs self.kwargs['message'] = message if 'code' not in self.kwargs: try: self.kwargs['code'] = self.code except AttributeError: pass for k, v in self.kwargs.items(): if isinstance(v, Exception): self.kwargs[k] = six.text_type(v) if self._should_format(): try: message = self.message % kwargs except Exception: exc_info = sys.exc_info() # kwargs doesn't match a variable in the message # log the issue and the kwargs LOG.exception(_LE('Exception in string format operation')) for name, value in kwargs.items(): LOG.error(_LE("%(name)s: %(value)s"), { 'name': name, 'value': value }) if CONF.fatal_exception_format_errors: six.reraise(*exc_info) # at least get the core message out if something happened message = self.message elif isinstance(message, Exception): message = six.text_type(message) # NOTE(luisg): We put the actual message in 'msg' so that we can access # it, because if we try to access the message via 'message' it will be # overshadowed by the class' message attribute self.msg = message super(GutsException, self).__init__(message) def _should_format(self): return self.kwargs['message'] is None or '%(message)' in self.message def __unicode__(self): return six.text_type(self.msg)
def migrate_vm(self, source_uuid): """Migrate VM stub. This is for drivers that don't implement migrate_vm(). """ msg = _("Migrate VM from source hypervisor to OpenStack " "is not implemented by the driver.") raise NotImplementedError(msg)
def _get_limit_param(params, max_limit=None): """Extract integer limit from request's dictionary or fail. Defaults to max_limit if not present and returns max_limit if present 'limit' is greater than max_limit. """ max_limit = max_limit or CONF.osapi_max_limit try: limit = int(params.pop('limit', max_limit)) except ValueError: msg = _('limit param must be an integer') raise webob.exc.HTTPBadRequest(explanation=msg) if limit < 0: msg = _('limit param must be positive') raise webob.exc.HTTPBadRequest(explanation=msg) limit = min(limit, max_limit) return limit
def _get_limit_param(params, max_limit=None): """Extract integer limit from request's dictionary or fail. Defaults to max_limit if not present and returns max_limit if present 'limit' is greater than max_limit. """ max_limit = max_limit or CONF.osapi_max_limit try: limit = int(params.pop("limit", max_limit)) except ValueError: msg = _("limit param must be an integer") raise webob.exc.HTTPBadRequest(explanation=msg) if limit < 0: msg = _("limit param must be positive") raise webob.exc.HTTPBadRequest(explanation=msg) limit = min(limit, max_limit) return limit
def construct(self): """Construct a template. Called to construct a template instance, which it must return. Only called once. """ raise NotImplementedError(_("subclasses must implement construct()!"))
def download_vm_disks(self, context, vm_uuid, base_path): """Download VM disks stub. This is for drivers that don't implement download_vm_disks(). """ msg = _("Method to download VM disks from source hypervisor to " "base_path is not implemented by the driver.") raise NotImplementedError(msg)
def purge(self, age_in_days): """Purge deleted rows older than a given age from guts tables.""" age_in_days = int(age_in_days) if age_in_days <= 0: print(_("Must supply a positive, non-zero value for age")) exit(1) ctxt = context.get_admin_context() db.purge_deleted_rows(ctxt, age_in_days)
def get_vms_list(self): """Get all VMs stub. This is for drivers that don't implement get_vms_list(). """ msg = _("Get VMs list from source hypervisor is not " "implemented by the driver.") raise NotImplementedError(msg)
def initialize(self, *args, **kwargs): """Initialize Migration Driver. This is for drivers that don't implement initialize(). """ msg = _("Initialize source hypervisor is not " "implemented by the driver.") raise NotImplementedError(msg)
def initialize(self, connection_dict): """Initialize Migration Driver. This is for drivers that don't implement initialize(). """ msg = _("Initialize source hypervisor is not " "implemented by the driver.") raise NotImplementedError(msg)
def get_vms_list(self, source_uuid): """Get all VMs stub. This is for drivers that don't implement get_vms_list(). """ msg = _("Get VMs list from source hypervisor is not " "implemented by the driver.") raise NotImplementedError(msg)
def get_source_type(context, id): """Retrieves single source type by ID.""" check_policy(context, 'get_source_type') if id is None: msg = _("ID cannot be None") raise exception.InvalidSourceType(reason=msg) return db.source_type_get(context, id)
def main(): objects.register_all() CONF(sys.argv[1:], project='guts', version=version.version_string()) logging.setup(CONF, "guts") utils.monkey_patch() launcher = service.get_launcher() LOG = logging.getLogger(__name__) source_service_started = False destination_service_started = False if CONF.enabled_source_hypervisors: for source in CONF.enabled_source_hypervisors: host = "%s@%s" % (CONF.host, source) try: server = service.Service.create(host=host, service_name=source, binary="guts-source") except Exception: msg = _('Source service %s failed to start.') % (host) LOG.exception(msg) else: launcher.launch_service(server) source_service_started = True if CONF.enabled_destination_hypervisors: for dest in CONF.enabled_destination_hypervisors: host = "%s@%s" % (CONF.host, dest) try: server = service.Service.create(host=host, service_name=dest, binary="guts-destination") except Exception: msg = _('Destination service %s failed to start.') % (host) LOG.exception(msg) else: launcher.launch_service(server) destination_service_started = True if not (source_service_started or destination_service_started): msg = _('No migration service(s) started successfully, terminating.') LOG.error(msg) sys.exit(1) launcher.wait()
def resource_fetch(self, resource_type): method_name = "get_%s_list" % resource_type try: return getattr(self, method_name)() except AttributeError: msg = (_("Method %(meth)s not implemented in the driver " "%(drv)s.") % {'meth': method_name, 'drv': self.cloud.driver}) LOG.error(msg)
def errors(self): """Get all of the errors from the log files.""" error_found = 0 if CONF.log_dir: logs = [x for x in os.listdir(CONF.log_dir) if x.endswith('.log')] for file in logs: log_file = os.path.join(CONF.log_dir, file) lines = [line.strip() for line in open(log_file, "r")] lines.reverse() print_name = 0 for index, line in enumerate(lines): if line.find(" ERROR ") > 0: error_found += 1 if print_name == 0: print(log_file + ":-") print_name = 1 print(_("Line %(dis)d : %(line)s") % {'dis': len(lines) - index, 'line': line}) if error_found == 0: print(_("No errors in logfiles!"))
def index(self, req): """Return a list of all running services. Filter by host & service name. """ context = req.environ['guts.context'] authorize(context, action='index') detailed = self.ext_mgr.is_loaded('os-extended-services') now = timeutils.utcnow(with_timezone=True) services = objects.ServiceList.get_all(context) host = '' if 'host' in req.GET: host = req.GET['host'] service = '' if 'service' in req.GET: service = req.GET['service'] versionutils.report_deprecated_feature(LOG, _( "Query by service parameter is deprecated. " "Please use binary parameter instead.")) binary = '' if 'binary' in req.GET: binary = req.GET['binary'] if host: services = [s for s in services if s.host == host] binary_key = binary or service if binary_key: services = [s for s in services if s.binary == binary_key] svcs = [] for svc in services: updated_at = svc.updated_at delta = now - (svc.updated_at or svc.created_at) delta_sec = delta.total_seconds() if svc.modified_at: delta_mod = now - svc.modified_at if abs(delta_sec) >= abs(delta_mod.total_seconds()): updated_at = svc.modified_at alive = abs(delta_sec) <= CONF.service_down_time art = (alive and "up") or "down" active = 'enabled' if svc.disabled: active = 'disabled' if updated_at: updated_at = timeutils.normalize_time(updated_at) ret_fields = {'binary': svc.binary, 'host': svc.host, 'status': active, 'state': art, 'updated_at': updated_at} if detailed: ret_fields['disabled_reason'] = svc.disabled_reason svcs.append(ret_fields) return {'services': svcs}
def remove(self, elem): """Remove a child element.""" # Unwrap templates... elem = elem.unwrap() # Check if element exists if elem.tag not in self._childmap or self._childmap[elem.tag] != elem: raise ValueError(_('element is not a child')) self._children.remove(elem) del self._childmap[elem.tag]
def __init__(self, ext_mgr=None): if ext_mgr is None: if self.ExtensionManager: ext_mgr = self.ExtensionManager() else: raise Exception(_("Must specify an ExtensionManager class")) mapper = ProjectMapper() self.resources = {} self._setup_routes(mapper, ext_mgr) self._setup_ext_routes(mapper, ext_mgr) self._setup_extensions(ext_mgr) super(APIRouter, self).__init__(mapper)
def get_by_id(cls, context, id, *args, **kwargs): # To get by id we need to have a model and for the model to # have an id field if 'id' not in cls.fields: msg = (_('VersionedObject %s cannot retrieve object by id.') % (cls.obj_name())) raise NotImplementedError(msg) model = db.get_model_for_versioned_object(cls) orm_obj = db.get_by_id(context, model, id, *args, **kwargs) kargs = {} if hasattr(cls, 'DEFAULT_EXPECTED_ATTR'): kargs = {'expected_attrs': getattr(cls, 'DEFAULT_EXPECTED_ATTR')} return cls._from_db_object(context, cls(context), orm_obj, **kargs)
def _extract_bytes(self, details): # Replace it with the byte amount real_size = self.SIZE_RE.search(details) if not real_size: raise ValueError(_('Invalid input value "%s".') % details) magnitude = real_size.group(1) unit_of_measure = real_size.group(2) bytes_info = real_size.group(3) if bytes_info: return int(real_size.group(4)) elif not unit_of_measure: return int(magnitude) return strutils.string_to_bytes('%s%sB' % (magnitude, unit_of_measure), return_int=True)
def _get_free_space(conversion_dir): """Calculate and return free space available.""" try: out = utils.execute('df', '--portability', '--block-size', '1', conversion_dir, run_as_root=True)[0] out = out.splitlines()[1] available = int(out.split()[3]) except Exception: msg = _("Failed to get the available free space.") LOG.exception(msg) raise exception.GutsException(msg) return available
def update(context, id, name=None, driver=None, description=None): """Update source type by id.""" if id is None: msg = _("ID cannot be None") raise exception.InvalidSourceType(reason=msg) try: type_updated = db.source_type_update( context, id, dict(name=name, description=description, driver=driver)) except db_exc.DBError: LOG.exception(_LE('DB error:')) raise exception.SourceTypeUpdateFailed(id=id) return type_updated