def test_read_instance_metadata_success(self):
     self.mox.StubOutWithMock(ovz_utils.context, 'get_admin_context')
     ovz_utils.context.get_admin_context().AndReturn(fakes.ADMINCONTEXT)
     self.mox.StubOutWithMock(ovz_utils.conductor, 'instance_get')
     ovz_utils.conductor.instance_get(
         fakes.ADMINCONTEXT, fakes.INSTANCE['id']).AndReturn(fakes.INSTANCE)
     self.mox.ReplayAll()
     meta = ovz_utils.read_instance_metadata(fakes.INSTANCE['id'])
     self.assertTrue(isinstance(meta, dict))
     self.assertEqual(meta[fakes.METAKEY], fakes.METAVALUE)
    def _generate_tc_rules(self, container, network_info, is_migration=False):
        """
        Utility method to generate tc info for instances that have been
        resized and/or migrated
        """
        LOG.debug(_('Setting network sizing'))
        boot_file = ovzboot.OVZBootFile(container.ovz_id, 755)
        shutdown_file = ovzshutdown.OVZShutdownFile(container.ovz_id, 755)

        if not is_migration:
            with shutdown_file:
                LOG.debug(_('Cleaning TC rules for %s') % container.nova_id)
                shutdown_file.read()
                shutdown_file.run_contents(raise_on_error=False)

        # On resize we throw away existing tc_id and make a new one
        # because the resize *could* have taken place on a different host
        # where the tc_id is already in use.
        meta = ovz_utils.read_instance_metadata(container.nova_id)
        tc_id = meta.get('tc_id', None)
        if tc_id:
            ovz_utils.remove_instance_metadata_key(container.nova_id, 'tc_id')

        with shutdown_file:
            shutdown_file.set_contents(list())

        with boot_file:
            boot_file.set_contents(list())

        LOG.debug(_('Getting network dict for: %s') % container.uuid)
        interfaces = ovz_utils.generate_network_dict(container,
                                                     network_info)
        for net_dev in interfaces:
            LOG.debug(_('Adding tc rules for: %s') %
                      net_dev['vz_host_if'])
            tc = ovztc.OVZTcRules()
            tc.instance_info(container.nova_id, net_dev['address'],
                             net_dev['vz_host_if'])
            with boot_file:
                boot_file.append(tc.container_start())

            with shutdown_file:
                shutdown_file.append(tc.container_stop())

        with boot_file:
            if not is_migration:
                # during migration, the instance isn't yet running, so it'll
                # just spew errors to attempt to apply these rules before then
                LOG.debug(_('Running TC rules for: %s') % container.ovz_id)
                boot_file.run_contents()
            LOG.debug(_('Saving TC rules for: %s') % container.ovz_id)
            boot_file.write()

        with shutdown_file:
            shutdown_file.write()
 def test_read_instance_metadata_dberror(self):
     self.mox.StubOutWithMock(ovz_utils.context, 'get_admin_context')
     ovz_utils.context.get_admin_context().AndReturn(fakes.ADMINCONTEXT)
     self.mox.StubOutWithMock(ovz_utils.conductor, 'instance_get')
     ovz_utils.conductor.instance_get(
         fakes.ADMINCONTEXT, fakes.INSTANCE['id']).AndRaise(
             exception.InstanceNotFound(fakes.ERRORMSG))
     self.mox.ReplayAll()
     meta = ovz_utils.read_instance_metadata(fakes.INSTANCE['id'])
     self.assertTrue(isinstance(meta, dict))
     self.assertTrue(len(meta) == 0)
Example #4
0
 def _get_instance_tc_id(self):
     """
     Look up instance metadata in the db and see if there is already
     a tc_id for the instance
     """
     instance_metadata = ovz_utils.read_instance_metadata(
         self.instance['id'])
     LOG.debug(_('Instances metadata: %s') % instance_metadata)
     if instance_metadata:
         tc_id = instance_metadata.get('tc_id')
         LOG.debug(
             _('TC id for instance %(instance_id)s is %(tc_id)s') %
             {'instance_id': self.instance['id'], 'tc_id': tc_id})
         return tc_id