def _send_instance_update_notification(context, instance, old_vm_state, old_task_state, new_vm_state, new_task_state, service=None, host=None): """Send 'compute.instance.exists' notification to inform observers about instance state changes""" payload = usage_from_instance(context, instance, None, None) states_payload = { "old_state": old_vm_state, "state": new_vm_state, "old_task_state": old_task_state, "new_task_state": new_task_state, } payload.update(states_payload) # add audit fields: (audit_start, audit_end) = audit_period_bounds(current_period=True) payload["audit_period_beginning"] = audit_start payload["audit_period_ending"] = audit_end # add bw usage info: bw = bandwidth_usage(instance, audit_start) payload["bandwidth"] = bw # if the service name (e.g. api/scheduler/compute) is not provided, default # to "compute" if not service: service = "compute" publisher_id = notifier_api.publisher_id(service, host) notifier_api.notify(context, publisher_id, 'compute.instance.update', notifier_api.INFO, payload)
def _set_instance_error(self, method, context, ex, *args, **kwargs): """Sets VM to Error state""" LOG.warning(_("Failed to schedule_%(method)s: %(ex)s") % locals()) # FIXME(comstud): Re-factor this somehow. Not sure this belongs # in the scheduler manager like this. Needs to support more than # run_instance if method != "run_instance": return # FIXME(comstud): We should make this easier. run_instance # only sends a request_spec, and an instance may or may not # have been created in the API (or scheduler) already. If it # was created, there's a 'uuid' set in the instance_properties # of the request_spec. request_spec = kwargs.get('request_spec', {}) properties = request_spec.get('instance_properties', {}) instance_uuid = properties.get('uuid', {}) if instance_uuid: LOG.warning( _("Setting instance %(instance_uuid)s to " "ERROR state.") % locals()) db.instance_update(context, instance_uuid, {'vm_state': vm_states.ERROR}) payload = dict(request_spec=request_spec, instance_properties=properties, instance_id=instance_uuid, state=vm_states.ERROR, method=method, reason=ex) notifier.notify(notifier.publisher_id("scheduler"), 'scheduler.run_instance', notifier.ERROR, payload)
def _set_vm_state_and_notify(self, method, updates, context, ex, *args, **kwargs): """changes VM state and notifies""" # FIXME(comstud): Re-factor this somehow. Not sure this belongs in the # scheduler manager like this. We should make this easier. # run_instance only sends a request_spec, and an instance may or may # not have been created in the API (or scheduler) already. If it was # created, there's a 'uuid' set in the instance_properties of the # request_spec. # (littleidea): I refactored this a bit, and I agree # it should be easier :) # The refactoring could go further but trying to minimize changes # for essex timeframe LOG.warning(_("Failed to schedule_%(method)s: %(ex)s") % locals()) vm_state = updates["vm_state"] request_spec = kwargs.get("request_spec", {}) properties = request_spec.get("instance_properties", {}) instance_uuid = properties.get("uuid", {}) if instance_uuid: state = vm_state.upper() LOG.warning(_("Setting instance to %(state)s state."), locals(), instance_uuid=instance_uuid) db.instance_update(context, instance_uuid, updates) payload = dict( request_spec=request_spec, instance_properties=properties, instance_id=instance_uuid, state=vm_state, method=method, reason=ex, ) notifier.notify(context, notifier.publisher_id("scheduler"), "scheduler." + method, notifier.ERROR, payload)
def _set_instance_error(self, method, context, ex, *args, **kwargs): """Sets VM to Error state""" LOG.warning(_("Failed to schedule_%(method)s: %(ex)s") % locals()) # FIXME(comstud): Re-factor this somehow. Not sure this belongs # in the scheduler manager like this. Needs to support more than # run_instance if method != "run_instance": return # FIXME(comstud): We should make this easier. run_instance # only sends a request_spec, and an instance may or may not # have been created in the API (or scheduler) already. If it # was created, there's a 'uuid' set in the instance_properties # of the request_spec. request_spec = kwargs.get('request_spec', {}) properties = request_spec.get('instance_properties', {}) instance_uuid = properties.get('uuid', {}) if instance_uuid: LOG.warning(_("Setting instance %(instance_uuid)s to " "ERROR state.") % locals()) db.instance_update(context, instance_uuid, {'vm_state': vm_states.ERROR}) payload = dict(request_spec=request_spec, instance_properties=properties, instance_id=instance_uuid, state=vm_states.ERROR, method=method, reason=ex) notifier.notify(notifier.publisher_id("scheduler"), 'scheduler.run_instance', notifier.ERROR, payload)
def schedule_run_instance(self, context, request_spec, reservations, *args, **kwargs): """This method is called from nova.compute.api to provision an instance. We first create a build plan (a list of WeightedHosts) and then provision. Returns a list of the instances created. """ elevated = context.elevated() num_instances = request_spec.get('num_instances', 1) LOG.debug( _("Attempting to build %(num_instances)d instance(s)") % locals()) payload = dict(request_spec=request_spec) notifier.notify(context, notifier.publisher_id("scheduler"), 'scheduler.run_instance.start', notifier.INFO, payload) weighted_hosts = self._schedule(context, "compute", request_spec, *args, **kwargs) if not weighted_hosts: raise exception.NoValidHost(reason="") # NOTE(comstud): Make sure we do not pass this through. It # contains an instance of RpcContext that cannot be serialized. kwargs.pop('filter_properties', None) instances = [] for num in xrange(num_instances): if not weighted_hosts: break weighted_host = weighted_hosts.pop(0) request_spec['instance_properties']['launch_index'] = num instance = self._provision_resource(elevated, weighted_host, request_spec, reservations, kwargs) if instance: instances.append(instance) notifier.notify(context, notifier.publisher_id("scheduler"), 'scheduler.run_instance.end', notifier.INFO, payload) return instances
def schedule_run_instance(self, context, request_spec, reservations, *args, **kwargs): """This method is called from nova.compute.api to provision an instance. We first create a build plan (a list of WeightedHosts) and then provision. Returns a list of the instances created. """ elevated = context.elevated() num_instances = request_spec.get('num_instances', 1) LOG.debug(_("Attempting to build %(num_instances)d instance(s)") % locals()) payload = dict(request_spec=request_spec) notifier.notify(context, notifier.publisher_id("scheduler"), 'scheduler.run_instance.start', notifier.INFO, payload) weighted_hosts = self._schedule(context, "compute", request_spec, *args, **kwargs) if not weighted_hosts: raise exception.NoValidHost(reason="") # NOTE(comstud): Make sure we do not pass this through. It # contains an instance of RpcContext that cannot be serialized. kwargs.pop('filter_properties', None) instances = [] for num in xrange(num_instances): if not weighted_hosts: break weighted_host = weighted_hosts.pop(0) request_spec['instance_properties']['launch_index'] = num instance = self._provision_resource(elevated, weighted_host, request_spec, reservations, kwargs) if instance: instances.append(instance) notifier.notify(context, notifier.publisher_id("scheduler"), 'scheduler.run_instance.end', notifier.INFO, payload) return instances
def alert_once(self): # TODO: dispose timeout worker here print "*" * 400 print "[WARNING]worker", self.worker_id, "is dead. email sendto admin" print "*" * 400 payload = dict() payload["host"] = FLAGS.my_ip payload["message"] = "kanyun-worker is dead" notifier.notify(notifier.publisher_id("compute"), "kanyun.worker", notifier.WARN, payload) self.alerted = True
def alert_once(self): # TODO: dispose timeout worker here print '*' * 400 print '[WARNING]worker', self.worker_id, "is dead. email sendto admin" print '*' * 400 payload = dict() payload['host'] = FLAGS.my_ip payload['message'] = 'kanyun-worker is dead' notifier.notify(notifier.publisher_id('compute'), 'kanyun.worker', notifier.WARN, payload) self.alerted = True
def _send_instance_update_notification(context, instance, old_vm_state, old_task_state, new_vm_state, new_task_state, service=None, host=None): """Send 'compute.instance.exists' notification to inform observers about instance state changes""" payload = usage_from_instance(context, instance, None, None) states_payload = { "old_state": old_vm_state, "state": new_vm_state, "old_task_state": old_task_state, "new_task_state": new_task_state, } payload.update(states_payload) # add audit fields: (audit_start, audit_end) = audit_period_bounds(current_period=True) payload["audit_period_beginning"] = audit_start payload["audit_period_ending"] = audit_end # add bw usage info: bw = bandwidth_usage(instance, audit_start) payload["bandwidth"] = bw try: system_metadata = db.instance_system_metadata_get( context, instance.uuid) except exception.NotFound: system_metadata = {} # add image metadata image_meta_props = image_meta(system_metadata) payload["image_meta"] = image_meta_props # if the service name (e.g. api/scheduler/compute) is not provided, default # to "compute" if not service: service = "compute" publisher_id = notifier_api.publisher_id(service, host) notifier_api.notify(context, publisher_id, 'compute.instance.update', notifier_api.INFO, payload)
def _provision_resource(self, context, weighted_host, request_spec, kwargs): """Create the requested resource in this Zone.""" instance = self.create_instance_db_entry(context, request_spec) payload = dict(request_spec=request_spec, weighted_host=weighted_host.to_dict(), instance_id=instance['uuid']) notifier.notify(notifier.publisher_id("scheduler"), 'scheduler.run_instance.scheduled', notifier.INFO, payload) driver.cast_to_compute_host(context, weighted_host.host_state.host, 'run_instance', instance_uuid=instance['uuid'], **kwargs) inst = driver.encode_instance(instance, local=True) # So if another instance is created, create_instance_db_entry will # actually create a new entry, instead of assume it's been created # already del request_spec['instance_properties']['uuid'] return inst
def _set_vm_state_and_notify(self, method, updates, context, ex, *args, **kwargs): """changes VM state and notifies""" # FIXME(comstud): Re-factor this somehow. Not sure this belongs in the # scheduler manager like this. We should make this easier. # run_instance only sends a request_spec, and an instance may or may # not have been created in the API (or scheduler) already. If it was # created, there's a 'uuid' set in the instance_properties of the # request_spec. # (littleidea): I refactored this a bit, and I agree # it should be easier :) # The refactoring could go further but trying to minimize changes # for essex timeframe LOG.warning(_("Failed to schedule_%(method)s: %(ex)s") % locals()) vm_state = updates['vm_state'] request_spec = kwargs.get('request_spec', {}) properties = request_spec.get('instance_properties', {}) instance_uuid = properties.get('uuid', {}) if instance_uuid: state = vm_state.upper() LOG.warning(_('Setting instance to %(state)s state.'), locals(), instance_uuid=instance_uuid) # update instance state and notify on the transition (old_ref, new_ref) = db.instance_update_and_get_original(context, instance_uuid, updates) notifications.send_update(context, old_ref, new_ref, service="scheduler") payload = dict(request_spec=request_spec, instance_properties=properties, instance_id=instance_uuid, state=vm_state, method=method, reason=ex) notifier.notify(context, notifier.publisher_id("scheduler"), 'scheduler.' + method, notifier.ERROR, payload)
def _provision_resource(self, context, weighted_host, request_spec, reservations, kwargs): """Create the requested resource in this Zone.""" instance = self.create_instance_db_entry(context, request_spec, reservations) payload = dict(request_spec=request_spec, weighted_host=weighted_host.to_dict(), instance_id=instance['uuid']) notifier.notify(context, notifier.publisher_id("scheduler"), 'scheduler.run_instance.scheduled', notifier.INFO, payload) driver.cast_to_compute_host(context, weighted_host.host_state.host, 'run_instance', instance_uuid=instance['uuid'], **kwargs) inst = driver.encode_instance(instance, local=True) # So if another instance is created, create_instance_db_entry will # actually create a new entry, instead of assume it's been created # already del request_spec['instance_properties']['uuid'] return inst