mirror of https://github.com/Kodomo/esxi-vm
PEP8, add PORT (-T) and KEY (-K) parameters.
parent
bafad5ff2f
commit
6ecf4d7fbc
|
@ -2,14 +2,9 @@
|
||||||
|
|
||||||
|
|
||||||
import argparse # Argument parser
|
import argparse # Argument parser
|
||||||
import datetime # For current Date/Time
|
|
||||||
import time
|
import time
|
||||||
import os.path # To check if file exists
|
|
||||||
import sys # For args
|
|
||||||
import re # For regex
|
import re # For regex
|
||||||
import paramiko # For remote ssh
|
import paramiko # For remote ssh
|
||||||
import yaml
|
|
||||||
import warnings
|
|
||||||
|
|
||||||
from esxi_vm_functions import *
|
from esxi_vm_functions import *
|
||||||
|
|
||||||
|
@ -21,8 +16,10 @@ isDryRun = ConfigData['isDryRun']
|
||||||
isVerbose = ConfigData['isVerbose']
|
isVerbose = ConfigData['isVerbose']
|
||||||
isSummary = ConfigData['isSummary']
|
isSummary = ConfigData['isSummary']
|
||||||
HOST = ConfigData['HOST']
|
HOST = ConfigData['HOST']
|
||||||
|
PORT = ConfigData['PORT']
|
||||||
USER = ConfigData['USER']
|
USER = ConfigData['USER']
|
||||||
PASSWORD = ConfigData['PASSWORD']
|
PASSWORD = ConfigData['PASSWORD']
|
||||||
|
KEY = ConfigData['KEY']
|
||||||
CPU = ConfigData['CPU']
|
CPU = ConfigData['CPU']
|
||||||
MEM = ConfigData['MEM']
|
MEM = ConfigData['MEM']
|
||||||
HDISK = int(ConfigData['HDISK'])
|
HDISK = int(ConfigData['HDISK'])
|
||||||
|
@ -44,11 +41,15 @@ DSSTORE=""
|
||||||
parser = argparse.ArgumentParser(description='ESXi Create VM utility.')
|
parser = argparse.ArgumentParser(description='ESXi Create 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 (" + str(HOST) + ")")
|
||||||
|
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("-n", "--name", dest='NAME', type=str, help="VM name")
|
parser.add_argument("-n", "--name", dest='NAME', type=str, help="VM name")
|
||||||
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',
|
||||||
parser.add_argument('--summary', dest='isSummaryarg', action='store_true', help="Display Summary (" + str(isSummary) + ")")
|
help="Enable Verbose mode (" + str(isVerbose) + ")")
|
||||||
|
parser.add_argument('--summary', dest='isSummaryarg', action='store_true',
|
||||||
|
help="Display Summary (" + str(isSummary) + ")")
|
||||||
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
@ -59,10 +60,14 @@ if args.isSummaryarg:
|
||||||
isSummary = True
|
isSummary = True
|
||||||
if args.HOST:
|
if args.HOST:
|
||||||
HOST = args.HOST
|
HOST = args.HOST
|
||||||
|
if args.PORT:
|
||||||
|
PORT = args.PORT
|
||||||
if args.USER:
|
if args.USER:
|
||||||
USER = args.USER
|
USER = args.USER
|
||||||
if args.PASSWORD:
|
if args.PASSWORD:
|
||||||
PASSWORD = args.PASSWORD
|
PASSWORD = args.PASSWORD
|
||||||
|
if args.KEY:
|
||||||
|
KEY = args.KEY
|
||||||
if args.NAME:
|
if args.NAME:
|
||||||
NAME = args.NAME
|
NAME = args.NAME
|
||||||
|
|
||||||
|
@ -70,7 +75,7 @@ if args.NAME:
|
||||||
# main()
|
# main()
|
||||||
#
|
#
|
||||||
LogOutput = '{'
|
LogOutput = '{'
|
||||||
LogOutput += '"datetime":"' + str(theCurrDateTime()) + '",'
|
LogOutput += '"datetime":"' + str(the_current_date_time()) + '",'
|
||||||
|
|
||||||
if NAME == "":
|
if NAME == "":
|
||||||
print "ERROR: Missing required option --name"
|
print "ERROR: Missing required option --name"
|
||||||
|
@ -79,22 +84,24 @@ if NAME == "":
|
||||||
try:
|
try:
|
||||||
ssh = paramiko.SSHClient()
|
ssh = paramiko.SSHClient()
|
||||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||||
ssh.connect(HOST, username=USER, password=PASSWORD)
|
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) = ssh.exec_command("esxcli system version get |grep Version")
|
||||||
type(stdin)
|
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: %s, username: %s" % (HOST, USER)
|
print "Unable to determine if this is a ESXi Host: %s, port: %s, username: %s" % (HOST, PORT, USER)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
except:
|
except:
|
||||||
print "The Error is " + str(sys.exc_info()[0])
|
print "The Error is " + str(sys.exc_info()[0])
|
||||||
print "Unable to access ESXi Host: %s, username: %s" % (HOST, USER)
|
print "Unable to access ESXi Host: %s, port: %s, username: %s" % (HOST, PORT, USER)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
#
|
#
|
||||||
# Check if VM exists
|
# Check if VM exists
|
||||||
#
|
#
|
||||||
VMID = -1
|
VMID = -1
|
||||||
|
CheckHasWarnings = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
(stdin, stdout, stderr) = ssh.exec_command("vim-cmd vmsvc/getallvms")
|
(stdin, stdout, stderr) = ssh.exec_command("vim-cmd vmsvc/getallvms")
|
||||||
type(stdin)
|
type(stdin)
|
||||||
|
@ -117,7 +124,8 @@ except:
|
||||||
|
|
||||||
# Get List of Volumes,
|
# Get List of Volumes,
|
||||||
try:
|
try:
|
||||||
(stdin, stdout, stderr) = ssh.exec_command("esxcli storage filesystem list |grep '/vmfs/volumes/.*true VMFS' |sort -nk7")
|
(stdin, stdout, stderr) = \
|
||||||
|
ssh.exec_command("esxcli storage filesystem list |grep '/vmfs/volumes/.*true VMFS' |sort -nk7")
|
||||||
type(stdin)
|
type(stdin)
|
||||||
VOLUMES = {}
|
VOLUMES = {}
|
||||||
for line in stdout.readlines():
|
for line in stdout.readlines():
|
||||||
|
@ -198,7 +206,7 @@ LogOutput += '"Verbose":"' + str(isVerbose) + '",'
|
||||||
if ErrorMessages != "":
|
if ErrorMessages != "":
|
||||||
LogOutput += '"Error Message":"' + ErrorMessages + '",'
|
LogOutput += '"Error Message":"' + ErrorMessages + '",'
|
||||||
LogOutput += '"Result":"' + Result + '",'
|
LogOutput += '"Result":"' + Result + '",'
|
||||||
LogOutput += '"Completion Time":"' + str(theCurrDateTime()) + '"'
|
LogOutput += '"Completion Time":"' + str(the_current_date_time()) + '"'
|
||||||
LogOutput += '}\n'
|
LogOutput += '}\n'
|
||||||
try:
|
try:
|
||||||
with open(LOG, "a+w") as FD:
|
with open(LOG, "a+w") as FD:
|
||||||
|
@ -221,4 +229,3 @@ else:
|
||||||
print "Success"
|
print "Success"
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue