Skip to content

Commit b198d79

Browse files
committed
Land #23, Added manageHostname sample
2 parents 2fc6268 + 7134c48 commit b198d79

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

samples/manageHostname.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import argparse
2+
import tempfile
3+
import vm_automation
4+
import sampleLib
5+
import time
6+
import os
7+
8+
def updateHostname(vm, hostname):
9+
# Remember machine state and ensure powered on
10+
powerState = vm.isPoweredOn()
11+
if powerState == False:
12+
vm.powerOn()
13+
startCount = 5
14+
while vm.checkTools() != 'TOOLS_READY' and startCount > 0:
15+
time.sleep(5)
16+
startCount -= 1
17+
18+
# File creation for uploading
19+
fPoint,fPath = tempfile.mkstemp()
20+
cmdStr = None
21+
cmdArg = None
22+
23+
# Windows path
24+
if ("Windows" in vm.vmOS):
25+
cmdStr = "wmic computersystem where name=\'%COMPUTERNAME%\' rename " + hostname + "\n"
26+
cmdArg = ["del", "C:\\windows\\temp\\updateHost.bat"]
27+
# Linux path
28+
else:
29+
cmdStr = "#!/bin/sh -x\n"
30+
cmdStr += "HOSTNAME=`hostname`\n"
31+
cmdStr += "sed \'s/\'\"$HOSTNAME\"\'/" + hostname + "/g\' /etc/hosts > /tmp/hosts.new\n"
32+
cmdStr += "sed \'s/\'\"$HOSTNAME\"\'/" + hostname + "/g\' /etc/hostname > /tmp/hostname.new\n"
33+
cmdStr += "sudo mv /tmp/hosts.new /etc/hosts\n"
34+
cmdStr += "sudo mv /tmp/hostname.new /etc/hostname\n"
35+
cmdArg = ["rm", "/tmp/updateHost.sh"]
36+
# Write changes and run
37+
os.write(fPoint, cmdStr)
38+
os.close(fPoint)
39+
vm.uploadAndRun(fPath, cmdArg[1])
40+
vm.runCmdOnGuest(cmdArg)
41+
os.remove(fPath)
42+
43+
# Power off VM for changes to persist
44+
if vm.checkTools() == 'TOOLS_READY' and vm.isPoweredOn():
45+
vm.vmObject.ShutdownGuest()
46+
else:
47+
vm.powerOff()
48+
# Wait
49+
while vm.isPoweredOn():
50+
time.sleep(5)
51+
# Return machine to original state
52+
if powerState != False:
53+
vm.powerOn()
54+
55+
56+
def main():
57+
parser = argparse.ArgumentParser()
58+
parser.add_argument("-k", "--keyword", help="VM search parameter")
59+
parser.add_argument("-cf", "--credsFile", help="credentials file for logging into the vm")
60+
parser.add_argument("-un", "--username", help="vm username")
61+
parser.add_argument("-pw", "--password", help="vm password")
62+
parser.add_argument("-n", "--hostname", help="New hostname for the VM")
63+
parser.add_argument("hypervisorConfig", help="json hypervisor config")
64+
65+
args = parser.parse_args()
66+
67+
hypervisorDic = {}
68+
vmServer = vm_automation.esxiServer.createFromFile(args.hypervisorConfig, './snapshot.log')
69+
70+
"""
71+
CHECK THAT WE HAVE REQUIRED CREDS SOMEHOW
72+
"""
73+
if (args.username == None or args.password == None) and args.credsFile == None:
74+
print("VM CREDENTIALS REQUIRED FOR THIS OPERATION")
75+
exit(0)
76+
77+
credsDictionary = None
78+
if args.credsFile is not None:
79+
credsDictionary = sampleLib.loadJsonFile(args.credsFile)
80+
if credsDictionary is None:
81+
print("FAILED TO LOAD CREDS FILE")
82+
exit(0)
83+
84+
if vmServer != None:
85+
vmsToChange = sampleLib.makeVmList(vmServer, args.keyword, None)
86+
for vm in vmsToChange:
87+
vm_user = args.username
88+
vm_pass = args.password
89+
if credsDictionary is not None:
90+
for machine in credsDictionary:
91+
if credsDictionary[machine]['NAME'] == vm.vmName:
92+
vm_user = credsDictionary[machine]['USERNAME']
93+
vm_pass = credsDictionary[machine]['PASSWORD']
94+
95+
vm.setUsername(vm_user)
96+
vm.setPassword(vm_pass)
97+
98+
updateHostname(vm, args.hostname)
99+
print("hostname changed for " + vm.vmName)
100+
101+
102+
103+
if __name__ == "__main__":
104+
main()

0 commit comments

Comments
 (0)