def update(self, req, id, body): """Enable/Disable scheduling for a service.""" context = req.environ['nova.context'] authorize(context) if id == "enable": disabled = False elif id == "disable": disabled = True else: raise webob.exc.HTTPNotFound("Unknown action") try: host = body['host'] binary = body['binary'] except (TypeError, KeyError): raise webob.exc.HTTPUnprocessableEntity() try: svc = db.service_get_by_args(context, host, binary) if not svc: raise webob.exc.HTTPNotFound('Unknown service') db.service_update(context, svc['id'], {'disabled': disabled}) except exception.ServiceNotFound: raise webob.exc.HTTPNotFound("service not found") status = id + 'd' return {'service': {'host': host, 'binary': binary, 'status': status}}
def update(self, req, id, body): """Enable/Disable scheduling for a service""" context = req.environ['nova.context'] authorize(context) if id == "enable": disabled = False elif id == "disable": disabled = True else: raise webob.exc.HTTPNotFound("Unknown action") try: host = body['host'] service = body['service'] except (TypeError, KeyError): raise webob.exc.HTTPUnprocessableEntity() try: svc = db.service_get_by_args(context, host, service) if not svc: raise webob.exc.HTTPNotFound('Unknown service') db.service_update(context, svc['id'], {'disabled': disabled}) except exception.ServiceNotFound: raise webob.exc.HTTPNotFound("service not found") return {'host': host, 'service': service, 'disabled': disabled}
def test_service_update(self): ctxt = self.context self.mox.StubOutWithMock(db, 'service_update') db.service_update(ctxt, '', {}).AndReturn('fake-result') self.mox.ReplayAll() result = self.conductor.service_update(self.context, {'id': ''}, {}) self.assertEqual(result, 'fake-result')
def test_service_update(self): ctxt = self.context self.mox.StubOutWithMock(db, "service_update") db.service_update(ctxt, "", {}).AndReturn("fake-result") self.mox.ReplayAll() result = self.conductor.service_update(self.context, {"id": ""}, {}) self.assertEqual(result, "fake-result")
def report_state(self): """Update the state of this service in the datastore.""" ctxt = context.get_admin_context() zone = FLAGS.node_availability_zone state_catalog = {} try: try: service_ref = db.service_get(ctxt, self.service_id) except exception.NotFound: logging.debug(_('The service database object disappeared, ' 'Recreating it.')) self._create_service_ref(ctxt) service_ref = db.service_get(ctxt, self.service_id) state_catalog['report_count'] = service_ref['report_count'] + 1 if zone != service_ref['availability_zone']: state_catalog['availability_zone'] = zone db.service_update(ctxt, self.service_id, state_catalog) # TODO(termie): make this pattern be more elegant. if getattr(self, 'model_disconnected', False): self.model_disconnected = False logging.error(_('Recovered model server connection!')) # TODO(vish): this should probably only catch connection errors except Exception: # pylint: disable=W0702 if not getattr(self, 'model_disconnected', False): self.model_disconnected = True logging.exception(_('model server went away'))
def _report_state(self, service): """Update the state of this service in the datastore.""" ctxt = context.get_admin_context() zone = CONF.node_availability_zone state_catalog = {} try: try: service_ref = db.service_get(ctxt, service.service_id) except exception.NotFound: LOG.debug( _('The service database object disappeared, ' 'Recreating it.')) service._create_service_ref(ctxt) service_ref = db.service_get(ctxt, service.service_id) state_catalog['report_count'] = service_ref['report_count'] + 1 if zone != service_ref['availability_zone']: state_catalog['availability_zone'] = zone db.service_update(ctxt, service.service_id, state_catalog) # TODO(termie): make this pattern be more elegant. if getattr(service, 'model_disconnected', False): service.model_disconnected = False LOG.error(_('Recovered model server connection!')) # TODO(vish): this should probably only catch connection errors except Exception: # pylint: disable=W0702 if not getattr(service, 'model_disconnected', False): service.model_disconnected = True LOG.exception(_('model server went away'))
def report_state(self): """Update the state of this service in the datastore.""" ctxt = context.get_admin_context() try: try: service_ref = db.service_get(ctxt, self.service_id) except exception.NotFound: logging.debug(_("The service database object disappeared, " "Recreating it.")) self._create_service_ref(ctxt) service_ref = db.service_get(ctxt, self.service_id) db.service_update(ctxt, self.service_id, {'report_count': service_ref['report_count'] + 1}) # TODO(termie): make this pattern be more elegant. if getattr(self, "model_disconnected", False): self.model_disconnected = False logging.error(_("Recovered model server connection!")) # TODO(vish): this should probably only catch connection errors except Exception: # pylint: disable=W0702 if not getattr(self, "model_disconnected", False): self.model_disconnected = True logging.exception(_("model server went away"))
def report_state(self): """Update the state of this service in the datastore.""" ctxt = context.get_admin_context() try: try: service_ref = db.service_get(ctxt, self.service_id) except exception.NotFound: logging.debug( _('The service database object disappeared, ' 'Recreating it.')) self._create_service_ref(ctxt) service_ref = db.service_get(ctxt, self.service_id) db.service_update( ctxt, self.service_id, {'report_count': service_ref['report_count'] + 1}) # TODO(termie): make this pattern be more elegant. if getattr(self, 'model_disconnected', False): self.model_disconnected = False logging.error(_('Recovered model server connection!')) # TODO(vish): this should probably only catch connection errors except Exception: # pylint: disable=W0702 if not getattr(self, 'model_disconnected', False): self.model_disconnected = True logging.exception(_('model server went away'))
def _create_compute_service(self, **kwargs): """Create a compute service.""" dic = {"binary": "nova-compute", "topic": "compute", "report_count": 0, "availability_zone": "dummyzone"} dic["host"] = kwargs.get("host", "dummy") s_ref = db.service_create(self.context, dic) if "created_at" in kwargs.keys() or "updated_at" in kwargs.keys(): t = utils.utcnow() - datetime.timedelta(0) dic["created_at"] = kwargs.get("created_at", t) dic["updated_at"] = kwargs.get("updated_at", t) db.service_update(self.context, s_ref["id"], dic) dic = { "service_id": s_ref["id"], "vcpus": 16, "memory_mb": 32, "local_gb": 100, "vcpus_used": 16, "local_gb_used": 10, "hypervisor_type": "qemu", "hypervisor_version": 12003, "cpu_info": "", } dic["memory_mb_used"] = kwargs.get("memory_mb_used", 32) dic["hypervisor_type"] = kwargs.get("hypervisor_type", "qemu") dic["hypervisor_version"] = kwargs.get("hypervisor_version", 12003) db.compute_node_create(self.context, dic) return db.service_get(self.context, s_ref["id"])
def _create_compute_service(self, **kwargs): """Create a compute service.""" dic = { 'binary': 'nova-compute', 'topic': 'compute', 'report_count': 0, 'availability_zone': 'dummyzone' } dic['host'] = kwargs.get('host', 'dummy') s_ref = db.service_create(self.context, dic) if 'created_at' in kwargs.keys() or 'updated_at' in kwargs.keys(): t = utils.utcnow() - datetime.timedelta(0) dic['created_at'] = kwargs.get('created_at', t) dic['updated_at'] = kwargs.get('updated_at', t) db.service_update(self.context, s_ref['id'], dic) dic = { 'service_id': s_ref['id'], 'vcpus': 16, 'memory_mb': 32, 'local_gb': 100, 'vcpus_used': 16, 'local_gb_used': 10, 'hypervisor_type': 'qemu', 'hypervisor_version': 12003, 'cpu_info': '' } dic['memory_mb_used'] = kwargs.get('memory_mb_used', 32) dic['hypervisor_type'] = kwargs.get('hypervisor_type', 'qemu') dic['hypervisor_version'] = kwargs.get('hypervisor_version', 12003) db.compute_node_create(self.context, dic) return db.service_get(self.context, s_ref['id'])
def update(self, req, id, body): """Enable/Disable scheduling for a service.""" context = req.environ["nova.context"] authorize(context) if id == "enable": disabled = False elif id == "disable": disabled = True else: raise webob.exc.HTTPNotFound("Unknown action") try: host = body["host"] service = body["service"] except (TypeError, KeyError): raise webob.exc.HTTPUnprocessableEntity() try: svc = db.service_get_by_args(context, host, service) if not svc: raise webob.exc.HTTPNotFound("Unknown service") db.service_update(context, svc["id"], {"disabled": disabled}) except exception.ServiceNotFound: raise webob.exc.HTTPNotFound("service not found") return {"host": host, "service": service, "disabled": disabled}
def test_save(self): self.mox.StubOutWithMock(db, "service_update") db.service_update(self.context, 123, {"host": "fake-host"}).AndReturn(fake_service) self.mox.ReplayAll() service_obj = service.Service() service_obj.id = 123 service_obj.host = "fake-host" service_obj.save(self.context)
def test_will_schedule_on_disabled_host_if_specified(self): compute1 = self.start_service("compute", host="host1") s1 = db.service_get_by_args(self.context, "host1", "nova-compute") db.service_update(self.context, s1["id"], {"disabled": True}) instance_id2 = self._create_instance(availability_zone="nova:host1") host = self.scheduler.driver.schedule_run_instance(self.context, instance_id2) self.assertEqual("host1", host) db.instance_destroy(self.context, instance_id2) compute1.kill()
def test_save(self): self.mox.StubOutWithMock(db, 'service_update') db.service_update(self.context, 123, {'host': 'fake-host'}).AndReturn( fake_service) self.mox.ReplayAll() service_obj = service.Service() service_obj.id = 123 service_obj.host = 'fake-host' service_obj.save(self.context)
def test_will_schedule_on_disabled_host_if_specified(self): compute1 = self.start_service('compute', host='host1') s1 = db.service_get_by_args(self.context, 'host1', 'nova-compute') db.service_update(self.context, s1['id'], {'disabled': True}) instance_id2 = self._create_instance(availability_zone='nova:host1') host = self.scheduler.driver.schedule_run_instance( self.context, instance_id2) self.assertEqual('host1', host) db.instance_destroy(self.context, instance_id2) compute1.kill()
def test_will_schedule_on_disabled_host_if_specified(self): compute1 = self.start_service('compute', host='host1') s1 = db.service_get_by_args(self.context, 'host1', 'nova-compute') db.service_update(self.context, s1['id'], {'disabled': True}) instance_id2 = self._create_instance(availability_zone='nova:host1') host = self.scheduler.driver.schedule_run_instance(self.context, instance_id2) self.assertEqual('host1', host) db.instance_destroy(self.context, instance_id2) compute1.kill()
def enable(self, host, service): """Enable scheduling for a service.""" ctxt = context.get_admin_context() try: svc = db.service_get_by_args(ctxt, host, service) db.service_update(ctxt, svc["id"], {"disabled": False}) except exception.NotFound as ex: print _("error: %s") % ex return 2 print _("Service %(service)s on host %(host)s enabled.") % locals()
def disable(self, host, service): """Disable scheduling for a service.""" ctxt = context.get_admin_context() try: svc = db.service_get_by_args(ctxt, host, service) db.service_update(ctxt, svc['id'], {'disabled': True}) except exception.NotFound as ex: print _("error: %s") % ex return(2) print _("Service %(service)s on host %(host)s disabled.") % locals()
def test_save(self): self.mox.StubOutWithMock(db, 'service_update') db.service_update(self.context, 123, {'host': 'fake-host', 'version': 1}).AndReturn( fake_service) self.mox.ReplayAll() service_obj = service.Service(context=self.context) service_obj.id = 123 service_obj.host = 'fake-host' service_obj.save() self.assertEqual(service.SERVICE_VERSION, service_obj.version)
def enable(self, host, service): """Enable scheduling for a service.""" ctxt = context.get_admin_context() try: svc = db.service_get_by_args(ctxt, host, service) db.service_update(ctxt, svc['id'], {'disabled': False}) except exception.NotFound as ex: print(_("error: %s") % ex) return(2) print((_("Service %(service)s on host %(host)s enabled.") % {'service': service, 'host': host}))
def test_doesnt_report_disabled_hosts_as_up(self): """Ensures driver doesn't find hosts before they are enabled""" compute1 = self.start_service("compute", host="host1") compute2 = self.start_service("compute", host="host2") s1 = db.service_get_by_args(self.context, "host1", "nova-compute") s2 = db.service_get_by_args(self.context, "host2", "nova-compute") db.service_update(self.context, s1["id"], {"disabled": True}) db.service_update(self.context, s2["id"], {"disabled": True}) hosts = self.scheduler.driver.hosts_up(self.context, "compute") self.assertEqual(0, len(hosts)) compute1.kill() compute2.kill()
def test_doesnt_report_disabled_hosts_as_up(self): """Ensures driver doesn't find hosts before they are enabled""" compute1 = self.start_service('compute', host='host1') compute2 = self.start_service('compute', host='host2') s1 = db.service_get_by_args(self.context, 'host1', 'nova-compute') s2 = db.service_get_by_args(self.context, 'host2', 'nova-compute') db.service_update(self.context, s1['id'], {'disabled': True}) db.service_update(self.context, s2['id'], {'disabled': True}) hosts = self.scheduler.driver.hosts_up(self.context, 'compute') self.assertEqual(0, len(hosts)) compute1.kill() compute2.kill()
def test_save(self): self.mox.StubOutWithMock(db, 'service_update') db.service_update(self.context, 123, { 'host': 'fake-host', 'version': fake_service['version'] }).AndReturn(fake_service) self.mox.ReplayAll() service_obj = service.Service(context=self.context) service_obj.id = 123 service_obj.host = 'fake-host' service_obj.save() self.assertEqual(service.SERVICE_VERSION, service_obj.version)
def test_wont_sechedule_if_specified_host_is_down(self): compute1 = self.start_service('compute', host='host1') s1 = db.service_get_by_args(self.context, 'host1', 'nova-compute') now = utils.utcnow() delta = datetime.timedelta(seconds=FLAGS.service_down_time * 2) past = now - delta db.service_update(self.context, s1['id'], {'updated_at': past}) instance_id2 = self._create_instance(availability_zone='nova:host1') self.assertRaises(driver.WillNotSchedule, self.scheduler.driver.schedule_run_instance, self.context, instance_id2) db.instance_destroy(self.context, instance_id2) compute1.kill()
def test_wont_sechedule_if_specified_host_is_down(self): compute1 = self.start_service("compute", host="host1") s1 = db.service_get_by_args(self.context, "host1", "nova-compute") now = utils.utcnow() delta = datetime.timedelta(seconds=FLAGS.service_down_time * 2) past = now - delta db.service_update(self.context, s1["id"], {"updated_at": past}) instance_id2 = self._create_instance(availability_zone="nova:host1") self.assertRaises( driver.WillNotSchedule, self.scheduler.driver.schedule_run_instance, self.context, instance_id2 ) db.instance_destroy(self.context, instance_id2) compute1.kill()
def enable(self, host, service): """Enable scheduling for a service.""" ctxt = context.get_admin_context() try: svc = db.service_get_by_args(ctxt, host, service) db.service_update(ctxt, svc['id'], {'disabled': False}) except exception.NotFound as ex: print(_("error: %s") % ex) return (2) print((_("Service %(service)s on host %(host)s enabled.") % { 'service': service, 'host': host }))
def test_wont_sechedule_if_specified_host_is_down(self): compute1 = self.start_service('compute', host='host1') s1 = db.service_get_by_args(self.context, 'host1', 'nova-compute') now = datetime.datetime.utcnow() delta = datetime.timedelta(seconds=FLAGS.service_down_time * 2) past = now - delta db.service_update(self.context, s1['id'], {'updated_at': past}) instance_id2 = self._create_instance(availability_zone='nova:host1') self.assertRaises(driver.WillNotSchedule, self.scheduler.driver.schedule_run_instance, self.context, instance_id2) db.instance_destroy(self.context, instance_id2) compute1.kill()
def save(self, context): updates = {} for key in self.obj_what_changed(): updates[key] = self[key] updates.pop('id', None) db_service = db.service_update(context, self.id, updates) self._from_db_object(context, self, db_service)
def test_doesnt_report_disabled_hosts_as_up(self): """Ensures driver doesn't find hosts before they are enabled""" # NOTE(vish): constructing service without create method # because we are going to use it without queue compute1 = service.Service("host1", "nova-compute", "compute", FLAGS.compute_manager) compute1.start() compute2 = service.Service("host2", "nova-compute", "compute", FLAGS.compute_manager) compute2.start() s1 = db.service_get_by_args(self.context, "host1", "nova-compute") s2 = db.service_get_by_args(self.context, "host2", "nova-compute") db.service_update(self.context, s1["id"], {"disabled": True}) db.service_update(self.context, s2["id"], {"disabled": True}) hosts = self.scheduler.driver.hosts_up(self.context, "compute") self.assertEqual(0, len(hosts)) compute1.kill() compute2.kill()
def save(self): updates = self.obj_get_changes() updates.pop('id', None) self._check_minimum_version() db_service = db.service_update(self._context, self.id, updates) self._from_db_object(self._context, self, db_service) self._send_status_update_notification(updates)
def save(self): updates = self.obj_get_changes() updates.pop('id', None) #剔除,id的改变不保存。 self._check_minimum_version() db_service = db.service_update(self._context, self.id, updates) #更新后,从新获取 self._from_db_object(self._context, self, db_service) #根据db数据从新生成instance self._send_status_update_notification(updates)
def test_doesnt_report_disabled_hosts_as_up(self): """Ensures driver doesn't find hosts before they are enabled""" # NOTE(vish): constructing service without create method # because we are going to use it without queue compute1 = service.Service('host1', 'nova-compute', 'compute', FLAGS.compute_manager) compute1.start() compute2 = service.Service('host2', 'nova-compute', 'compute', FLAGS.compute_manager) compute2.start() s1 = db.service_get_by_args(self.context, 'host1', 'nova-compute') s2 = db.service_get_by_args(self.context, 'host2', 'nova-compute') db.service_update(self.context, s1['id'], {'disabled': True}) db.service_update(self.context, s2['id'], {'disabled': True}) hosts = self.scheduler.driver.hosts_up(self.context, 'compute') self.assertEqual(0, len(hosts)) compute1.kill() compute2.kill()
def save(self): updates = self.obj_get_changes() updates.pop('id', None) if list(updates.keys()) == ['version']: # NOTE(danms): Since we set/dirty version in init, don't # do a save if that's all that has changed. This keeps the # "save is a no-op if nothing has changed" behavior. return db_service = db.service_update(self._context, self.id, updates) self._from_db_object(self._context, self, db_service)
def save(self): updates = self.obj_get_changes() updates.pop("id", None) if list(updates.keys()) == ["version"]: # NOTE(danms): Since we set/dirty version in init, don't # do a save if that's all that has changed. This keeps the # "save is a no-op if nothing has changed" behavior. return db_service = db.service_update(self._context, self.id, updates) self._from_db_object(self._context, self, db_service)
def save(self): updates = self.obj_get_changes() updates.pop('id', None) if list(updates.keys()) == ['version']: # NOTE(danms): Since we set/dirty version in init, don't # do a save if that's all that has changed. This keeps the # "save is a no-op if nothing has changed" behavior. return self._check_minimum_version() db_service = db.service_update(self._context, self.id, updates) self._from_db_object(self._context, self, db_service) self._send_status_update_notification(updates)
def _create_compute_service(self, **kwargs): """Create a compute service.""" dic = {'binary': 'nova-compute', 'topic': 'compute', 'report_count': 0, 'availability_zone': 'dummyzone'} dic['host'] = kwargs.get('host', 'dummy') s_ref = db.service_create(self.context, dic) if 'created_at' in kwargs.keys() or 'updated_at' in kwargs.keys(): t = datetime.datetime.utcnow() - datetime.timedelta(0) dic['created_at'] = kwargs.get('created_at', t) dic['updated_at'] = kwargs.get('updated_at', t) db.service_update(self.context, s_ref['id'], dic) dic = {'service_id': s_ref['id'], 'vcpus': 16, 'memory_mb': 32, 'local_gb': 100, 'vcpus_used': 16, 'local_gb_used': 10, 'hypervisor_type': 'qemu', 'hypervisor_version': 12003, 'cpu_info': ''} dic['memory_mb_used'] = kwargs.get('memory_mb_used', 32) dic['hypervisor_type'] = kwargs.get('hypervisor_type', 'qemu') dic['hypervisor_version'] = kwargs.get('hypervisor_version', 12003) db.compute_node_create(self.context, dic) return db.service_get(self.context, s_ref['id'])
def save(self): updates = self.obj_get_changes() updates.pop('id', None) db_service = db.service_update(self._context, self.id, updates) self._from_db_object(self._context, self, db_service)
def update(self, req, id): context = req.environ['nova.context'].elevated() env = self._deserialize(req.body, req.get_content_type()) name = env['service'].get('disabled') db.service_update(context, id, env['service']) return exc.HTTPAccepted()
def save(self, context): updates = self.obj_get_changes() updates.pop('id', None) db_service = db.service_update(context, self.id, updates) self._from_db_object(context, self, db_service)
def update(self, req, id, body): context = req.environ["nova.context"] name = body["service"].get("disabled") db.service_update(context, id, body["service"]) return exc.HTTPAccepted()
def update(self, req, id, body): context = req.environ['nova.context'].elevated() name = body['service'].get('disabled') db.service_update(context, id, body['service']) return exc.HTTPAccepted()
def start(self, debug=False): vcs_string = version.version_string_with_vcs() LOG.audit(_('Starting %(topic)s node (version %(vcs_string)s)'), {'topic': self.topic, 'vcs_string': vcs_string}) self.manager.init_host() self.model_disconnected = False ctxt = context.get_admin_context() try: service_ref = db.service_get_by_args(ctxt, self.host, self.binary) self.service_id = service_ref['id'] host_ip = self._get_host_ip_by_ifname(FLAGS.host_ip_ifname) if host_ip != service_ref['host_ip']: host_ip_addr = {'host_ip': host_ip} db.service_update(ctxt, self.service_id, host_ip_addr) LOG.info(_("update host ip to %(host_ip)s") % locals()) except exception.NotFound: self._create_service_ref(ctxt) if 'nova-compute' == self.binary: self.manager.update_available_resource(ctxt) if 'nova-compute' == self.binary and not debug: loop_call_type = utils.ProcessLoopingCall else: loop_call_type = utils.LoopingCall pulse = None if self.report_interval: pulse = loop_call_type(self.report_state) periodic = None if self.periodic_interval: if self.periodic_fuzzy_delay: initial_delay = random.randint(0, self.periodic_fuzzy_delay) else: initial_delay = None periodic = loop_call_type(self.periodic_tasks) self.conn = rpc.create_connection(new=True) LOG.debug(_("Creating Consumer connection for Service %s") % self.topic) rpc_dispatcher = self.manager.create_rpc_dispatcher() # Share this same connection for these Consumers self.conn.create_consumer(self.topic, rpc_dispatcher, fanout=False) node_topic = '%s.%s' % (self.topic, self.host) self.conn.create_consumer(node_topic, rpc_dispatcher, fanout=False) self.conn.create_consumer(self.topic, rpc_dispatcher, fanout=True) # Consume from all consumers in a thread self.conn.consume_in_thread() if pulse: pulse.start(interval=self.report_interval, initial_delay=self.report_interval) self.timers.append(pulse) if periodic: periodic.start(interval=self.periodic_interval, initial_delay=initial_delay) self.timers.append(periodic)
def update(self, req, id, body): context = req.environ['nova.context'] name = body['service'].get('disabled') db.service_update(context, id, body['service']) return exc.HTTPAccepted()