Exemplo n.º 1
0
def execute(vm_name, cmd, *args):
    global vm_conf_file
    # pysphere, vi_server
    vmachine_cmds = ["startup", "shutdown", "reboot",
                     "get_snapshots", "revert_last_snapshot", "revert_to_snapshot", "create_snapshot",
                     "delete_snapshot",
                     "is_powered_on", "is_powered_off", "get_status",
                     "list_directory", "make_directory", "get_file", "send_file", "list_processes"]
    # vmware tools
    vmrun_cmds = ["executeCmd", "runTest", "takeScreenshot", "listProcesses",
                  "mkdirInGuest", "copyFileToGuest", "copyFileFromGuest", "deleteDirectoryInGuest",
                  "listDirectoryInGuest", "refreshSnapshot"]

    if config.verbose:
        logging.debug("vm: %s, command: %s" % (vm_name, cmd))

    try:
        vm = VMachine(vm_name)
        vm.get_params(vm_conf_file)

        assert vm.config

        if cmd in vmrun_cmds:
            vmrun = VMRun(vm_conf_file)
            f = getattr(vmrun, cmd)
            if not args:
                return f(vm)
            else:
                return f(vm, *args)

        elif cmd in vmachine_cmds:
            f = getattr(vm, cmd)
            if not args:
                return f()
            else:
                return f(args)
        else:
            logging.error("command not found: %s" % cmd)
            raise Exception("Command not found")
    except AssertionError as ae:
        logging.error("Assertion found: %s" % ae)
        raise
    except Exception as e:
        logging.error("Exception found. %s" % e)
        raise