Beispiel #1
0
    def export_vms(self, vms_names, standalone, dst_dir, compress,
                   init_file_name, out_format):
        if not vms_names:
            vms_names = self._vms.keys()

        running_vms = []
        vms = []
        for name in vms_names:
            try:
                vm = self._vms[name]
                vms.append(vm)
                if vm.defined():
                    running_vms.append(vm)
            except KeyError:
                raise utils.LagoUserException(
                    'Entity {} does not exist'.format(name))

        if running_vms:
            raise utils.LagoUserException(
                'The following vms must be off:\n{}'.format('\n'.join(
                    [_vm.name() for _vm in running_vms])))
        # TODO: run the export task in parallel

        with LogTask('Exporting disks to: {}'.format(dst_dir)):
            for _vm in vms:
                _vm.export_disks(standalone, dst_dir, compress)

        self.generate_init(os.path.join(dst_dir, init_file_name), out_format)
Beispiel #2
0
    def export_vms(self,
                   vms_names,
                   standalone,
                   dst_dir,
                   compress,
                   init_file_name,
                   out_format,
                   collect_only=False,
                   with_threads=True):
        # todo: move this logic to PrefixExportManager
        if not vms_names:
            vms_names = list(self._vms.keys())

        running_vms = []
        vms = []
        for name in vms_names:
            try:
                vm = self._vms[name]
                if not vm.spec.get('skip-export'):
                    vms.append(vm)
                    if vm.running():
                        running_vms.append(vm)
            except KeyError:
                raise utils.LagoUserException(
                    'Entity {} does not exist'.format(name))

        if running_vms:
            raise utils.LagoUserException(
                'The following vms must be off:\n{}'.format('\n'.join(
                    [_vm.name() for _vm in running_vms])))

        with LogTask('Exporting disks to: {}'.format(dst_dir)):
            if not os.path.isdir(dst_dir):
                os.mkdir(dst_dir)

            def _export_disks(vm):
                return vm.export_disks(standalone, dst_dir, compress,
                                       collect_only, with_threads)

            if collect_only:
                return (functools.reduce(lambda x, y: x.update(y) or x,
                                         [_export_disks(v) for v in vms]))
            else:
                if with_threads:
                    results = utils.invoke_in_parallel(_export_disks, vms)
                else:
                    results = [_export_disks(v) for v in vms]

                results = functools.reduce(lambda x, y: x.update(y) or x,
                                           results)

        self.generate_init(os.path.join(dst_dir, init_file_name), out_format,
                           vms)

        results['init-file'] = os.path.join(dst_dir, init_file_name)

        return results
Beispiel #3
0
    def get_vms(self, vm_names=None):
        """
        Returns the vm objects associated with vm_names
        if vm_names is None, return all the vms in the prefix
        Args:
            vm_names (list of str): The names of the requested vms
        Returns
            dict: Which contains the requested vm objects indexed by name
        Raises:
            utils.LagoUserException: If a vm name doesn't exist
        """
        if not vm_names:
            return self._vms.copy()

        missing_vms = []
        vms = {}
        for name in vm_names:
            try:
                vms[name] = self._vms[name]
            except KeyError:
                # TODO: add resolver by suffix
                missing_vms.append(name)

        if missing_vms:
            raise utils.LagoUserException(
                'The following vms do not exist: \n{}'.format(
                    '\n'.join(missing_vms)))

        return vms
Beispiel #4
0
Datei: vm.py Projekt: bellle/lago
    def shutdown(self, *args, **kwargs):
        super().shutdown(*args, **kwargs)

        self._shutdown(libvirt_cmd=libvirt.virDomain.shutdown,
                       ssh_cmd=['poweroff'],
                       msg='Shutdown')

        try:
            with utils.ExceptionTimer(timeout=60 * 5):
                while self.alive():
                    time.sleep(1)
        except utils.TimerException:
            raise utils.LagoUserException('Failed to shutdown vm: {}'.format(
                self.vm.name()))