mirror of https://github.com/Kodomo/esxi-vm
Make verbose actually verbose (output SSH commands)
parent
f6e17f5c5d
commit
d7c4e3cfda
|
@ -7,6 +7,14 @@ import paramiko # For remote ssh
|
||||||
from esxi_vm_functions import *
|
from esxi_vm_functions import *
|
||||||
|
|
||||||
|
|
||||||
|
def exec_ssh_command(message, command, verbose):
|
||||||
|
if verbose:
|
||||||
|
if message:
|
||||||
|
print(message)
|
||||||
|
print("SSH: " + command)
|
||||||
|
return ssh.exec_command(command)
|
||||||
|
|
||||||
|
|
||||||
# Defaults and Variable setup
|
# Defaults and Variable setup
|
||||||
ConfigData = setup_config()
|
ConfigData = setup_config()
|
||||||
NAME = ""
|
NAME = ""
|
||||||
|
@ -153,8 +161,7 @@ try:
|
||||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||||
ssh.connect(HOST, port=PORT, username=USER, password=PASSWORD, key_filename=KEY)
|
ssh.connect(HOST, port=PORT, username=USER, password=PASSWORD, key_filename=KEY)
|
||||||
|
|
||||||
(stdin, stdout, stderr) = ssh.exec_command("esxcli system version get |grep Version")
|
(stdin, stdout, stderr) = exec_ssh_command("Get ESXi version", "esxcli system version get |grep Version", isVerbose)
|
||||||
type(stdin)
|
|
||||||
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)
|
||||||
|
@ -165,8 +172,9 @@ except Exception as e:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
(stdin, stdout, stderr) = \
|
(stdin, stdout, stderr) = \
|
||||||
ssh.exec_command("esxcli storage filesystem list |grep '/vmfs/volumes/.*true VMFS' |sort -nk7")
|
exec_ssh_command("Get existing volumes",
|
||||||
type(stdin)
|
"esxcli storage filesystem list |grep '/vmfs/volumes/.*true VMFS' |sort -nk7",
|
||||||
|
isVerbose)
|
||||||
VOLUMES = {}
|
VOLUMES = {}
|
||||||
for line in stdout.readlines():
|
for line in stdout.readlines():
|
||||||
splitLine = line.split()
|
splitLine = line.split()
|
||||||
|
@ -182,8 +190,9 @@ if STORE == "LeastUsed":
|
||||||
|
|
||||||
try:
|
try:
|
||||||
(stdin, stdout, stderr) = \
|
(stdin, stdout, stderr) = \
|
||||||
ssh.exec_command("esxcli network vswitch standard list|grep Portgroups|sed 's/^ Portgroups: //g'")
|
exec_ssh_command("Get existing networks",
|
||||||
type(stdin)
|
"esxcli network vswitch standard list|grep Portgroups|sed 's/^ Portgroups: //g'",
|
||||||
|
isVerbose)
|
||||||
VMNICS = []
|
VMNICS = []
|
||||||
for line in stdout.readlines():
|
for line in stdout.readlines():
|
||||||
splitLine = re.split('[,\n]', line)
|
splitLine = re.split('[,\n]', line)
|
||||||
|
@ -212,17 +221,17 @@ if ISO != "":
|
||||||
try:
|
try:
|
||||||
# If ISO has no "/", try to find the ISO
|
# If ISO has no "/", try to find the ISO
|
||||||
if not re.match('/', ISO):
|
if not re.match('/', ISO):
|
||||||
(stdin, stdout, stderr) = \
|
(stdin, stdout, stderr) = exec_ssh_command("Search ISO image",
|
||||||
ssh.exec_command("find /vmfs/volumes/ -type f -name " + ISO +
|
"find /vmfs/volumes/ -type f -name {}".format(ISO) +
|
||||||
" -exec sh -c 'echo $1; kill $PPID' sh {} 2>/dev/null \;")
|
" -exec sh -c 'echo $1; kill $PPID' sh {} 2>/dev/null \;",
|
||||||
type(stdin)
|
isVerbose
|
||||||
|
)
|
||||||
FoundISOPath = str(stdout.readlines()[0]).strip('\n')
|
FoundISOPath = str(stdout.readlines()[0]).strip('\n')
|
||||||
if isVerbose:
|
if isVerbose:
|
||||||
print("FoundISOPath: {}".format(FoundISOPath))
|
print("FoundISOPath: {}".format(FoundISOPath))
|
||||||
ISO = str(FoundISOPath)
|
ISO = str(FoundISOPath)
|
||||||
|
|
||||||
(stdin, stdout, stderr) = ssh.exec_command("ls " + str(ISO))
|
(stdin, stdout, stderr) = exec_ssh_command("Check ISO", "ls {}".format(ISO), isVerbose)
|
||||||
type(stdin)
|
|
||||||
if stdout.readlines() and not stderr.readlines():
|
if stdout.readlines() and not stderr.readlines():
|
||||||
ISOfound = True
|
ISOfound = True
|
||||||
|
|
||||||
|
@ -232,15 +241,13 @@ if ISO != "":
|
||||||
|
|
||||||
VMID = -1
|
VMID = -1
|
||||||
try:
|
try:
|
||||||
(stdin, stdout, stderr) = ssh.exec_command("vim-cmd vmsvc/getallvms")
|
(stdin, stdout, stderr) = exec_ssh_command("Get list of VMs", "vim-cmd vmsvc/getallvms", isVerbose)
|
||||||
type(stdin)
|
|
||||||
for line in stdout.readlines():
|
for line in stdout.readlines():
|
||||||
splitLine = line.split()
|
splitLine = line.split()
|
||||||
if NAME == splitLine[1]:
|
if NAME == splitLine[1]:
|
||||||
VMID = splitLine[0]
|
VMID = splitLine[0]
|
||||||
print("ERROR: VM {} already exists.".format(NAME))
|
print("ERROR: VM {} already exists.".format(NAME))
|
||||||
ErrorMessages += " VM " + NAME + " already exists."
|
sys.exit(1)
|
||||||
CheckHasErrors = True
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("The Error is {}".format(e))
|
print("The Error is {}".format(e))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
@ -294,8 +301,7 @@ if ISO != "" and not ISOfound:
|
||||||
# Check if DSPATH/NAME already exists
|
# Check if DSPATH/NAME already exists
|
||||||
try:
|
try:
|
||||||
FullPath = DSPATH + "/" + NAME
|
FullPath = DSPATH + "/" + NAME
|
||||||
(stdin, stdout, stderr) = ssh.exec_command("ls -d " + FullPath)
|
(stdin, stdout, stderr) = exec_ssh_command("List VMs directories", "ls -d {}".format(FullPath), isVerbose)
|
||||||
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 {} already exists.".format(FullPath)
|
ErrorMessages += " Directory {} already exists.".format(FullPath)
|
||||||
|
@ -386,38 +392,29 @@ else:
|
||||||
|
|
||||||
if not isDryRun and not CheckHasErrors:
|
if not isDryRun and not CheckHasErrors:
|
||||||
try:
|
try:
|
||||||
if isVerbose:
|
(stdin, stdout, stderr) = exec_ssh_command("Create {}.vmx file".format(NAME),
|
||||||
print("Create {}.vmx file".format(NAME))
|
"mkdir {}".format(FullPath), isVerbose)
|
||||||
(stdin, stdout, stderr) = ssh.exec_command("mkdir {}".format(FullPath))
|
|
||||||
type(stdin)
|
|
||||||
for k, v in vmx.items():
|
for k, v in vmx.items():
|
||||||
(stdin, stdout, stderr) = ssh.exec_command("echo '{} = \"{}\"' >> {}.vmx".format(k, v, MyVM))
|
(stdin, stdout, stderr) = exec_ssh_command(None, "echo '{} = \"{}\"' >> {}.vmx".format(k, v, MyVM),
|
||||||
type(stdin)
|
isVerbose)
|
||||||
|
|
||||||
if isVerbose:
|
(stdin, stdout, stderr) = exec_ssh_command("Create {}.vmdk file".format(NAME),
|
||||||
print("Create {}.vmdk file".format(NAME))
|
"vmkfstools -c {}G -d {} {}.vmdk".format(HDISK, DISKFORMAT, MyVM),
|
||||||
(stdin, stdout, stderr) = \
|
isVerbose)
|
||||||
ssh.exec_command("vmkfstools -c {}G -d {} {}.vmdk".format(HDISK, DISKFORMAT, MyVM))
|
|
||||||
type(stdin)
|
|
||||||
|
|
||||||
if isVerbose:
|
(stdin, stdout, stderr) = exec_ssh_command("Register VM",
|
||||||
print("Register VM")
|
"vim-cmd solo/registervm {}.vmx".format(MyVM), isVerbose)
|
||||||
(stdin, stdout, stderr) = ssh.exec_command("vim-cmd solo/registervm {}.vmx".format(MyVM))
|
|
||||||
type(stdin)
|
|
||||||
VMID = int(stdout.readlines()[0])
|
VMID = int(stdout.readlines()[0])
|
||||||
|
|
||||||
if isVerbose:
|
(stdin, stdout, stderr) = exec_ssh_command("Power ON VM", "vim-cmd vmsvc/power.on {}".format(VMID), isVerbose)
|
||||||
print("Power ON VM")
|
|
||||||
(stdin, stdout, stderr) = ssh.exec_command("vim-cmd vmsvc/power.on {}".format(VMID))
|
|
||||||
type(stdin)
|
|
||||||
if stderr.readlines():
|
if stderr.readlines():
|
||||||
print("Error Powering-on VM.")
|
print("Error Powering-on VM.")
|
||||||
Result = "Fail"
|
Result = "Fail"
|
||||||
|
|
||||||
if NET != "None":
|
if NET != "None":
|
||||||
(stdin, stdout, stderr) = ssh.exec_command(
|
(stdin, stdout, stderr) = exec_ssh_command("Get MAC address",
|
||||||
"grep -i 'ethernet0.*ddress = ' {}.vmx |tail -1|awk '{print $NF}'".format(MyVM))
|
"grep -i 'ethernet0.*ddress = ' {}.vmx".format(MyVM) +
|
||||||
type(stdin)
|
" |tail -1|awk '{print $NF}'", isVerbose)
|
||||||
GeneratedMAC = str(stdout.readlines()[0]).strip('\n"')
|
GeneratedMAC = str(stdout.readlines()[0]).strip('\n"')
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
Loading…
Reference in New Issue