Пример #1
0
def main():
    args = get_args(__doc__, __version__, 'vm_power.log')
    server = script_setup(args=args, script_info=(__file__, __version__))

    operation = str(input("Enter the power operation you wish to perform"
                          " [on | off | reset | suspend]: "))
    attempt_guest = ask_question("Attempt to use guest OS operations, "
                                 "if available? ")

    if ask_question("Multiple VMs? ", default="yes"):
        folder, folder_name = resolve_path(server, "folder", "with VMs")
        vms = [VM(vm=x) for x in folder.childEntity if is_vm(x)]
        logging.info("Found %d VMs in folder '%s'", len(vms), folder_name)
        if ask_question("Show the status of the VMs in the folder? "):
            logging.info("Folder structure: \n%s", format_structure(
                folder.enumerate(recursive=True, power_status=True)))
        if ask_question("Continue? ", default="yes"):
            for vm in vms:
                vm.change_state(operation, attempt_guest)

    else:
        vm = resolve_path(server, "VM")[0]
        logging.info("Changing power state of '%s' to '%s'", vm.name, operation)
        vm.change_state(operation, attempt_guest)
Пример #2
0
def main():
    args = get_args(__doc__, __version__, 'vm_snapshots.log')
    server = script_setup(args=args, script_info=(__file__, __version__))

    op = str(input("Enter Snapshot operation [create | revert | revert-current "
                   "| remove | remove-all | get | "
                   "get-current | get-all | disk-usage]: "))
    if op == "create" or op == "revert" or op == "remove" or op == "get":
        name = str(input("Name of snapshot to %s: " % op))
        if op == "create":
            desc = str(input("Description of snapshot to create: "))
            memory = ask_question("Include memory?")
            quiesce = ask_question("Quiesce disks? (Requires VMware Tools "
                                   "to be running on the VM)")
        elif op == "remove":
            children = ask_question("Remove any children of the snapshot?",
                                    default="yes")

    if ask_question("Multiple VMs? ", default="yes"):
        f, f_name = resolve_path(server, "folder", "with VMs")
        vms = [VM(vm=x) for x in f.childEntity if is_vm(x)]
        logging.info("Found %d VMs in folder '%s'", len(vms), f_name)
        if ask_question("Show the status of the VMs in the folder? "):
            logging.info("Folder structure: \n%s", format_structure(
                f.enumerate(recursive=True, power_status=True)))
        if not ask_question("Continue? ", default="yes"):
            logging.info("User cancelled operation, exiting...")
            exit(0)
    else:
        vms = [resolve_path(server, "vm",
                            "to perform snapshot operations on")[0]]

    # Perform the operations
    for vm in vms:
        logging.info("Performing operation '%s' on VM '%s'", op, vm.name)
        if op == "create":
            vm.create_snapshot(name=name, description=desc,
                               memory=memory, quiesce=quiesce)
        elif op == "revert":
            vm.revert_to_snapshot(snapshot=name)
        elif op == "revert-current":
            vm.revert_to_current_snapshot()
        elif op == "remove":
            vm.remove_snapshot(snapshot=name, remove_children=children)
        elif op == "remove-all":
            vm.remove_all_snapshots()
        elif op == "get":
            logging.info(vm.get_snapshot_info(name))
        elif op == "get-current":
            logging.info(vm.get_snapshot_info())
        elif op == "get-all":
            logging.info(vm.get_all_snapshots_info())
        elif op == "disk-usage":
            logging.info(vm.snapshot_disk_usage())
        else:
            logging.error("Unknown operation: %s", op)
Пример #3
0
def main():
    args = get_args(__doc__, __version__, 'vsphere_info.log')
    server = script_setup(args=args, script_info=(__file__, __version__))

    thing_type = str(
        input("What type of thing do you want"
              "to get information on?"
              " (vm | datastore | vsphere | folder) "))

    # Single Virtual Machine
    if thing_type == "vm":
        vm = resolve_path(server, "vm", "you want to get information on")[0]
        logging.info(
            vm.get_info(detailed=True, uuids=True, snapshot=True, vnics=True))

    # Datastore
    elif thing_type == "datastore":
        ds = server.get_datastore(
            str(
                input("Enter name of the Datastore"
                      "[leave blank for "
                      "first datastore found]: ")))
        logging.info(ds.get_info())

    # vCenter server
    elif thing_type == "vsphere":
        logging.info(str(server))

    # Folder
    elif thing_type == "folder":
        folder, folder_name = resolve_path(server, "folder")
        if "VirtualMachine" in folder.childType \
                and ask_question("Want to see power state "
                                 "of VMs in the folder?"):
            contents = folder.enumerate(recursive=True, power_status=True)
        else:
            contents = folder.enumerate(recursive=True, power_status=False)
        logging.info(
            "Information for Folder %s\n"
            "Types of items folder can contain: %s\n%s", folder_name,
            str(folder.childType), format_structure(contents))

    # That's not a thing!
    else:
        logging.info("Invalid selection: %s", thing_type)
Пример #4
0
def main():
    args = get_args(__doc__, __version__, 'cleanup_vms.log')
    server = script_setup(args=args, script_info=(__file__, __version__))

    if ask_question("Multiple VMs? ", default="yes"):
        folder, folder_name = resolve_path(
            server, "folder", "that has the VMs/folders "
            "you want to destroy")

        # Display folder structure
        if ask_question("Display the folder structure? "):
            logging.info(
                "Folder structure: \n%s",
                format_structure(
                    folder.enumerate(recursive=True, power_status=True)))

        # Prompt user to configure destruction options
        print("Answer the following questions to configure the cleanup")
        if ask_question("Destroy everything in and including the folder? "):
            vm_prefix = ''
            folder_prefix = ''
            recursive = True
            destroy_folders = True
            destroy_self = True
        else:
            vm_prefix = default_prompt(
                "Prefix of VMs you wish to destroy"
                " (CASE SENSITIVE!)",
                default='')
            recursive = ask_question("Recursively descend into folders? ")
            destroy_folders = ask_question("Destroy folders "
                                           "in addition to VMs? ")
            if destroy_folders:
                folder_prefix = default_prompt(
                    "Prefix of folders "
                    "you wish to destroy"
                    " (CASE SENSITIVE!)",
                    default='')
                destroy_self = ask_question("Destroy the folder itself? ")
            else:
                folder_prefix = ''
                destroy_self = False

        # Show user what options they selected
        logging.info(
            "Options selected\nVM Prefix: %s\n"
            "Folder Prefix: %s\nRecursive: %s\n"
            "Folder-destruction: %s\nSelf-destruction: %s", str(vm_prefix),
            str(folder_prefix), recursive, destroy_folders, destroy_self)

        # Show how many items matched the options
        v, f = folder.retrieve_items(vm_prefix, folder_prefix, recursive=True)
        num_vms = len(v)
        if destroy_folders:
            num_folders = len(f)
            if destroy_self:
                num_folders += 1
        else:
            num_folders = 0
        logging.info("%d VMs and %d folders match the options", num_vms,
                     num_folders)

        # Confirm and destroy
        if ask_question("Continue with destruction? "):
            logging.info("Destroying folder '%s'...", folder_name)
            folder.cleanup(vm_prefix=vm_prefix,
                           folder_prefix=folder_prefix,
                           recursive=recursive,
                           destroy_folders=destroy_folders,
                           destroy_self=destroy_self)
        else:
            logging.info("Destruction cancelled")
    else:
        vm = resolve_path(server, "vm", "to destroy")[0]

        if ask_question("Display VM info? "):
            logging.info(
                vm.get_info(detailed=True,
                            uuids=True,
                            snapshot=True,
                            vnics=True))

        if vm.is_template():  # Warn if template
            if not ask_question("VM '%s' is a Template. Continue? " % vm.name):
                exit(0)

        if ask_question("Continue with destruction? "):
            logging.info("Destroying VM '%s'", vm.name)
            vm.destroy()
        else:
            logging.info("Destruction cancelled")
Пример #5
0
def main():
    args = get_args(__doc__, __version__, 'clone_vms.log')
    server = script_setup(args=args, script_info=(__file__, __version__))

    vms = []
    vm_names = []

    # Single-vm source
    if ask_question("Do you want to clone from a single VM?"):
        v = resolve_path(server, "VM", "or template you wish to clone")[0]
        vms.append(v)
        vm_names.append(str(input("Base name for instances to be created: ")))
    # Multi-VM source
    else:
        folder_from, from_name = resolve_path(server, "folder",
                                              "you want to clone all VMs in")
        # Get VMs in the folder
        v = [VM(vm=x) for x in folder_from.childEntity if is_vm(x)]
        vms.extend(v)
        logging.info("%d VMs found in source folder %s", len(v), from_name)
        if not ask_question("Keep the same names? "):
            names = []
            for i in range(len(v)):
                names.append(str(input("Enter base name for VM %d: " % i)))
        else:
            names = list(map(lambda x: x.name, v))  # Same names as sources
        vm_names.extend(names)

    create_in, create_in_name = resolve_path(server, "folder",
                                             "in which to create VMs")
    instance_folder_base = None
    if ask_question("Do you want to create a folder for each instance? "):
        instance_folder_base = str(input("Enter instance folder base name: "))

    num_instances = int(input("Number of instances to be created: "))

    pool_name = server.get_pool().name  # Determine what will be the default
    pool_name = default_prompt(prompt="Resource pool to assign VMs to",
                               default=pool_name)
    pool = server.get_pool(pool_name)

    datastore_name = default_prompt(prompt="Datastore to put clones on")
    datastore = server.get_datastore(datastore_name)

    logging.info("Creating %d instances under folder %s", num_instances,
                 create_in_name)
    for instance in range(num_instances):
        for vm, name in zip(vms, vm_names):
            if instance_folder_base:
                # Create instance folders for a nested clone
                f = server.create_folder(instance_folder_base + pad(instance),
                                         create_in=create_in)
                vm_name = name
            else:
                f = create_in
                vm_name = name + pad(instance)  # Append instance number
            new_vm = VM(name=vm_name,
                        folder=f,
                        resource_pool=pool,
                        datastore=datastore)
            new_vm.create(template=vm.get_vim_vm())