def notify_usage_exists(instance_ref, current_period=False): """ Generates 'exists' notification for an instance for usage auditing purposes. Generates usage for last completed period, unless 'current_period' is True.""" admin_context = context.get_admin_context() begin, end = utils.current_audit_period() bw = {} if current_period: audit_start = end audit_end = utils.utcnow() else: audit_start = begin audit_end = end for b in db.bw_usage_get_by_instance(admin_context, instance_ref['id'], audit_start): bw[b.network_label] = dict(bw_in=b.bw_in, bw_out=b.bw_out) usage_info = utils.usage_from_instance(instance_ref, audit_period_beginning=str(audit_start), audit_period_ending=str(audit_end), bandwidth=bw) notifier_api.notify('compute.%s' % FLAGS.host, 'compute.instance.exists', notifier_api.INFO, usage_info)
def notify_usage_exists(instance_ref, current_period=False): """ Generates 'exists' notification for an instance for usage auditing purposes. Generates usage for last completed period, unless 'current_period' is True.""" admin_context = context.get_admin_context() begin, end = utils.current_audit_period() bw = {} if current_period: audit_start = end audit_end = utils.utcnow() else: audit_start = begin audit_end = end for b in db.bw_usage_get_by_instance(admin_context, instance_ref['id'], audit_start): bw[b.network_label] = dict(bw_in=b.bw_in, bw_out=b.bw_out) usage_info = utils.usage_from_instance( instance_ref, audit_period_beginning=str(audit_start), audit_period_ending=str(audit_end), bandwidth=bw) notifier_api.notify('compute.%s' % FLAGS.host, 'compute.instance.exists', notifier_api.INFO, usage_info)
def bless_instance(self, context, instance_uuid, migration_url=None): """ Construct the blessed instance, with the uuid instance_uuid. If migration_url is specified then bless will ensure a memory server is available at the given migration url. """ LOG.debug(_("bless instance called: instance_uuid=%s, migration_url=%s"), instance_uuid, migration_url) instance_ref = self.db.instance_get_by_uuid(context, instance_uuid) if migration_url: # Tweak only this instance directly. source_instance_ref = instance_ref migration = True else: usage_info = utils.usage_from_instance(instance_ref) notifier.notify('gridcentric.%s' % self.host, 'gridcentric.instance.bless.start', notifier.INFO, usage_info) source_instance_ref = self._get_source_instance(context, instance_uuid) migration = False self._instance_update(context, instance_ref.id, vm_state=vm_states.BUILDING) try: # Create a new 'blessed' VM with the given name. name, migration_url, blessed_files = self.vms_conn.bless(context, source_instance_ref.name, instance_ref, migration_url=migration_url, use_image_service=FLAGS.gridcentric_use_image_service) if not(migration): usage_info = utils.usage_from_instance(instance_ref) notifier.notify('gridcentric.%s' % self.host, 'gridcentric.instance.bless.end', notifier.INFO, usage_info) self._instance_update(context, instance_ref.id, vm_state="blessed", task_state=None, launched_at=utils.utcnow()) except Exception, e: LOG.debug(_("Error during bless %s: %s"), str(e), traceback.format_exc()) self._instance_update(context, instance_ref.id, vm_state=vm_states.ERROR, task_state=None) # Short-circuit, nothing to be done. return
def discard_instance(self, context, instance_uuid): """ Discards an instance so that and no further instances maybe be launched from it. """ LOG.debug(_("discard instance called: instance_uuid=%s"), instance_uuid) context.elevated() # Grab the DB representation for the VM. instance_ref = self.db.instance_get_by_uuid(context, instance_uuid) usage_info = utils.usage_from_instance(instance_ref) notifier.notify('gridcentric.%s' % self.host, 'gridcentric.instance.discard.start', notifier.INFO, usage_info) metadata = self._instance_metadata(context, instance_uuid) image_refs = self._extract_image_refs(metadata) # Call discard in the backend. self.vms_conn.discard(context, instance_ref.name, use_image_service=FLAGS.gridcentric_use_image_service, image_refs=image_refs) # Update the instance metadata (for completeness). metadata['blessed'] = False self._instance_metadata_update(context, instance_uuid, metadata) # Remove the instance. self._instance_update(context, instance_uuid, vm_state=vm_states.DELETED, task_state=None, terminated_at=timeutils.utcnow()) self.db.instance_destroy(context, instance_uuid) usage_info = utils.usage_from_instance(instance_ref) notifier.notify('gridcentric.%s' % self.host, 'gridcentric.instance.discard.end', notifier.INFO, usage_info)
def notify_usage_exists(instance_ref, current_period=False): """ Generates 'exists' notification for an instance for usage auditing purposes. Generates usage for last completed period, unless 'current_period' is True.""" admin_context = context.get_admin_context(read_deleted='yes') begin, end = utils.current_audit_period() bw = {} if current_period: audit_start = end audit_end = utils.utcnow() else: audit_start = begin audit_end = end if (instance_ref.get('info_cache') and instance_ref['info_cache'].get('network_info')): cached_info = instance_ref['info_cache']['network_info'] nw_info = network_model.NetworkInfo.hydrate(cached_info) else: nw_info = network.API().get_instance_nw_info(admin_context, instance_ref) macs = [vif['address'] for vif in nw_info] for b in db.bw_usage_get_by_macs(admin_context, macs, audit_start): label = 'net-name-not-found-%s' % b['mac'] for vif in nw_info: if vif['address'] == b['mac']: label = vif['network']['label'] break bw[label] = dict(bw_in=b.bw_in, bw_out=b.bw_out) usage_info = utils.usage_from_instance(instance_ref, audit_period_beginning=str(audit_start), audit_period_ending=str(audit_end), bandwidth=bw) notifier_api.notify('compute.%s' % FLAGS.host, 'compute.instance.exists', notifier_api.INFO, usage_info)
def notify_usage_exists(instance_ref, current_period=False): """ Generates 'exists' notification for an instance for usage auditing purposes. Generates usage for last completed period, unless 'current_period' is True.""" admin_context = context.get_admin_context(read_deleted='yes') begin, end = utils.current_audit_period() bw = {} if current_period: audit_start = end audit_end = utils.utcnow() else: audit_start = begin audit_end = end if (instance_ref.get('info_cache') and instance_ref['info_cache'].get('network_info')): cached_info = instance_ref['info_cache']['network_info'] nw_info = network_model.NetworkInfo.hydrate(cached_info) else: nw_info = network.API().get_instance_nw_info(admin_context, instance_ref) for b in db.bw_usage_get_by_instance(admin_context, instance_ref['id'], audit_start): label = 'net-name-not-found-%s' % b['mac'] for vif in nw_info: if vif['address'] == b['mac']: label = vif['network']['label'] break bw[label] = dict(bw_in=b.bw_in, bw_out=b.bw_out) usage_info = utils.usage_from_instance(instance_ref, audit_period_beginning=str(audit_start), audit_period_ending=str(audit_end), bandwidth=bw) notifier_api.notify('compute.%s' % FLAGS.host, 'compute.instance.exists', notifier_api.INFO, usage_info)
def launch_instance(self, context, instance_uuid, params={}, migration_url=None): """ Construct the launched instance, with uuid instance_uuid. If migration_url is not none then the instance will be launched using the memory server at the migration_url """ LOG.debug(_("Launching new instance: instance_uuid=%s, migration_url=%s"), instance_uuid, migration_url) # Grab the DB representation for the VM. instance_ref = self.db.instance_get_by_uuid(context, instance_uuid) if migration_url: # Just launch the given blessed instance. source_instance_ref = instance_ref # Load the old network info. network_info = self.network_api.get_instance_nw_info(context, instance_ref) # Update the instance state to be migrating. This will be set to # active again once it is completed in do_launch() as per all # normal launched instances. self._instance_update(context, instance_ref['uuid'], vm_state=vm_states.MIGRATING, task_state=task_states.SPAWNING, host=self.host) instance_ref['host'] = self.host else: usage_info = utils.usage_from_instance(instance_ref) notifier.notify('gridcentric.%s' % self.host, 'gridcentric.instance.launch.start', notifier.INFO, usage_info) # Create a new launched instance. source_instance_ref = self._get_source_instance(context, instance_uuid) if not FLAGS.stub_network: # TODO(dscannell): We need to set the is_vpn parameter correctly. # This information might come from the instance, or the user might # have to specify it. Also, we might be able to convert this to a # cast because we are not waiting on any return value. LOG.debug(_("Making call to network for launching instance=%s"), \ instance_ref.name) self._instance_update(context, instance_ref.id, vm_state=vm_states.BUILDING, task_state=task_states.NETWORKING, host=self.host) instance_ref['host'] = self.host is_vpn = False requested_networks = None try: network_info = self.network_api.allocate_for_instance(context, instance_ref, vpn=is_vpn, requested_networks=requested_networks) except Exception, e: LOG.debug(_("Error during network allocation: %s"), str(e)) self._instance_update(context, instance_ref['uuid'], vm_state=vm_states.ERROR, task_state=None) # Short-circuit, can't proceed. return LOG.debug(_("Made call to network for launching instance=%s, network_info=%s"), instance_ref.name, network_info) else:
'block_migration': False, 'disk': None}}, timeout=FLAGS.gridcentric_compute_timeout) self.vms_conn.launch(context, source_instance_ref.name, str(target), instance_ref, network_info, migration_url=migration_url, use_image_service=FLAGS.gridcentric_use_image_service, image_refs=image_refs, params=params) # Perform our database update. if migration_url == None: usage_info = utils.usage_from_instance(instance_ref, network_info=network_info) notifier.notify('gridcentric.%s' % self.host, 'gridcentric.instance.launch.end', notifier.INFO, usage_info) self._instance_update(context, instance_ref['uuid'], vm_state=vm_states.ACTIVE, host=self.host, launched_at=utils.utcnow(), task_state=None) except Exception, e: LOG.debug(_("Error during launch %s: %s"), str(e), traceback.format_exc()) self._instance_update(context, instance_ref['uuid'], vm_state=vm_states.ERROR, task_state=None) # Raise the error up. raise e