Exemple #1
0
    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"))
Exemple #2
0
    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'))
Exemple #3
0
 def test_with_compute_node(self):
     self.mox.StubOutWithMock(db, 'service_get')
     self.mox.StubOutWithMock(db, 'compute_node_get_by_service_id')
     _fake_service = dict(
         fake_service, compute_node=[test_compute_node.fake_compute_node])
     db.service_get(self.context, 123).AndReturn(_fake_service)
     self.mox.ReplayAll()
     service_obj = service.Service.get_by_id(self.context, 123)
     self.assertTrue(service_obj.obj_attr_is_set('compute_node'))
     compare(service_obj.compute_node, test_compute_node.fake_compute_node)
Exemple #4
0
 def test_with_compute_node(self):
     self.mox.StubOutWithMock(db, 'service_get')
     self.mox.StubOutWithMock(db, 'compute_node_get_by_service_id')
     _fake_service = dict(
         fake_service, compute_node=[test_compute_node.fake_compute_node])
     db.service_get(self.context, 123).AndReturn(_fake_service)
     self.mox.ReplayAll()
     service_obj = service.Service.get_by_id(self.context, 123)
     self.assertTrue(service_obj.obj_attr_is_set('compute_node'))
     self.compare_obj(service_obj.compute_node,
                      test_compute_node.fake_compute_node,
                      allow_missing=OPTIONAL,
                      comparators={'stats': self.json_comparator})
    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"])
Exemple #6
0
    def _create_compute_service(self):
        """Create compute-manager(ComputeNode and Service record)."""
        ctxt = context.get_admin_context()
        dic = {
            "host": "dummy",
            "binary": "nova-compute",
            "topic": "compute",
            "report_count": 0,
            "availability_zone": "dummyzone",
        }
        s_ref = db.service_create(ctxt, dic)

        dic = {
            "service_id": s_ref["id"],
            "vcpus": 16,
            "memory_mb": 32,
            "local_gb": 100,
            "vcpus_used": 16,
            "memory_mb_used": 32,
            "local_gb_used": 10,
            "hypervisor_type": "qemu",
            "hypervisor_version": 12003,
            "cpu_info": "",
        }
        db.compute_node_create(ctxt, dic)

        return db.service_get(ctxt, s_ref["id"])
Exemple #7
0
 def test_force_disabled_is_false(self):
     # Test that forced_disable flag works (false)
     self.flags(force_disabled=False, group='saver')
     self.prepare_mock([vm_states.ACTIVE])
     self.raise_on_evacuate = True
     self.manager._host_evacuate(self.context, self.fake_service)
     svc = db.service_get(self.context, self.fake_service['id'])
     self.assertEqual(svc['disabled'], False)
Exemple #8
0
 def test_service_enabled_on_create_based_on_flag(self):
     self.flags(enable_new_services=True)
     host = "foo"
     binary = "nova-fake"
     app = service.Service.create(host=host, binary=binary)
     app.start()
     app.stop()
     ref = db.service_get(context.get_admin_context(), app.service_id)
     db.service_destroy(context.get_admin_context(), app.service_id)
     self.assert_(not ref["disabled"])
Exemple #9
0
 def test_service_disabled_on_create_based_on_flag(self):
     self.flags(enable_new_services=False)
     host = 'foo'
     binary = 'nova-fake'
     app = service.Service.create(host=host, binary=binary)
     app.start()
     app.stop()
     ref = db.service_get(context.get_admin_context(), app.service_id)
     db.service_destroy(context.get_admin_context(), app.service_id)
     self.assert_(ref['disabled'])
Exemple #10
0
    def test_update_available_resource_works_correctly(self):
        """Confirm compute_node table is updated successfully."""
        org_path = FLAGS.instances_path = ''
        FLAGS.instances_path = '.'

        # Prepare mocks
        def getVersion():
            return 12003

        def getType():
            return 'qemu'

        def listDomainsID():
            return []

        service_ref = self.create_service(host='dummy')
        self.create_fake_libvirt_mock(getVersion=getVersion,
                                      getType=getType,
                                      listDomainsID=listDomainsID)
        self.mox.StubOutWithMock(libvirt_conn.LibvirtConnection,
                                 'get_cpu_info')
        libvirt_conn.LibvirtConnection.get_cpu_info().AndReturn('cpuinfo')

        # Start test
        self.mox.ReplayAll()
        conn = libvirt_conn.LibvirtConnection(False)
        conn.update_available_resource(self.context, 'dummy')
        service_ref = db.service_get(self.context, service_ref['id'])
        compute_node = service_ref['compute_node'][0]

        if sys.platform.upper() == 'LINUX2':
            self.assertTrue(compute_node['vcpus'] >= 0)
            self.assertTrue(compute_node['memory_mb'] > 0)
            self.assertTrue(compute_node['local_gb'] > 0)
            self.assertTrue(compute_node['vcpus_used'] == 0)
            self.assertTrue(compute_node['memory_mb_used'] > 0)
            self.assertTrue(compute_node['local_gb_used'] > 0)
            self.assertTrue(len(compute_node['hypervisor_type']) > 0)
            self.assertTrue(compute_node['hypervisor_version'] > 0)
        else:
            self.assertTrue(compute_node['vcpus'] >= 0)
            self.assertTrue(compute_node['memory_mb'] == 0)
            self.assertTrue(compute_node['local_gb'] > 0)
            self.assertTrue(compute_node['vcpus_used'] == 0)
            self.assertTrue(compute_node['memory_mb_used'] == 0)
            self.assertTrue(compute_node['local_gb_used'] > 0)
            self.assertTrue(len(compute_node['hypervisor_type']) > 0)
            self.assertTrue(compute_node['hypervisor_version'] > 0)

        db.service_destroy(self.context, service_ref['id'])
        FLAGS.instances_path = org_path
Exemple #11
0
    def _create_compute_service(self):
        """Create compute-manager(ComputeNode and Service record)."""
        ctxt = context.get_admin_context()
        dic = {'host': 'dummy', 'binary': 'nova-compute', 'topic': 'compute',
               'report_count': 0, 'availability_zone': 'dummyzone'}
        s_ref = db.service_create(ctxt, dic)

        dic = {'service_id': s_ref['id'],
               'vcpus': 16, 'memory_mb': 32, 'local_gb': 100,
               'vcpus_used': 16, 'memory_mb_used': 32, 'local_gb_used': 10,
               'hypervisor_type': 'qemu', 'hypervisor_version': 12003,
               'cpu_info': '', 'stats': {}}
        db.compute_node_create(ctxt, dic)

        return db.service_get(ctxt, s_ref['id'])
Exemple #12
0
    def _create_compute_service(self):
        """Create compute-manager(ComputeNode and Service record)."""
        ctxt = self.req.environ["nova.context"]
        dic = {'host': 'dummy', 'binary': 'nova-compute', 'topic': 'compute',
               'report_count': 0}
        s_ref = db.service_create(ctxt, dic)

        dic = {'service_id': s_ref['id'],
               'host': s_ref['host'],
               'uuid': uuidsentinel.compute_node,
               'vcpus': 16, 'memory_mb': 32, 'local_gb': 100,
               'vcpus_used': 16, 'memory_mb_used': 32, 'local_gb_used': 10,
               'hypervisor_type': 'qemu', 'hypervisor_version': 12003,
               'cpu_info': '', 'stats': ''}
        db.compute_node_create(ctxt, dic)

        return db.service_get(ctxt, s_ref['id'])
Exemple #13
0
    def _create_compute_service(self):
        """Create compute-manager(ComputeNode and Service record)."""
        ctxt = self.req.environ["nova.context"]
        dic = {"host": "dummy", "binary": "nova-compute", "topic": "compute", "report_count": 0}
        s_ref = db.service_create(ctxt, dic)

        dic = {
            "service_id": s_ref["id"],
            "vcpus": 16,
            "memory_mb": 32,
            "local_gb": 100,
            "vcpus_used": 16,
            "memory_mb_used": 32,
            "local_gb_used": 10,
            "hypervisor_type": "qemu",
            "hypervisor_version": 12003,
            "cpu_info": "",
            "stats": {},
        }
        db.compute_node_create(ctxt, dic)

        return db.service_get(ctxt, s_ref["id"])
Exemple #14
0
    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'])
Exemple #15
0
 def show(self, req, id):
     context = req.environ['nova.context']
     service = self._format_service(db.service_get(context, id))
     return {'service': service}
Exemple #16
0
 def get_by_id(cls, context, service_id):
     db_service = db.service_get(context, service_id)
     return cls._from_db_object(context, cls(), db_service)
Exemple #17
0
 def _get_by_service(cls, context, service_id):
     db_service = db.service_get(context,
                                 service_id,
                                 with_compute_node=True)
     return base.obj_make_list(context, ComputeNodeList(), ComputeNode,
                               db_service['compute_node'])
Exemple #18
0
    def _host_evacuate(self, ctxt, service):
        """Evacuate all instances on the host"""

        # Get the host name
        host = service['host']

        with lockutils.lock('host-evacuate-%s' % host):

            # Skip host evacuation if service is already disabled
            service = db.service_get(ctxt, service['id'], use_slave=True)
            if service['disabled']:
                LOG.warn("Host %s already disabled, skipping", host)
                return

            LOG.warn("Evacuating all instances on %s started", host)

            # Execute pre host-evacuation script
            if CONF.saver.pre_script_host:
                utils.execute(CONF.saver.pre_script_host, host)

            # Make the compute service disabled if all instances are evacuated
            if CONF.saver.force_disabled:
                db.service_update(ctxt, service['id'], {'disabled': True})

            # Get instances on the host
            instances = db.instance_get_all_by_host(ctxt, host, use_slave=True)

            # Get a new auth session
            admin_context = self._get_admin_context_with_token()

            # Evacuate all instances on the host
            all_instances_evacuated = True
            for instance in instances:

                try:
                    LOG.warn("Evacuating instance %s started",
                             instance['uuid'])

                    # Skip actions if debug mode
                    if CONF.saver.debug:
                        continue

                    # Execute pre instance-evacuation script
                    if CONF.saver.pre_script_instance:
                        utils.execute(CONF.saver.pre_script_instance,
                                      instance['uuid'])

                    # Reset VM state if it's one of active, shutoff, error
                    instance_obj = self.compute_api.get(ctxt, instance['uuid'],
                                                        want_objects=True)
                    if instance_obj.vm_state not in [vm_states.ACTIVE,
                                                     vm_states.STOPPED,
                                                     vm_states.ERROR]:
                        instance_obj.vm_state = vm_states.STOPPED
                        instance_obj.task_state = None
                        instance_obj.save(admin_state_reset=True)

                    # Evacuate an instance
                    self.compute_api.evacuate(admin_context,
                                              instance_obj, None,
                                              CONF.saver.on_shared_storage,
                                              None)

                    # Execute post instance-evacuation script
                    if CONF.saver.post_script_instance:
                        utils.execute(CONF.saver.post_script_instance,
                                      instance['uuid'])

                    LOG.warn("Evacuating instance %s finished",
                             instance['uuid'])

                except:
                    LOG.exception("Evacuating instance %s failed",
                                  instance['uuid'])
                    all_instances_evacuated = False
                finally:
                    # Make interval for the next evacuation
                    time.sleep(CONF.saver.evacuation_interval)

            # Make the compute service disabled if all instances are evacuated
            if all_instances_evacuated:
                db.service_update(ctxt, service['id'], {'disabled': True})

            # Execute post host-evacuation script
            if CONF.saver.post_script_host:
                utils.execute(CONF.saver.post_script_host, host)

            LOG.warn("Evacuating all instances on %s finished", host)
Exemple #19
0
 def show(self, req, id):
     context = req.environ['nova.context'].elevated()
     service = self._set_attr(db.service_get(context, id))
     return {'service': service}
Exemple #20
0
 def show(self, req, id):
     context = req.environ['nova.context'].elevated()
     service = self._format_service(db.service_get(context, id))
     return {'service': service}
Exemple #21
0
 def show(self, req, id):
     context = req.environ["nova.context"]
     service = self._format_service(db.service_get(context, id))
     return {"service": service}
Exemple #22
0
 def _get_by_service(cls, context, service_id, use_slave=False):
     db_service = db.service_get(context, service_id,
                                 with_compute_node=True,
                                 use_slave=use_slave)
     return base.obj_make_list(context, cls(context), objects.ComputeNode,
                               db_service['compute_node'])
Exemple #23
0
 def _get_by_service(cls, context, service_id):
     db_service = db.service_get(context, service_id,
                                 with_compute_node=True)
     return base.obj_make_list(context, ComputeNodeList(), ComputeNode,
                               db_service['compute_node'])
Exemple #24
0
 def get_by_id(cls, context, service_id):
     db_service = db.service_get(context, service_id)
     return cls._from_db_object(context, cls(), db_service)