Esempio n. 1
0
    def destroy(self,
                instance,
                context=None,
                network_info=None,
                block_device_info=None,
                destroy_disks=True,
                migrate_data=None):
        """Destroy the specified instance from the Hypervisor."""
        LOG.info(i18n._("Got request to destroy instance"), instance=instance)
        if not self.instance_exists(instance):
            LOG.warning(i18n._("Instance do not exists."), instance=instance)
            return

        power_state = vmutils.get_power_state(instance)
        if power_state not in (constants.STATE_POWER_OFF,
                               constants.STATE_SAVED):
            self._vbox_manage.control_vm(instance, constants.STATE_POWER_OFF)

        try:
            self._vbox_manage.unregister_vm(instance, delete=destroy_disks)
            if destroy_disks:
                pathutils.instance_basepath(instance,
                                            action=constants.PATH_DELETE)
        except vbox_exc.VBoxException:
            with excutils.save_and_reraise_exception():
                LOG.exception(i18n._('Failed to destroy instance: %s'),
                              instance.name)
Esempio n. 2
0
    def destroy(self,
                instance,
                context=None,
                network_info=None,
                block_device_info=None,
                destroy_disks=True,
                migrate_data=None):
        """Destroy the specified instance from the Hypervisor."""
        LOG.info(i18n._("Got request to destroy instance"), instance=instance)
        if not self.instance_exists(instance):
            LOG.warning(i18n._("Instance do not exists."), instance=instance)
            return

        power_state = vmutils.get_power_state(instance)
        if power_state not in (constants.STATE_POWER_OFF,
                               constants.STATE_SAVED):
            self._vbox_manage.control_vm(instance, constants.STATE_POWER_OFF)

        try:
            self._vbox_manage.unregister_vm(instance, delete=destroy_disks)
            if destroy_disks:
                pathutils.instance_basepath(
                    instance, action=constants.PATH_DELETE)
        except vbox_exc.VBoxException:
            with excutils.save_and_reraise_exception():
                LOG.exception(
                    i18n._('Failed to destroy instance: %s'), instance.name)
Esempio n. 3
0
    def create_instance(self, instance, image_meta, network_info,
                        overwrite=True):
        image_properties = image_meta.get("properties", {})
        action = constants.PATH_DELETE if overwrite else None

        basepath = pathutils.instance_basepath(instance, action=action)
        self._vbox_manage.create_vm(
            instance.name, basefolder=os.path.dirname(basepath),
            register=True)

        vmutils.set_os_type(instance, image_properties.get('os_type', None))
        vmutils.set_memory(instance)
        vmutils.set_cpus(instance)
        self._network_setup(instance, network_info)
Esempio n. 4
0
    def _migrate_disk_files(self, instance, disk_files, destination):
        local_ips = hostutils.get_local_ips()
        local_ips.append(hostutils.get_ip())
        same_host = destination in local_ips
        LOG.debug("Destination `%(dest)s` %(local_ips)s", {
            "dest": destination,
            "local_ips": local_ips
        })

        instance_basepath = pathutils.instance_basepath(instance)
        revert_path = pathutils.revert_dir(instance,
                                           action=constants.PATH_OVERWRITE)

        if same_host:
            destination_path = ("%(path)s%(suffix)s" % {
                "path": instance_basepath,
                "suffix": self._SUFFIX
            })
        else:
            LOG.warning(i18n._LW("Only resize on the same host is supported!"))
            raise NotImplementedError()

        # Delete the destination path if already exists
        pathutils.delete_path(destination_path)

        # Create the destination path
        pathutils.create_path(destination_path)

        try:
            self._migrate_disk(disk_files[0], destination_path, root_disk=True)
            for disk_file in disk_files[1:]:
                self._migrate_disk(disk_file, destination_path)

            # Remove the instance from the Hypervisor
            self._vbox_ops.destroy(instance, destroy_disks=False)

            # Move files to revert path
            os.rename(instance_basepath, revert_path)
            if same_host:
                os.rename(destination_path, instance_basepath)
        except (OSError, vbox_exc.VBoxException):
            with excutils.save_and_reraise_exception():
                try:
                    self._cleanup_failed_disk_migration(
                        instance_basepath, revert_path, destination_path)
                except vbox_exc.VBoxException as exc:
                    # Log and ignore this exception
                    LOG.exception(exc)
Esempio n. 5
0
    def create_instance(self,
                        instance,
                        image_meta,
                        network_info,
                        overwrite=True):
        image_properties = image_meta.get("properties", {})
        action = constants.PATH_DELETE if overwrite else None

        basepath = pathutils.instance_basepath(instance, action=action)
        self._vbox_manage.create_vm(instance.name,
                                    basefolder=os.path.dirname(basepath),
                                    register=True)

        vmutils.set_os_type(instance, image_properties.get('os_type', None))
        vmutils.set_memory(instance)
        vmutils.set_cpus(instance)
        self._network_setup(instance, network_info)
    def _migrate_disk_files(self, instance, disk_files, destination):
        local_ips = hostutils.get_local_ips()
        local_ips.append(hostutils.get_ip())
        same_host = destination in local_ips
        LOG.debug("Destination `%(dest)s` %(local_ips)s",
                  {"dest": destination, "local_ips": local_ips})

        instance_basepath = pathutils.instance_basepath(instance)
        revert_path = pathutils.revert_dir(
            instance, action=constants.PATH_OVERWRITE)

        if same_host:
            destination_path = (
                "%(path)s%(suffix)s" %
                {"path": instance_basepath, "suffix": self._SUFFIX})
        else:
            LOG.warning(
                i18n._LW("Only resize on the same host is supported!"))
            raise NotImplementedError()

        # Delete the destination path if already exists
        pathutils.delete_path(destination_path)

        # Create the destination path
        pathutils.create_path(destination_path)

        try:
            self._migrate_disk(disk_files[0], destination_path, root_disk=True)
            for disk_file in disk_files[1:]:
                self._migrate_disk(disk_file, destination_path)

            # Remove the instance from the Hypervisor
            self._vbox_ops.destroy(instance, destroy_disks=False)

            # Move files to revert path
            os.rename(instance_basepath, revert_path)
            if same_host:
                os.rename(destination_path, instance_basepath)
        except (OSError, vbox_exc.VBoxException):
            with excutils.save_and_reraise_exception():
                try:
                    self._cleanup_failed_disk_migration(
                        instance_basepath, revert_path, destination_path)
                except vbox_exc.VBoxException as exc:
                    # Log and ignore this exception
                    LOG.exception(exc)
Esempio n. 7
0
    def test_instance_basepath(self, mock_instance_dir, mock_join):
        pathutils.instance_basepath(self._instance)

        mock_join.assert_called_once_with(mock_instance_dir(),
                                          self._instance.name)
 def _revert_migration_files(self, instance):
     instance_basepath = pathutils.instance_basepath(instance)
     revert_path = pathutils.revert_dir(instance)
     os.rename(revert_path, instance_basepath)
    def test_instance_basepath(self, mock_instance_dir, mock_join):
        pathutils.instance_basepath(self._instance)

        mock_join.assert_called_once_with(mock_instance_dir(), self._instance.name)
Esempio n. 10
0
 def _revert_migration_files(self, instance):
     instance_basepath = pathutils.instance_basepath(instance)
     revert_path = pathutils.revert_dir(instance)
     os.rename(revert_path, instance_basepath)