mirror of https://github.com/Kodomo/esxi-vm
Optional writing to log file
parent
ebd2ac4ca1
commit
1b1b9e6adc
|
@ -1,8 +1,6 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
import argparse # Argument parser
|
import argparse # Argument parser
|
||||||
import re # For regex
|
|
||||||
import paramiko # For remote ssh
|
|
||||||
|
|
||||||
from esxi_vm_functions import *
|
from esxi_vm_functions import *
|
||||||
|
|
||||||
|
@ -11,6 +9,7 @@ from esxi_vm_functions import *
|
||||||
ConfigData = setup_config()
|
ConfigData = setup_config()
|
||||||
NAME = ""
|
NAME = ""
|
||||||
LOG = ConfigData['LOG']
|
LOG = ConfigData['LOG']
|
||||||
|
writeLog = ConfigData['writeLog']
|
||||||
isDryRun = ConfigData['isDryRun']
|
isDryRun = ConfigData['isDryRun']
|
||||||
isVerbose = ConfigData['isVerbose']
|
isVerbose = ConfigData['isVerbose']
|
||||||
isSummary = ConfigData['isSummary']
|
isSummary = ConfigData['isSummary']
|
||||||
|
@ -47,25 +46,27 @@ parser = argparse.ArgumentParser(description='ESXi Create VM utility.')
|
||||||
|
|
||||||
parser.add_argument('-d', '--dry', dest='isDryRunarg', action='store_true',
|
parser.add_argument('-d', '--dry', dest='isDryRunarg', action='store_true',
|
||||||
help="Enable Dry Run mode (" + str(isDryRun) + ")")
|
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 ({})".format(HOST))
|
||||||
parser.add_argument("-T", "--Port", dest='PORT', type=int, help="ESXi Port number (" + str(PORT) + ")")
|
parser.add_argument("-T", "--Port", dest='PORT', type=int, help="ESXi Port number ({})".format(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 ({})".format(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 path to private key ({})".format(KEY))
|
||||||
parser.add_argument("-n", "--name", dest='NAME', type=str, help="VM name")
|
parser.add_argument("-n", "--name", dest='NAME', type=str, help="VM name ()".format(NAME))
|
||||||
parser.add_argument("-c", "--cpu", dest='CPU', type=int, help="Number of vCPUS (" + str(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 GB (" + str(MEM) + ")")
|
parser.add_argument("-m", "--mem", type=int, help="Memory in GB ({})".format(MEM))
|
||||||
parser.add_argument("-v", "--vdisk", dest='HDISK', type=str, help="Size of virt hdisk (" + str(HDISK) + ")")
|
parser.add_argument("-v", "--vdisk", dest='HDISK', type=str, help="Size of virt hdisk ({})".format(HDISK))
|
||||||
parser.add_argument("-i", "--iso", dest='ISO', type=str, help="CDROM ISO Path | None (" + str(ISO) + ")")
|
parser.add_argument("-i", "--iso", dest='ISO', type=str, help="CDROM ISO Path | None ({})".format(ISO))
|
||||||
parser.add_argument("-N", "--net", dest='NET', type=str, help="Network Interface | None (" + str(NET) + ")")
|
parser.add_argument("-N", "--net", dest='NET', type=str, help="Network Interface | None ({})".format(NET))
|
||||||
parser.add_argument("-M", "--mac", dest='MAC', type=str, help="MAC address")
|
parser.add_argument("-M", "--mac", dest='MAC', type=str, help="MAC address")
|
||||||
parser.add_argument("-S", "--store", dest='STORE', type=str, help="vmfs Store | LeastUsed (" + str(STORE) + ")")
|
parser.add_argument("-S", "--store", dest='STORE', type=str, help="vmfs Store | LeastUsed ({})".format(STORE))
|
||||||
parser.add_argument("-g", "--guestos", dest='GUESTOS', type=str, help="Guest OS. (" + str(GUESTOS) + ")")
|
parser.add_argument("-g", "--guestos", dest='GUESTOS', type=str, help="Guest OS ({})".format(GUESTOS))
|
||||||
parser.add_argument("-o", "--options", dest='VMXOPTS', type=str, default='NIL', help="Comma list of VMX Options.")
|
parser.add_argument("-o", "--options", dest='VMXOPTS', type=str, default='NIL', help="Comma list of VMX options")
|
||||||
parser.add_argument('-V', '--verbose', dest='isVerbosearg', action='store_true',
|
parser.add_argument('-V', '--verbose', dest='isVerbosearg', action='store_true',
|
||||||
help="Enable Verbose mode (" + str(isVerbose) + ")")
|
help="Enable Verbose mode ({})".format(isVerbose))
|
||||||
|
parser.add_argument('-f', '--logfile', dest='LOG', type=str, help='Path to the log file ({})'.format(LOG))
|
||||||
|
parser.add_argument('-l', '--log', dest='writeLog', action='store_true', help='Write to log file ({})'.format(writeLog))
|
||||||
parser.add_argument('--summary', dest='isSummaryarg', action='store_true',
|
parser.add_argument('--summary', dest='isSummaryarg', action='store_true',
|
||||||
help="Display Summary (" + str(isSummary) + ")")
|
help="Display Summary ({})".format(isSummary))
|
||||||
parser.add_argument("-u", "--updateDefaults", dest='UPDATE', action='store_true',
|
parser.add_argument("-u", "--updateDefaults", dest='UPDATE', action='store_true',
|
||||||
help="Update Default VM settings stored in ~/.esxi-vm.yml")
|
help="Update Default VM settings stored in ~/.esxi-vm.yml")
|
||||||
|
|
||||||
|
@ -76,6 +77,10 @@ if args.isDryRunarg:
|
||||||
isDryRun = True
|
isDryRun = True
|
||||||
if args.isVerbosearg:
|
if args.isVerbosearg:
|
||||||
isVerbose = True
|
isVerbose = True
|
||||||
|
if args.LOG:
|
||||||
|
LOG = args.LOG
|
||||||
|
if args.LOG or args.writeLog:
|
||||||
|
writeLog = True
|
||||||
if args.isSummaryarg:
|
if args.isSummaryarg:
|
||||||
isSummary = True
|
isSummary = True
|
||||||
if args.HOST:
|
if args.HOST:
|
||||||
|
@ -201,8 +206,7 @@ if ISO != "":
|
||||||
(stdin, stdout, stderr) = exec_ssh_command("Search ISO image",
|
(stdin, stdout, stderr) = exec_ssh_command("Search ISO image",
|
||||||
"find /vmfs/volumes/ -type f -name {}".format(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 \;",
|
||||||
ssh, isVerbose
|
ssh, 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))
|
||||||
|
@ -413,10 +417,12 @@ if ErrorMessages != "":
|
||||||
LogOutput += '"Error Message":"{}",'.format(ErrorMessages)
|
LogOutput += '"Error Message":"{}",'.format(ErrorMessages)
|
||||||
LogOutput += '"Result":"{}","Completion Time":"{}"'.format(Result, the_current_date_time())
|
LogOutput += '"Result":"{}","Completion Time":"{}"'.format(Result, the_current_date_time())
|
||||||
LogOutput += '}\n'
|
LogOutput += '}\n'
|
||||||
try:
|
|
||||||
|
if writeLog:
|
||||||
|
try:
|
||||||
with open(LOG, "a") as FD:
|
with open(LOG, "a") as FD:
|
||||||
FD.write(LogOutput)
|
FD.write(LogOutput)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Error writing to log file: {}".format(LOG))
|
print("Error writing to log file: {}".format(LOG))
|
||||||
|
|
||||||
if isSummary:
|
if isSummary:
|
||||||
|
|
|
@ -3,8 +3,6 @@
|
||||||
|
|
||||||
import argparse # Argument parser
|
import argparse # Argument parser
|
||||||
import time
|
import time
|
||||||
import re # For regex
|
|
||||||
import paramiko # For remote ssh
|
|
||||||
|
|
||||||
from esxi_vm_functions import *
|
from esxi_vm_functions import *
|
||||||
|
|
||||||
|
@ -12,6 +10,7 @@ from esxi_vm_functions import *
|
||||||
ConfigData = setup_config()
|
ConfigData = setup_config()
|
||||||
NAME = ""
|
NAME = ""
|
||||||
LOG = ConfigData['LOG']
|
LOG = ConfigData['LOG']
|
||||||
|
writeLog = ConfigData['writeLog']
|
||||||
isDryRun = ConfigData['isDryRun']
|
isDryRun = ConfigData['isDryRun']
|
||||||
isVerbose = ConfigData['isVerbose']
|
isVerbose = ConfigData['isVerbose']
|
||||||
isSummary = ConfigData['isSummary']
|
isSummary = ConfigData['isSummary']
|
||||||
|
@ -38,24 +37,29 @@ DSSTORE = ""
|
||||||
#
|
#
|
||||||
# Process Arguments
|
# Process Arguments
|
||||||
#
|
#
|
||||||
parser = argparse.ArgumentParser(description='ESXi Create VM utility.')
|
parser = argparse.ArgumentParser(description='ESXi Destroy VM utility.')
|
||||||
|
|
||||||
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 ({})".format(HOST))
|
||||||
parser.add_argument("-T", "--Port", dest='PORT', type=int, help="ESXi Port number (" + str(PORT) + ")")
|
parser.add_argument("-T", "--Port", dest='PORT', type=int, help="ESXi Port number ({})".format(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 ({})".format(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 path to private key ({})".format(KEY))
|
||||||
parser.add_argument("-n", "--name", dest='NAME', type=str, help="VM name")
|
parser.add_argument("-n", "--name", dest='NAME', type=str, help="VM name ()".format(NAME))
|
||||||
parser.add_argument('-V', '--verbose', dest='isVerbosearg', action='store_true',
|
parser.add_argument('-V', '--verbose', dest='isVerbosearg', action='store_true',
|
||||||
help="Enable Verbose mode (" + str(isVerbose) + ")")
|
help="Enable Verbose mode ({})".format(isVerbose))
|
||||||
|
parser.add_argument('-f', '--logfile', dest='LOG', type=str, help='Path to the log file ({})'.format(LOG))
|
||||||
|
parser.add_argument('-l', '--log', dest='writeLog', action='store_true', help='Write to log file ({})'.format(writeLog))
|
||||||
parser.add_argument('--summary', dest='isSummaryarg', action='store_true',
|
parser.add_argument('--summary', dest='isSummaryarg', action='store_true',
|
||||||
help="Display Summary (" + str(isSummary) + ")")
|
help="Display Summary ({})".format(isSummary))
|
||||||
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if args.isVerbosearg:
|
if args.isVerbosearg:
|
||||||
isVerbose = True
|
isVerbose = True
|
||||||
|
if args.LOG:
|
||||||
|
LOG = args.LOG
|
||||||
|
if args.LOG or args.writeLog:
|
||||||
|
writeLog = True
|
||||||
if args.isSummaryarg:
|
if args.isSummaryarg:
|
||||||
isSummary = True
|
isSummary = True
|
||||||
if args.HOST:
|
if args.HOST:
|
||||||
|
@ -172,10 +176,11 @@ if ErrorMessages != "":
|
||||||
LogOutput += '"Result":"{}","Completion Time":"{}"'.format(Result, the_current_date_time())
|
LogOutput += '"Result":"{}","Completion Time":"{}"'.format(Result, the_current_date_time())
|
||||||
LogOutput += '}\n'
|
LogOutput += '}\n'
|
||||||
|
|
||||||
try:
|
if writeLog:
|
||||||
|
try:
|
||||||
with open(LOG, "a") as FD:
|
with open(LOG, "a") as FD:
|
||||||
FD.write(LogOutput)
|
FD.write(LogOutput)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Error writing to log file: {}".format(e))
|
print("Error writing to log file: {}".format(e))
|
||||||
|
|
||||||
if isSummary:
|
if isSummary:
|
||||||
|
|
|
@ -6,6 +6,7 @@ import datetime # For current Date/Time
|
||||||
import re # For regex
|
import re # For regex
|
||||||
import paramiko # For remote ssh
|
import paramiko # For remote ssh
|
||||||
|
|
||||||
|
|
||||||
def setup_config():
|
def setup_config():
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -15,6 +16,7 @@ def setup_config():
|
||||||
|
|
||||||
# Your logfile
|
# Your logfile
|
||||||
LOG=os.path.expanduser("~") + "/esxi-vm.log",
|
LOG=os.path.expanduser("~") + "/esxi-vm.log",
|
||||||
|
writeLog=False, # By default, do NOT write logs
|
||||||
|
|
||||||
# Enable/Disable dryrun by default
|
# Enable/Disable dryrun by default
|
||||||
isDryRun=False,
|
isDryRun=False,
|
||||||
|
|
Loading…
Reference in New Issue