Ejemplo n.º 1
0
def test_session(disconnect, connect):
    connection(
        vcdriver_username='******', vcdriver_password='******',
        vcdriver_host='something', vcdriver_port='something'
    )
    connection(
        vcdriver_username='******', vcdriver_password='******',
        vcdriver_host='something', vcdriver_port='something'
    )
    id()
    close()
    close()
    assert connect.call_count == 1
    assert disconnect.call_count == 1
Ejemplo n.º 2
0
 def refresh(self):
     """ Close session and create a new session """
     if self._vm_object:
         close()
         # Refresh object with updated data (connection id changed)
         self._vm_object = get_vcenter_object_by_name(
             connection(), vim.VirtualMachine, self.name)
Ejemplo n.º 3
0
 def create(self, **kwargs):
     """ Create the virtual machine and update the vm object """
     conn = connection()
     if not self._vm_object:
         data_store_name = kwargs['vcdriver_data_store']
         data_store = get_vcenter_object_by_name(conn, vim.Datastore,
                                                 data_store_name)
         capacity = float(data_store.summary.capacity)
         free_space = float(data_store.summary.freeSpace)
         free_percentage = 100 * free_space / capacity
         threshold = kwargs['vcdriver_data_store_threshold']
         if free_percentage < float(threshold):
             raise NotEnoughDiskSpace(data_store_name, threshold,
                                      free_percentage)
         self._vm_object = wait_for_vcenter_task(
             get_vcenter_object_by_name(
                 conn, vim.VirtualMachine, self.template).CloneVM_Task(
                     folder=get_vcenter_object_by_name(
                         conn, vim.Folder, kwargs['vcdriver_folder']),
                     name=self.name,
                     spec=vim.vm.CloneSpec(location=vim.vm.RelocateSpec(
                         datastore=data_store,
                         pool=get_vcenter_object_by_name(
                             conn, vim.ResourcePool,
                             kwargs['vcdriver_resource_pool'])),
                                           powerOn=True,
                                           template=False)),
             'Create virtual machine "{}" from template "{}"'.format(
                 self.name, self.template), self.timeout)
Ejemplo n.º 4
0
    def _schedule_vcenter_task_on_vm(self, task, task_name, delay_by):
        """
        :param task: A vcenter task method
        :param task_description: The task description
        :param delay_by: if specified, it has to be a timedelta that indicates
            when in the future this task will be executed.
        """

        if not isinstance(delay_by, datetime.timedelta):
            raise TypeError(
                "Invalid type for delay_by. Expected datetime.timedelta.")

        spec = vim.scheduler.ScheduledTaskSpec()
        spec.name = task_name
        spec.description = ""
        spec.scheduler = vim.scheduler.OnceTaskScheduler()
        # it seems that CreateScheduledTask will add timezone without
        # converting it, so if you use utcnow here you may end up to
        # scheduling 1 hour in the past when BST is active
        spec.scheduler.runAt = datetime.datetime.now() + delay_by
        spec.action = vim.action.MethodAction()
        spec.action.name = task
        spec.enabled = True

        conn = connection()
        conn.content.scheduledTaskManager.CreateScheduledTask(
            self._vm_object, spec)
Ejemplo n.º 5
0
def get_all_virtual_machines():
    """
    Get all the virtual machines from your Vcenter Instance.
    It will update both the internal _vm_object and the name.

    :return: A list with all the VirtualMachine objects
    """
    machines = []
    for vm_object in get_all_vcenter_objects(connection(), vim.VirtualMachine):
        machine = VirtualMachine()
        machine.__setattr__('_vm_object', vm_object)
        machine.name = vm_object.summary.config.name
        machines.append(machine)
    return machines
Ejemplo n.º 6
0
def destroy_virtual_machines(folder_name, timeout=600):
    """
    Destroy all the virtual machines in the folder with the given name
    :param folder_name: The folder name
    :param timeout: The timeout for vcenter tasks in seconds

    :return: A list with the destroyed vms
    """
    folder = get_vcenter_object_by_name(connection(), vim.Folder, folder_name)
    destroyed_vms = []
    for entity in folder.childEntity:
        if isinstance(entity, vim.VirtualMachine):
            vm = VirtualMachine(name=entity.summary.config.name,
                                timeout=timeout)
            vm.__setattr__('_vm_object', entity)
            vm.destroy()
            destroyed_vms.append(vm)
    return destroyed_vms
Ejemplo n.º 7
0
 def find(self):
     """ Find and update the vm object based on the name """
     if not self._vm_object:
         self._vm_object = get_vcenter_object_by_name(
             connection(), vim.VirtualMachine, self.name)