Fix a bug when a VMX file is generated: double-quotes were removed and thus VMX files were invalid. Add single-quotes (quick and dirty solution for the moment)

master
Sebastien Andrivet 2018-01-07 23:50:49 +01:00
parent 59c4a5b11c
commit aa5960eec6
1 changed files with 62 additions and 62 deletions

View File

@ -37,8 +37,8 @@ GeneratedMAC = ""
ISOfound = False ISOfound = False
CheckHasErrors = False CheckHasErrors = False
LeastUsedDS = "" LeastUsedDS = ""
DSPATH="" DSPATH = ""
DSSTORE="" DSSTORE = ""
FullPathExists = False FullPathExists = False
# #
@ -48,7 +48,7 @@ parser = argparse.ArgumentParser(description='ESXi Create VM utility.')
parser.add_argument('-d', '--dry', dest='isDryRunarg', action='store_true', help="Enable Dry Run mode (" + str(isDryRun) + ")") parser.add_argument('-d', '--dry', dest='isDryRunarg', action='store_true', help="Enable Dry Run mode (" + str(isDryRun) + ")")
parser.add_argument("-H", "--Host", dest='HOST', type=str, help="ESXi Host/IP (" + str(HOST) + ")") parser.add_argument("-H", "--Host", dest='HOST', type=str, help="ESXi Host/IP (" + str(HOST) + ")")
parser.add_argument("-T", "--Port", dest='PORT', type=str, help="ESXi Port number (" + str(PORT) + ")") parser.add_argument("-T", "--Port", dest='PORT', type=int, help="ESXi Port number (" + str(PORT) + ")")
parser.add_argument("-U", "--User", dest='USER', type=str, help="ESXi Host username (" + str(USER) + ")") parser.add_argument("-U", "--User", dest='USER', type=str, help="ESXi Host username (" + str(USER) + ")")
parser.add_argument("-P", "--Password", dest='PASSWORD', type=str, help="ESXi Host password (*****)") parser.add_argument("-P", "--Password", dest='PASSWORD', type=str, help="ESXi Host password (*****)")
parser.add_argument("-K", "--Key", dest='KEY', type=str, help="ESXi Host connection key (path to private key)") parser.add_argument("-K", "--Key", dest='KEY', type=str, help="ESXi Host connection key (path to private key)")
@ -65,7 +65,7 @@ parser.add_argument("-o", "--options", dest='VMXOPTS', type=str, default='NIL',
parser.add_argument('-V', '--verbose', dest='isVerbosearg', action='store_true', help="Enable Verbose mode (" + str(isVerbose) + ")") parser.add_argument('-V', '--verbose', dest='isVerbosearg', action='store_true', help="Enable Verbose mode (" + str(isVerbose) + ")")
parser.add_argument('--summary', dest='isSummaryarg', action='store_true', help="Display Summary (" + str(isSummary) + ")") parser.add_argument('--summary', dest='isSummaryarg', action='store_true', help="Display Summary (" + str(isSummary) + ")")
parser.add_argument("-u", "--updateDefaults", dest='UPDATE', action='store_true', help="Update Default VM settings stored in ~/.esxi-vm.yml") parser.add_argument("-u", "--updateDefaults", dest='UPDATE', action='store_true', help="Update Default VM settings stored in ~/.esxi-vm.yml")
#parser.add_argument("--showDefaults", dest='SHOW', action='store_true', help="Show Default VM settings stored in ~/.esxi-vm.yml") # parser.add_argument("--showDefaults", dest='SHOW', action='store_true', help="Show Default VM settings stored in ~/.esxi-vm.yml")
args = parser.parse_args() args = parser.parse_args()
@ -107,9 +107,9 @@ if STORE == "":
if args.GUESTOS: if args.GUESTOS:
GUESTOS=args.GUESTOS GUESTOS=args.GUESTOS
if args.VMXOPTS == '' and VMXOPTS != '': if args.VMXOPTS == '' and VMXOPTS != '':
VMXOPTS='' VMXOPTS = ''
if args.VMXOPTS and args.VMXOPTS != 'NIL': if args.VMXOPTS and args.VMXOPTS != 'NIL':
VMXOPTS=args.VMXOPTS.split(",") VMXOPTS = args.VMXOPTS.split(",")
if args.UPDATE: if args.UPDATE:
@ -320,80 +320,80 @@ except:
# #
# Create the VM # Create the VM
# #
VMX = [] 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 = "' + NAME + '"')
VMX.append('floppy0.present = "FALSE"') vmx.append('floppy0.present = "FALSE"')
VMX.append('numvcpus = "' + str(CPU) + '"') vmx.append('numvcpus = "' + str(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 = "' + str(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 = "' + NAME + '.vmdk"')
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"')
VMX.append('ide1:0.fileName = "emptyBackingString"') vmx.append('ide1:0.fileName = "emptyBackingString"')
VMX.append('ide1:0.deviceType = "atapi-cdrom"') vmx.append('ide1:0.deviceType = "atapi-cdrom"')
VMX.append('ide1:0.startConnected = "FALSE"') vmx.append('ide1:0.startConnected = "FALSE"')
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 = "' + 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"')
VMX.append('pciBridge4.virtualDev = "pcieRootPort"') vmx.append('pciBridge4.virtualDev = "pcieRootPort"')
VMX.append('pciBridge4.functions = "8"') vmx.append('pciBridge4.functions = "8"')
VMX.append('pciBridge5.present = "TRUE"') vmx.append('pciBridge5.present = "TRUE"')
VMX.append('pciBridge5.virtualDev = "pcieRootPort"') vmx.append('pciBridge5.virtualDev = "pcieRootPort"')
VMX.append('pciBridge5.functions = "8"') vmx.append('pciBridge5.functions = "8"')
VMX.append('pciBridge6.present = "TRUE"') vmx.append('pciBridge6.present = "TRUE"')
VMX.append('pciBridge6.virtualDev = "pcieRootPort"') vmx.append('pciBridge6.virtualDev = "pcieRootPort"')
VMX.append('pciBridge6.functions = "8"') 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 = "' + 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 = "' + 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 = "' + 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:
k="" k = ""
v="" v = ""
key = k.lstrip().strip() key = k.lstrip().strip()
value = v.lstrip().strip() value = v.lstrip().strip()
for i in VMX: for i in vmx:
try: try:
ikey,ivalue = i.split("=") ikey, ivalue = i.split("=")
except: except:
break break
if ikey.lstrip().strip().lower() == key.lower(): if ikey.lstrip().strip().lower() == key.lower():
index = VMX.index(i) index = vmx.index(i)
VMX[index] = ikey + " = " + value vmx[index] = ikey + " = " + value
break break
else: else:
if key != '' and value != '': if key != '' and value != '':
VMX.append(key + " = " + value) vmx.append(key + " = " + value)
if isVerbose and VMXOPTS != '': if isVerbose and VMXOPTS != '':
print "VMX file:" print "VMX file:"
for i in VMX: for i in vmx:
print i print i
MyVM = FullPath + "/" + NAME MyVM = FullPath + "/" + NAME
@ -410,8 +410,8 @@ if not isDryRun and not CheckHasErrors:
print "Create " + NAME + ".vmx file" print "Create " + NAME + ".vmx file"
(stdin, stdout, stderr) = ssh.exec_command("mkdir " + FullPath ) (stdin, stdout, stderr) = ssh.exec_command("mkdir " + FullPath )
type(stdin) type(stdin)
for line in VMX: for line in vmx:
(stdin, stdout, stderr) = ssh.exec_command("echo " + line + " >>" + MyVM + ".vmx") (stdin, stdout, stderr) = ssh.exec_command("echo '" + line + "' >>" + MyVM + ".vmx")
type(stdin) type(stdin)
# Create vmdk # Create vmdk
@ -433,7 +433,7 @@ if not isDryRun and not CheckHasErrors:
(stdin, stdout, stderr) = ssh.exec_command("vim-cmd vmsvc/power.on " + str(VMID)) (stdin, stdout, stderr) = ssh.exec_command("vim-cmd vmsvc/power.on " + str(VMID))
type(stdin) type(stdin)
if stderr.readlines(): if stderr.readlines():
print "Error Power.on VM." print "Error Powering-on VM."
Result="Fail" Result="Fail"
# Get Generated MAC # Get Generated MAC
@ -453,7 +453,7 @@ if not isDryRun and not CheckHasErrors:
# #
# The output log string # The output log string
LogOutput += '"Host":"' + HOST + '",' LogOutput += '"Host":"' + HOST + '",'
LogOutput += '"Port":"' + PORT + '",' LogOutput += '"Port":"' + str(PORT) + '",'
LogOutput += '"Name":"' + NAME + '",' LogOutput += '"Name":"' + NAME + '",'
LogOutput += '"CPU":"' + str(CPU) + '",' LogOutput += '"CPU":"' + str(CPU) + '",'
LogOutput += '"Mem":"' + str(MEM) + '",' LogOutput += '"Mem":"' + str(MEM) + '",'