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()
def setUp(self): super(FetchVhdImageTestCase, self).setUp() self.context = context.get_admin_context() self.context.auth_token = "auth_token" self.session = FakeSession() self.instance = {"uuid": "uuid"} self.image_id = "image_id" self.uuid_stack = ["uuid_stack"] self.sr_path = "sr_path" self.params = {"image_id": self.image_id, "uuid_stack": self.uuid_stack, "sr_path": self.sr_path} self.vdis = {"root": {"uuid": "vdi"}} self.mox.StubOutWithMock(vm_utils, "_make_uuid_stack") self.mox.StubOutWithMock(vm_utils, "get_sr_path") self.mox.StubOutWithMock(vm_utils, "_image_uses_bittorrent") self.mox.StubOutWithMock(vm_utils, "_add_bittorrent_params") self.mox.StubOutWithMock(vm_utils, "safe_find_sr") self.mox.StubOutWithMock(vm_utils, "_scan_sr") self.mox.StubOutWithMock(vm_utils, "_check_vdi_size") self.mox.StubOutWithMock(vm_utils, "destroy_vdi") self.mox.StubOutWithMock(self.session, "call_plugin_serialized_with_retry") self.mox.StubOutWithMock(vm_utils, "_add_torrent_url") vm_utils._make_uuid_stack().AndReturn(self.uuid_stack) vm_utils.get_sr_path(self.session).AndReturn(self.sr_path)
def setUp(self): super(FetchVhdImageTestCase, self).setUp() self.context = context.get_admin_context() self.context.auth_token = 'auth_token' self.session = FakeSession() self.instance = {"uuid": "uuid"} self.mox.StubOutWithMock(vm_utils, '_make_uuid_stack') vm_utils._make_uuid_stack().AndReturn(["uuid_stack"]) self.mox.StubOutWithMock(vm_utils, 'get_sr_path') vm_utils.get_sr_path(self.session).AndReturn('sr_path')
def _common_params_setup(self, uses_bittorrent): self.context = "context" self.session = FakeSession() self.instance = {"uuid": "uuid"} self.image_id = "image_id" self.uuid_stack = ["uuid_stack"] self.sr_path = "sr_path" self.params = {'image_id': self.image_id, 'uuid_stack': self.uuid_stack, 'sr_path': self.sr_path} self.vdis = {'root': {'uuid': 'vdi'}} vm_utils._make_uuid_stack().AndReturn(self.uuid_stack) vm_utils.get_sr_path(self.session).AndReturn(self.sr_path) vm_utils._image_uses_bittorrent(self.context, self.instance).AndReturn(uses_bittorrent)
def download_image(self, context, session, instance, image_id): params = {} params['image_id'] = image_id params['uuid_stack'] = vm_utils._make_uuid_stack() params['sr_path'] = vm_utils.get_sr_path(session) params['torrent_seed_duration'] = CONF.xenserver.torrent_seed_duration params['torrent_seed_chance'] = CONF.xenserver.torrent_seed_chance params['torrent_max_last_accessed'] = \ CONF.xenserver.torrent_max_last_accessed params['torrent_listen_port_start'] = \ CONF.xenserver.torrent_listen_port_start params['torrent_listen_port_end'] = \ CONF.xenserver.torrent_listen_port_end params['torrent_download_stall_cutoff'] = \ CONF.xenserver.torrent_download_stall_cutoff params['torrent_max_seeder_processes_per_host'] = \ CONF.xenserver.torrent_max_seeder_processes_per_host lookup_fn = self._lookup_torrent_url_fn() params['torrent_url'] = lookup_fn(instance, image_id) vdis = session.call_plugin_serialized( 'bittorrent', 'download_vhd', **params) return vdis
def upload_image(self, context, session, instance, vdi_uuids, image_id): """Requests that the Glance plugin bundle the specified VDIs and push them into Glance using the specified human-friendly name. """ # NOTE(sirp): Currently we only support uploading images as VHD, there # is no RAW equivalent (yet) LOG.debug(_("Asking xapi to upload to glance %(vdi_uuids)s as" " ID %(image_id)s"), locals(), instance=instance) glance_api_servers = glance.get_api_servers() glance_host, glance_port, glance_use_ssl = glance_api_servers.next() properties = { 'auto_disk_config': instance['auto_disk_config'], 'os_type': instance['os_type'] or CONF.default_os_type, } params = {'vdi_uuids': vdi_uuids, 'image_id': image_id, 'glance_host': glance_host, 'glance_port': glance_port, 'glance_use_ssl': glance_use_ssl, 'sr_path': vm_utils.get_sr_path(session), 'auth_token': getattr(context, 'auth_token', None), 'properties': properties} session.call_plugin_serialized('glance', 'upload_vhd', **params)
def upload_image(self, context, session, instance, vdi_uuids, image_id): """Requests that the Glance plugin bundle the specified VDIs and push them into Glance using the specified human-friendly name. """ # NOTE(sirp): Currently we only support uploading images as VHD, there # is no RAW equivalent (yet) max_attempts = CONF.glance_num_retries + 1 sleep_time = 0.5 glance_api_servers = glance.get_api_servers() properties = { 'auto_disk_config': instance['auto_disk_config'], 'os_type': instance['os_type'] or CONF.default_os_type, } if agent.USE_AGENT_SM_KEY in instance["system_metadata"]: properties[agent.USE_AGENT_KEY] = \ instance["system_metadata"][agent.USE_AGENT_SM_KEY] for attempt_num in xrange(1, max_attempts + 1): (glance_host, glance_port, glance_use_ssl) = glance_api_servers.next() try: params = {'vdi_uuids': vdi_uuids, 'image_id': image_id, 'glance_host': glance_host, 'glance_port': glance_port, 'glance_use_ssl': glance_use_ssl, 'sr_path': vm_utils.get_sr_path(session), 'auth_token': getattr(context, 'auth_token', None), 'properties': properties} LOG.debug(_("Asking xapi to upload to glance %(vdi_uuids)s as" " ID %(image_id)s" " glance server: %(glance_host)s:%(glance_port)d" " attempt %(attempt_num)d/%(max_attempts)d"), locals(), instance=instance) return session.call_plugin_serialized('glance', 'upload_vhd', **params) except session.XenAPI.Failure as exc: _type, _method, error = exc.details[:3] if error == 'RetryableError': LOG.error(_('upload_vhd failed: %r') % (exc.details[3:],)) else: raise time.sleep(sleep_time) sleep_time = min(2 * sleep_time, 15) raise exception.CouldNotUploadImage(image_id=image_id)
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")
def _common_params_setup(self, uses_bittorrent): self.context = "context" self.session = FakeSession() self.instance = {"uuid": "uuid"} self.image_id = "image_id" self.uuid_stack = ["uuid_stack"] self.sr_path = "sr_path" self.params = {'image_id': self.image_id, 'uuid_stack': self.uuid_stack, 'sr_path': self.sr_path} self.bt_params = copy.copy(self.params) self.bt_params['torrent_url'] = "%s.torrent" % self.image_id self.vdis = {'root': {'uuid': 'vdi'}} vm_utils._make_uuid_stack().AndReturn(self.uuid_stack) vm_utils.get_sr_path(self.session).AndReturn(self.sr_path) vm_utils._image_uses_bittorrent(self.context, self.instance).AndReturn(uses_bittorrent) if uses_bittorrent: def set_url(instance, image_id, params): params['torrent_url'] = "%s.torrent" % image_id vm_utils._add_torrent_url(self.instance, self.image_id, self.params).WithSideEffects(set_url).AndReturn(True)
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")
def _call_glance_plugin(self, context, instance, session, fn, image_id, params): glance_api_servers = glance.get_api_servers(context) sr_path = vm_utils.get_sr_path(session) extra_headers = glance.generate_identity_headers(context) def pick_glance(kwargs): server = next(glance_api_servers) kwargs['endpoint'] = server kwargs['api_version'] = 2 # NOTE(sdague): is the return significant here at all? return server def retry_cb(context, instance, exc=None): if exc: exc_info = sys.exc_info() LOG.debug(six.text_type(exc), exc_info=exc_info) compute_utils.add_instance_fault_from_exc( context, instance, exc, exc_info) cb = functools.partial(retry_cb, context, instance) return fn(session, CONF.glance.num_retries, pick_glance, cb, image_id, sr_path, extra_headers, **params)
def _make_params(self, context, session, image_id): return {'image_id': image_id, 'sr_path': vm_utils.get_sr_path(session), 'extra_headers': glance.generate_identity_headers(context)}
def _make_params(self, context, session, image_id): return {'image_id': image_id, 'sr_path': vm_utils.get_sr_path(session), 'auth_token': getattr(context, 'auth_token', None)}
def _make_params(self, context, session, image_id): return { "image_id": image_id, "sr_path": vm_utils.get_sr_path(session), "extra_headers": glance.generate_identity_headers(context), }
def _make_params(self, context, session, image_id): return { 'image_id': image_id, 'sr_path': vm_utils.get_sr_path(session), 'extra_headers': glance.generate_identity_headers(context) }
def upload_image(self, context, session, instance, vdi_uuids, image_id): """Requests that the Glance plugin bundle the specified VDIs and push them into Glance using the specified human-friendly name. """ # NOTE(sirp): Currently we only support uploading images as VHD, there # is no RAW equivalent (yet) max_attempts = CONF.glance_num_retries + 1 sleep_time = 0.5 glance_api_servers = glance.get_api_servers() properties = { 'auto_disk_config': instance['auto_disk_config'], 'os_type': instance['os_type'] or CONF.default_os_type, } if agent.USE_AGENT_SM_KEY in instance["system_metadata"]: properties[agent.USE_AGENT_KEY] = \ instance["system_metadata"][agent.USE_AGENT_SM_KEY] for attempt_num in xrange(1, max_attempts + 1): (glance_host, glance_port, glance_use_ssl) = glance_api_servers.next() try: params = { 'vdi_uuids': vdi_uuids, 'image_id': image_id, 'glance_host': glance_host, 'glance_port': glance_port, 'glance_use_ssl': glance_use_ssl, 'sr_path': vm_utils.get_sr_path(session), 'auth_token': getattr(context, 'auth_token', None), 'properties': properties } LOG.debug(_("Asking xapi to upload to glance %(vdi_uuids)s as" " ID %(image_id)s" " glance server: %(glance_host)s:%(glance_port)d" " attempt %(attempt_num)d/%(max_attempts)d"), { 'vdi_uuids': vdi_uuids, 'image_id': image_id, 'glance_host': glance_host, 'glance_port': glance_port, 'attempt_num': attempt_num, 'max_attempts': max_attempts }, instance=instance) return session.call_plugin_serialized('glance', 'upload_vhd', **params) except session.XenAPI.Failure as exc: _type, _method, error = exc.details[:3] if error == 'RetryableError': LOG.error(_('upload_vhd failed: %r') % (exc.details[3:], )) else: raise time.sleep(sleep_time) sleep_time = min(2 * sleep_time, 15) raise exception.CouldNotUploadImage(image_id=image_id)