powercli中的invoke-vmscript的pyvmomi等价物是什么?

时间:2017-10-25 08:57:40

标签: python vmware powercli pyvmomi

我正在考虑将现有的powercli部署脚本移动到python / pyvmomi,以获得多线程(它部署了大量的VM)。原始脚本大量使用Invoke-VMScript通过VMware Tools将powershell片段推送给每个guest虚拟机。

pyvmomi中的等效功能是什么?具体来说 - 通过工具(而不是访客的网络)向访客发送一个powershell脚本,让它运行所提供的凭据,然后收集输出?

我可以看到processManager.StartProgramInGuest,但这似乎是一个笨重的三步过程(上传文件,运行文件,下载重定向的结果) - 这就是powercli在后台做的事情吗?

1 个答案:

答案 0 :(得分:1)

所以为了提供一些关闭,并且因为我无论如何都找不到完整的例子,这是我第一次接受这个。它是已使用SmartConnect连接到vCenter Server并设置self.si的类的一部分。它还没有做太多的错误检查。您可以选择是否要等待并获取输出,或者在启动命令后返回。 remote_cmd最初来自pyvmomi-community-samples,因此目前这两种方法之间存在一些重复。

def invoke_vmscript(self, vm_username, vm_password, vm_name, script_content, wait_for_output=False):

    script_content_crlf = script_content.replace('\n', '\r\n')

    content = self.si.content
    creds = vim.vm.guest.NamePasswordAuthentication(username=vm_username, password=vm_password)
    vm = self.get_vm(vm_name)
    logger.debug("Invoke-VMScript Started for %s", vm_name)
    logger.debug("CREATING TEMP OUTPUT DIR")
    file_manager = content.guestOperationsManager.fileManager

    temp_dir = file_manager.CreateTemporaryDirectoryInGuest(vm, creds, "nodebldr_",
                                                            "_scripts")
    try:
        file_manager.MakeDirectoryInGuest(vm, creds, temp_dir, False)
    except vim.fault.FileAlreadyExists:
        pass
    temp_script_file = file_manager.CreateTemporaryFileInGuest(vm, creds, "nodebldr_",
                                                               "_script.ps1",
                                                               temp_dir)
    temp_output_file = file_manager.CreateTemporaryFileInGuest(vm, creds, "nodebldr_",
                                                               "_output.txt",
                                                               temp_dir)
    logger.debug("SCRIPT FILE: " + temp_script_file)
    logger.debug("OUTPUT FILE: " + temp_output_file)
    file_attribute = vim.vm.guest.FileManager.FileAttributes()
    url = file_manager.InitiateFileTransferToGuest(vm, creds, temp_script_file,
                                                   file_attribute,
                                                   len(script_content_crlf), True)
    logger.debug("UPLOAD SCRIPT TO: " + url)
    r = requests.put(url, data=script_content_crlf, verify=False)
    if not r.status_code == 200:
        logger.debug("Error while uploading file")
    else:
        logger.debug("Successfully uploaded file")

    self.remote_cmd(vm_name, vm_username, vm_password, 'C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe',
                    "-Noninteractive {0} > {1}".format(temp_script_file, temp_output_file), temp_dir,
                    wait_for_end=wait_for_output)

    output = None
    if wait_for_output:
        dl_url = file_manager.InitiateFileTransferFromGuest(vm, creds,
                                                            temp_output_file)
        logger.debug("DOWNLOAD OUTPUT FROM: " + dl_url.url)
        r = requests.get(dl_url.url, verify=False)
        output = r.text
        logger.debug("Script Output was: %s", output)

    logger.debug("DELETING temp files & directory")
    file_manager.DeleteFileInGuest(vm, creds, temp_script_file)
    file_manager.DeleteFileInGuest(vm, creds, temp_output_file)
    file_manager.DeleteDirectoryInGuest(vm, creds, temp_dir, True)
    logger.debug("Invoke-VMScript COMPLETE")

    return output

def remote_cmd(self, vm_name, vm_username, vm_password, command, args, working_dir, wait_for_end=False, timeout=60):
    creds = vim.vm.guest.NamePasswordAuthentication(username=vm_username, password=vm_password)
    vm = self.get_vm(vm_name)
    try:
        cmdspec = vim.vm.guest.ProcessManager.ProgramSpec(arguments=args, programPath=command)
        pid = self.si.content.guestOperationsManager.processManager.StartProgramInGuest(vm=vm, auth=creds,
                                                                                        spec=cmdspec)
        logger.debug("Started process %d on %s", pid, vm_name)
    except vmodl.MethodFault as error:
        print("Caught vmodl fault : ", error.msg)
        return -1

    n = timeout
    if wait_for_end:
        while n > 0:
            info = self.si.content.guestOperationsManager.processManager.ListProcessesInGuest(vm=vm, auth=creds,
                                                                                              pids=[pid])
            if info[0].endTime is not None:
                break
            logger.debug("Process not yet completed. Will wait %d seconds", n)
            sleep(1)
            n = n - 1
        logger.debug("Process completed with state: %s", info[0])