mirror of https://github.com/Kodomo/esxi-vm
Some cleaning
parent
249ed34bec
commit
3fc2aa89b4
|
@ -6,7 +6,7 @@ import paramiko # For remote ssh
|
||||||
|
|
||||||
from esxi_vm_functions import *
|
from esxi_vm_functions import *
|
||||||
|
|
||||||
# Defaults and Variable setup
|
# Defaults and Variable setup
|
||||||
ConfigData = setup_config()
|
ConfigData = setup_config()
|
||||||
NAME = ""
|
NAME = ""
|
||||||
LOG = ConfigData['LOG']
|
LOG = ConfigData['LOG']
|
||||||
|
@ -157,14 +157,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 " + str(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)
|
||||||
|
|
||||||
#
|
|
||||||
# Get list of DataStores, store in VOLUMES
|
|
||||||
#
|
|
||||||
try:
|
try:
|
||||||
(stdin, stdout, stderr) = \
|
(stdin, stdout, stderr) = \
|
||||||
ssh.exec_command("esxcli storage filesystem list |grep '/vmfs/volumes/.*true VMFS' |sort -nk7")
|
ssh.exec_command("esxcli storage filesystem list |grep '/vmfs/volumes/.*true VMFS' |sort -nk7")
|
||||||
|
@ -182,9 +179,6 @@ if STORE == "LeastUsed":
|
||||||
STORE = LeastUsedDS
|
STORE = LeastUsedDS
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
# Get list of Networks available, store in VMNICS
|
|
||||||
#
|
|
||||||
try:
|
try:
|
||||||
(stdin, stdout, stderr) = \
|
(stdin, stdout, stderr) = \
|
||||||
ssh.exec_command("esxcli network vswitch standard list|grep Portgroups|sed 's/^ Portgroups: //g'")
|
ssh.exec_command("esxcli network vswitch standard list|grep Portgroups|sed 's/^ Portgroups: //g'")
|
||||||
|
@ -197,9 +191,6 @@ except Exception as e:
|
||||||
print("The Error is {}".format(e))
|
print("The Error is {}".format(e))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
#
|
|
||||||
# Check MAC address
|
|
||||||
#
|
|
||||||
MACarg = MAC
|
MACarg = MAC
|
||||||
if MAC != "":
|
if MAC != "":
|
||||||
MACregex = '^([a-fA-F0-9]{2}[:|\-]){5}[a-fA-F0-9]{2}$'
|
MACregex = '^([a-fA-F0-9]{2}[:|\-]){5}[a-fA-F0-9]{2}$'
|
||||||
|
@ -213,10 +204,6 @@ if MAC != "":
|
||||||
ErrorMessages += " " + MAC + " Invalid MAC address."
|
ErrorMessages += " " + MAC + " Invalid MAC address."
|
||||||
CheckHasErrors = True
|
CheckHasErrors = True
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
# Get from ESXi host if ISO exists
|
|
||||||
#
|
|
||||||
ISOarg = ISO
|
ISOarg = ISO
|
||||||
if ISO == "None":
|
if ISO == "None":
|
||||||
ISO = ""
|
ISO = ""
|
||||||
|
@ -238,13 +225,10 @@ if ISO != "":
|
||||||
if stdout.readlines() and not stderr.readlines():
|
if stdout.readlines() and not stderr.readlines():
|
||||||
ISOfound = True
|
ISOfound = 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)
|
||||||
|
|
||||||
#
|
|
||||||
# Check if VM already exists
|
|
||||||
#
|
|
||||||
VMID = -1
|
VMID = -1
|
||||||
try:
|
try:
|
||||||
(stdin, stdout, stderr) = ssh.exec_command("vim-cmd vmsvc/getallvms")
|
(stdin, stdout, stderr) = ssh.exec_command("vim-cmd vmsvc/getallvms")
|
||||||
|
@ -256,14 +240,10 @@ try:
|
||||||
print("ERROR: VM {} already exists.".format(NAME))
|
print("ERROR: VM {} already exists.".format(NAME))
|
||||||
ErrorMessages += " VM " + NAME + " already exists."
|
ErrorMessages += " VM " + NAME + " already exists."
|
||||||
CheckHasErrors = True
|
CheckHasErrors = 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)
|
||||||
|
|
||||||
#
|
|
||||||
# Do checks here
|
|
||||||
#
|
|
||||||
|
|
||||||
# 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))
|
||||||
|
@ -319,12 +299,9 @@ try:
|
||||||
print("ERROR: Directory {} already exists.".format(FullPath))
|
print("ERROR: Directory {} already exists.".format(FullPath))
|
||||||
ErrorMessages += " Directory {} already exists.".format(FullPath)
|
ErrorMessages += " Directory {} already exists.".format(FullPath)
|
||||||
CheckHasErrors = True
|
CheckHasErrors = True
|
||||||
except:
|
except Exception as e:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
#
|
|
||||||
# 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"')
|
||||||
|
@ -409,8 +386,6 @@ else:
|
||||||
|
|
||||||
if not isDryRun and not CheckHasErrors:
|
if not isDryRun and not CheckHasErrors:
|
||||||
try:
|
try:
|
||||||
|
|
||||||
# Create NAME.vmx
|
|
||||||
if isVerbose:
|
if isVerbose:
|
||||||
print("Create {}.vmx file".format(NAME))
|
print("Create {}.vmx file".format(NAME))
|
||||||
(stdin, stdout, stderr) = ssh.exec_command("mkdir " + FullPath)
|
(stdin, stdout, stderr) = ssh.exec_command("mkdir " + FullPath)
|
||||||
|
@ -419,21 +394,18 @@ if not isDryRun and not CheckHasErrors:
|
||||||
(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
|
|
||||||
if isVerbose:
|
if isVerbose:
|
||||||
print("Create {}.vmdk file".format(NAME))
|
print("Create {}.vmdk file".format(NAME))
|
||||||
(stdin, stdout, stderr) = \
|
(stdin, stdout, stderr) = \
|
||||||
ssh.exec_command("vmkfstools -c " + str(HDISK) + "G -d " + DISKFORMAT + " " + MyVM + ".vmdk")
|
ssh.exec_command("vmkfstools -c " + str(HDISK) + "G -d " + DISKFORMAT + " " + MyVM + ".vmdk")
|
||||||
type(stdin)
|
type(stdin)
|
||||||
|
|
||||||
# Register VM
|
|
||||||
if isVerbose:
|
if isVerbose:
|
||||||
print("Register VM")
|
print("Register VM")
|
||||||
(stdin, stdout, stderr) = ssh.exec_command("vim-cmd solo/registervm " + MyVM + ".vmx")
|
(stdin, stdout, stderr) = ssh.exec_command("vim-cmd solo/registervm " + MyVM + ".vmx")
|
||||||
type(stdin)
|
type(stdin)
|
||||||
VMID = int(stdout.readlines()[0])
|
VMID = int(stdout.readlines()[0])
|
||||||
|
|
||||||
# Power on VM
|
|
||||||
if isVerbose:
|
if isVerbose:
|
||||||
print("Power ON VM")
|
print("Power ON VM")
|
||||||
(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))
|
||||||
|
@ -442,22 +414,17 @@ if not isDryRun and not CheckHasErrors:
|
||||||
print("Error Powering-on VM.")
|
print("Error Powering-on VM.")
|
||||||
Result = "Fail"
|
Result = "Fail"
|
||||||
|
|
||||||
# Get Generated MAC
|
|
||||||
if NET != "None":
|
if NET != "None":
|
||||||
(stdin, stdout, stderr) = ssh.exec_command(
|
(stdin, stdout, stderr) = ssh.exec_command(
|
||||||
"grep -i 'ethernet0.*ddress = ' " + MyVM + ".vmx |tail -1|awk '{print $NF}'")
|
"grep -i 'ethernet0.*ddress = ' " + MyVM + ".vmx |tail -1|awk '{print $NF}'")
|
||||||
type(stdin)
|
type(stdin)
|
||||||
GeneratedMAC = str(stdout.readlines()[0]).strip('\n"')
|
GeneratedMAC = str(stdout.readlines()[0]).strip('\n"')
|
||||||
|
|
||||||
except:
|
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"
|
||||||
|
|
||||||
# Print Summary
|
|
||||||
|
|
||||||
#
|
|
||||||
# The output log string
|
|
||||||
LogOutput += '"Host":"{}","Port":"{}","Name":"{}",'.format(HOST, PORT, NAME)
|
LogOutput += '"Host":"{}","Port":"{}","Name":"{}",'.format(HOST, PORT, NAME)
|
||||||
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)
|
||||||
|
@ -474,7 +441,7 @@ LogOutput += '}\n'
|
||||||
try:
|
try:
|
||||||
with open(LOG, "a") 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(LOG))
|
||||||
|
|
||||||
if isSummary:
|
if isSummary:
|
||||||
|
|
|
@ -152,7 +152,7 @@ if not CheckHasErrors:
|
||||||
for i in range(0, 10):
|
for i in range(0, 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 ".format((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:
|
||||||
|
@ -179,9 +179,9 @@ if not CheckHasErrors:
|
||||||
if isVerbose:
|
if isVerbose:
|
||||||
print("destroy: {}".format(lines))
|
print("destroy: {}".format(lines))
|
||||||
|
|
||||||
except:
|
except Exception as e:
|
||||||
print("There was an error destroying the VM.")
|
print("There was an error destroying the VM: {}".format(e))
|
||||||
ErrorMessages += " There was an error destroying the VM."
|
ErrorMessages += " There was an error destroying the VM: {}".format(e)
|
||||||
CheckHasErrors = True
|
CheckHasErrors = True
|
||||||
Result = "Fail"
|
Result = "Fail"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue