forked from rapid7/vm-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esxiVm.py
executable file
·720 lines (667 loc) · 32.4 KB
/
esxiVm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim, vmodl
from datetime import datetime
import atexit
import requests
from socket import error as SocketError
import ssl
import time
import json
def createFromConfig(configDictionary, logFile = "defaultLogfile.log"):
"""
IN MOST USE CASES, I JUST EXPECT A JSON FILE, SO I MADE AN OPTIONAL
CONSTRUCTOR THAT TAKES A DICTIONARY GENERATED BY A JSON FILE
"""
try:
hostname = configDictionary['HYPERVISOR_HOST']
username = configDictionary['HYPERVISOR_USERNAME']
password = configDictionary['HYPERVISOR_PASSWORD']
port = configDictionary['HYPERVISOR_LISTENING_PORT']
except KeyError as e:
print "CONFIG FILE DID NOT CONTAIN ALL REQUIRED DATA: " + str(e)
return None
return esxiServer(hostname, username, password, port, logFile)
def createFromFile(configFile, logFile = "defaultLogfile.log"):
"""
IN MOST USE CASES, I JUST EXPECT A JSON FILE, SO I MADE AN OPTIONAL
CONSTRUCTOR THAT TAK
"""
try:
fileObj = open(configFile, 'r')
configStr = fileObj.read()
fileObj.close()
except IOError as e:
print "UNABLE TO OPEN FILE: " + str(configFile) + '\n' + str(e)
return None
try:
hypervisorDic = json.loads(configStr)
except Exception as e:
print "UNABLE TO PARSE FILE: " + str(configFile) + '\n' + str(e)
return None
return createFromConfig(hypervisorDic, logFile)
class esxiServer:
"""
THE esxiServer CLASS IS A CLASS THAT STORES INFORMATION ON AND SIMPLIFIES INTERACTION
WITH AN ESXI SERVER.
"""
def __init__(self, hostname, username, password, port, logFile = "defaultLogfile.log"):
self.hostname = hostname
self.type = "ESXi"
self.username = username
self.password = password
self.logFile = logFile
self.port = port
self.vmList = []
self.fullName = ""
self.connection = None
self.connect()
def connect(self):
"""
connect() INTITATES A CONNECTION TO THE ESXi SERVER AND STORES THE RESULT IN
THE CLASS VARIABLE connection. AFTER THE INTITIAL CONNECT, MEMBER FUNCTUONS
USE THE connection VARIABLE.
"""
retVal = True
context = None
if hasattr(ssl, '_create_unverified_context'):
context = ssl._create_unverified_context()
try:
self.connection = SmartConnect(host=self.hostname,
user=self.username,
pwd=self.password,
port=int(self.port),
sslContext=context)
except SocketError as e:
self.logMsg("[ERROR]: CANNOT CONTACT SERVER " + self.hostname)
self.logMsg("SYSTEM ERROR MESSAGE:\n" + str(e))
retVal = False
except vim.fault.InvalidLogin as e:
self.logMsg("[ERROR]: INCORRECT USERNAME/PASSWORD FOR " + self.hostname)
self.logMsg("SYSTEM ERROR MESSAGE:\n" + str(e))
retVal = False
except vim.fault.NoPermission as e:
self.logMsg("[ERROR]: INCORRECT PERMISSIONS TO LOGIN FOR USER " + self.username + "@" + self.hostname)
self.logMsg("SYSTEM ERROR MESSAGE:\n" + str(e))
retVal = False
except Exception as e:
self.logMsg("[ERROR]: UNKNOWN ERROR (SORRY!) WHILE CONNECTING TO" + self.hostname)
self.logMsg("SYSTEM ERROR MESSAGE:\n" + str(e))
retVal = False
else:
if None == self.connection:
self.logMsg("CONNECTION TO" + self.hostname + " WAS UNSUCCESSFUL")
retVal = False
else:
retVal = True
atexit.register(Disconnect, self.connection)
return retVal
def logMsg(self, strMsg):
if strMsg == None:
strMsg="[None]"
dateStamp = 'serverlog:[' + str(datetime.now())+ '] '
#DELETE THIS LATER:
print dateStamp + strMsg
try:
logFileObj = open(self.logFile, 'ab')
logFileObj.write(dateStamp + strMsg + '\n')
logFileObj.close()
except IOError:
return False
return True
def getObject(self, thingToGet):
"""
OBJECTS YOU CAN GET:
vim.ResourcePool
vim.Folder
vim.HostSystem
vim.DataCenter
"""
content = self.connection.RetrieveContent()
container = content.viewManager.CreateContainerView(content.rootFolder, [thingToGet], True)
return container.view
def waitForVmsToBoot(self, vmList):
"""
IF YOU TRY AND INTERACT WITH A VM BEFORE IT FINISHES LOADING VMWARE TOOLS, IT CAUSES A FAULT AND CRASHES
IF YOU TRY AND INTERACT WITH A VM BEFORE IT FINISHES BOOTING, IT CAUSES A FAULT AND CRASHES
IF YOU TRY AND CHECK IF IT BOOTED BEFORE TOOLS ARE RUNNING, IT CAUSES A FAULT AND CRASHES
THIS JUST POLLS THE TOOLS INSTALLATION UNTIL TOOLS REPONDS CORRECTLY, THEN STARTS ASKING FOR THE IP
ADDRESS. ONCE THE IP ADDRESS COMES UP, YOU CAN USE IT.
"""
self.logMsg("WAITING FOR VMS TO BE READY; THIS COULD TAKE A FEW MINUTES")
vmsReady = False
while(vmsReady == False):
vmsReady = True
for i in vmList:
if i.isPoweredOn() == False:
time.sleep(1)
self.logMsg(i.vmName + " DID NOT POWER ON AS EXPECTED; RETRYING")
i.powerOn(True)
if i.checkTools(True) == False:
vmsReady = False
time.sleep(1)
retVal = True
self.logMsg("VMS APPEAR TO BE READY; PULLING IP ADDRESSES TO VERIFY")
time.sleep(5)
for i in vmList:
for j in range(5):
ipAddress = i.getVmIp()
if ipAddress != None:
break;
else:
self.logMsg("IP ADDRESS LOOKUP FAILED FOR " + i.vmName + " = " + str(ipAddress))
time.sleep(1)
if ipAddress == None:
retVal = False
self.logMsg(i.vmName + " FAILED TO INITIALIZE")
else:
self.logMsg("IP ADDRESS FOR " + i.vmName + " = " + ipAddress)
return retVal
def enumerateVms(self, negFilter = None):
"""
THERE ARE SEVERAL WAYS TO ENUMERATE VMs. THIS IS BY FAR THE EASIEST,
BUT I AM NOT SURE IF IT WILL WORK ON LARGER DEPLOYMENTS BECAUSE OF
vSPHERE'S HEIRARCHIAL STORAGE METHODOLOGY
"""
content = self.connection.content
objView = content.viewManager.CreateContainerView(content.rootFolder,
[vim.VirtualMachine],
True)
vimVmList = objView.view
objView.Destroy()
self.vmList = []
for i in vimVmList:
if negFilter != None and negFilter.upper() in i.name.upper():
continue
else:
self.vmList.append(esxiVm(self, i))
def getVersion(self):
"""
RETURNS ESXI VERSION
"""
content = self.connection.RetrieveContent()
self.fullName = content.about.fullName
return self.fullName
class esxiVm:
def __init__(self, serverObject, vmObject):
self.server = serverObject
self.vmObject = vmObject
self.procList = []
self.revertSnapshots = []
self.snapshotList = []
self.testVm = False
self.vmIdentifier = vmObject.summary.config.vmPathName
self.vmIp = None
self.vmName = vmObject.summary.config.name
self.vmOS = vmObject.summary.config.guestFullName
self.vmPassword = ""
self.vmUsername = ""
self.uploadDir = ""
self.payloadList = []
self.resultDict = {}
if '64-bit' in self.vmOS:
self.arch = 'x64'
else:
self.arch = 'x86'
def checkTools(self, waitForTools = True):
"""
THERE HAS TO BE A BETTER WAY TO DO THIS....
WHEN I WROTE IT, THERE WERE ONLY 2 POSSIBLE VALUES, ON AND NOT ON YET...
THERE ARE A FEW OPTIONS I FOUND LATER THAT ARE THE EQUIVALENT OF "NEVER COMING ON, YOU BETTER QUIT"
DIFFICULT TO EXIT FROM WITHIN A FUNCTION, AND RETURNING False MEANS "WAIT"
PROBABLY NEED TO TRUNCATE THIS TO
CHECKING FOR STATE,
ENCAPSULATE WITH try/except
RETURN STRING OR FLAG FOR "READY" "NOT READY" "FAULT"
"""
tools_status = self.vmObject.guest.toolsStatus
if tools_status == 'toolsNotRunning':
retVal = False
elif tools_status == 'toolsOld':
self.server.logMsg("YOU SHOULD UPGRADE THE VMWARE TOOLS ON " + self.vmName)
retVal = True
elif tools_status == 'toolsNotInstalled':
self.server.logMsg("YOU SHOULD INSTALL VMWARE TOOLS ON " + self.vmName)
retVal = False
elif tools_status == 'toolsOk':
retVal = True
else:
self.server.logMsg("UNKNOWN STATE OF VMWARE TOOLS ON " + self.vmName + "::" +tools_status)
retVal = False
return retVal
def deleteSnapshot(self, snapshotName):
self.getSnapshots()
for i in self.snapshotList:
if i[0].name == snapshotName:
self.server.logMsg("DELETING SNAPSHOT " + snapshotName + " FROM " + self.vmName)
return self.waitForTask(i[0].snapshot.RemoveSnapshot_Task(False))
def enumerateSnapshotsRecursively(self, snapshots, snapshot_location):
if not snapshots:
return
for snapshot in snapshots:
if snapshot_location:
current_snapshot_path = snapshot_location + '/' + snapshot.name
else:
current_snapshot_path = snapshot.name
self.snapshotList.append((snapshot, current_snapshot_path))
self.enumerateSnapshotsRecursively(snapshot.childSnapshotList, current_snapshot_path)
return
def getArch(self):
return self.arch
def ghettoClone(self, newVmName):
"""
TODO
"""
vmDataCenter = self.getDataCenter()
vim.FileManager.CopyDataStoreFile(self.vmObject.parent )
vim.Datastore.FileManager.CopyDatastoreFile(vmToCopy.parent )
def getDataCenter(self):
content = self.server.connection.RetrieveContent()
for child in content.rootFolder.childEntity:
if hasattr(child, 'vmFolder'):
datacenter = child
print child.name
vmFolder = datacenter.vmFolder
print vmFolder
vmList = vmFolder.childEntity
if self.vmObject in vmList:
return child
def getFileFromGuest(self, srcFile, dstFile):
for i in range(3):
self.server.logMsg("ATTEMPTING TO GET " +srcFile)
retVal = False
if self.checkTools():
creds = vim.vm.guest.NamePasswordAuthentication(username=self.vmUsername,
password=self.vmPassword)
content = self.server.connection.RetrieveContent()
try:
file_attribute = vim.vm.guest.FileManager.FileAttributes()
vmFileManager = content.guestOperationsManager.fileManager
ftInfo = vmFileManager.InitiateFileTransferFromGuest(self.vmObject,
creds,
srcFile)
#THIS IS STUPID, BUT THERE IS SOME ASSEMBLY REQUIRED
splitUrl = ftInfo.url.split('*')
realUrl = splitUrl[0] + self.server.hostname + splitUrl[1]
self.server.logMsg(srcFile + " URL = " + realUrl)
self.server.logMsg(srcFile +" SIZE = " + str(ftInfo.size))
resp = requests.get(realUrl, verify=False)
if not resp.status_code == 200:
self.server.logMsg("ERROR GETTING FILE " + \
srcFile + " FROM " +\
self.vmName + " HTTP CODE " + \
str(resp.status_code))
retVal = True
else:
getFile = open(dstFile, 'wb')
getFile.write(resp.content)
getFile.close()
self.server.logMsg("SAVED FILE FROM " + self.vmName + \
" AS " + dstFile + \
" HTTP RESPONSE WAS " + str(resp.status_code))
retVal=True
except Exception as e:
self.server.logMsg(str(e))
pass
#SOMETIMES THE VM GETS SQUIRRLEY; IF SO, JUST TRY AGAIN
except Exception as e:
self.server.logMsg("UNPREDICTED EXCEPTION:\n" + str(e))
continue
else:
self.server.logMsg("THERE IS A PROBLEM WITH THE VMWARE TOOLS ON " + self.vmName)
return retVal
def getSnapshots(self):
"""
SEARCHING FOR SNAPSHOTS IS UNPLEASANT
SINCE SNAPSHOTS ARE NESTED, RECURSIVE CALLS ARE NECESSARY
"""
self.server.logMsg("FINDING SNAPSHOTS FOR " + self.vmName)
self.snapshotList = []
if hasattr(self.vmObject.snapshot, 'rootSnapshotList'):
self.enumerateSnapshotsRecursively(self.vmObject.snapshot.rootSnapshotList, '')
return
def getVmIp(self):
"""
IT IS POSSIBLE TO GET NO IP ADDRESS IN THE GAP BETWEEN WHEN VMWARE
TOOLS FINISHES LOADING AND BEFORE NETWORKING SERVICES START
THIS WILL TRY TO GET THE IP ADDRESS FOR 2 MINUTES
"""
ipAttempts = 120
for i in range(ipAttempts):
self.vmIp = self.vmObject.summary.guest.ipAddress
if self.vmIp != None:
break
else:
strAttempt = "(ATTEMPT " + str(i) + " OF " + str(ipAttempts) + ")"
self.server.logMsg(strAttempt + " FAILED TO GET IP ADDRESS FROM " + self.vmName)
time.sleep(1)
return self.vmIp
def getVmInterfaces(self):
"""
IT IS POSSIBLE TO GET NO IP ADDRESS IN THE GAP BETWEEN WHEN VMWARE
TOOLS FINISHES LOADING AND BEFORE NETWORKING SERVICES START
THIS WILL TRY TO GET THE IP ADDRESS FOR 2 MINUTES
"""
ipAttempts = 120
for i in range(ipAttempts):
self.vmInterfaces = []
for nic in self.vmObject.guest.net:
addresses = nic.ipConfig.ipAddress
IPs = []
for addr in addresses:
IPs.append("%s/%s" % (addr.ipAddress,addr.prefixLength))
self.vmInterfaces.append((nic.macAddress,IPs,nic.network))
if self.vmInterfaces != []:
break
else:
strAttempt = "(ATTEMPT " + str(i) + " OF " + str(ipAttempts) + ")"
self.server.logMsg(strAttempt + " FAILED TO GET IP ADDRESS FROM " + self.vmName)
time.sleep(1)
return self.vmInterfaces
def getUsername(self):
return self.vmUsername
def isTestVm(self):
return self.testVm
def isPoweredOff(self):
return not self.isPoweredOn()
def isPoweredOn(self):
if self.vmObject.runtime.powerState == vim.VirtualMachinePowerState.poweredOn:
return True
else:
return False
def makeDirOnGuest(self, dirPath):
self.server.logMsg("CREATING " + dirPath + " ON " + self.vmName + " ")
retVal = True
if self.checkTools():
creds = vim.vm.guest.NamePasswordAuthentication(username=self.vmUsername,
password=self.vmPassword)
content = self.server.connection.RetrieveContent()
try:
content.guestOperationsManager.fileManager.MakeDirectoryInGuest(self.vmObject,
creds,
dirPath,
False)
retVal = True
except IOError as e:
self.server.logMsg("[ERROR]: FILE NOT FOUND")
self.server.logMsg("SYSTEM ERROR: " + str(e))
retVal = False
except vim.fault.FileAlreadyExists as f:
self.server.logMsg("[WARNING]: DIRECTORY " + dirPath + " ALREADY EXISTS ON " + self.vmName)
self.server.logMsg("SYSTEM ERROR: " + str(f))
retVal = True
except vim.fault.InvalidGuestLogin as g:
self.server.logMsg("[ERROR]: INCORRECT USERNAME/PASSWORD PROVIDED FOR " + self.vmName)
self.server.logMsg("SYSTEM ERROR: " + str(g))
retVal = False
except Exception as g:
self.server.logMsg("[ERROR]: UNKNOWN EXCEPTION WHILE MAKING " + dirPath + " ON " + self.vmName)
self.server.logMsg("SYSTEM ERROR: " + str(g))
retVal = False
else:
self.server.logMsg("ERROR: VMWARE TOOLS NOT INSTALLED ON " + self.vmName)
retVal = False
return retVal
def powerOn(self, asyncFlag = False):
"""
WARNING: DURING POWER-ON, TOOLS CAN GET INTO TEMPORARY FUNKY STATES WHERE IT WILL
ACCEPT COMMANDS AS THOUGH IT IS RUNNING, BUT THEN IT REALIZES THAT ITS NOT READY,
AND FAULTS OUT FOR UNPREDICTABLE AND ODD REASONS. I DO NOT SUGGEST THAT YOU CALL THIS
WITH asyncFlag SET TO True, BUT IT IS YOUR LIFE, AND I AM NOT YOUR MOTHER.
DO WHAT YOU WANT.
"""
if self.isPoweredOn():
self.server.logMsg(self.vmName + " IS ALREADY RUNNING, CANNOT POWER-ON HARDER")
return None
else:
self.server.logMsg("POWERING ON " + self.vmName)
if asyncFlag:
return self.vmObject.PowerOnVM_Task()
else:
return self.waitForTask(self.vmObject.PowerOnVM_Task())
def powerOff(self, asyncFlag = False):
if self.isPoweredOff():
self.server.logMsg(self.vmName + " IS ALREADY OFF, CANNOT POWER-OFF HARDER")
return None
else:
self.server.logMsg("POWERING OFF " + self.vmName)
if asyncFlag:
return self.vmObject.PowerOnVM_Task()
else:
return self.waitForTask(self.vmObject.PowerOffVM_Task())
def prepVm(self):
"""
prepVm USED TO RUN A BUNCH OF COMMANDS TO PREP THE VM, BUT I'VE OFFLOADED MOST
OF THEM TO powerOn WITH THE ASYNC FLAG, SO THIS IS A KIND OF A SAD FUNCTION, NOW.
"""
self.server.logMsg("PREPARING " + self.vmName + " FOR TESTING")
self.server.logMsg(self.vmName + " OPERATING SYSTEM: " + self.vmOS)
self.server.logMsg(self.vmName + " ARCHITECTURE: " + self.getArch())
self.getSnapshots()
self.powerOn(False)
def revertToTestingBase(self):
self.server.logMsg("RESETTING VM " + self.vmName)
self.getSnapshots()
for i in self.snapshotList:
if 'testing_base' in i[0].name.lower():
self.server.logMsg("REVERTING VM TO " + i[0].name)
return self.revertToSnapshot(i[0].snapshot)
return None
def revertToSnapshot(self, snapshotObj):
return self.waitForTask(snapshotObj.RevertToSnapshot_Task())
def revertToSnapshotByName(self, snapshotObj):
self.server.logMsg("RESETTING VM " + self.vmName)
self.getSnapshots()
for snapshotObject in self.snapshotList:
if 'testing_base' in snapshotObject[0].name.lower():
self.server.logMsg("REVERTING VM TO " + snapshotObject[0].name)
return self.revertToSnapshot(snapshotObject[0].snapshot)
return None
def revertMsfVm(self):
self.getSnapshots()
for i in self.snapshotList:
if "PAYLOAD_TESTING-" in i[0].name:
self.server.logMsg("REVERTING " + self.vmName + " TO " + i[0].name)
self.revertToSnapshot(i[0].snapshot)
self.deleteSnapshot(i[0].name)
def runCmdOnGuest(self, cmdAndArgList):
self.server.logMsg("RUNNING '" + ' '.join(cmdAndArgList) + "' ON " + self.vmName)
if self.checkTools():
try:
creds = vim.vm.guest.NamePasswordAuthentication(username=self.vmUsername,
password=self.vmPassword)
content = self.server.connection.RetrieveContent()
cmdspec = vim.vm.guest.ProcessManager.ProgramSpec(programPath=cmdAndArgList[0],
arguments=' '.join(cmdAndArgList[1:]))
cmdpid = content.guestOperationsManager.processManager.StartProgramInGuest(vm=self.vmObject,
auth=creds,
spec=cmdspec)
retVal = cmdpid
self.server.logMsg("LAUNCHING '" + ' '.join(cmdAndArgList) + "' ON " + self.vmName)
retVal = True
except vim.fault.InvalidGuestLogin as e:
self.server.logMsg("INCORRECT USERTNAME/PASSWORD PROVIDED FOR " + self.vmName)
self.server.logMsg("SYSTEM ERROR:\n" + str(e))
retVal = False
except vim.fault.GuestPermissionDenied as f:
self.server.logMsg("INSUFFICIENT PERMISSIONS TO RUN " + \
' '.join(cmdAndArgList) + " ON " + self.vmName)
self.server.logMsg("SYSTEM ERROR:\n" + str(f))
retVal = False
except vim.fault.FileNotFound as g:
self.server.logMsg("UNABLE TO LOCATE THE FILE YOU ARE TRYING TO EXECUTE " + \
' '.join(cmdAndArgList) + " ON " + self.vmName)
self.server.logMsg("SYSTEM ERROR:\n" + str(g))
retVal = False
else:
self.server.logMsg("FAILED TO RUN '" + ' '.join(cmdAndArgList) + "' ON " + self.vmName)
retVal = False
return retVal
def setPassword(self, vmPassword):
self.vmPassword = vmPassword
def setTestVm(self):
self.testVm = True
def setUsername(self, vmUsername):
self.vmUsername = vmUsername
def setVmIp(self, ipAddress):
self.vmIp = ipAddress
return True
def takeSnapshot(self,
snapshotName,
asyncFlag = False,
snapshotDescription = '',
dumpMemory = False,
setQuiescent = False):
self.server.logMsg("TAKING SNAPSHOT " + snapshotName + " ON " + self.vmName)
snapshotTask = self.vmObject.CreateSnapshot_Task(snapshotName,
snapshotDescription,
dumpMemory,
setQuiescent)
if not asyncFlag:
return self.waitForTask(snapshotTask)
else:
return None
def takeTempSnapshot(self, asyncFlag = False):
snapshotName = "PAYLOAD_TESTING-" + str(time.time()).split('.')[0]
return self.takeSnapshot(snapshotName, asyncFlag)
def updateProcList(self):
content = self.server.connection.RetrieveContent()
creds = vim.vm.guest.NamePasswordAuthentication(username=self.vmUsername,
password=self.vmPassword)
"""
UNDER HEAVY LOAD, VMTools CAN GET IN AN ODD STATE AND FLAKE OUT WHEN YOU TRY TO
GET THE PROCLIST. WHEN IT GETS LIKE THAT, IT THROWS the EXCEPTION
vim.fault.InvalidState. IF THAT HAPPENS, TRYING AGAIN ALMOST ALWAYS WORKS.
THAT'S WHY THE CALL SITS IN A LOOP LIKE THIS....
"""
for i in range(5):
try:
processList = content.guestOperationsManager.processManager.ListProcessesInGuest(vm=self.vmObject,
auth=creds)
except vim.fault.InvalidState as e:
self.server.logMsg("[WARNING]: VM IN A STRANGE STATE; RETRYING PROCLIST UPDATE")
self.server.logMsg("SYSTEM ERROR:\n" + str(e))
retVal = False
time.sleep(1)
pass
except Exception as f:
self.server.logMsg("[ERROR]: UNKNOWN ERROR (SORRY!)")
self.server.logMsg("SYSTEM ERROR:\n" + str(f))
retVal = False
break
else:
self.procList[:]=[]
for runningProc in processList:
self.procList.append(str(runningProc.pid) + "\t\t" + \
runningProc.name + "\t\t" + \
runningProc.cmdLine + "\t\t" + \
runningProc.owner)
retVal = True
return retVal
def uploadAndRun(self, srcFile, dstFile, remoteInterpreter = None):
"""
THIS JUST COMBIENS THE UPLOAD AND EXECUTE FUNCTIONS, BUT IF THE VM IS 'NIX, IT ALSO
CHMODS THE FILE SO WE CAN EXECUTE IT
"""
self.server.logMsg("SOURCE FILE = " + srcFile + "; DESTINATION FILE = " + dstFile)
if remoteInterpreter!= None:
remoteCmd = [remoteInterpreter, dstFile]
else:
remoteCmd = [dstFile]
if not self.uploadFileToGuest(srcFile, dstFile):
self.server.logMsg("[FATAL ERROR]: FAILED TO UPLOAD " + srcFile + " TO " + self.vmName)
return False
if 'win' not in self.vmName.lower():
chmodCmdList = "/bin/chmod 755".split() + [dstFile]
if not self.runCmdOnGuest(chmodCmdList):
self.server.logMsg("[FATAL ERROR]: FAILED TO RUN " + ' '.join(chmodCmdList) + " ON " + self.devVm)
return False
if not self.runCmdOnGuest(remoteCmd):
self.server.logMsg("[FATAL ERROR]: FAILED TO RUN '" + ' '.join(remoteCmd) + "' ON " + self.devVm)
return False
return True
def uploadFileToGuest(self, srcFile, dstFile):
"""
uploadFileToGuest UPLOADS A FILE TO A VM
"""
self.server.logMsg("ATTEMPTING TO UPLOAD " +srcFile + " TO " + dstFile + " ON " + self.vmName)
self.server.logMsg("USING " + self.vmUsername + " PW " + self.vmPassword + " ON " + self.vmName)
retVal = False
if self.checkTools():
creds = vim.vm.guest.NamePasswordAuthentication(username=self.vmUsername,
password=self.vmPassword)
content = self.server.connection.RetrieveContent()
self.server.logMsg("TOOLS CHECKS OUT")
try:
srcFileObj = open(srcFile, 'rb')
fileContent = srcFileObj.read()
srcFileObj.close()
except IOError:
self.server.logMsg("FAILED TO OPEN FILE " + srcFile)
return retVal
try:
file_attribute = vim.vm.guest.FileManager.FileAttributes()
vmFileManager = content.guestOperationsManager.fileManager
incompleteUrl = vmFileManager.InitiateFileTransferToGuest(self.vmObject,
creds,
dstFile,
file_attribute,
len(fileContent),
True)
self.server.logMsg(incompleteUrl)
#THIS IS STUPID, BUT THERE IS SOME ASSEMBLY REQUIRED
splitUrl = incompleteUrl.split('*')
realUrl = splitUrl[0] + self.server.hostname + splitUrl[1]
self.server.logMsg(realUrl)
resp = requests.put(realUrl, data=fileContent, verify=False)
if not resp.status_code == 200:
self.server.logMsg("ERROR UPLOADING FILE TO " + self.vmName + " HTTP CODE " + str(resp.status_code))
retVal = True
else:
self.server.logMsg("UPLOADED FILE TO " + self.vmName + " HTTP CODE " + str(resp.status_code))
retVal=True
except IOError as e:
self.server.logMsg("FILE NOT FOUND: " + srcFile)
self.server.logMsg("SYSTEM ERROR: " + str(e))
except vim.fault.InvalidGuestLogin as f:
self.server.logMsg("INCORRECT USERTNAME/PASSWORD PROVIDED FOR " + self.vmName)
self.server.logMsg("USERNAME: " + self.vmUsername + " PASSWORD: " + self.vmPassword)
self.server.logMsg("SYSTEM ERROR: " + str(f))
except vmodl.fault.InvalidArgument as f:
self.server.logMsg("INVALID ARGUMENT; OFTEN THIS IS BECAUSE THE SPECIFIED REMOTE PATH IS NOT VALID")
self.server.logMsg("SYSTEM ERROR: " + str(f))
else:
self.server.logMsg("THERE IS A PROBLEM WITH THE VMWARE TOOLS ON " + self.vmName)
return retVal
def waitForTask(self, task):
"""
YES, THIS IS A DISASTER...
EVEN IN VIM, NESTED LOOPS ARE REQUIRED AS TASKS CAN BE CHILDREN OF TASKS, JUST LIKE
SNAPSHOTS
"""
pc = self.server.connection.content.propertyCollector
objSpec = vmodl.query.PropertyCollector.ObjectSpec(obj=task)
propSpec = vmodl.query.PropertyCollector.PropertySpec(type=vim.Task,
pathSet=[], all=True)
filterSpec = vmodl.query.PropertyCollector.FilterSpec()
filterSpec.objectSet = [objSpec]
filterSpec.propSet = [propSpec]
filter = pc.CreateFilter(filterSpec, True)
for i in range(20):
update = pc.WaitForUpdates(None)
for filterSet in update.filterSet:
for filterObject in filterSet.objectSet:
if filterObject.obj == task:
for change in filterObject.changeSet:
taskStatus = "UNKNOWN"
if change.name == 'info':
taskStatus = change.val.state
elif change.name == 'info.state':
taskStatus = change.val
else:
continue
if taskStatus == 'success':
self.server.logMsg("DONE")
return True
time.sleep(5)
self.server.logMsg("TASK NOT COMPLETED IN ALLOTTED TIME")
return False