String formatting with format and change exception handling

master
Sebastien Andrivet 2018-01-10 22:44:14 +01:00
parent f9f1d16251
commit 249ed34bec
3 changed files with 52 additions and 87 deletions

View File

@ -141,7 +141,7 @@ if args.UPDATE:
# main() # main()
# #
LogOutput = '{' LogOutput = '{'
LogOutput += '"datetime":"' + str(the_current_date_time()) + '",' LogOutput += '"datetime":"{}",'.format(the_current_date_time())
if NAME == "": if NAME == "":
print("ERROR: Missing required option --name") print("ERROR: Missing required option --name")
@ -174,8 +174,8 @@ try:
splitLine = line.split() splitLine = line.split()
VOLUMES[splitLine[0]] = splitLine[1] VOLUMES[splitLine[0]] = splitLine[1]
LeastUsedDS = splitLine[1] LeastUsedDS = splitLine[1]
except: except Exception as e:
print("The Error is {}".format(sys.exc_info()[0])) print("The Error is {}".format(e))
sys.exit(1) sys.exit(1)
if STORE == "LeastUsed": if STORE == "LeastUsed":
@ -193,8 +193,8 @@ try:
for line in stdout.readlines(): for line in stdout.readlines():
splitLine = re.split('[,\n]', line) splitLine = re.split('[,\n]', line)
VMNICS.append(splitLine[0]) VMNICS.append(splitLine[0])
except: except Exception as e:
print("The Error is {}".format(sys.exc_info()[0])) print("The Error is {}".format(e))
sys.exit(1) sys.exit(1)
# #
@ -267,19 +267,19 @@ except:
# Check CPU # Check CPU
if CPU < 1 or CPU > 128: if CPU < 1 or CPU > 128:
print("{} CPU out of range. [1-128].".format(CPU)) print("{} CPU out of range. [1-128].".format(CPU))
ErrorMessages += " " + str(CPU) + " CPU out of range. [1-128]." ErrorMessages += " {} CPU out of range. [1-128].".format(CPU)
CheckHasErrors = True CheckHasErrors = True
# Check MEM # Check MEM
if MEM < 1 or MEM > 4080: if MEM < 1 or MEM > 4080:
print("{} GB Memory out of range. [1-4080].".format(MEM)) print("{} GB Memory out of range. [1-4080].".format(MEM))
ErrorMessages += " " + str(MEM) + "GB Memory out of range. [1-4080]." ErrorMessages += " {} GB Memory out of range. [1-4080].".format(MEM)
CheckHasErrors = True CheckHasErrors = True
# Check HDISK # Check HDISK
if HDISK < 1 or HDISK > 63488: if HDISK < 1 or HDISK > 63488:
print("Virtual Disk size {} GB out of range. [1-63488].".format(HDISK)) print("Virtual Disk size {} GB out of range. [1-63488].".format(HDISK))
ErrorMessages += " Virtual Disk size " + str(HDISK) + "GB out of range. [1-63488]." ErrorMessages += " Virtual Disk size {} GB out of range. [1-63488].".format(HDISK)
CheckHasErrors = True CheckHasErrors = True
# Convert STORE to path and visa-versa # Convert STORE to path and visa-versa
@ -301,13 +301,13 @@ if DSSTORE not in V:
if (NET not in VMNICS) and (NET != "None"): if (NET not in VMNICS) and (NET != "None"):
print("ERROR: Virtual NIC {} doesn't exist.".format(NET)) print("ERROR: Virtual NIC {} doesn't exist.".format(NET))
print(" Available VM NICs: {} or 'None'".format([str(item) for item in VMNICS])) print(" Available VM NICs: {} or 'None'".format([str(item) for item in VMNICS]))
ErrorMessages += " Virtual NIC " + NET + " doesn't exist." ErrorMessages += " Virtual NIC {} doesn't exist.".format(NET)
CheckHasErrors = True CheckHasErrors = True
# Check ISO exists # Check ISO exists
if ISO != "" and not ISOfound: if ISO != "" and not ISOfound:
print("ERROR: ISO {} not found. Use full path to ISO".format(ISO)) print("ERROR: ISO {} not found. Use full path to ISO".format(ISO))
ErrorMessages += " ISO " + ISO + " not found. Use full path to ISO" ErrorMessages += " ISO {} not found. Use full path to ISO".format(ISO)
CheckHasErrors = True CheckHasErrors = True
# Check if DSPATH/NAME aready exists # Check if DSPATH/NAME aready exists
@ -317,7 +317,7 @@ try:
type(stdin) type(stdin)
if stdout.readlines() and not stderr.readlines(): if stdout.readlines() and not stderr.readlines():
print("ERROR: Directory {} already exists.".format(FullPath)) print("ERROR: Directory {} already exists.".format(FullPath))
ErrorMessages += " Directory " + FullPath + " already exists." ErrorMessages += " Directory {} already exists.".format(FullPath)
CheckHasErrors = True CheckHasErrors = True
except: except:
pass pass
@ -329,15 +329,15 @@ vmx = []
vmx.append('config.version = "8"') vmx.append('config.version = "8"')
vmx.append('virtualHW.version = "8"') vmx.append('virtualHW.version = "8"')
vmx.append('vmci0.present = "TRUE"') vmx.append('vmci0.present = "TRUE"')
vmx.append('displayName = "' + NAME + '"') vmx.append('displayName = "{}"'.format(NAME))
vmx.append('floppy0.present = "FALSE"') vmx.append('floppy0.present = "FALSE"')
vmx.append('numvcpus = "' + str(CPU) + '"') vmx.append('numvcpus = "{}"'.format(CPU))
vmx.append('scsi0.present = "TRUE"') vmx.append('scsi0.present = "TRUE"')
vmx.append('scsi0.sharedBus = "none"') vmx.append('scsi0.sharedBus = "none"')
vmx.append('scsi0.virtualDev = "pvscsi"') vmx.append('scsi0.virtualDev = "pvscsi"')
vmx.append('memsize = "' + str(MEM * 1024) + '"') vmx.append('memsize = "{}"'.format(MEM * 1024))
vmx.append('scsi0:0.present = "TRUE"') vmx.append('scsi0:0.present = "TRUE"')
vmx.append('scsi0:0.fileName = "' + NAME + '.vmdk"') vmx.append('scsi0:0.fileName = "{}.vmdk"'.format(NAME))
vmx.append('scsi0:0.deviceType = "scsi-hardDisk"') vmx.append('scsi0:0.deviceType = "scsi-hardDisk"')
if ISO == "": if ISO == "":
vmx.append('ide1:0.present = "TRUE"') vmx.append('ide1:0.present = "TRUE"')
@ -347,7 +347,7 @@ if ISO == "":
vmx.append('ide1:0.clientDevice = "TRUE"') vmx.append('ide1:0.clientDevice = "TRUE"')
else: else:
vmx.append('ide1:0.present = "TRUE"') vmx.append('ide1:0.present = "TRUE"')
vmx.append('ide1:0.fileName = "' + ISO + '"') vmx.append('ide1:0.fileName = "{}"'.format(ISO))
vmx.append('ide1:0.deviceType = "cdrom-image"') vmx.append('ide1:0.deviceType = "cdrom-image"')
vmx.append('pciBridge0.present = "TRUE"') vmx.append('pciBridge0.present = "TRUE"')
vmx.append('pciBridge4.present = "TRUE"') vmx.append('pciBridge4.present = "TRUE"')
@ -362,23 +362,23 @@ vmx.append('pciBridge6.functions = "8"')
vmx.append('pciBridge7.present = "TRUE"') vmx.append('pciBridge7.present = "TRUE"')
vmx.append('pciBridge7.virtualDev = "pcieRootPort"') vmx.append('pciBridge7.virtualDev = "pcieRootPort"')
vmx.append('pciBridge7.functions = "8"') vmx.append('pciBridge7.functions = "8"')
vmx.append('guestOS = "' + GUESTOS + '"') vmx.append('guestOS = "{}"'.format(GUESTOS))
if NET != "None": if NET != "None":
vmx.append('ethernet0.virtualDev = "vmxnet3"') vmx.append('ethernet0.virtualDev = "vmxnet3"')
vmx.append('ethernet0.present = "TRUE"') vmx.append('ethernet0.present = "TRUE"')
vmx.append('ethernet0.networkName = "' + NET + '"') vmx.append('ethernet0.networkName = "{}"'.format(NET))
if MAC == "": if MAC == "":
vmx.append('ethernet0.addressType = "generated"') vmx.append('ethernet0.addressType = "generated"')
else: else:
vmx.append('ethernet0.addressType = "static"') vmx.append('ethernet0.addressType = "static"')
vmx.append('ethernet0.address = "' + MAC + '"') vmx.append('ethernet0.address = "{}"'.format(MAC))
# #
# Merge extra VMX options # Merge extra VMX options
for VMXopt in VMXOPTS: for VMXopt in VMXOPTS:
try: try:
k, v = VMXopt.split("=") k, v = VMXopt.split("=")
except: except Exception:
k = "" k = ""
v = "" v = ""
key = k.lstrip().strip() key = k.lstrip().strip()
@ -386,7 +386,7 @@ for VMXopt in VMXOPTS:
for i in vmx: for i in vmx:
try: try:
ikey, ivalue = i.split("=") ikey, ivalue = i.split("=")
except: except Exception:
break break
if ikey.lstrip().strip().lower() == key.lower(): if ikey.lstrip().strip().lower() == key.lower():
index = vmx.index(i) index = vmx.index(i)
@ -458,31 +458,21 @@ if not isDryRun and not CheckHasErrors:
# #
# The output log string # The output log string
LogOutput += '"Host":"' + HOST + '",' LogOutput += '"Host":"{}","Port":"{}","Name":"{}",'.format(HOST, PORT, NAME)
LogOutput += '"Port":"' + str(PORT) + '",' LogOutput += '"CPU":"{}","Mem":"{}",'.format(CPU, MEM)
LogOutput += '"Name":"' + NAME + '",' LogOutput += '"Hdisk":"{}","DiskFormat":"{}","Virtual Device":"{}",'.format(HDISK, DISKFORMAT, VIRTDEV)
LogOutput += '"CPU":"' + str(CPU) + '",' LogOutput += '"Store":"{}","Store Used":"{}",'.format(STORE, DSPATH)
LogOutput += '"Mem":"' + str(MEM) + '",' LogOutput += '"Network":"{}",'.format(NET)
LogOutput += '"Hdisk":"' + str(HDISK) + '",' LogOutput += '"ISO":"{}","ISO used":"{}",'.format(ISOarg, ISO)
LogOutput += '"DiskFormat":"' + DISKFORMAT + '",' LogOutput += '"Guest OS":"{}",'.format(GUESTOS)
LogOutput += '"Virtual Device":"' + VIRTDEV + '",' LogOutput += '"MAC":"{}","MAC Used":"'.format(MACarg, GeneratedMAC)
LogOutput += '"Store":"' + STORE + '",' LogOutput += '"Dry Run":"{}","Verbose":"{}",'.format(isDryRun, isVerbose)
LogOutput += '"Store Used":"' + DSPATH + '",'
LogOutput += '"Network":"' + NET + '",'
LogOutput += '"ISO":"' + ISOarg + '",'
LogOutput += '"ISO used":"' + ISO + '",'
LogOutput += '"Guest OS":"' + GUESTOS + '",'
LogOutput += '"MAC":"' + MACarg + '",'
LogOutput += '"MAC Used":"' + GeneratedMAC + '",'
LogOutput += '"Dry Run":"' + str(isDryRun) + '",'
LogOutput += '"Verbose":"' + str(isVerbose) + '",'
if ErrorMessages != "": if ErrorMessages != "":
LogOutput += '"Error Message":"' + ErrorMessages + '",' LogOutput += '"Error Message":"{}",'.format(ErrorMessages)
LogOutput += '"Result":"' + Result + '",' LogOutput += '"Result":"{}","Completion Time":"{}"'.format(Result, the_current_date_time())
LogOutput += '"Completion Time":"' + str(the_current_date_time()) + '"'
LogOutput += '}\n' LogOutput += '}\n'
try: try:
with open(LOG, "a+w") as FD: with open(LOG, "a") as FD:
FD.write(LogOutput) FD.write(LogOutput)
except: except:
print("Error writing to log file: {}".format(LOG)) print("Error writing to log file: {}".format(LOG))

View File

@ -91,14 +91,11 @@ try:
if re.match("Version", str(stdout.readlines())) is not None: if re.match("Version", str(stdout.readlines())) is not None:
print("Unable to determine if this is a ESXi Host: {}, port: {}, username: {}".format(HOST, PORT, USER)) print("Unable to determine if this is a ESXi Host: {}, port: {}, username: {}".format(HOST, PORT, USER))
sys.exit(1) sys.exit(1)
except: except Exception as e:
print("The Error is {}".format(sys.exc_info()[0])) print("The Error is {}".format(e))
print("Unable to access ESXi Host: {}, port: {}, username: {}".format(HOST, PORT, USER)) print("Unable to access ESXi Host: {}, port: {}, username: {}".format(HOST, PORT, USER))
sys.exit(1) sys.exit(1)
#
# Check if VM exists
#
VMID = -1 VMID = -1
CheckHasWarnings = False CheckHasWarnings = False
@ -118,8 +115,8 @@ try:
ErrorMessages += " VM " + NAME + " doesn't exists." ErrorMessages += " VM " + NAME + " doesn't exists."
CheckHasErrors = True CheckHasErrors = True
CheckHasWarnings = True CheckHasWarnings = True
except: except Exception as e:
print("The Error is {}".format(sys.exc_info()[0])) print("The Error is {}".format(e))
sys.exit(1) sys.exit(1)
# Get List of Volumes, # Get List of Volumes,
@ -131,8 +128,8 @@ try:
for line in stdout.readlines(): for line in stdout.readlines():
splitLine = line.split() splitLine = line.split()
VOLUMES[splitLine[0]] = splitLine[1] VOLUMES[splitLine[0]] = splitLine[1]
except: except Exception as e:
print("The Error is {}".format(sys.exc_info()[0])) print("The Error is {}".format(e))
sys.exit(1) sys.exit(1)
@ -152,13 +149,10 @@ else:
if not CheckHasErrors: if not CheckHasErrors:
try: try:
for i in range(0, 10):
CurrentState = ""
CurrentStateCounter = 0
while CurrentStateCounter < 10:
if isVerbose: if isVerbose:
print("Get state of VM") print("Get state of VM")
(stdin, stdout, stderr) = ssh.exec_command("vim-cmd vmsvc/power.getstate " + str(VMID)) (stdin, stdout, stderr) = ssh.exec_command("vim-cmd vmsvc/power.getstate ".format((VMID)))
type(stdin) type(stdin)
lines = str(stdout.readlines()) + str(stderr.readlines()) lines = str(stdout.readlines()) + str(stderr.readlines())
if isVerbose: if isVerbose:
@ -166,22 +160,20 @@ if not CheckHasErrors:
if re.search("Powered off", lines): if re.search("Powered off", lines):
break break
# Power off VM
if isVerbose: if isVerbose:
print("Power OFF VM") print("Power OFF VM")
(stdin, stdout, stderr) = ssh.exec_command("vim-cmd vmsvc/power.off " + str(VMID) + " ||echo") (stdin, stdout, stderr) = ssh.exec_command("vim-cmd vmsvc/power.off {} ||echo".format(VMID))
type(stdin) type(stdin)
lines = str(stdout.readlines()) + str(stderr.readlines()) lines = str(stdout.readlines()) + str(stderr.readlines())
if isVerbose: if isVerbose:
print("power.off: {}".format(lines)) print("power.off: {}".format(lines))
CurrentStateCounter += 1
time.sleep(1) time.sleep(1)
# destroy VM # destroy VM
if isVerbose: if isVerbose:
print("Destroy VM") print("Destroy VM")
(stdin, stdout, stderr) = ssh.exec_command("vim-cmd vmsvc/destroy " + str(VMID)) (stdin, stdout, stderr) = ssh.exec_command("vim-cmd vmsvc/destroy {}".format(VMID))
type(stdin) type(stdin)
lines = str(stdout.readlines()) + str(stderr.readlines()) lines = str(stdout.readlines()) + str(stderr.readlines())
if isVerbose: if isVerbose:
@ -193,24 +185,17 @@ if not CheckHasErrors:
CheckHasErrors = True CheckHasErrors = True
Result = "Fail" Result = "Fail"
# Print Summary LogOutput += '"Host":"{}","Name":"{}","Store Used":"{}","Verbose":"{}"'.format(HOST, NAME, DSPATH, isVerbose)
#
# The output log string
LogOutput += '"Host":"' + HOST + '",'
LogOutput += '"Name":"' + NAME + '",'
LogOutput += '"Store Used":"' + DSPATH + '",'
LogOutput += '"Verbose":"' + str(isVerbose) + '",'
if ErrorMessages != "": if ErrorMessages != "":
LogOutput += '"Error Message":"' + ErrorMessages + '",' LogOutput += '"Error Message":"{}",'.format(ErrorMessages)
LogOutput += '"Result":"' + Result + '",' LogOutput += '"Result":"{}","Completion Time":"{}"'.format(Result, the_current_date_time())
LogOutput += '"Completion Time":"' + str(the_current_date_time()) + '"'
LogOutput += '}\n' LogOutput += '}\n'
try: try:
with open(LOG, "a+w") as FD: with open(LOG, "a") as FD:
FD.write(LogOutput) FD.write(LogOutput)
except: except Exception as e:
print("Error writing to log file: {}".format(LOG)) print("Error writing to log file: {}".format(e))
if isSummary: if isSummary:
if isVerbose: if isVerbose:

View File

@ -3,14 +3,6 @@ import os.path
import sys import sys
import yaml import yaml
import datetime # For current Date/Time import datetime # For current Date/Time
from math import log
#
#
# Functions
#
#
def setup_config(): def setup_config():
@ -79,9 +71,8 @@ def setup_config():
with open(config_data_file_location, 'w') as FD: with open(config_data_file_location, 'w') as FD:
yaml.dump(config_data, FD, default_flow_style=False) yaml.dump(config_data, FD, default_flow_style=False)
FD.close() FD.close()
except: except Exception as e:
print("Unable to create/update config file {}".format(config_data_file_location)) print("Unable to create/update config file {}".format(config_data_file_location))
e = sys.exc_info()[0]
print("The Error is {}".format(e)) print("The Error is {}".format(e))
sys.exit(1) sys.exit(1)
return config_data return config_data
@ -93,9 +84,8 @@ def save_config(config_data):
with open(config_data_file_location, 'w') as FD: with open(config_data_file_location, 'w') as FD:
yaml.dump(config_data, FD, default_flow_style=False) yaml.dump(config_data, FD, default_flow_style=False)
FD.close() FD.close()
except: except Exception as e:
print("Unable to create/update config file {}".format(config_data_file_location)) print("Unable to create/update config file {}".format(config_data_file_location))
e = sys.exc_info()[0]
print("The Error is {}".format(e)) print("The Error is {}".format(e))
return 1 return 1
return 0 return 0