Example #1
0
def attach(caller_id, storage_image_id, vm_id):
    # vm_id, img_id, destination='usb', check=True/False
    """
    Attaches selected storage disk to specified Virtual Machine. Such disk may be
    mounted to VM so that data generated by VM may be stored on it. VM also gains
    access to data already stored on that storage disk.

    @cmview_user

    @parameter{storage_image_id,int} id of block device (should be Storage type) - Disk Volume Image
    @parameter{vm_id,int} id of the VM which Storage Image should be attached to

    @response{None}
    """
    vm = VM.get(caller_id, vm_id)
    disk = StorageImage.get(caller_id, storage_image_id)

    # Check if disk is already attached to a vm
    if disk.vm:
        raise CMException('image_attached')

    disk.attach(vm)

    try:
        disk.save()
    except:
        raise CMException('storage_image_attach')
Example #2
0
def destroy(caller_id, vm_ids):
    """
    This function only destroys VM. All the cleanup (removing disk, saving,
    rescuing resources, ...) is done by hook through
    \c contextualization.update_vm method (yeah, intuitive).

    Simple sequence diagram:

    @code
            CLM        CM         CTX           Node (HOOK)
             .
            Destroy -->destroy
             |          |       (LV.destroy)
             |          |------------------------->HookScript
             .          .                          |
             .          .          ctx.update_vm<--|
             .          .           |              |
             .          .           |------------->cp
             .          .           |------------->rm
             .          .          update_resources
    @endcode

    @cmview_user
    @param_post{vm_ids,list} list of virtual machines' ids

    @response{list(dict)} VM.destroy() retval
    """
    vms = []
    for vm_id in vm_ids:
        vms.append(VM.get(caller_id, vm_id))
    return VM.destroy(vms)
Example #3
0
def attach(caller_id, iso_image_id, vm_id):
    # vm_id, img_id, destination='usb', check=True/False
    """
    Attaches specified IsoImage to specified VM. It makes possible booting
    any operating system on created VM.

    @cmview_user
    @param_post{iso_image_id,int} id of block device (should be IsoImage type)
    @param_post{vm_id,int} id of the VM which IsoImage should be attached to

    @response{None}
    """

    vm = VM.get(caller_id, vm_id)
    disk = IsoImage.get(caller_id, iso_image_id)

    # Check if disk is already attached to a vm
    if disk.vm:
        raise CMException('image_attached')

    disk.attach(vm)

    try:
        disk.save()
    except:
        raise CMException('iso_image_attach')
Example #4
0
File: vm.py Project: cc1-cloud/cc1
def destroy(caller_id, vm_ids):
    """
    This function only destroys VM. All the cleanup (removing disk, saving,
    rescuing resources, ...) is done by hook through
    \c contextualization.update_vm method (yeah, intuitive).

    Simple sequence diagram:

    @code
            CLM        CM         CTX           Node (HOOK)
             .
            Destroy -->destroy
             |          |       (LV.destroy)
             |          |------------------------->HookScript
             .          .                          |
             .          .          ctx.update_vm<--|
             .          .           |              |
             .          .           |------------->cp
             .          .           |------------->rm
             .          .          update_resources
    @endcode

    @cmview_user
    @param_post{vm_ids,list} list of virtual machines' ids

    @response{list(dict)} VM.destroy() retval
    """
    vms = []
    for vm_id in vm_ids:
        vms.append(VM.get(caller_id, vm_id))
    return VM.destroy(vms)
Example #5
0
def attach(caller_id, iso_image_id, vm_id):
    # vm_id, img_id, destination='usb', check=True/False
    """
    Attaches specified IsoImage to specified VM. It makes possible booting
    any operating system on created VM.

    @cmview_user
    @param_post{iso_image_id,int} id of block device (should be IsoImage type)
    @param_post{vm_id,int} id of the VM which IsoImage should be attached to

    @response{None}
    """

    vm = VM.get(caller_id, vm_id)
    disk = IsoImage.get(caller_id, iso_image_id)

    # Check if disk is already attached to a vm
    if disk.vm:
        raise CMException('image_attached')

    disk.attach(vm)

    try:
        disk.save()
    except:
        raise CMException('iso_image_attach')
Example #6
0
    def execute(name, user_id, vm_id, **kwargs):
        """
        Method executes command @prm{name} on the specified VM.
        User with id @prm{user_id} must be the owner of that VM.

        @parameter{name,string} name of the function to execute
        @parameter{user_id,long} id of the declared VM owner
        @parameter{vm_id,int} id of the VM on which command needs to be executed
        @parameter{kwargs,dict} keyword args for the called function

        @raises{ctx_timeout,CMException}
        @raises{ctx_execute_command,CMException}
        """
        vm = VM.get(user_id, vm_id)

        try:
            cmd = Command.add_command(name, user_id, vm_id, **kwargs)
            transaction.commit()
            log.debug(user_id, "Command state %s for machine %s" % (cmd.state, vm_id))

            dom = vm.lv_domain()
            dom.sendKey(0, 500, [113], 1, 0)

            retry = 3
            retry_factor = 1.2
            retry_time = 1
            try:
                while retry > 0:
                    log.debug(user_id, "Check if command %s is finished for machine %s" % (cmd.id, vm_id))
                    Command.objects.update()
                    cmd = Command.objects.get(id=cmd.id)
                    log.debug(user_id, "Checked command status: %s, %s, %s" % (cmd.state, command_states['finished'], bool(cmd.state == command_states['finished'])))
                    if cmd.state == command_states['finished']:
                        log.debug(user_id, "Response %s from machine %s" % (cmd.response, vm_id))
                        break
                    elif cmd.state == command_states['failed']:
                        raise CMException('ctx_' + name)
                    retry -= 1
                    retry_time *= retry_factor
                    sleep(retry_time)
            except:
                raise
            finally:
                cmd.delete()

            if retry == 0:
                log.debug(user_id, "Command %s for machine %s - TIMEOUT" % (name, vm_id))
                raise CMException('ctx_timeout')

            return cmd.response or ''
        except CMException:
            raise
        except Exception:
            log.exception(user_id, 'Execute command')
            raise CMException('ctx_execute_command')
Example #7
0
File: vm.py Project: cc1-cloud/cc1
def get_by_id(caller_id, vm_id):
    """
    Returns requested caller's VM.

    @cmview_user
    @param_post{vm_id,int} id of the requested VM

    @response{dict} VM.dict property of the requested VM
    """
    vm = VM.get(caller_id, vm_id)
    vm_mod = vm.long_dict
    return vm_mod
Example #8
0
def get_by_id(caller_id, vm_id):
    """
    Returns requested caller's VM.

    @cmview_user
    @param_post{vm_id,int} id of the requested VM

    @response{dict} VM.dict property of the requested VM
    """
    vm = VM.get(caller_id, vm_id)
    vm_mod = vm.long_dict
    return vm_mod
Example #9
0
File: vm.py Project: cloudcache/cc1
def get_by_id(caller_id, vm_id):
    """
    Returns requested caller's VM.
    @cmview_user

    @parameter{vm_id,int} id of the requested VM

    @response{dict} VM's extended info
    """
    vm = VM.get(caller_id, vm_id)
    vm_mod = vm.long_dict
    return vm_mod
Example #10
0
def detach_vnc(caller_id, vm_id):
    """
    Detaches VNC redirection from VM.

    @cmview_user
    @param_post{vm_id,int} id of the VM to have detached VM redirection
    """
    vm = VM.get(caller_id, vm_id)
    vm.detach_vnc()

    try:
        vm.save()
    except:
        raise CMException('vnc_detach')
Example #11
0
File: vm.py Project: cc1-cloud/cc1
def detach_vnc(caller_id, vm_id):
    """
    Detaches VNC redirection from VM.

    @cmview_user
    @param_post{vm_id,int} id of the VM to have detached VM redirection
    """
    vm = VM.get(caller_id, vm_id)
    vm.detach_vnc()

    try:
        vm.save()
    except:
        raise CMException('vnc_detach')
Example #12
0
def edit(caller_id, vm_id, name, description):
    """
    Updates VM's attributes.

    @cmview_user
    @param_post{vm_id,int} id of the VM to edit
    @param_post{name,string}
    @param_post{description,string}

    @response{src.cm.views.utils.image.edit()}
    """
    vm = VM.get(caller_id, vm_id)

    vm.name = name
    vm.description = description
    vm.save(update_fields=['name', 'description'])
Example #13
0
def reset(caller_id, vm_ids):
    """
    Safely restarts selected callers VMs

    @cmview_user
    @param_post{vm_ids,list(int)} ids of the VMs to restart

    @response{src.cm.views.utils.image.restart()}
    """

    # get to check permissions on vms
    vms = []
    for vm_id in vm_ids:
        vms.append(VM.get(caller_id, vm_id))

    return VM.reset(vms)
Example #14
0
File: vm.py Project: cc1-cloud/cc1
def edit(caller_id, vm_id, name, description):
    """
    Updates VM's attributes.

    @cmview_user
    @param_post{vm_id,int} id of the VM to edit
    @param_post{name,string}
    @param_post{description,string}

    @response{src.cm.views.utils.image.edit()}
    """
    vm = VM.get(caller_id, vm_id)

    vm.name = name
    vm.description = description
    vm.save(update_fields=['name', 'description'])
Example #15
0
File: vm.py Project: cloudcache/cc1
def attach_vnc(caller_id, vm_id):
    """
    Attaches VNC redirection to VM.
    @cmview_user

    @parameter{vm_id,int} id of the VM to have attached VM redirection

    @response{None}
    """
    vm = VM.get(caller_id, vm_id)
    vm.attach_vnc()

    try:
        vm.save()
    except:
        raise CMException('vnc_attach')
Example #16
0
File: vm.py Project: cc1-cloud/cc1
def reset(caller_id, vm_ids):
    """
    Safely restarts selected callers VMs

    @cmview_user
    @param_post{vm_ids,list(int)} ids of the VMs to restart

    @response{src.cm.views.utils.image.restart()}
    """

    # get to check permissions on vms
    vms = []
    for vm_id in vm_ids:
        vms.append(VM.get(caller_id, vm_id))

    return VM.reset(vms)
Example #17
0
File: vm.py Project: cloudcache/cc1
def edit(caller_id, vm_id, name, description):
    """
    Changes selected VMs' parameters.
    Current should be get by src.cm.views.user.vm.get_by_id().
    @cmview_user

    @parameter{vm_id,int} id of the VM to edit
    @parameter{name,string}
    @parameter{description,string}

    @response{src.cm.views.utils.image.edit()}
    """
    vm = VM.get(caller_id, vm_id)

    vm.name = name
    vm.description = description
    vm.save(update_fields=['name', 'description'])
Example #18
0
File: vm.py Project: cc1-cloud/cc1
def save_and_shutdown(caller_id, vm_id, name, description):
    """
    Calls VM.save_and_shutdown() on specified VM

    @cmview_user
    @param_post{vm_id,int} id of the VM to save and shutdown.
    @param_post{name,string} name of the new SystemImage VM should be saved to
    @param_post{description,string} description of the new SystemImage VM
    should be saved to
    """
    user = User.get(caller_id)
    vm = VM.get(caller_id, vm_id)

    if user.used_storage + vm.system_image.size > user.storage:
        raise CMException('user_storage_limit')

    VM.save_and_shutdown(caller_id, vm, name, description)
Example #19
0
def detach(caller_id, storage_image_id, vm_id):
    """
    Detaches specified StorageImage from specified VM.

    @cmview_user
    @param_post{vm_id,int} id of the VM StorageImage should be detached from
    @param_post{storage_image_id,int} id of the StorageImage to detach
    """
    vm = VM.get(caller_id, vm_id)
    disk = StorageImage.get(caller_id, storage_image_id)

    disk.detach(vm)

    try:
        disk.save()
    except:
        raise CMException('storage_image_attach')
Example #20
0
File: vm.py Project: cloudcache/cc1
def save_and_shutdown(caller_id, vm_id, name, description):
    """
    Calls src.cm.views.utils.image.save_and_shutdown() for the VM selected.

    @cmview_user

    @parameter{vm_id,int} id of the VM to save and shutdown.
    @parameter{name,string}
    @parameter{description,string}
    """
    user = User.get(caller_id)
    vm = VM.get(caller_id, vm_id)

    if user.used_storage + vm.system_image.size > user.storage:
        raise CMException('user_storage_limit')

    VM.save_and_shutdown(caller_id, vm, name, description)
Example #21
0
def save_and_shutdown(caller_id, vm_id, name, description):
    """
    Calls VM.save_and_shutdown() on specified VM

    @cmview_user
    @param_post{vm_id,int} id of the VM to save and shutdown.
    @param_post{name,string} name of the new SystemImage VM should be saved to
    @param_post{description,string} description of the new SystemImage VM
    should be saved to
    """
    user = User.get(caller_id)
    vm = VM.get(caller_id, vm_id)

    if user.used_storage + vm.system_image.size > user.storage:
        raise CMException('user_storage_limit')

    VM.save_and_shutdown(caller_id, vm, name, description)
Example #22
0
def detach(caller_id, iso_image_id, vm_id):
    """
    Detaches specified IsoImage from specified VM.

    @cmview_user
    @param_post{iso_image_id,int} id of the IsoImage to detach
    @param_post{vm_id,int} id of the VM from which IsoImage should be detached

    @response{None}
    """
    vm = VM.get(caller_id, vm_id)
    disk = IsoImage.get(caller_id, iso_image_id)

    disk.detach(vm)

    try:
        disk.save()
    except:
        raise CMException('iso_image_attach')
Example #23
0
def detach(caller_id, storage_image_id, vm_id):
    """
    Detaches specified storage disk from specified VM.
    @cmview_user

    @parameter{vm_id,int} id of the VM from which to detach Image
    @parameter{storage_image_id,int} id of the Storage Image to detach - Disk Volume Image

    @response{None}
    """
    vm = VM.get(caller_id, vm_id)
    disk = StorageImage.get(caller_id, storage_image_id)

    disk.detach(vm)

    try:
        disk.save()
    except:
        raise CMException('storage_image_attach')
Example #24
0
def detach(caller_id, iso_image_id, vm_id):
    """
    Detaches specified IsoImage from specified VM.

    @cmview_user
    @param_post{iso_image_id,int} id of the IsoImage to detach
    @param_post{vm_id,int} id of the VM from which IsoImage should be detached

    @response{None}
    """
    vm = VM.get(caller_id, vm_id)
    disk = IsoImage.get(caller_id, iso_image_id)

    disk.detach(vm)

    try:
        disk.save()
    except:
        raise CMException('iso_image_attach')
Example #25
0
def attach(caller_id, storage_image_id, vm_id):
    # vm_id, img_id, destination='usb', check=True/False
    """
    Attaches selected StorageImage to specified VM. Such a disk may be mounted
    to VM so that data generated by VM could be stored on it. VM also has
    access to data already stored on that attached StorageImage.

    @cmview_user
    @param_post{storage_image_id,int} id of a StorageImage block device - Disk Volume Image
    @param_post{vm_id,int} id of the VM which StorageImage should be attached to
    """
    vm = VM.get(caller_id, vm_id)
    disk = StorageImage.get(caller_id, storage_image_id)

    # Check if disk is already attached to a vm
    if disk.vm:
        raise CMException('image_attached')

    disk.attach(vm)

    try:
        disk.save()
    except:
        raise CMException('storage_image_attach')
Example #26
0
    def execute(name, user_id, vm_id, **kwargs):
        """
        Method executes command @prm{name} on the specified VM.
        User with id @prm{user_id} must be the owner of that VM.

        @parameter{name,string} name of the function to execute
        @parameter{user_id,long} id of the declared VM owner
        @parameter{vm_id,int} id of the VM on which command needs to be executed
        @parameter{kwargs,dict} keyword args for the called function

        @raises{ctx_timeout,CMException}
        @raises{ctx_execute_command,CMException}
        """
        vm = VM.get(user_id, vm_id)

        try:
            cmd = Command.add_command(name, user_id, vm_id, **kwargs)
            transaction.commit()
            log.debug(user_id,
                      "Command state %s for machine %s" % (cmd.state, vm_id))

            dom = vm.lv_domain()
            dom.sendKey(0, 500, [113], 1, 0)

            retry = 3
            retry_factor = 1.2
            retry_time = 1
            try:
                while retry > 0:
                    log.debug(
                        user_id,
                        "Check if command %s is finished for machine %s" %
                        (cmd.id, vm_id))
                    Command.objects.update()
                    cmd = Command.objects.get(id=cmd.id)
                    log.debug(
                        user_id, "Checked command status: %s, %s, %s" %
                        (cmd.state, command_states['finished'],
                         bool(cmd.state == command_states['finished'])))
                    if cmd.state == command_states['finished']:
                        log.debug(
                            user_id, "Response %s from machine %s" %
                            (cmd.response, vm_id))
                        break
                    elif cmd.state == command_states['failed']:
                        raise CMException('ctx_' + name)
                    retry -= 1
                    retry_time *= retry_factor
                    sleep(retry_time)
            except:
                raise
            finally:
                cmd.delete()

            if retry == 0:
                log.debug(
                    user_id,
                    "Command %s for machine %s - TIMEOUT" % (name, vm_id))
                raise CMException('ctx_timeout')

            return cmd.response or ''
        except CMException:
            raise
        except Exception:
            log.exception(user_id, 'Execute command')
            raise CMException('ctx_execute_command')