def test_instance_type_get_with_extra_specs(self): instance_type = db.instance_type_get( self.context, self.instance_type_id) self.assertEquals(instance_type['extra_specs'], dict(cpu_arch="x86_64", cpu_model="Nehalem", xpu_arch="fermi", xpus="2", xpu_model="Tesla 2050")) instance_type = db.instance_type_get( self.context, 5) self.assertEquals(instance_type['extra_specs'], {})
def schedule_prep_resize(self, context, request_spec, *args, **kwargs): """Select a target for resize. Selects a target host for the instance, post-resize, and casts the prep_resize operation to it. """ # We need the new instance type ID... instance_type_id = kwargs['instance_type_id'] elevated = context.elevated() LOG.debug(_("Attempting to determine target host for resize to " "instance type %(instance_type_id)s") % locals()) # Convert it to an actual instance type instance_type = db.instance_type_get(elevated, instance_type_id) # Now let's grab a possibility hosts = self._schedule(elevated, 'compute', request_spec, *args, **kwargs) if not hosts: raise exception.NoValidHost(reason=_("")) host = hosts.pop(0) # Forward off to the host driver.cast_to_host(context, 'compute', host.host, 'prep_resize', **kwargs)
def schedule_prep_resize(self, context, request_spec, *args, **kwargs): """Select a target for resize. Selects a target host for the instance, post-resize, and casts the prep_resize operation to it. """ # We need the new instance type ID... instance_type_id = kwargs['instance_type_id'] elevated = context.elevated() LOG.debug( _("Attempting to determine target host for resize to " "instance type %(instance_type_id)s") % locals()) # Convert it to an actual instance type instance_type = db.instance_type_get(elevated, instance_type_id) # Now let's grab a possibility hosts = self._schedule(elevated, 'compute', request_spec, *args, **kwargs) if not hosts: raise exception.NoValidHost(reason=_("")) host = hosts.pop(0) # Forward off to the host driver.cast_to_host(context, 'compute', host.host, 'prep_resize', **kwargs)
def get_instance_type(instance_type_id): """Retrieves single instance type by id.""" if instance_type_id is None: return get_default_instance_type() ctxt = context.get_admin_context() try: return db.instance_type_get(ctxt, instance_type_id) except exception.DBError: msg = _("Unknown instance type: %s") % instance_type_id raise exception.ApiError(msg)
def _usage_for_period(self, context, period_start, period_stop, tenant_id=None): fields = [ "id", "image_ref", "project_id", "user_id", "vcpus", "hostname", "display_name", "host", "task_state", "instance_type_id", "launched_at", "terminated_at", ] tenant_clause = "" if tenant_id: tenant_clause = " and project_id='%s'" % tenant_id connection = get_session().connection() rows = connection.execute( "select %s from instances where \ (terminated_at is NULL or terminated_at > '%s') \ and (launched_at < '%s') %s" % (",".join(fields), period_start.isoformat(" "), period_stop.isoformat(" "), tenant_clause) ).fetchall() rval = {} flavors = {} for row in rows: o = {} for i in range(len(fields)): o[fields[i]] = row[i] o["hours"] = self._hours_for(o, period_start, period_stop) flavor_type = o["instance_type_id"] try: flavors[flavor_type] = db.instance_type_get_by_id(context, flavor_type) except AttributeError: # The most recent version of engine renamed this function flavors[flavor_type] = db.instance_type_get(context, flavor_type) except exception.InstanceTypeNotFound: # can't bill if there is no instance type continue flavor = flavors[flavor_type] o["name"] = o["display_name"] del (o["display_name"]) o["ram_size"] = flavor["memory_mb"] o["disk_size"] = flavor["local_gb"] o["tenant_id"] = o["project_id"] del (o["project_id"]) o["flavor"] = flavor["name"] del (o["instance_type_id"]) o["started_at"] = o["launched_at"] del (o["launched_at"]) o["ended_at"] = o["terminated_at"] del (o["terminated_at"]) if o["ended_at"]: o["state"] = "terminated" else: o["state"] = o["task_state"] del (o["task_state"]) now = datetime.utcnow() if o["state"] == "terminated": delta = self._parse_datetime(o["ended_at"]) - self._parse_datetime(o["started_at"]) else: delta = now - self._parse_datetime(o["started_at"]) o["uptime"] = delta.days * 24 * 60 + delta.seconds if not o["tenant_id"] in rval: summary = {} summary["tenant_id"] = o["tenant_id"] summary["instances"] = [] summary["total_disk_usage"] = 0 summary["total_cpu_usage"] = 0 summary["total_ram_usage"] = 0 summary["total_active_ram_size"] = 0 summary["total_active_disk_size"] = 0 summary["total_active_vcpus"] = 0 summary["total_active_instances"] = 0 summary["total_hours"] = 0 summary["begin"] = period_start summary["stop"] = period_stop rval[o["tenant_id"]] = summary rval[o["tenant_id"]]["total_disk_usage"] += o["disk_size"] * o["hours"] rval[o["tenant_id"]]["total_cpu_usage"] += o["vcpus"] * o["hours"] rval[o["tenant_id"]]["total_ram_usage"] += o["ram_size"] * o["hours"] if o["state"] is not "terminated": rval[o["tenant_id"]]["total_active_ram_size"] += o["ram_size"] rval[o["tenant_id"]]["total_active_vcpus"] += o["vcpus"] rval[o["tenant_id"]]["total_active_disk_size"] += o["disk_size"] rval[o["tenant_id"]]["total_active_instances"] += 1 rval[o["tenant_id"]]["total_hours"] += o["hours"] rval[o["tenant_id"]]["instances"].append(o) return rval.values()
def _usage_for_period(self, context, period_start, period_stop, tenant_id=None): fields = [ 'id', 'image_ref', 'project_id', 'user_id', 'vcpus', 'hostname', 'display_name', 'host', 'task_state', 'instance_type_id', 'launched_at', 'terminated_at' ] tenant_clause = '' if tenant_id: tenant_clause = " and project_id='%s'" % tenant_id connection = get_session().connection() rows = connection.execute("select %s from instances where \ (terminated_at is NULL or terminated_at > '%s') \ and (launched_at < '%s') %s" %\ (','.join(fields), period_start.isoformat(' '),\ period_stop.isoformat(' '), tenant_clause )).fetchall() rval = {} flavors = {} for row in rows: o = {} for i in range(len(fields)): o[fields[i]] = row[i] o['hours'] = self._hours_for(o, period_start, period_stop) flavor_type = o['instance_type_id'] try: flavors[flavor_type] = \ db.instance_type_get_by_id(context, flavor_type) except AttributeError: # The most recent version of engine renamed this function flavors[flavor_type] = \ db.instance_type_get(context, flavor_type) except exception.InstanceTypeNotFound: # can't bill if there is no instance type continue flavor = flavors[flavor_type] o['name'] = o['display_name'] del (o['display_name']) o['ram_size'] = flavor['memory_mb'] o['disk_size'] = flavor['local_gb'] o['tenant_id'] = o['project_id'] del (o['project_id']) o['flavor'] = flavor['name'] del (o['instance_type_id']) o['started_at'] = o['launched_at'] del (o['launched_at']) o['ended_at'] = o['terminated_at'] del (o['terminated_at']) if o['ended_at']: o['state'] = 'terminated' else: o['state'] = o['task_state'] del (o['task_state']) now = datetime.utcnow() if o['state'] == 'terminated': delta = self._parse_datetime(o['ended_at'])\ - self._parse_datetime(o['started_at']) else: delta = now - self._parse_datetime(o['started_at']) o['uptime'] = delta.days * 24 * 60 + delta.seconds if not o['tenant_id'] in rval: summary = {} summary['tenant_id'] = o['tenant_id'] summary['instances'] = [] summary['total_disk_usage'] = 0 summary['total_cpu_usage'] = 0 summary['total_ram_usage'] = 0 summary['total_active_ram_size'] = 0 summary['total_active_disk_size'] = 0 summary['total_active_vcpus'] = 0 summary['total_active_instances'] = 0 summary['total_hours'] = 0 summary['begin'] = period_start summary['stop'] = period_stop rval[o['tenant_id']] = summary rval[o['tenant_id']][ 'total_disk_usage'] += o['disk_size'] * o['hours'] rval[o['tenant_id']]['total_cpu_usage'] += o['vcpus'] * o['hours'] rval[o['tenant_id']][ 'total_ram_usage'] += o['ram_size'] * o['hours'] if o['state'] is not 'terminated': rval[o['tenant_id']]['total_active_ram_size'] += o['ram_size'] rval[o['tenant_id']]['total_active_vcpus'] += o['vcpus'] rval[ o['tenant_id']]['total_active_disk_size'] += o['disk_size'] rval[o['tenant_id']]['total_active_instances'] += 1 rval[o['tenant_id']]['total_hours'] += o['hours'] rval[o['tenant_id']]['instances'].append(o) return rval.values()