Ejemplo n.º 1
0
def vmware_details_server_tab(request, obj_id):
    """
    Renders the VMWare Info tab in the Server view

    :param request: the HTTP request
    :param obj_id: the server ID
    :return: a rendered object
    """
    server = Server.objects.get(id=obj_id)

    si = get_vmware_service_instance(server.resource_handler)
    vm = get_vm_by_uuid(si, server.resource_handler_svr_id)

    assert isinstance(vm, pyVmomi.vim.VirtualMachine)

    # You can pass basically anything from the pyvmomi vm object here into the template
    # Either as raw api result (vm.config.version) or after a modification/lookup (vm.guest.toolsRunningStatus)

    conv = {
        'guestToolsRunning':
        'VMware Tools is running.',
        'guestToolsNotRunning':
        'VMware Tools is not running.',
        'guestToolsExecutingScripts':
        'VMware Tools is starting.',
        'guestToolsBlacklisted':
        'VMware Tools is installed, but should be upgraded immediately due to a known bug',
        'guestToolsUnmanaged':
        'VMware Tools is installed, but it is not managed by VMWare. Probably open-vm-tools',
        'guestToolsNeedUpgrade':
        'VMware Tools is installed, but the version is not current.',
        'guestToolsSupportedOld':
        'VMware Tools is installed, supported, but a newer version is available.',
        'guestToolsTooOld':
        'VMware Tools is installed, but the version is too old.',
        'guestToolsTooNew':
        'VMware Tools is installed, but the version is too new for this virtual machine.',
        'guestToolsNotInstalled':
        'VMware Tools has never been installed.',
        'guestToolsSupportedNew':
        'VMware Tools is installed, supported, and newer than the version on the host.',
        'guestToolsCurrent':
        'VMware Tools is installed, and the version is current.'
    }

    vm_details = {
        'vmx_version':
        vm.config.version,
        'vmtools_status':
        conv.get(vm.guest.toolsRunningStatus,
                 'Unknown ({})'.format(vm.guest.toolsRunningStatus)),
        'vmtools_version':
        conv.get(vm.guest.toolsVersionStatus2,
                 'Unknown ({})'.format(vm.guest.toolsVersionStatus2))
    }

    return render(request, 'vmware_details/templates/vmware_tab.html',
                  dict(server=server, vm_details=vm_details))
Ejemplo n.º 2
0
def run(job, logger=None, server=None, **kwargs):

    si = None
    for server in job.server_set.all():
        if not si:
            si = get_vmware_service_instance(server.resource_handler.cast())
        vm = get_vm_by_uuid(si, server.resource_handler_svr_id)

        assert isinstance(vm, pyVmomi.vim.VirtualMachine)

        server.refresh_info()

        server_original_power_status = server.power_status
        set_progress("Performing VM hard reset...")
        task = vm.ResetVM_Task()
        wait_for_tasks(si, [task])

    return "", "", ""
def run(job, logger=None, server=None, **kwargs):

    si = None
    for server in job.server_set.all():
        if not si:
            si = get_vmware_service_instance(server.resource_handler.cast())
        vm = get_vm_by_uuid(si, server.resource_handler_svr_id)

        assert isinstance(vm, pyVmomi.vim.VirtualMachine)

        server.refresh_info()

        server_original_power_status = server.power_status
        set_progress("Performing VM power down...")
        task = vm.PowerOffVM_Task()
        wait_for_tasks(si, [task])

    return "", "", ""
Ejemplo n.º 4
0
def run(job, logger=None, **kwargs):
    server = job.server_set.first()
    rh = server.resource_handler.cast()

    # Connect to RH
    si = get_vmware_service_instance(rh)
    vm = get_vm_by_uuid(si, server.resource_handler_svr_id)
    assert isinstance(vm, pyVmomi.vim.VirtualMachine)

    # Set the VM annotation
    set_progress("Updating new virtual machine annotation")
    annotation = str('{{ append_annotation }}')
    annotation = server.notes + annotation
    assert isinstance(vm, pyVmomi.vim.VirtualMachine)
    configSpec = pyVmomi.vim.vm.ConfigSpec()
    configSpec.annotation = annotation
    vm.ReconfigVM_Task(configSpec)

    set_progress("Updating server info from VMware")
    time.sleep(15)
    server.refresh_info()

    return "", "", ""
Ejemplo n.º 5
0
def run(job, logger=None, **kwargs):
    server = job.server_set.first()
    rh = server.resource_handler.cast()
    group = server.group
    env = server.environment
    owner = job.owner
    new_name = str('{{ clone_name }}')
    do_linked_clone = True if '{{ linked_clone }}' == 'True' else False

    # Connect to RH
    si = get_vmware_service_instance(rh)
    vm = get_vm_by_uuid(si, server.resource_handler_svr_id)
    assert isinstance(vm, pyVmomi.vim.VirtualMachine)

    # Define the location, empty defaults to same location as the source vm
    set_progress("Generating VMware Clone Config")
    relocate_spec = pyVmomi.vim.vm.RelocateSpec()

    # Linked clone
    if do_linked_clone:
        set_progress('Clones as "Linked Clone"')
        relocate_spec.diskMoveType = 'createNewChildDiskBacking'

    # Define the clone config specs
    cloneSpec = pyVmomi.vim.vm.CloneSpec(powerOn=False,
                                         template=False,
                                         snapshot=None,
                                         location=relocate_spec)

    # Clone the Virtual Machine with provided specs
    set_progress("Cloning {} to {}".format(server.hostname, new_name))
    clone_task = vm.Clone(name=new_name, folder=vm.parent, spec=cloneSpec)

    # Wait for completion and get the new vm uuid
    uuid = check_task(si, clone_task)

    # Set the new VM annotation
    set_progress("Updating new virtual machine annotation")
    clone_add_date = datetime.datetime.now()
    annotation = 'Cloned by {} using CloudBolt on {} [Job ID={}]'.format(
        owner, clone_add_date, job.id)
    new_vm = get_vm_by_uuid(si, uuid)
    assert isinstance(new_vm, pyVmomi.vim.VirtualMachine)
    configSpec = pyVmomi.vim.vm.ConfigSpec()
    configSpec.annotation = annotation
    new_vm.ReconfigVM_Task(configSpec)

    # Sync the cloned VM to CloudBolt
    # TODO: change this to just Server.objects.create() so that we can have the first history
    # event for the server say it was created by this job, rather than discovered by sync VMs
    vm = {}
    vm['hostname'] = new_name
    vm['uuid'] = uuid
    vm['power_status'] = 'POWEROFF'
    sync_class = SyncVMsClass()
    (newserver, status, errors) = sync_class.import_vm(vm, rh, group, env,
                                                       owner)
    if newserver:
        set_progress("Adding server to job")
        job.server_set.add(newserver)
        set_progress("Updating server info from VMware")
        newserver.refresh_info()

    if errors:
        return (
            "WARNING",
            "The clone task completed, but the new server could not be detected",
            errors)

    return "", "", ""
def run(job, logger=None, server=None, **kwargs):

    si = None
    for server in job.server_set.all():
        if not si:
            si = get_vmware_service_instance(server.resource_handler.cast())
        vm = get_vm_by_uuid(si, server.resource_handler_svr_id)

        assert isinstance(vm, pyVmomi.vim.VirtualMachine)

        if vm.config.version == "vmx-08":
            set_progress("Hardware version already updated. Nothing to do.")
            continue

        server.refresh_info()

        server_original_power_status = server.power_status
        if server_original_power_status != "POWERON":
            set_progress(
                "Server is off. Turning it on to upgrade VMware Tools.")
            # Make sure VM is powered on
            task = vm.PowerOnVM_Task()
            wait_for_tasks(si, [task])

        set_progress("Upgrading VMware Tools")
        # Upgrade VMware tools
        try:
            task = vm.UpgradeTools_Task()
            wait_for_tasks(si, [task])
        except:
            set_progress(
                "Cannot upgrade VM tools. Will still try to upgrade hardware version. "
            )
            pass

        # Power off VM for hw upgrade
        set_progress("Powering off server to upgrade HW version")
        task = vm.PowerOffVM_Task()
        wait_for_tasks(si, [task])

        # Snapshot VM
        set_progress("Creating snapshot")
        #server.resource_handler.cast().create_snapshot(server, "version4hw-{}".format(time.time()), "Pre Hardware Upgrade Snapshot")

        task = vm.CreateSnapshot_Task("version4hw-{}".format(time.time()),
                                      "Pre Hardware Upgrade Snapshot", False,
                                      True)
        wait_for_tasks(si, [task])

        failure_msg = ""

        # Upgrade VM
        try:
            set_progress("Updating HW version")
            task = vm.UpgradeVM_Task(version="vmx-08")
            wait_for_tasks(si, [task])
        except:
            failure_msg = "Failed to upgrade hardware version"
            set_progress(
                "{}. Will now return VM to original power state.".format(
                    failure_msg))
            pass

        if server_original_power_status == "POWERON":
            set_progress("Server was originally on, so power it on again")
            task = vm.PowerOnVM_Task()
            wait_for_tasks(si, [task])

        if failure_msg:
            return "FAILURE", "", failure_msg

        return "", "", ""

    return "", "", ""
def run(job, logger=None, server=None, **kwargs):

    si = None
    for server in job.server_set.all():
        if not si:
            si = get_vmware_service_instance(server.resource_handler.cast())
        vm = get_vm_by_uuid(si, server.resource_handler_svr_id)
    
        assert isinstance(vm, pyVmomi.vim.VirtualMachine)
        
        if vm.config.version == "vmx-08":
            set_progress("Hardware version already updated. Nothing to do.")
            continue
        
        server.refresh_info()

        server_original_power_status = server.power_status
        if server_original_power_status != "POWERON":
            set_progress("Server is off. Turning it on to upgrade VMware Tools.")
            # Make sure VM is powered on
            task = vm.PowerOnVM_Task()
            wait_for_tasks(si, [task])

        set_progress("Upgrading VMware Tools")
        # Upgrade VMware tools
        try:
            task = vm.UpgradeTools_Task()
            wait_for_tasks(si, [task])
        except:
            set_progress("Cannot upgrade VM tools. Will still try to upgrade hardware version. ")
            pass
            
        
        # Power off VM for hw upgrade
        set_progress("Powering off server to upgrade HW version")
        task = vm.PowerOffVM_Task()
        wait_for_tasks(si, [task])

        # Snapshot VM
        set_progress("Creating snapshot")
        #server.resource_handler.cast().create_snapshot(server, "version4hw-{}".format(time.time()), "Pre Hardware Upgrade Snapshot")
        
        task = vm.CreateSnapshot_Task("version4hw-{}".format(time.time()), "Pre Hardware Upgrade Snapshot", False, True)
        wait_for_tasks(si, [task])
        
        failure_msg = ""

        # Upgrade VM
        try:
            set_progress("Updating HW version")
            task = vm.UpgradeVM_Task(version="vmx-08")
            wait_for_tasks(si, [task])
        except:
            failure_msg = "Failed to upgrade hardware version"
            set_progress("{}. Will now return VM to original power state.".format(failure_msg))
            pass
        
        if server_original_power_status == "POWERON":
            set_progress("Server was originally on, so power it on again")
            task = vm.PowerOnVM_Task()
            wait_for_tasks(si, [task])
        
        if failure_msg:
            return "FAILURE","",failure_msg
        
        return "","",""

    return "", "", ""