Пример #1
0
def disconnect_nic(pod_num: str, vmname: str, nic_num: int):
    esxi_content, esxi_connector = vsphere_connect()
    auth = request.authorization
    for the_pod in pods:
        if the_pod['pod_number'] == pod_num and the_pod[
                'username'] == auth.username:
            for vm in the_pod['vms']:
                if vm['vmname'] == vmname:
                    vm = vmware_vcenter.get_vm(esxi_content, vmname)
                    vmware_vcenter.disconnect_network_adapter(vm)
                    name = get_human_readable_vm_name(pod_num=pod_num,
                                                      vmname=vmname)
                    update_status_text = "Disconnecting NIC for %s" % name
                    log.info(
                        f"Disconnecting NIC for VM {name} in pod {pod_num}.")
                    return render_template(
                        "update_status.html",
                        pod_num=pod_num,
                        vmname=vmname,
                        nic_num=nic_num,
                        update_status_text=update_status_text)
    # Return to index if invalid pod number, username, or vmname is referenced.
    log.info(
        f"Attempt to disconnect NIC for {vmname} in pod {pod_num} failed.  Rendering redirect URL."
    )
    return render_template("none_shall_pass.html")
Пример #2
0
def set_portgroup(pod_num: str, vmname: str, portgroup: str):
    esxi_content, esxi_connector = vsphere_connect()
    task_list = []
    auth = request.authorization
    for the_pod in pods:
        if the_pod['pod_number'] == pod_num and the_pod[
                'username'] == auth.username:
            for vm in the_pod['vms']:
                if vm['vmname'] == vmname:
                    for pg in vm['portgroup_options']:
                        if pg['portgroup'] == portgroup:
                            vm = vmware_vcenter.get_vm(esxi_content, vmname)
                            new_portgroup = vmware_vcenter.get_portgroup(
                                esxi_content, portgroup)
                            vmware_vcenter.change_vm_adapter_portgroup(
                                vm,
                                idx=0,
                                new_portgroup=new_portgroup,
                                disable_adapter_before_change=True,
                                tasks=task_list)
                            name = get_human_readable_vm_name(pod_num=pod_num,
                                                              vmname=vmname)
                            portgroup_name = get_human_readable_portgroup_name(
                                pod_num=pod_num,
                                vmname=vmname,
                                portgroup=portgroup)
                            update_status_text = "Moving %s NIC to %s" % (
                                name, portgroup_name)
                            log.info(
                                f"Setting portgroup {portgroup_name} for VM {name}."
                            )
                            return render_template(
                                "update_status.html",
                                pod_num=pod_num,
                                vmname=vmname,
                                portgroup=portgroup,
                                update_status_text=update_status_text)
    # Return to index if invalid pod number, username, or vmname is referenced.
    log.info(
        f"Attempt to change portgroup for {vmname} in pod {pod_num} to {portgroup} failed.  "
        f"Rendering redirect URL.")
    return render_template("none_shall_pass.html")
Пример #3
0
def update_vms(esxi_content: classmethod, vms: dict) -> dict:
    # Populate/Update vms with current information.
    for vm in vms:
        # Get the object that matches the name of this vm.
        vm_object = vmware_vcenter.get_vm(esxi_content, vm['vmname'])
        # Get this vm's current power state and update the dictionary.
        vm['power_status'] = vmware_vcenter.get_vm_status(vm_object)
        # Get the vm's currently assigned portgroup and update the dictionary.
        try:
            network_adapter = vmware_vcenter.get_vm_network_adapters(vm_object)
            first_nic = 0
            vm['portgroup'] = network_adapter[first_nic].name
        except:
            log.info(f"No NIC attached to {vm['vmname']}")
        # Get the Status (Connected/Disconnected) of the NIC connected to the aforementioned portgroup.
        if 'portgroup_options' in vm or 'nic_status' in vm:
            status = vmware_vcenter.get_vm_network_adapter_status(vm_object)
            if status:
                vm['nic_status'] = 'Connected'
            else:
                vm['nic_status'] = 'Disconnected'
    log.info("Updated 'vms' dict with current info from vCenter.")
    return vms
Пример #4
0
def poweron(pod_num: str, vmname: str):
    esxi_content, esxi_connector = vsphere_connect()
    auth = request.authorization
    for the_pod in pods:
        if the_pod['pod_number'] == pod_num and the_pod[
                'username'] == auth.username:
            for vm in the_pod['vms']:
                if vm['vmname'] == vmname:
                    vm = vmware_vcenter.get_vm(esxi_content, vmname)
                    vmware_vcenter.power_on_vm(vm)
                    name = get_human_readable_vm_name(pod_num=pod_num,
                                                      vmname=vmname)
                    update_status_text = "Powering on %s" % name
                    log.info(f"Powering on {name} for pod {pod_num}.")
                    return render_template(
                        "update_status.html",
                        pod_num=pod_num,
                        vmname=vmname,
                        update_status_text=update_status_text)
    # Return to index if invalid pod number, username, or vmname is referenced.
    log.info(
        f"Attempt to power off {vmname} for {auth.username} in pod {pod_num} failed.  Rendering redirect URL."
    )
    return render_template("none_shall_pass.html")