Exemple #1
0
def delete(oquery, ovirt=None):
    """
    Delete VMs

    :param str oquery: a query string to select VMs to delete
    :param ovirtsdk.api.API ovirt: a connected oVirt API object

    :returns: How many VMs were deleted
    :rtype: int
    """
    vms = ovirt.vms.list(query=oquery)
    for vm in vms:
        vm.delete(action=oVirtParams.Action(async=False))
    puts('{0} VMs deleted'.format(len(vms)))
    return len(vms)
Exemple #2
0
def delete(oquery, ovirt=None):
    """
    Delete templates

    :param str oquery: a query string to select tempaltes to delete
    :param ovirtsdk.api.API ovirt: a connected oVirt API object

    :returns: How many templates were deleted
    :rtype: int
    """
    templates = ovirt.templates.list(query=oquery)
    for template in templates:
        template.delete(async=False)
    puts('{0} templates deleted'.format(len(templates)))
    return len(templates)
Exemple #3
0
def delete(oquery, ovirt=None):
    """
    Delete VMs

    :param str oquery: a query string to select VMs to delete
    :param ovirtsdk.api.API ovirt: a connected oVirt API object

    :returns: How many VMs were deleted
    :rtype: int
    """
    vms = ovirt.vms.list(query=oquery)
    for vm in vms:
        vm.delete(action=oVirtParams.Action(async=False))
    puts('{0} VMs deleted'.format(len(vms)))
    return len(vms)
Exemple #4
0
def remove_disk(vm_id, disk_id=None, disk_name=None, erase='no', ovirt=None):
    """
    Remove the specified disk from the specified VM

    :param str vm_id:      The ID of the VM to add the disk to
    :param str disk_id:    The Id of the disk to remove
    :param str disk_name`: The name of the disk to remove
    :param str erase:      'yes' to erase the removed disks from the system,
                           anything else to leave them detached. default is
                           'no'
    :param oVirtApi ovirt: An open oVirt API connection

    One of disk_id or disk_name must be specified, if both are specified,
    disk_id will be used
    """
    vm = ovirt.vms.get(id=vm_id)
    if vm is None:
        abort("VM with specified ID '{0}' not found".format(vm_id))
    if disk_id is not None:
        disk = vm.disks.get(id=disk_id)
    elif disk_name is not None:
        disk = vm.disks.get(name=disk_name)
    else:
        abort('Niether disk_id nor disk_name specified')
    if not disk:
        abort("Disk with specified ID or name not found")
    if disk.active:
        puts("Deactivating disk: {0}".format(disk.name))
        disk.deactivate(oVirtParams.Action(async=False))
    if erase == 'yes':
        puts("Erasing disk: {0}".format(disk.name))
        disk.delete(oVirtParams.Action(detach=False, async=False))
    else:
        puts("Detaching disk: {0}".format(disk.name))
        disk.delete(oVirtParams.Action(detach=True, async=False))
Exemple #5
0
def remove_disk(vm_id, disk_id=None, disk_name=None, erase='no', ovirt=None):
    """
    Remove the specified disk from the specified VM

    :param str vm_id:      The ID of the VM to add the disk to
    :param str disk_id:    The Id of the disk to remove
    :param str disk_name`: The name of the disk to remove
    :param str erase:      'yes' to erase the removed disks from the system,
                           anything else to leave them detached. default is
                           'no'
    :param oVirtApi ovirt: An open oVirt API connection

    One of disk_id or disk_name must be specified, if both are specified,
    disk_id will be used
    """
    vm = ovirt.vms.get(id=vm_id)
    if vm is None:
        abort("VM with specified ID '{0}' not found".format(vm_id))
    if disk_id is not None:
        disk = vm.disks.get(id=disk_id)
    elif disk_name is not None:
        disk = vm.disks.get(name=disk_name)
    else:
        abort('Niether disk_id nor disk_name specified')
    if not disk:
        abort("Disk with specified ID or name not found")
    if disk.active:
        puts("Deactivating disk: {0}".format(disk.name))
        disk.deactivate(oVirtParams.Action(async=False))
    if erase == 'yes':
        puts("Erasing disk: {0}".format(disk.name))
        disk.delete(oVirtParams.Action(detach=False, async=False))
    else:
        puts("Detaching disk: {0}".format(disk.name))
        disk.delete(oVirtParams.Action(detach=True, async=False))
Exemple #6
0
    def print_table(self, obj_list, show=None, headers='yes'):
        """
        Print a table of oVirt objects of the type from the passed list

        :param iterable obj_list: The list of objects to print as a talbe
        :param str show: A colon (:) separated list of object fields to show,
                         will fallback to self.default_fields if unspecified

        See the 'query' task for description of other parameters
        """
        show = show or self.default_fields
        format_str = ' '.join(
            '{{{0}:{1}}}'.format(field, max(self.fields[field], len(field)))
            for field in show.split(':')
            if field in self.fields
        )
        if headers == 'yes':
            puts(format_str.format(
                **dict((field, field.upper()) for field in self.fields)
            ).rstrip())
        for obj in obj_list:
            puts(format_str.format(
                **dict((field, getattr(obj, field)) for field in self.fields)
            ).rstrip())