def init_host(self): """Perform any required initialization.""" if not os.path.isdir(CONF.conversion_dir): utils.execute('mkdir', '-m', '755', '-p', CONF.conversion_dir, run_as_root=True) ctxt = context.get_admin_context() self.publish_service_capabilities(ctxt)
def create_con_dir(migration_id): conversion_dir = os.path.join(CONF.conversion_dir, migration_id) utils.execute('mkdir', '-m', 755, '-p', conversion_dir, run_as_root=True) return conversion_dir
def _get_instance_disk(self, device_url, dest_disk_path): url = device_url.url if not os.path.exists(dest_disk_path): utils.execute('wget', url, '--no-check-certificate', '-O', dest_disk_path, run_as_root=True)
def create_migration(self, context, migration_ref): """Creates the migration process of a VM.""" try: vm_id = migration_ref.get('source_instance_id') vm = db.vm_get(context, vm_id) source = db.source_get(context, vm.get('source_id')) migration_id = migration_ref.get('id') self._migration_status_update(context, migration_id, MIGRATION_EVENT['connect'], MIGRATION_STATUS['init']) driver = self._get_driver_from_source(context, source) source_vm_id = vm.get('uuid_at_source') vm_conversion_dir = os.path.join(CONF.conversion_dir, vm_id) utils.execute('mkdir', '-p', vm_conversion_dir, run_as_root=False) self._migration_status_update(context, migration_id, MIGRATION_EVENT['fetch'], MIGRATION_STATUS['inprogress']) disks = driver.download_vm_disks(context, source_vm_id, vm_conversion_dir) self._convert_disks(context, migration_id, disks) image_name_prefix = vm.get('name') if not image_name_prefix: image_name_prefix = vm_id self._upload_to_glance(context, migration_id, image_name_prefix, disks) name = vm.get('id') memory = int(vm.get('memory')) cpus = int(vm.get('vcpus')) root_gb = int(disks[0].get('size')) / 1024 / 1024 / 1024 flavor = self._flavor_create(context, name, memory, cpus, root_gb) dest_id = self._boot_vm(context, migration_id, disks, image_name_prefix, flavor) self._migration_status_update(context, migration_id, MIGRATION_EVENT['done'], MIGRATION_STATUS['complete']) db.vm_update(context, vm_id, { 'migrated': True, 'dest_id': dest_id }) except Exception: self._migration_status_update(context, migration_id, None, MIGRATION_STATUS['error']) raise
def _get_vm_disk(self, device_url, dest_disk_path): url = device_url.url r = requests.get(url, verify=False) if os.path.exists(dest_disk_path): utils.execute('rm', dest_disk_path) f = open(dest_disk_path, "wb") for chunk in r.iter_content(chunk_size=CHUNK_SIZE): if chunk: f.write(chunk) f.close()
def create_migration(self, context, migration_ref): """Creates the migration process of a VM.""" try: vm_id = migration_ref.get('source_instance_id') vm = db.vm_get(context, vm_id) source = db.source_get(context, vm.get('source_id')) migration_id = migration_ref.get('id') self._migration_status_update(context, migration_id, MIGRATION_EVENT['connect'], MIGRATION_STATUS['init']) driver = self._get_driver_from_source(context, source) source_vm_id = vm.get('uuid_at_source') vm_conversion_dir = os.path.join(CONF.conversion_dir, vm_id) utils.execute('mkdir', '-p', vm_conversion_dir, run_as_root = False) self._migration_status_update(context, migration_id, MIGRATION_EVENT['fetch'], MIGRATION_STATUS['inprogress']) disks = driver.download_vm_disks(context, source_vm_id, vm_conversion_dir) self._convert_disks(context, migration_id, disks) image_name_prefix = vm.get('name') if not image_name_prefix: image_name_prefix = vm_id self._upload_to_glance(context, migration_id, image_name_prefix, disks) dest_id = self._boot_vm(context, migration_id, disks, image_name_prefix) self._migration_status_update(context, migration_id, MIGRATION_EVENT['done'], MIGRATION_STATUS['complete']) db.vm_update(context, vm_id, {'migrated': True, 'dest_id': dest_id}) except Exception: self._migration_status_update(context, migration_id, None, MIGRATION_STATUS['error']) raise
def _download_image_from_glance(self, image_id, file_path): out, err = utils.execute( 'glance', '--os-username', self.configuration.username, '--os-password', self.configuration.password, '--os-tenant-name', self.configuration.tenant_name, '--os-auth-url', self.configuration.auth_url, 'image-download', '--file', file_path, image_id, run_as_root=True)
def _get_instance_disk(self, device_url, dest_disk_path): url = device_url.url # TODO: Remove this temp hack parsed_uri = urlparse(url) if parsed_uri.netloc == '*': host = str(self.cloud.params['host']) url = '{uri.scheme}://{host}{uri.path}'.format(uri=parsed_uri,host=host) if not os.path.exists(dest_disk_path): try: utils.execute('wget', url, '--no-check-certificate', '-O', dest_disk_path, run_as_root=True) except Exception as error: msg = (_('Disk download failed. %s') % error.message) LOG.exception(msg) raise exception.VSphereException(msg)
def nova_boot(self, instance_name, image_name): out, err = utils.execute('nova', '--os-username', self.configuration.username, '--os-password', self.configuration.password, '--os-tenant-name', self.configuration.tenant_name, '--os-auth-url', self.configuration.auth_url, 'boot', '--image', image_name, '--flavor', '2', instance_name, run_as_root=True)
def _upload_image_to_glance(self, image_name, file_path): out, err = utils.execute('glance', '--os-username', self.configuration.username, '--os-password', self.configuration.password, '--os-tenant-name', self.configuration.tenant_name, '--os-auth-url', self.configuration.auth_url, 'image-create', '--file', file_path, '--disk-format', 'raw', '--container-format', 'bare', '--name', image_name, run_as_root=True)
def create_volume(self, context, **kwargs): if not self._initialized: self.do_setup(context) image_name = kwargs['mig_ref_id'] try: self._upload_image_to_glance(image_name, kwargs['path']) utils.execute('rm', kwargs['path'], run_as_root=True) img = self.glance.images.find(name=image_name) if img.status != 'active': raise Exception vol = self.cinder.volumes.create(display_name=kwargs['name'], size=int(kwargs['size']), imageRef=img.id) while vol.status != 'available': vol = self.cinder.volumes.get(vol.id) self.glance.images.delete(img.id) except Exception as e: LOG.error(_LE('Failed to create volume from image at destination ' 'image_name: %s %s'), image_name, e) raise exception.VolumeCreationFailed(reason=e.message)
def _get_free_space(conversion_dir): """Calculate and return free space available.""" try: out = utils.execute('df', '--portability', '--block-size', '1', conversion_dir, run_as_root=True)[0] out = out.splitlines()[1] available = int(out.split()[3]) except Exception: msg = _("Failed to get the available free space.") LOG.exception(msg) raise exception.GutsException(msg) return available