ESXI v11 + Resource Pools

master
ifiguero 2020-07-23 22:40:55 -04:00
parent 205f089605
commit eea1aa3a53
2 changed files with 60 additions and 33 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/python3 #!/usr/bin/python3
import argparse # Argument parser import argparse # Argument parser
import os
from esxi_vm_functions import * from esxi_vm_functions import *
@ -26,6 +26,7 @@ VIRTDEV = ConfigData['VIRTDEV']
STORE = ConfigData['STORE'] STORE = ConfigData['STORE']
NET = ConfigData['NET'] NET = ConfigData['NET']
ISO = ConfigData['ISO'] ISO = ConfigData['ISO']
POOL = ConfigData['POOL']
GUESTOS = ConfigData['GUESTOS'] GUESTOS = ConfigData['GUESTOS']
VMXOPTS = ConfigData['VMXOPTS'] VMXOPTS = ConfigData['VMXOPTS']
@ -52,6 +53,7 @@ parser.add_argument("-U", "--User", dest='USER', type=str, help="ESXi Host usern
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 path to private key ({})".format(KEY)) parser.add_argument("-K", "--Key", dest='KEY', type=str, help="ESXi Host path to private key ({})".format(KEY))
parser.add_argument("-n", "--name", dest='NAME', type=str, help="VM name ()".format(NAME)) parser.add_argument("-n", "--name", dest='NAME', type=str, help="VM name ()".format(NAME))
parser.add_argument("-p", "--pool", dest='POOL', type=str, help="Resource Pool for the VM ({})".format(POOL))
parser.add_argument("-c", "--cpu", dest='CPU', type=int, help="Number of vCPUS ({})".format(CPU)) parser.add_argument("-c", "--cpu", dest='CPU', type=int, help="Number of vCPUS ({})".format(CPU))
parser.add_argument("-m", "--mem", type=int, help="Memory in MB ({})".format(MEM)) parser.add_argument("-m", "--mem", type=int, help="Memory in MB ({})".format(MEM))
parser.add_argument("-v", "--vdisk", dest='HDISK', type=str, help="Size of virt hdisk ({})".format(HDISK)) parser.add_argument("-v", "--vdisk", dest='HDISK', type=str, help="Size of virt hdisk ({})".format(HDISK))
@ -83,6 +85,8 @@ if args.LOG or args.writeLog:
writeLog = True writeLog = True
if args.isSummaryarg: if args.isSummaryarg:
isSummary = True isSummary = True
if args.POOL:
POOL = args.POOL
if args.HOST: if args.HOST:
HOST = args.HOST HOST = args.HOST
if args.PORT: if args.PORT:
@ -136,6 +140,7 @@ if args.UPDATE:
ConfigData['VIRTDEV'] = VIRTDEV ConfigData['VIRTDEV'] = VIRTDEV
ConfigData['STORE'] = STORE ConfigData['STORE'] = STORE
ConfigData['NET'] = NET ConfigData['NET'] = NET
ConfigData['POOL'] = POOL
ConfigData['ISO'] = ISO ConfigData['ISO'] = ISO
ConfigData['GUESTOS'] = GUESTOS ConfigData['GUESTOS'] = GUESTOS
ConfigData['VMXOPTS'] = VMXOPTS ConfigData['VMXOPTS'] = VMXOPTS
@ -235,6 +240,16 @@ except Exception as e:
print("The Error is {}".format(e)) print("The Error is {}".format(e))
sys.exit(1) sys.exit(1)
try:
(stdin, stdout, stderr) = exec_ssh_command("Check for the Reource Pool", "vim-cmd hostsvc/rsrc/pool_config_get {}".format(POOL), ssh, isVerbose)
if stdout.readlines() and not stderr.readlines():
print("ERROR: Resource Pool '{}' not found.".format(POOL))
sys.exit(1)
except Exception as e:
print("The Error is {}".format(e))
sys.exit(1)
# 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))
@ -293,20 +308,12 @@ except Exception as e:
pass pass
vmx = { vmx = {
'.encoding': 'UTF-8'
'config.version': '8', 'config.version': '8',
'virtualHW.version': '8', 'virtualHW.version': '11',
'vmci0.present': 'TRUE', 'nvram': u'{}.nvram'.format(NAME)
'displayName': NAME,
'floppy0.present': 'FALSE',
'numvcpus': CPU,
'scsi0.present': 'TRUE',
'scsi0.sharedBus': 'none',
'scsi0.virtualDev': 'pvscsi',
'memsize': str(MEM),
'scsi0:0.present': 'TRUE',
'scsi0:0.fileName': "{}.vmdk".format(NAME),
'scsi0:0.deviceType': 'scsi-hardDisk',
'pciBridge0.present': 'TRUE', 'pciBridge0.present': 'TRUE',
'svga.present': 'TRUE',
'pciBridge4.present': 'TRUE', 'pciBridge4.present': 'TRUE',
'pciBridge4.virtualDev': 'pcieRootPort', 'pciBridge4.virtualDev': 'pcieRootPort',
'pciBridge4.functions': '8', 'pciBridge4.functions': '8',
@ -319,14 +326,38 @@ vmx = {
'pciBridge7.present': 'TRUE', 'pciBridge7.present': 'TRUE',
'pciBridge7.virtualDev': 'pcieRootPort', 'pciBridge7.virtualDev': 'pcieRootPort',
'pciBridge7.functions': '8', 'pciBridge7.functions': '8',
'vmci0.present': 'TRUE',
'hpet0.present': 'TRUE',
'numvcpus': CPU,
'cpuid.coresPerSocket': CPU,
'memsize': str(MEM),
'powerType.powerOff': 'default',
'powerType.suspend': 'default',
'powerType.reset': 'default',
'tools.upgrade.policy': 'manual',
'scsi0.virtualDev': 'pvscsi',
'scsi0.present': 'TRUE',
'scsi0:0.present': 'TRUE',
'scsi0:0.fileName': "{}.vmdk".format(NAME),
'scsi0:0.deviceType': 'scsi-hardDisk',
'guestOS': GUESTOS 'guestOS': GUESTOS
'displayName': NAME,
'annotation': 'VM created by {} ({}@{}) at {}'.format(USER,os.getlogin(),local_host_name(),the_current_date_time()),
'floppy0.present': 'FALSE',
'chipset.onlineStandby': 'FALSE',
'tools.syncTime': 'FALSE',
'toolScripts.afterPowerOn': 'TRUE',
'toolScripts.afterResume': 'TRUE',
'toolScripts.beforeSuspend': 'TRUE',
'toolScripts.beforePowerOff': 'TRUE',
} }
if ISO == "": if ISO == "":
vmx.update({ vmx.update({
'ide1:0.present': 'TRUE', 'ide1:0.present': 'TRUE',
'ide1:0.fileName': 'emptyBackingString', 'ide1:0.fileName': 'CD/DVD drive 0',
'ide1:0.deviceType': 'atapi-cdrom', 'ide1:0.deviceType': 'cdrom-raw',
'ide1:0.startConnected': 'FALSE', 'ide1:0.startConnected': 'FALSE',
'ide1:0.clientDevice': 'TRUE' 'ide1:0.clientDevice': 'TRUE'
}) })
@ -334,7 +365,7 @@ else:
vmx.update({ vmx.update({
'ide1:0.present': 'TRUE', 'ide1:0.present': 'TRUE',
'ide1:0.fileName': ISO, 'ide1:0.fileName': ISO,
'ide1:0.deviceType': 'atapi-cdrom', 'ide1:0.deviceType': 'cdrom-raw',
'ide1:0.startConnected': 'TRUE', 'ide1:0.startConnected': 'TRUE',
}) })
@ -386,34 +417,22 @@ if not isDryRun and not CheckHasErrors:
ssh, isVerbose) ssh, isVerbose)
(stdin, stdout, stderr) = exec_ssh_command("Register VM", (stdin, stdout, stderr) = exec_ssh_command("Register VM",
"vim-cmd solo/registervm {}.vmx".format(MyVM), ssh, isVerbose) "vim-cmd solo/registervm {}.vmx {} {}".format(MyVM, NAME, POOL), ssh, isVerbose)
VMID = int(stdout.readlines()[0]) VMID = int(stdout.readlines()[0])
(stdin, stdout, stderr) = exec_ssh_command("Power ON VM", "vim-cmd vmsvc/power.on {}".format(VMID),
ssh, isVerbose)
if stderr.readlines():
print("Error Powering-on VM.")
Result = "Fail"
if NET != "None":
(stdin, stdout, stderr) = exec_ssh_command("Get MAC address",
"grep -i 'ethernet0.*ddress = ' {}.vmx".format(MyVM) +
" |tail -1|awk '{print $NF}'", ssh, isVerbose)
GeneratedMAC = str(stdout.readlines()[0]).strip('\n"')
except Exception as e: except Exception as e:
print("There was an error creating the VM.") print("There was an error creating the VM.")
ErrorMessages += " There was an error creating the VM." ErrorMessages += " There was an error creating the VM."
Result = "Fail" Result = "Fail"
LogOutput += '"Host":"{}","Port":"{}","Name":"{}",'.format(HOST, PORT, NAME) LogOutput += '"Host":"{}","Port":"{}","Name":"{}","POOL":"{}"'.format(HOST, PORT, NAME, POOL)
LogOutput += '"CPU":"{}","Mem":"{}",'.format(CPU, MEM) LogOutput += '"CPU":"{}","Mem":"{}",'.format(CPU, MEM)
LogOutput += '"Hdisk":"{}","DiskFormat":"{}","Virtual Device":"{}",'.format(HDISK, DISKFORMAT, VIRTDEV) LogOutput += '"Hdisk":"{}","DiskFormat":"{}","Virtual Device":"{}",'.format(HDISK, DISKFORMAT, VIRTDEV)
LogOutput += '"Store":"{}","Store Used":"{}",'.format(STORE, DSPATH) LogOutput += '"Store":"{}","Store Used":"{}",'.format(STORE, DSPATH)
LogOutput += '"Network":"{}",'.format(NET) LogOutput += '"Network":"{}",'.format(NET)
LogOutput += '"ISO":"{}","ISO used":"{}",'.format(ISOarg, ISO) LogOutput += '"ISO":"{}","ISO used":"{}",'.format(ISOarg, ISO)
LogOutput += '"Guest OS":"{}",'.format(GUESTOS) LogOutput += '"Guest OS":"{}",'.format(GUESTOS)
LogOutput += '"MAC":"{}","MAC Used":"'.format(MACarg, GeneratedMAC)
LogOutput += '"Dry Run":"{}","Verbose":"{}",'.format(isDryRun, isVerbose) LogOutput += '"Dry Run":"{}","Verbose":"{}",'.format(isDryRun, isVerbose)
if ErrorMessages != "": if ErrorMessages != "":
LogOutput += '"Error Message":"{}",'.format(ErrorMessages) LogOutput += '"Error Message":"{}",'.format(ErrorMessages)
@ -436,6 +455,7 @@ if isSummary:
if isVerbose: if isVerbose:
print("ESXi Host: {}".format(HOST)) print("ESXi Host: {}".format(HOST))
print("ESXi Port: {}".format(PORT)) print("ESXi Port: {}".format(PORT))
print("RESOURCE POOL: {}".format(POOL))
print("VM NAME: {}".format(NAME)) print("VM NAME: {}".format(NAME))
print("vCPU: {}".format(CPU)) print("vCPU: {}".format(CPU))
print("Memory: {} MB".format(MEM)) print("Memory: {} MB".format(MEM))
@ -448,7 +468,7 @@ if isSummary:
print("ISO: {}".format(ISO)) print("ISO: {}".format(ISO))
if isVerbose: if isVerbose:
print("Guest OS: {}".format(GUESTOS)) print("Guest OS: {}".format(GUESTOS))
print("MAC: {}".format(GeneratedMAC))
else: else:
pass pass
@ -459,6 +479,5 @@ if CheckHasErrors:
else: else:
if isDryRun: if isDryRun:
print("Dry Run: Success.") print("Dry Run: Success.")
else:
print(GeneratedMAC)
sys.exit(0) sys.exit(0)

View File

@ -56,6 +56,7 @@ def setup_config():
# Default GuestOS type. (See VMware documentation for all available options) # Default GuestOS type. (See VMware documentation for all available options)
GUESTOS="debian8-64", GUESTOS="debian8-64",
POOL="ha-root-pool",
# Extra VMX options # Extra VMX options
VMXOPTS="" VMXOPTS=""
@ -94,6 +95,13 @@ def save_config(config_data):
return 0 return 0
def local_host_name():
import socket
try:
return str(socket.gethostname())
except:
return 'localhost'
def the_current_date_time(): def the_current_date_time():
i = datetime.datetime.now() i = datetime.datetime.now()
return str(i.isoformat()) return str(i.isoformat())