Last active
October 26, 2015 13:59
-
-
Save micheller/56455639cd6cdfb89d9a to your computer and use it in GitHub Desktop.
накостыленный под существующие настройки скрипт
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import sys | |
import logging | |
import atexit | |
import argparse | |
from pyVim import connect | |
from pyVmomi import vmodl, vim | |
logging.basicConfig() | |
log = logging.getLogger("taskmounter") | |
log.setLevel(logging.INFO) | |
def _iterable(arg): | |
if hasattr(arg, "__iter__"): | |
return arg | |
else: | |
return (arg,) | |
def find_vm(content, vm_name): | |
children = content.rootFolder.childEntity | |
for child in children: | |
if hasattr(child, 'vmFolder'): | |
datacenter = child | |
else: | |
# some other non-datacenter type object | |
continue | |
vm_folder = datacenter.vmFolder | |
vm_list = vm_folder.childEntity | |
for virtual_machine in vm_list: | |
if virtual_machine.name == vm_name: | |
log.info("found vm: {}".format(virtual_machine)) | |
return virtual_machine | |
def share_vm_folder(content, vm, creds, name, folder): | |
ps = vim.vm.guest.ProcessManager.ProgramSpec(programPath="C:\\WINDOWS\\system32\\net.exe", | |
arguments="share {}=\"{}\" /GRANT:{},FULL".format(name, folder, creds.username)) | |
pid = content.guestOperationsManager.processManager.StartProgramInGuest(vm, creds, ps) | |
if pid < 0: | |
raise Exception("Couldn't run program on VM {}".format(vm)) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("taskId") | |
parser.add_argument("--pcs", nargs='+', help="Operate on PCs:", default="pc1") | |
parser.add_argument("--share", action="store_true", help="Share a folder") | |
parser.add_argument("--name", type=str, help="This will be the share name") | |
parser.add_argument("--folder", type=str, help="The path to the folder to share") | |
parser.add_argument("-s", action="store_true", help="Silent, output only useful data") | |
args = parser.parse_args() | |
share_name = args.name | |
share_folder = args.folder | |
vms_data = [(args.taskId + pc, pc) for pc in _iterable(args.pcs)] | |
vm = None | |
esx = None | |
for vm_name, vm_type in vms_data: | |
try: | |
esx = connect.SmartConnect(host='10.250.79.1', | |
user='root', | |
pwd='qwe123QWE') | |
atexit.register(connect.Disconnect, esx) | |
content = esx.RetrieveContent() | |
vm = find_vm(content, vm_name) | |
if vm: | |
if args.share: | |
if vm_type == "pc1": | |
passw = 'qwerty' | |
elif vm_type == "pc2": | |
passw = 'pc2pwd' | |
creds = vim.vm.guest.NamePasswordAuthentication(username="autotest", password=passw) | |
share_vm_folder(content, vm, creds, share_name, share_folder) | |
if not args.s: | |
print vm_type, | |
print vm.summary.guest.ipAddress | |
except vmodl.MethodFault as error: | |
log.fatal("Caught vmodl fault : " + error.msg) | |
exit(-1) | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment