Example #1
0
 def __init__(self, **kargs):
     self.conn = kargs['conn']
     self.objstore = kargs['objstore']
     self.task = TaskModel(**kargs)
     self.vmstorages = VMStoragesModel(**kargs)
     self.vmstorage = VMStorageModel(**kargs)
Example #2
0
class VMSnapshotsModel(object):
    def __init__(self, **kargs):
        self.conn = kargs['conn']
        self.objstore = kargs['objstore']
        self.task = TaskModel(**kargs)
        self.vmstorages = VMStoragesModel(**kargs)
        self.vmstorage = VMStorageModel(**kargs)

    def create(self, vm_name, params=None):
        """Create a snapshot with the current domain state.

        The VM must be stopped and contain only disks with format 'qcow2';
        otherwise an exception will be raised.

        Parameters:
        vm_name -- the name of the VM where the snapshot will be created.
        params -- a dict with the following values:
            "name": The snapshot name (optional). If omitted, a default value
            based on the current time will be used.

        Return:
        A Task running the operation.
        """
        if params is None:
            params = {}

        # if the VM has a non-CDROM disk with type 'raw', abort.
        for storage_name in self.vmstorages.get_list(vm_name):
            storage = self.vmstorage.lookup(vm_name, storage_name)
            type = storage['type']
            format = storage['format']

            if type != u'cdrom' and format != u'qcow2':
                raise InvalidOperation(
                    'KCHSNAP0010E', {'vm': vm_name, 'format': format}
                )

        name = params.get('name', str(int(time.time())))

        task_params = {'vm_name': vm_name, 'name': name}
        taskid = AsyncTask(
            u'/plugins/kimchi/vms/%s/snapshots/%s' % (vm_name, name),
            self._create_task,
            task_params,
        ).id
        return self.task.lookup(taskid)

    def _create_task(self, cb, params):
        """Asynchronous function which actually creates the snapshot.

        Parameters:
        cb -- a callback function to signal the Task's progress.
        params -- a dict with the following values:
            "vm_name": the name of the VM where the snapshot will be created.
            "name": the snapshot name.
        """
        vm_name = params['vm_name']
        name = params['name']

        cb('building snapshot XML')
        root_elem = E.domainsnapshot()
        root_elem.append(E.name(name))
        xml = ET.tostring(root_elem, encoding='utf-8').decode('utf-8')

        try:
            cb('fetching snapshot domain')
            vir_dom = VMModel.get_vm(vm_name, self.conn)
            cb('creating snapshot')
            vir_dom.snapshotCreateXML(xml, 0)
        except (NotFoundError, OperationFailed, libvirt.libvirtError) as e:
            raise OperationFailed(
                'KCHSNAP0002E', {'name': name, 'vm': vm_name, 'err': str(e)}
            )

        cb('OK', True)

    def get_list(self, vm_name):
        vir_dom = VMModel.get_vm(vm_name, self.conn)

        try:
            vir_snaps = vir_dom.listAllSnapshots(0)
            return sorted([s.getName() for s in vir_snaps], key=str.lower)
        except libvirt.libvirtError as e:
            raise OperationFailed(
                'KCHSNAP0005E', {'vm': vm_name, 'err': str(e)})
Example #3
0
class VMSnapshotsModel(object):
    def __init__(self, **kargs):
        self.conn = kargs['conn']
        self.objstore = kargs['objstore']
        self.task = TaskModel(**kargs)
        self.vmstorages = VMStoragesModel(**kargs)
        self.vmstorage = VMStorageModel(**kargs)

    def create(self, vm_name, params=None):
        """Create a snapshot with the current domain state.

        The VM must be stopped and contain only disks with format 'qcow2';
        otherwise an exception will be raised.

        Parameters:
        vm_name -- the name of the VM where the snapshot will be created.
        params -- a dict with the following values:
            "name": The snapshot name (optional). If omitted, a default value
            based on the current time will be used.

        Return:
        A Task running the operation.
        """
        if params is None:
            params = {}

        # if the VM has a non-CDROM disk with type 'raw', abort.
        for storage_name in self.vmstorages.get_list(vm_name):
            storage = self.vmstorage.lookup(vm_name, storage_name)
            type = storage['type']
            format = storage['format']

            if type != u'cdrom' and format != u'qcow2':
                raise InvalidOperation('KCHSNAP0010E', {
                    'vm': vm_name,
                    'format': format
                })

        name = params.get('name', str(int(time.time())))

        task_params = {'vm_name': vm_name, 'name': name}
        taskid = AsyncTask(
            u'/plugins/kimchi/vms/%s/snapshots/%s' % (vm_name, name),
            self._create_task,
            task_params,
        ).id
        return self.task.lookup(taskid)

    def _create_task(self, cb, params):
        """Asynchronous function which actually creates the snapshot.

        Parameters:
        cb -- a callback function to signal the Task's progress.
        params -- a dict with the following values:
            "vm_name": the name of the VM where the snapshot will be created.
            "name": the snapshot name.
        """
        vm_name = params['vm_name']
        name = params['name']

        cb('building snapshot XML')
        root_elem = E.domainsnapshot()
        root_elem.append(E.name(name))
        xml = ET.tostring(root_elem, encoding='unicode')

        try:
            cb('fetching snapshot domain')
            vir_dom = VMModel.get_vm(vm_name, self.conn)
            cb('creating snapshot')
            vir_dom.snapshotCreateXML(xml, 0)
        except (NotFoundError, OperationFailed, libvirt.libvirtError) as e:
            raise OperationFailed('KCHSNAP0002E', {
                'name': name,
                'vm': vm_name,
                'err': str(e)
            })

        cb('OK', True)

    def get_list(self, vm_name):
        vir_dom = VMModel.get_vm(vm_name, self.conn)

        try:
            vir_snaps = vir_dom.listAllSnapshots(0)
            return sorted([s.getName() for s in vir_snaps], key=str.lower)
        except libvirt.libvirtError as e:
            raise OperationFailed('KCHSNAP0005E', {
                'vm': vm_name,
                'err': str(e)
            })
Example #4
0
 def __init__(self, **kargs):
     self.conn = kargs['conn']
     self.objstore = kargs['objstore']
     self.task = TaskModel(**kargs)
     self.vmstorages = VMStoragesModel(**kargs)
     self.vmstorage = VMStorageModel(**kargs)
Example #5
0
class VMSnapshotsModel(object):
    def __init__(self, **kargs):
        self.conn = kargs["conn"]
        self.objstore = kargs["objstore"]
        self.task = TaskModel(**kargs)
        self.vmstorages = VMStoragesModel(**kargs)
        self.vmstorage = VMStorageModel(**kargs)

    def create(self, vm_name, params=None):
        """Create a snapshot with the current domain state.

        The VM must be stopped and contain only disks with format 'qcow2';
        otherwise an exception will be raised.

        Parameters:
        vm_name -- the name of the VM where the snapshot will be created.
        params -- a dict with the following values:
            "name": The snapshot name (optional). If omitted, a default value
            based on the current time will be used.

        Return:
        A Task running the operation.
        """
        if params is None:
            params = {}
        vir_dom = VMModel.get_vm(vm_name, self.conn)
        if DOM_STATE_MAP[vir_dom.info()[0]] != u"shutoff":
            raise InvalidOperation("KCHSNAP0001E", {"vm": vm_name})

        # if the VM has a non-CDROM disk with type 'raw', abort.
        for storage_name in self.vmstorages.get_list(vm_name):
            storage = self.vmstorage.lookup(vm_name, storage_name)
            type = storage["type"]
            format = storage["format"]

            if type != u"cdrom" and format != u"qcow2":
                raise InvalidOperation("KCHSNAP0010E", {"vm": vm_name, "format": format})

        name = params.get("name", unicode(int(time.time())))

        task_params = {"vm_name": vm_name, "name": name}
        taskid = add_task(
            u"/plugins/kimchi/vms/%s/snapshots/%s" % (vm_name, name), self._create_task, self.objstore, task_params
        )
        return self.task.lookup(taskid)

    def _create_task(self, cb, params):
        """Asynchronous function which actually creates the snapshot.

        Parameters:
        cb -- a callback function to signal the Task's progress.
        params -- a dict with the following values:
            "vm_name": the name of the VM where the snapshot will be created.
            "name": the snapshot name.
        """
        vm_name = params["vm_name"]
        name = params["name"]

        cb("building snapshot XML")
        root_elem = E.domainsnapshot()
        root_elem.append(E.name(name))
        xml = ET.tostring(root_elem, encoding="utf-8")

        try:
            cb("fetching snapshot domain")
            vir_dom = VMModel.get_vm(vm_name, self.conn)
            cb("creating snapshot")
            vir_dom.snapshotCreateXML(xml, 0)
        except (NotFoundError, OperationFailed, libvirt.libvirtError), e:
            raise OperationFailed("KCHSNAP0002E", {"name": name, "vm": vm_name, "err": e.message})

        cb("OK", True)