Пример #1
0
    def test_fetch_vhd_image_works_with_glance(self):
        self.mox.StubOutWithMock(vm_utils, '_image_uses_bittorrent')
        vm_utils._image_uses_bittorrent(
            self.context, self.instance).AndReturn(False)

        self.mox.StubOutWithMock(
                self.session, 'call_plugin_serialized_with_retry')
        self.session.call_plugin_serialized_with_retry(
                'glance', 'download_vhd', 0, mox.IgnoreArg(),
                extra_headers={'X-Service-Catalog': '[]',
                               'X-Auth-Token': 'auth_token',
                               'X-Roles': '',
                               'X-Tenant-Id': None,
                               'X-User-Id': None,
                               'X-Identity-Status': 'Confirmed'},
                image_id='image_id',
                uuid_stack=["uuid_stack"],
                sr_path='sr_path').AndReturn({'root': {'uuid': 'vdi'}})

        self.mox.StubOutWithMock(vm_utils, 'safe_find_sr')
        vm_utils.safe_find_sr(self.session).AndReturn("sr")

        self.mox.StubOutWithMock(vm_utils, '_scan_sr')
        vm_utils._scan_sr(self.session, "sr")

        self.mox.StubOutWithMock(vm_utils, '_check_vdi_size')
        vm_utils._check_vdi_size(
                self.context, self.session, self.instance, "vdi")

        self.mox.ReplayAll()

        self.assertEqual("vdi", vm_utils._fetch_vhd_image(self.context,
            self.session, self.instance, 'image_id')['root']['uuid'])

        self.mox.VerifyAll()
Пример #2
0
    def test_fetch_vhd_image_cleans_up_vdi_on_fail(self):
        self._apply_stubouts()
        self._common_params_setup(True)
        self.mox.StubOutWithMock(self.session, 'call_xenapi')

        vm_utils._add_bittorrent_params(self.image_id, self.params)

        vm_utils._fetch_using_dom0_plugin_with_retry(self.context,
            self.session, self.image_id, "bittorrent", self.params,
            callback=None).AndReturn(self.vdis)

        vm_utils.safe_find_sr(self.session).AndReturn("sr")
        vm_utils._scan_sr(self.session, "sr")
        vm_utils._check_vdi_size(self.context, self.session, self.instance,
                "vdi").AndRaise(exception.InstanceTypeDiskTooSmall)

        self.session.call_xenapi("VDI.get_by_uuid", "vdi").AndReturn("ref")
        vm_utils.destroy_vdi(self.session, "ref")

        self.mox.ReplayAll()

        self.assertRaises(exception.InstanceTypeDiskTooSmall,
                vm_utils._fetch_vhd_image, self.context, self.session,
                self.instance, self.image_id)

        self.mox.VerifyAll()
Пример #3
0
    def test_fetch_vhd_image_cleans_up_vdi_on_fail(self):
        self._apply_stubouts()
        self._common_params_setup(True)
        self.mox.StubOutWithMock(self.session, 'call_xenapi')

        vm_utils._add_bittorrent_params(self.params)

        vm_utils._fetch_using_dom0_plugin_with_retry(self.context,
            self.session, self.image_id, "bittorrent", self.params,
            callback=None).AndReturn(self.vdis)

        vm_utils.safe_find_sr(self.session).AndReturn("sr")
        vm_utils._scan_sr(self.session, "sr")
        vm_utils._check_vdi_size(self.context, self.session, self.instance,
                "vdi").AndRaise(exception.InstanceTypeDiskTooSmall)

        self.session.call_xenapi("VDI.get_by_uuid", "vdi").AndReturn("ref")
        vm_utils.destroy_vdi(self.session, "ref")

        self.mox.ReplayAll()

        self.assertRaises(exception.InstanceTypeDiskTooSmall,
                vm_utils._fetch_vhd_image, self.context, self.session,
                self.instance, self.image_id)

        self.mox.VerifyAll()
Пример #4
0
    def test_fetch_vhd_image_works_with_glance(self):
        self.mox.StubOutWithMock(vm_utils, '_image_uses_bittorrent')
        vm_utils._image_uses_bittorrent(
            self.context, self.instance).AndReturn(False)

        self.mox.StubOutWithMock(
                self.session, 'call_plugin_serialized_with_retry')
        self.session.call_plugin_serialized_with_retry(
                'glance', 'download_vhd', 0, mox.IgnoreArg(),
                extra_headers={'X-Service-Catalog': '[]',
                               'X-Auth-Token': 'auth_token',
                               'X-Roles': '',
                               'X-Tenant-Id': None,
                               'X-User-Id': None,
                               'X-Identity-Status': 'Confirmed'},
                image_id='image_id',
                uuid_stack=["uuid_stack"],
                sr_path='sr_path').AndReturn({'root': {'uuid': 'vdi'}})

        self.mox.StubOutWithMock(vm_utils, 'safe_find_sr')
        vm_utils.safe_find_sr(self.session).AndReturn("sr")

        self.mox.StubOutWithMock(vm_utils, '_scan_sr')
        vm_utils._scan_sr(self.session, "sr")

        self.mox.StubOutWithMock(vm_utils, '_check_vdi_size')
        vm_utils._check_vdi_size(
                self.context, self.session, self.instance, "vdi")

        self.mox.ReplayAll()

        self.assertEqual("vdi", vm_utils._fetch_vhd_image(self.context,
            self.session, self.instance, 'image_id')['root']['uuid'])

        self.mox.VerifyAll()
Пример #5
0
    def test_fallback_to_default_handler(self):
        cfg.CONF.import_opt('xenapi_torrent_base_url',
                            'nova.virt.xenapi.image.bittorrent')
        self.flags(xenapi_torrent_base_url='http://foo')

        self.mox.StubOutWithMock(vm_utils, '_image_uses_bittorrent')
        vm_utils._image_uses_bittorrent(
            self.context, self.instance).AndReturn(True)

        self._stub_bittorrent_download_vhd(raise_exc=RuntimeError)

        vm_utils._make_uuid_stack().AndReturn(["uuid_stack"])
        vm_utils.get_sr_path(self.session).AndReturn('sr_path')

        self._stub_glance_download_vhd()

        self.mox.StubOutWithMock(vm_utils, 'safe_find_sr')
        vm_utils.safe_find_sr(self.session).AndReturn("sr")

        self.mox.StubOutWithMock(vm_utils, '_scan_sr')
        vm_utils._scan_sr(self.session, "sr")

        self.mox.StubOutWithMock(vm_utils, '_check_vdi_size')
        vm_utils._check_vdi_size(self.context, self.session, self.instance,
                                 "vdi")

        self.mox.ReplayAll()

        self.assertEqual("vdi", vm_utils._fetch_vhd_image(self.context,
            self.session, self.instance, 'image_id')['root']['uuid'])

        self.mox.VerifyAll()
Пример #6
0
    def test_fetch_vhd_image_works_with_glance(self):
        self._apply_stubouts()
        self._common_params_setup(False)

        vm_utils._generate_glance_callback(self.context).AndReturn("dummy")

        vm_utils._fetch_using_dom0_plugin_with_retry(
            self.context,
            self.session,
            self.image_id,
            "glance",
            self.params,
            callback="dummy").AndReturn(self.vdis)

        vm_utils.safe_find_sr(self.session).AndReturn("sr")
        vm_utils._scan_sr(self.session, "sr")
        vm_utils._check_vdi_size(self.context, self.session, self.instance,
                                 "vdi")

        self.mox.ReplayAll()

        self.assertEqual(
            "vdi",
            vm_utils._fetch_vhd_image(self.context, self.session,
                                      self.instance,
                                      self.image_id)['root']['uuid'])

        self.mox.VerifyAll()
Пример #7
0
    def test_fetch_vhd_image_works_with_bittorrent(self):
        cfg.CONF.import_opt('xenapi_torrent_base_url',
                            'nova.virt.xenapi.image.bittorrent')
        self.flags(xenapi_torrent_base_url='http://foo')

        self.mox.StubOutWithMock(vm_utils, '_image_uses_bittorrent')
        vm_utils._image_uses_bittorrent(
            self.context, self.instance).AndReturn(True)

        self._stub_bittorrent_download_vhd()

        self.mox.StubOutWithMock(vm_utils, 'safe_find_sr')
        vm_utils.safe_find_sr(self.session).AndReturn("sr")

        self.mox.StubOutWithMock(vm_utils, '_scan_sr')
        vm_utils._scan_sr(self.session, "sr")

        self.mox.StubOutWithMock(vm_utils, '_check_vdi_size')
        vm_utils._check_vdi_size(self.context, self.session, self.instance,
                                 "vdi")

        self.mox.ReplayAll()

        self.assertEqual("vdi", vm_utils._fetch_vhd_image(self.context,
            self.session, self.instance, 'image_id')['root']['uuid'])

        self.mox.VerifyAll()
Пример #8
0
    def test_fetch_vhd_image_cleans_up_vdi_on_fail(self):
        self.mox.StubOutWithMock(vm_utils, '_image_uses_bittorrent')
        vm_utils._image_uses_bittorrent(
            self.context, self.instance).AndReturn(False)

        self._stub_glance_download_vhd()

        self.mox.StubOutWithMock(vm_utils, 'safe_find_sr')
        vm_utils.safe_find_sr(self.session).AndReturn("sr")

        self.mox.StubOutWithMock(vm_utils, '_scan_sr')
        vm_utils._scan_sr(self.session, "sr")

        self.mox.StubOutWithMock(vm_utils, '_check_vdi_size')
        vm_utils._check_vdi_size(self.context, self.session, self.instance,
                "vdi").AndRaise(exception.InstanceTypeDiskTooSmall)

        self.mox.StubOutWithMock(self.session, 'call_xenapi')
        self.session.call_xenapi("VDI.get_by_uuid", "vdi").AndReturn("ref")

        self.mox.StubOutWithMock(vm_utils, 'destroy_vdi')
        vm_utils.destroy_vdi(self.session, "ref")

        self.mox.ReplayAll()

        self.assertRaises(exception.InstanceTypeDiskTooSmall,
                vm_utils._fetch_vhd_image, self.context, self.session,
                self.instance, 'image_id')

        self.mox.VerifyAll()
Пример #9
0
    def test_fetch_vhd_image_works_with_bittorrent(self):
        self._apply_stubouts()
        self._common_params_setup(True)

        vm_utils._add_bittorrent_params(self.params)

        vm_utils._fetch_using_dom0_plugin_with_retry(self.context,
                                                     self.session,
                                                     self.image_id,
                                                     "bittorrent",
                                                     self.params,
                                                     callback=None).AndReturn(
                                                         self.vdis)

        vm_utils.safe_find_sr(self.session).AndReturn("sr")
        vm_utils._scan_sr(self.session, "sr")
        vm_utils._check_vdi_size(self.context, self.session, self.instance,
                                 "vdi")

        self.mox.ReplayAll()

        self.assertEqual(
            "vdi",
            vm_utils._fetch_vhd_image(self.context, self.session,
                                      self.instance,
                                      self.image_id)['root']['uuid'])

        self.mox.VerifyAll()
Пример #10
0
    def test_fetch_vhd_image_cleans_up_vdi_on_fail(self):
        self.mox.StubOutWithMock(self.session, "call_xenapi")
        self._setup_bittorrent()

        vm_utils.safe_find_sr(self.session).AndReturn("sr")
        vm_utils._scan_sr(self.session, "sr")
        vm_utils._check_vdi_size(self.context, self.session, self.instance, "vdi").AndRaise(
            exception.InstanceTypeDiskTooSmall
        )

        self.session.call_xenapi("VDI.get_by_uuid", "vdi").AndReturn("ref")
        vm_utils.destroy_vdi(self.session, "ref")

        self.mox.ReplayAll()

        self.assertRaises(
            exception.InstanceTypeDiskTooSmall,
            vm_utils._fetch_vhd_image,
            self.context,
            self.session,
            self.instance,
            self.image_id,
        )

        self.mox.VerifyAll()
Пример #11
0
    def test_no_admin_pass(self):
        # This is here to avoid masking errors, it shouldn't be used normally
        self.useFixture(fixtures.MonkeyPatch("nova.virt.xenapi.vm_utils.destroy_vdi", _fake_noop))

        # Mocks
        instance = {}

        self.mox.StubOutWithMock(vm_utils, "safe_find_sr")
        vm_utils.safe_find_sr("session").AndReturn("sr_ref")

        self.mox.StubOutWithMock(vm_utils, "create_vdi")
        vm_utils.create_vdi("session", "sr_ref", instance, "config-2", "configdrive", 64 * 1024 * 1024).AndReturn(
            "vdi_ref"
        )

        self.mox.StubOutWithMock(vm_utils, "vdi_attached_here")
        vm_utils.vdi_attached_here("session", "vdi_ref", read_only=False).AndReturn(contextified("mounted_dev"))

        class FakeInstanceMetadata(object):
            def __init__(self, instance, content=None, extra_md=None):
                pass

            def metadata_for_config_drive(self):
                return []

        self.useFixture(fixtures.MonkeyPatch("nova.api.metadata.base.InstanceMetadata", FakeInstanceMetadata))

        self.mox.StubOutWithMock(utils, "execute")
        utils.execute(
            "genisoimage",
            "-o",
            mox.IgnoreArg(),
            "-ldots",
            "-allow-lowercase",
            "-allow-multidot",
            "-l",
            "-publisher",
            mox.IgnoreArg(),
            "-quiet",
            "-J",
            "-r",
            "-V",
            "config-2",
            mox.IgnoreArg(),
            attempts=1,
            run_as_root=False,
        ).AndReturn(None)
        utils.execute("dd", mox.IgnoreArg(), mox.IgnoreArg(), run_as_root=True).AndReturn(None)

        self.mox.StubOutWithMock(vm_utils, "create_vbd")
        vm_utils.create_vbd("session", "vm_ref", "vdi_ref", mox.IgnoreArg(), bootable=False, read_only=True).AndReturn(
            None
        )

        self.mox.ReplayAll()

        # And the actual call we're testing
        vm_utils.generate_configdrive("session", instance, "vm_ref", "userdevice")
Пример #12
0
    def test_defined(self):
        self.mox.StubOutWithMock(vm_utils, "safe_find_sr")
        self.mox.StubOutWithMock(self.session, "call_xenapi")
        self.mox.StubOutWithMock(self.session, "get_xenapi_host")

        vm_utils.safe_find_sr(self.session).AndReturn("sr_ref")
        self.session.get_xenapi_host().AndReturn("host_ref")
        self.session.call_xenapi('PBD.get_all_records_where',
            'field "host"="host_ref" and field "SR"="sr_ref"').AndReturn(
            {'pbd_ref': {'device_config': {'path': 'sr_path'}}})

        self.mox.ReplayAll()
        self.assertEqual(vm_utils.get_sr_path(self.session), "sr_path")
Пример #13
0
    def test_fetch_vhd_image_cleans_up_vdi_on_fail(self):
        self.mox.StubOutWithMock(vm_utils, '_image_uses_bittorrent')
        vm_utils._image_uses_bittorrent(self.context,
                                        self.instance).AndReturn(False)

        self.mox.StubOutWithMock(self.session,
                                 'call_plugin_serialized_with_retry')
        self.session.call_plugin_serialized_with_retry(
            'glance',
            'download_vhd',
            0,
            mox.IgnoreArg(),
            extra_headers={
                'X-Service-Catalog': '[]',
                'X-Auth-Token': 'auth_token',
                'X-Roles': '',
                'X-Tenant-Id': None,
                'X-User-Id': None,
                'X-Identity-Status': 'Confirmed'
            },
            image_id='image_id',
            uuid_stack=["uuid_stack"],
            sr_path='sr_path').AndReturn({'root': {
                'uuid': 'vdi'
            }})

        self.mox.StubOutWithMock(vm_utils, 'safe_find_sr')
        vm_utils.safe_find_sr(self.session).AndReturn("sr")

        self.mox.StubOutWithMock(vm_utils, '_scan_sr')
        vm_utils._scan_sr(self.session, "sr")

        self.mox.StubOutWithMock(vm_utils, '_check_vdi_size')
        vm_utils._check_vdi_size(self.context, self.session, self.instance,
                                 "vdi").AndRaise(
                                     exception.InstanceTypeDiskTooSmall)

        self.mox.StubOutWithMock(self.session, 'call_xenapi')
        self.session.call_xenapi("VDI.get_by_uuid", "vdi").AndReturn("ref")

        self.mox.StubOutWithMock(vm_utils, 'destroy_vdi')
        vm_utils.destroy_vdi(self.session, "ref")

        self.mox.ReplayAll()

        self.assertRaises(exception.InstanceTypeDiskTooSmall,
                          vm_utils._fetch_vhd_image, self.context,
                          self.session, self.instance, 'image_id')

        self.mox.VerifyAll()
Пример #14
0
    def test_no_admin_pass(self):
        # This is here to avoid masking errors, it shouldn't be used normally
        self.useFixture(fixtures.MonkeyPatch(
                'nova.virt.xenapi.vm_utils.destroy_vdi', _fake_noop))

        # Mocks
        instance = {}

        self.mox.StubOutWithMock(vm_utils, 'safe_find_sr')
        vm_utils.safe_find_sr('session').AndReturn('sr_ref')

        self.mox.StubOutWithMock(vm_utils, 'create_vdi')
        vm_utils.create_vdi('session', 'sr_ref', instance, 'config-2',
                            'configdrive',
                            64 * 1024 * 1024).AndReturn('vdi_ref')

        self.mox.StubOutWithMock(vm_utils, 'vdi_attached_here')
        vm_utils.vdi_attached_here(
            'session', 'vdi_ref', read_only=False).AndReturn(
                contextified('mounted_dev'))

        class FakeInstanceMetadata(object):
            def __init__(self, instance, content=None, extra_md=None):
                pass

            def metadata_for_config_drive(self):
                return []

        self.useFixture(fixtures.MonkeyPatch(
                'nova.api.metadata.base.InstanceMetadata',
                FakeInstanceMetadata))

        self.mox.StubOutWithMock(utils, 'execute')
        utils.execute('genisoimage', '-o', mox.IgnoreArg(), '-ldots',
                      '-allow-lowercase', '-allow-multidot', '-l',
                      '-publisher', mox.IgnoreArg(), '-quiet',
                      '-J', '-r', '-V', 'config-2', mox.IgnoreArg(),
                      attempts=1, run_as_root=False).AndReturn(None)
        utils.execute('dd', mox.IgnoreArg(), mox.IgnoreArg(),
                      run_as_root=True).AndReturn(None)

        self.mox.StubOutWithMock(vm_utils, 'create_vbd')
        vm_utils.create_vbd('session', 'vm_ref', 'vdi_ref', mox.IgnoreArg(),
                            bootable=False, read_only=True).AndReturn(None)

        self.mox.ReplayAll()

        # And the actual call we're testing
        vm_utils.generate_configdrive('session', instance, 'vm_ref',
                                      'userdevice')
Пример #15
0
    def test_fetch_vhd_image_works_with_bittorrent(self):
        self._setup_bittorrent()

        vm_utils.safe_find_sr(self.session).AndReturn("sr")
        vm_utils._scan_sr(self.session, "sr")
        vm_utils._check_vdi_size(self.context, self.session, self.instance, "vdi")

        self.mox.ReplayAll()

        self.assertEqual(
            "vdi", vm_utils._fetch_vhd_image(self.context, self.session, self.instance, self.image_id)["root"]["uuid"]
        )

        self.mox.VerifyAll()
Пример #16
0
    def test_no_admin_pass(self):
        # This is here to avoid masking errors, it shouldn't be used normally
        self.useFixture(fixtures.MonkeyPatch(
                'nova.virt.xenapi.vm_utils.destroy_vdi', _fake_noop))

        # Mocks
        instance = {}

        self.mox.StubOutWithMock(vm_utils, 'safe_find_sr')
        vm_utils.safe_find_sr('session').AndReturn('sr_ref')

        self.mox.StubOutWithMock(vm_utils, 'create_vdi')
        vm_utils.create_vdi('session', 'sr_ref', instance, 'config-2',
                            'configdrive',
                            64 * 1024 * 1024).AndReturn('vdi_ref')

        self.mox.StubOutWithMock(vm_utils, 'vdi_attached_here')
        vm_utils.vdi_attached_here(
            'session', 'vdi_ref', read_only=False).AndReturn(
                contextified('mounted_dev'))

        class FakeInstanceMetadata(object):
            def __init__(self, instance, content=None, extra_md=None):
                pass

            def metadata_for_config_drive(self):
                return []

        self.useFixture(fixtures.MonkeyPatch(
                'nova.api.metadata.base.InstanceMetadata',
                FakeInstanceMetadata))

        self.mox.StubOutWithMock(utils, 'execute')
        utils.execute('genisoimage', '-o', mox.IgnoreArg(), '-ldots',
                      '-allow-lowercase', '-allow-multidot', '-l',
                      '-publisher', mox.IgnoreArg(), '-quiet',
                      '-J', '-r', '-V', 'config-2', mox.IgnoreArg(),
                      attempts=1, run_as_root=False).AndReturn(None)
        utils.execute('dd', mox.IgnoreArg(), mox.IgnoreArg(),
                      run_as_root=True).AndReturn(None)

        self.mox.StubOutWithMock(vm_utils, 'create_vbd')
        vm_utils.create_vbd('session', 'vm_ref', 'vdi_ref', mox.IgnoreArg(),
                            bootable=False, read_only=True).AndReturn(None)

        self.mox.ReplayAll()

        # And the actual call we're testing
        vm_utils.generate_configdrive('session', instance, 'vm_ref',
                                      'userdevice')
Пример #17
0
    def test_default(self):
        self.mox.StubOutWithMock(vm_utils, "safe_find_sr")
        self.mox.StubOutWithMock(self.session, "call_xenapi")
        self.mox.StubOutWithMock(self.session, "get_xenapi_host")

        vm_utils.safe_find_sr(self.session).AndReturn("sr_ref")
        self.session.get_xenapi_host().AndReturn("host_ref")
        self.session.call_xenapi('PBD.get_all_records_where',
            'field "host"="host_ref" and field "SR"="sr_ref"').AndReturn(
            {'pbd_ref': {'device_config': {}}})
        self.session.call_xenapi("SR.get_record", "sr_ref").AndReturn(
            {'uuid': 'sr_uuid', 'type': 'ext'})
        self.mox.ReplayAll()
        self.assertEqual(vm_utils.get_sr_path(self.session),
                         "/var/run/sr-mount/sr_uuid")
Пример #18
0
 def update_status(self):
     """Since under Xenserver, a compute node runs on a given host,
     we can get host status information using xenapi.
     """
     LOG.debug(_("Updating host stats"))
     data = call_xenhost(self._session, "host_data", {})
     if data:
         sr_ref = vm_utils.safe_find_sr(self._session)
         self._session.call_xenapi("SR.scan", sr_ref)
         sr_rec = self._session.call_xenapi("SR.get_record", sr_ref)
         total = int(sr_rec["physical_size"])
         used = int(sr_rec["physical_utilisation"])
         data["disk_total"] = total
         data["disk_used"] = used
         data["disk_available"] = total - used
         data["supported_instances"] = to_supported_instances(
             data.get("host_capabilities")
         )
         host_memory = data.get('host_memory', None)
         if host_memory:
             data["host_memory_total"] = host_memory.get('total', 0)
             data["host_memory_overhead"] = host_memory.get('overhead', 0)
             data["host_memory_free"] = host_memory.get('free', 0)
             data["host_memory_free_computed"] = host_memory.get(
                                                 'free-computed', 0)
             del data['host_memory']
         data['hypervisor_hostname'] = data['host_hostname']
         self._stats = data
Пример #19
0
def main():
    """By default, this script will only cleanup unused cached images.

    Options:

    --all_cached - Destroy all cached images instead of just unused cached
                   images.
    --dry_run    - Don't actually destroy the VDIs.
    """
    config.parse_args(sys.argv)
    utils.monkey_patch()

    _session = session.XenAPISession(CONF.xenserver.connection_url,
                                     CONF.xenserver.connection_username,
                                     CONF.xenserver.connection_password)

    sr_ref = vm_utils.safe_find_sr(_session)
    destroyed = vm_utils.destroy_cached_images(_session,
                                               sr_ref,
                                               all_cached=CONF.all_cached,
                                               dry_run=CONF.dry_run)

    if '--verbose' in sys.argv:
        print('\n'.join(destroyed))

    print("Destroyed %d cached VDIs" % len(destroyed))
Пример #20
0
 def update_status(self):
     """Since under Xenserver, a compute node runs on a given host,
     we can get host status information using xenapi.
     """
     LOG.debug(_("Updating host stats"))
     data = call_xenhost(self._session, "host_data", {})
     if data:
         try:
             # Get the SR usage
             sr_ref = vm_utils.safe_find_sr(self._session)
         except exception.NotFound as e:
             # No SR configured
             LOG.error(_("Unable to get SR for this host: %s") % e)
             return
         self._session.call_xenapi("SR.scan", sr_ref)
         sr_rec = self._session.call_xenapi("SR.get_record", sr_ref)
         total = int(sr_rec["physical_size"])
         used = int(sr_rec["physical_utilisation"])
         data["disk_total"] = total
         data["disk_used"] = used
         data["disk_available"] = total - used
         data["supported_instances"] = to_supported_instances(
             data.get("host_capabilities"))
         host_memory = data.get('host_memory', None)
         if host_memory:
             data["host_memory_total"] = host_memory.get('total', 0)
             data["host_memory_overhead"] = host_memory.get('overhead', 0)
             data["host_memory_free"] = host_memory.get('free', 0)
             data["host_memory_free_computed"] = host_memory.get(
                 'free-computed', 0)
             del data['host_memory']
         data['hypervisor_hostname'] = data['host_hostname']
         self._stats = data
Пример #21
0
 def update_status(self):
     """Since under Xenserver, a compute node runs on a given host,
     we can get host status information using xenapi.
     """
     LOG.debug(_("Updating host stats"))
     data = call_xenhost(self._session, "host_data", {})
     if data:
         try:
             # Get the SR usage
             sr_ref = vm_utils.safe_find_sr(self._session)
         except exception.NotFound as e:
             # No SR configured
             LOG.error(_("Unable to get SR for this host: %s") % e)
             return
         self._session.call_xenapi("SR.scan", sr_ref)
         sr_rec = self._session.call_xenapi("SR.get_record", sr_ref)
         total = int(sr_rec["physical_size"])
         used = int(sr_rec["physical_utilisation"])
         data["disk_total"] = total
         data["disk_used"] = used
         data["disk_available"] = total - used
         data["supported_instances"] = to_supported_instances(data.get("host_capabilities"))
         host_memory = data.get("host_memory", None)
         if host_memory:
             data["host_memory_total"] = host_memory.get("total", 0)
             data["host_memory_overhead"] = host_memory.get("overhead", 0)
             data["host_memory_free"] = host_memory.get("free", 0)
             data["host_memory_free_computed"] = host_memory.get("free-computed", 0)
             del data["host_memory"]
         data["hypervisor_hostname"] = data["host_hostname"]
         self._stats = data
Пример #22
0
 def update_status(self):
     """Since under Xenserver, a compute node runs on a given host,
     we can get host status information using xenapi.
     """
     LOG.debug(_("Updating host stats"))
     data = call_xenhost(self._session, "host_data", {})
     if data:
         try:
             # Get the SR usage
             sr_ref = vm_utils.safe_find_sr(self._session)
         except exception.NotFound as e:
             # No SR configured
             LOG.error(_("Unable to get SR for this host: %s") % e)
             return
         sr_rec = self._session.call_xenapi("SR.get_record", sr_ref)
         total = int(sr_rec["virtual_allocation"])
         used = int(sr_rec["physical_utilisation"])
         data["disk_total"] = total
         data["disk_used"] = used
         data["disk_available"] = total - used
         host_memory = data.get('host_memory', None)
         if host_memory:
             data["host_memory_total"] = host_memory.get('total', 0)
             data["host_memory_overhead"] = host_memory.get('overhead', 0)
             data["host_memory_free"] = host_memory.get('free', 0)
             data["host_memory_free_computed"] = host_memory.get(
                                                 'free-computed', 0)
             del data['host_memory']
         self._stats = data
Пример #23
0
    def test_fetch_vhd_image_works_with_bittorrent(self):
        cfg.CONF.import_opt('xenapi_torrent_base_url',
                            'nova.virt.xenapi.image.bittorrent')
        self.flags(xenapi_torrent_base_url='http://foo')

        self.mox.StubOutWithMock(vm_utils, '_image_uses_bittorrent')
        vm_utils._image_uses_bittorrent(self.context,
                                        self.instance).AndReturn(True)

        self.mox.StubOutWithMock(self.session, 'call_plugin_serialized')
        self.session.call_plugin_serialized(
            'bittorrent',
            'download_vhd',
            image_id='image_id',
            uuid_stack=["uuid_stack"],
            sr_path='sr_path',
            torrent_download_stall_cutoff=600,
            torrent_listen_port_start=6881,
            torrent_listen_port_end=6891,
            torrent_max_last_accessed=86400,
            torrent_max_seeder_processes_per_host=1,
            torrent_seed_chance=1.0,
            torrent_seed_duration=3600,
            torrent_url='http://foo/image_id.torrent').AndReturn(
                {'root': {
                    'uuid': 'vdi'
                }})

        self.mox.StubOutWithMock(vm_utils, 'safe_find_sr')
        vm_utils.safe_find_sr(self.session).AndReturn("sr")

        self.mox.StubOutWithMock(vm_utils, '_scan_sr')
        vm_utils._scan_sr(self.session, "sr")

        self.mox.StubOutWithMock(vm_utils, '_check_vdi_size')
        vm_utils._check_vdi_size(self.context, self.session, self.instance,
                                 "vdi")

        self.mox.ReplayAll()

        self.assertEqual(
            "vdi",
            vm_utils._fetch_vhd_image(self.context, self.session,
                                      self.instance,
                                      'image_id')['root']['uuid'])

        self.mox.VerifyAll()
Пример #24
0
    def test_fetch_vhd_image_works_with_bittorrent(self):
        self._setup_bittorrent()

        vm_utils.safe_find_sr(self.session).AndReturn("sr")
        vm_utils._scan_sr(self.session, "sr")
        vm_utils._check_vdi_size(self.context, self.session, self.instance,
                                 "vdi")

        self.mox.ReplayAll()

        self.assertEqual(
            "vdi",
            vm_utils._fetch_vhd_image(self.context, self.session,
                                      self.instance,
                                      self.image_id)['root']['uuid'])

        self.mox.VerifyAll()
Пример #25
0
    def test_defined(self):
        self.mox.StubOutWithMock(vm_utils, "safe_find_sr")
        self.mox.StubOutWithMock(self.session, "call_xenapi")
        self.mox.StubOutWithMock(self.session, "get_xenapi_host")

        vm_utils.safe_find_sr(self.session).AndReturn("sr_ref")
        self.session.get_xenapi_host().AndReturn("host_ref")
        self.session.call_xenapi(
            'PBD.get_all_records_where',
            'field "host"="host_ref" and field "SR"="sr_ref"').AndReturn(
                {'pbd_ref': {
                    'device_config': {
                        'path': 'sr_path'
                    }
                }})

        self.mox.ReplayAll()
        self.assertEqual(vm_utils.get_sr_path(self.session), "sr_path")
Пример #26
0
    def test_fetch_vhd_image_works_with_glance(self):
        vm_utils._image_uses_bittorrent(self.context, self.instance).AndReturn(False)
        self.params["auth_token"] = "auth_token"

        self.session.call_plugin_serialized_with_retry(
            "glance", "download_vhd", 0, mox.IgnoreArg(), **self.params
        ).AndReturn(self.vdis)

        vm_utils.safe_find_sr(self.session).AndReturn("sr")
        vm_utils._scan_sr(self.session, "sr")
        vm_utils._check_vdi_size(self.context, self.session, self.instance, "vdi")

        self.mox.ReplayAll()

        self.assertEqual(
            "vdi", vm_utils._fetch_vhd_image(self.context, self.session, self.instance, self.image_id)["root"]["uuid"]
        )

        self.mox.VerifyAll()
Пример #27
0
    def test_fetch_vhd_image_cleans_up_vdi_on_fail(self):
        self.mox.StubOutWithMock(vm_utils, '_image_uses_bittorrent')
        vm_utils._image_uses_bittorrent(
            self.context, self.instance).AndReturn(False)

        self.mox.StubOutWithMock(
                self.session, 'call_plugin_serialized_with_retry')
        self.session.call_plugin_serialized_with_retry(
                'glance', 'download_vhd', 0, mox.IgnoreArg(),
                extra_headers={'X-Service-Catalog': '[]',
                               'X-Auth-Token': 'auth_token',
                               'X-Roles': '',
                               'X-Tenant-Id': None,
                               'X-User-Id': None,
                               'X-Identity-Status': 'Confirmed'},
                image_id='image_id',
                uuid_stack=["uuid_stack"],
                sr_path='sr_path').AndReturn({'root': {'uuid': 'vdi'}})

        self.mox.StubOutWithMock(vm_utils, 'safe_find_sr')
        vm_utils.safe_find_sr(self.session).AndReturn("sr")

        self.mox.StubOutWithMock(vm_utils, '_scan_sr')
        vm_utils._scan_sr(self.session, "sr")

        self.mox.StubOutWithMock(vm_utils, '_check_vdi_size')
        vm_utils._check_vdi_size(self.context, self.session, self.instance,
                "vdi").AndRaise(exception.InstanceTypeDiskTooSmall)

        self.mox.StubOutWithMock(self.session, 'call_xenapi')
        self.session.call_xenapi("VDI.get_by_uuid", "vdi").AndReturn("ref")

        self.mox.StubOutWithMock(vm_utils, 'destroy_vdi')
        vm_utils.destroy_vdi(self.session, "ref")

        self.mox.ReplayAll()

        self.assertRaises(exception.InstanceTypeDiskTooSmall,
                vm_utils._fetch_vhd_image, self.context, self.session,
                self.instance, 'image_id')

        self.mox.VerifyAll()
Пример #28
0
    def test_fetch_vhd_image_cleans_up_vdi_on_fail(self):
        self.mox.StubOutWithMock(self.session, 'call_xenapi')
        self._setup_bittorrent()

        vm_utils.safe_find_sr(self.session).AndReturn("sr")
        vm_utils._scan_sr(self.session, "sr")
        vm_utils._check_vdi_size(self.context, self.session, self.instance,
                                 "vdi").AndRaise(
                                     exception.InstanceTypeDiskTooSmall)

        self.session.call_xenapi("VDI.get_by_uuid", "vdi").AndReturn("ref")
        vm_utils.destroy_vdi(self.session, "ref")

        self.mox.ReplayAll()

        self.assertRaises(exception.InstanceTypeDiskTooSmall,
                          vm_utils._fetch_vhd_image, self.context,
                          self.session, self.instance, self.image_id)

        self.mox.VerifyAll()
Пример #29
0
    def test_fetch_vhd_image_works_with_bittorrent(self):
        cfg.CONF.import_opt('xenapi_torrent_base_url',
                            'nova.virt.xenapi.image.bittorrent')
        self.flags(xenapi_torrent_base_url='http://foo')

        self.mox.StubOutWithMock(vm_utils, '_image_uses_bittorrent')
        vm_utils._image_uses_bittorrent(
            self.context, self.instance).AndReturn(True)

        self.mox.StubOutWithMock(
                self.session, 'call_plugin_serialized')
        self.session.call_plugin_serialized('bittorrent', 'download_vhd',
            image_id='image_id',
            uuid_stack=["uuid_stack"],
            sr_path='sr_path',
            torrent_download_stall_cutoff=600,
            torrent_listen_port_start=6881,
            torrent_listen_port_end=6891,
            torrent_max_last_accessed=86400,
            torrent_max_seeder_processes_per_host=1,
            torrent_seed_chance=1.0,
            torrent_seed_duration=3600,
            torrent_url='http://foo/image_id.torrent'
        ).AndReturn({'root': {'uuid': 'vdi'}})

        self.mox.StubOutWithMock(vm_utils, 'safe_find_sr')
        vm_utils.safe_find_sr(self.session).AndReturn("sr")

        self.mox.StubOutWithMock(vm_utils, '_scan_sr')
        vm_utils._scan_sr(self.session, "sr")

        self.mox.StubOutWithMock(vm_utils, '_check_vdi_size')
        vm_utils._check_vdi_size(self.context, self.session, self.instance,
                                 "vdi")

        self.mox.ReplayAll()

        self.assertEqual("vdi", vm_utils._fetch_vhd_image(self.context,
            self.session, self.instance, 'image_id')['root']['uuid'])

        self.mox.VerifyAll()
 def download_image(self, context, session, instance, image_id):
     try:
         host_url = CONF.xenserver.connection_url
         image_data = IMAGE_API.download(context, image_id)
         image_stream = utils.IterableToFileAdapter(image_data)
         sr_ref = vm_utils.safe_find_sr(session)
         vdis = xenapi_image.stream_to_vdis(context, session, instance,
                                            host_url, sr_ref, image_stream)
     except xenapi_exception.OsXenApiException as e:
         LOG.error("Image download failed with exception: %s", e)
         raise exception.CouldNotFetchImage(image_id=image_id)
     return vdis
Пример #31
0
    def test_fetch_vhd_image_works_with_glance(self):
        self._apply_stubouts()
        self._common_params_setup(False)

        vm_utils._generate_glance_callback(self.context).AndReturn("dummy")

        vm_utils._fetch_using_dom0_plugin_with_retry(self.context,
            self.session, self.image_id, "glance", self.params,
            callback="dummy").AndReturn(self.vdis)

        vm_utils.safe_find_sr(self.session).AndReturn("sr")
        vm_utils._scan_sr(self.session, "sr")
        vm_utils._check_vdi_size(self.context, self.session, self.instance,
                                 "vdi")

        self.mox.ReplayAll()

        self.assertEqual("vdi", vm_utils._fetch_vhd_image(self.context,
            self.session, self.instance, self.image_id)['root']['uuid'])

        self.mox.VerifyAll()
Пример #32
0
    def test_fetch_vhd_image_works_with_bittorrent(self):
        self._apply_stubouts()
        self._common_params_setup(True)

        vm_utils._add_bittorrent_params(self.image_id, self.params)

        vm_utils._fetch_using_dom0_plugin_with_retry(self.context,
            self.session, self.image_id, "bittorrent", self.params,
            callback=None).AndReturn(self.vdis)

        vm_utils.safe_find_sr(self.session).AndReturn("sr")
        vm_utils._scan_sr(self.session, "sr")
        vm_utils._check_vdi_size(self.context, self.session, self.instance,
                                 "vdi")

        self.mox.ReplayAll()

        self.assertEqual("vdi", vm_utils._fetch_vhd_image(self.context,
            self.session, self.instance, self.image_id)['root']['uuid'])

        self.mox.VerifyAll()
Пример #33
0
    def test_default(self):
        self.mox.StubOutWithMock(vm_utils, "safe_find_sr")
        self.mox.StubOutWithMock(self.session, "call_xenapi")
        self.mox.StubOutWithMock(self.session, "get_xenapi_host")

        vm_utils.safe_find_sr(self.session).AndReturn("sr_ref")
        self.session.get_xenapi_host().AndReturn("host_ref")
        self.session.call_xenapi(
            'PBD.get_all_records_where',
            'field "host"="host_ref" and field "SR"="sr_ref"').AndReturn(
                {'pbd_ref': {
                    'device_config': {}
                }})
        self.session.call_xenapi("SR.get_record", "sr_ref").AndReturn({
            'uuid':
            'sr_uuid',
            'type':
            'ext'
        })
        self.mox.ReplayAll()
        self.assertEqual(vm_utils.get_sr_path(self.session),
                         "/var/run/sr-mount/sr_uuid")
Пример #34
0
 def download_image(self, context, session, instance, image_id):
     try:
         host_url = CONF.xenserver.connection_url
         image_data = IMAGE_API.download(context, image_id)
         image_stream = utils.IterableToFileAdapter(image_data)
         sr_ref = vm_utils.safe_find_sr(session)
         vdis = xenapi_image.stream_to_vdis(context, session,
                                            instance, host_url,
                                            sr_ref, image_stream)
     except xenapi_exception.OsXenApiException as e:
         LOG.error("Image download failed with exception: %s", e)
         raise exception.CouldNotFetchImage(image_id=image_id)
     return vdis
Пример #35
0
    def test_fetch_vhd_image_works_with_glance(self):
        self.mox.StubOutWithMock(vm_utils, '_image_uses_bittorrent')
        vm_utils._image_uses_bittorrent(
            self.context, self.instance).AndReturn(False)

        self._stub_glance_download_vhd()

        self.mox.StubOutWithMock(vm_utils, 'safe_find_sr')
        vm_utils.safe_find_sr(self.session).AndReturn("sr")

        self.mox.StubOutWithMock(vm_utils, '_scan_sr')
        vm_utils._scan_sr(self.session, "sr")

        self.mox.StubOutWithMock(vm_utils, '_check_vdi_size')
        vm_utils._check_vdi_size(
                self.context, self.session, self.instance, "vdi")

        self.mox.ReplayAll()

        self.assertEqual("vdi", vm_utils._fetch_vhd_image(self.context,
            self.session, self.instance, 'image_id')['root']['uuid'])

        self.mox.VerifyAll()
Пример #36
0
    def test_fetch_vhd_image_works_with_glance(self):
        vm_utils._image_uses_bittorrent(self.context,
                                        self.instance).AndReturn(False)
        self.params['auth_token'] = 'auth_token'

        self.session.call_plugin_serialized_with_retry(
            'glance', 'download_vhd', 0, mox.IgnoreArg(),
            **self.params).AndReturn(self.vdis)

        vm_utils.safe_find_sr(self.session).AndReturn("sr")
        vm_utils._scan_sr(self.session, "sr")
        vm_utils._check_vdi_size(self.context, self.session, self.instance,
                                 "vdi")

        self.mox.ReplayAll()

        self.assertEqual(
            "vdi",
            vm_utils._fetch_vhd_image(self.context, self.session,
                                      self.instance,
                                      self.image_id)['root']['uuid'])

        self.mox.VerifyAll()
Пример #37
0
def main():
    config.parse_args(sys.argv)
    utils.monkey_patch()

    xenapi = xenapi_driver.XenAPIDriver()
    session = xenapi._session

    sr_ref = vm_utils.safe_find_sr(session)
    destroyed = vm_utils.destroy_cached_images(session, sr_ref, all_cached=CONF.all_cached, dry_run=CONF.dry_run)

    if "--verbose" in sys.argv:
        print "\n".join(destroyed)

    print "Destroyed %d cached VDIs" % len(destroyed)
Пример #38
0
def main():
    config.parse_args(sys.argv)
    utils.monkey_patch()

    _session = session.XenAPISession(
        CONF.xenserver.connection_url, CONF.xenserver.connection_username, CONF.xenserver.connection_password
    )

    sr_ref = vm_utils.safe_find_sr(_session)
    destroyed = vm_utils.destroy_cached_images(_session, sr_ref, all_cached=CONF.all_cached, dry_run=CONF.dry_run)

    if "--verbose" in sys.argv:
        print("\n".join(destroyed))

    print("Destroyed %d cached VDIs" % len(destroyed))
Пример #39
0
def main():
    flags.parse_args(sys.argv)
    utils.monkey_patch()

    xenapi = xenapi_driver.XenAPIDriver()
    session = xenapi._session

    sr_ref = vm_utils.safe_find_sr(session)
    destroyed = vm_utils.destroy_cached_images(
            session, sr_ref, all_cached=FLAGS.all_cached,
            dry_run=FLAGS.dry_run)

    if '--verbose' in sys.argv:
        print '\n'.join(destroyed)

    print "Destroyed %d cached VDIs" % len(destroyed)
Пример #40
0
def main():
    config.parse_args(sys.argv)

    _session = session.XenAPISession(CONF.xenserver.connection_url,
                                     CONF.xenserver.connection_username,
                                     CONF.xenserver.connection_password)

    sr_ref = vm_utils.safe_find_sr(_session)
    destroyed = vm_utils.destroy_cached_images(
        _session, sr_ref, all_cached=CONF.all_cached,
        dry_run=CONF.dry_run, keep_days=CONF.keep_days)

    if '--verbose' in sys.argv:
        print('\n'.join(destroyed))

    print("Destroyed %d cached VDIs" % len(destroyed))
Пример #41
0
def main():
    config.parse_args(sys.argv)
    utils.monkey_patch()

    xenapi = xenapi_driver.XenAPIDriver()
    session = xenapi._session

    sr_ref = vm_utils.safe_find_sr(session)
    destroyed = vm_utils.destroy_cached_images(
            session, sr_ref, all_cached=CONF.all_cached,
            dry_run=CONF.dry_run)

    if '--verbose' in sys.argv:
        print '\n'.join(destroyed)

    print "Destroyed %d cached VDIs" % len(destroyed)
Пример #42
0
 def update_status(self):
     """Since under Xenserver, a compute node runs on a given host,
     we can get host status information using xenapi.
     """
     LOG.debug(_("Updating host stats"))
     # Make it something unlikely to match any actual instance ID
     task_id = random.randint(-80000, -70000)
     task = self._session.async_call_plugin("xenhost", "host_data", {})
     task_result = self._session.wait_for_task(task, task_id)
     if not task_result:
         task_result = json.dumps("")
     try:
         data = json.loads(task_result)
     except ValueError as e:
         # Invalid JSON object
         LOG.error(_("Unable to get updated status: %s") % e)
         return
     # Get the SR usage
     try:
         sr_ref = vm_utils.safe_find_sr(self._session)
     except exception.NotFound as e:
         # No SR configured
         LOG.error(_("Unable to get SR for this host: %s") % e)
         return
     sr_rec = self._session.get_xenapi().SR.get_record(sr_ref)
     total = int(sr_rec["virtual_allocation"])
     used = int(sr_rec["physical_utilisation"])
     data["disk_total"] = total
     data["disk_used"] = used
     data["disk_available"] = total - used
     host_memory = data.get('host_memory', None)
     if host_memory:
         data["host_memory_total"] = host_memory.get('total', 0)
         data["host_memory_overhead"] = host_memory.get('overhead', 0)
         data["host_memory_free"] = host_memory.get('free', 0)
         data["host_memory_free_computed"] = \
                     host_memory.get('free-computed', 0)
         del data['host_memory']
     self._stats = data
Пример #43
0
 def update_status(self):
     """Since under Xenserver, a compute node runs on a given host,
     we can get host status information using xenapi.
     """
     LOG.debug(_("Updating host stats"))
     # Make it something unlikely to match any actual instance ID
     task_id = random.randint(-80000, -70000)
     task = self._session.async_call_plugin("xenhost", "host_data", {})
     task_result = self._session.wait_for_task(task, task_id)
     if not task_result:
         task_result = json.dumps("")
     try:
         data = json.loads(task_result)
     except ValueError as e:
         # Invalid JSON object
         LOG.error(_("Unable to get updated status: %s") % e)
         return
     # Get the SR usage
     try:
         sr_ref = vm_utils.safe_find_sr(self._session)
     except exception.NotFound as e:
         # No SR configured
         LOG.error(_("Unable to get SR for this host: %s") % e)
         return
     sr_rec = self._session.get_xenapi().SR.get_record(sr_ref)
     total = int(sr_rec["virtual_allocation"])
     used = int(sr_rec["physical_utilisation"])
     data["disk_total"] = total
     data["disk_used"] = used
     data["disk_available"] = total - used
     host_memory = data.get('host_memory', None)
     if host_memory:
         data["host_memory_total"] = host_memory.get('total', 0)
         data["host_memory_overhead"] = host_memory.get('overhead', 0)
         data["host_memory_free"] = host_memory.get('free', 0)
         data["host_memory_free_computed"] = \
                     host_memory.get('free-computed', 0)
         del data['host_memory']
     self._stats = data