2017-05-03 17:00:03 -03:00
|
|
|
import os.path
|
2018-01-10 13:30:01 -03:00
|
|
|
|
|
|
|
import sys
|
2017-05-03 17:00:03 -03:00
|
|
|
import yaml
|
|
|
|
import datetime # For current Date/Time
|
|
|
|
from math import log
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# Functions
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
def setup_config():
|
|
|
|
|
|
|
|
#
|
|
|
|
# System wide defaults
|
|
|
|
#
|
2018-01-10 13:30:01 -03:00
|
|
|
config_data = dict(
|
2017-05-16 12:49:38 -04:00
|
|
|
|
|
|
|
# Your logfile
|
2018-01-10 13:30:01 -03:00
|
|
|
LOG=os.path.expanduser("~") + "/esxi-vm.log",
|
2017-05-16 12:49:38 -04:00
|
|
|
|
|
|
|
# Enable/Disable dryrun by default
|
2017-05-03 17:00:03 -03:00
|
|
|
isDryRun=False,
|
2017-05-16 12:49:38 -04:00
|
|
|
|
|
|
|
# Enable/Disable Verbose output by default
|
2017-05-03 17:00:03 -03:00
|
|
|
isVerbose=False,
|
2017-05-16 12:49:38 -04:00
|
|
|
|
|
|
|
# Enable/Disable exit summary by default
|
|
|
|
isSummary=False,
|
|
|
|
|
2018-01-07 18:37:33 -03:00
|
|
|
# ESXi host/IP, port, root login & password
|
2017-05-03 17:00:03 -03:00
|
|
|
HOST="esxi",
|
2018-01-07 18:37:33 -03:00
|
|
|
PORT=22,
|
2017-05-03 17:00:03 -03:00
|
|
|
USER="root",
|
|
|
|
PASSWORD="",
|
2018-01-07 18:37:33 -03:00
|
|
|
KEY="",
|
2017-05-16 12:49:38 -04:00
|
|
|
|
|
|
|
# Default number of vCPU's, GB Mem, & GB boot disk
|
2017-05-03 17:00:03 -03:00
|
|
|
CPU=2,
|
|
|
|
MEM=4,
|
2017-05-16 12:49:38 -04:00
|
|
|
HDISK=20,
|
|
|
|
|
|
|
|
# Default Disk format thin, zeroedthick, eagerzeroedthick
|
2017-05-03 17:00:03 -03:00
|
|
|
DISKFORMAT="thin",
|
2017-05-16 12:49:38 -04:00
|
|
|
|
|
|
|
# Virtual Disk device type
|
2017-05-03 17:00:03 -03:00
|
|
|
VIRTDEV="pvscsi",
|
2017-05-16 12:49:38 -04:00
|
|
|
|
|
|
|
# Specify default Disk store to "LeastUsed"
|
2017-05-03 17:00:03 -03:00
|
|
|
STORE="LeastUsed",
|
2017-05-16 12:49:38 -04:00
|
|
|
|
|
|
|
# Default Network Interface (vswitch)
|
2017-05-03 17:00:03 -03:00
|
|
|
NET="None",
|
2017-05-16 12:49:38 -04:00
|
|
|
|
|
|
|
# Default ISO
|
2017-05-03 17:00:03 -03:00
|
|
|
ISO="None",
|
2017-05-16 12:49:38 -04:00
|
|
|
|
|
|
|
# Default GuestOS type. (See VMware documentation for all available options)
|
2017-10-23 22:44:46 -03:00
|
|
|
GUESTOS="centos-64",
|
|
|
|
|
|
|
|
# Extra VMX options
|
|
|
|
VMXOPTS=""
|
2017-05-03 17:00:03 -03:00
|
|
|
)
|
|
|
|
|
2018-01-10 13:30:01 -03:00
|
|
|
config_data_file_location = os.path.expanduser("~") + "/.esxi-vm.yml"
|
2017-05-03 17:00:03 -03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Get ConfigData from ConfigDataFile, then merge.
|
2017-05-16 12:49:38 -04:00
|
|
|
#
|
2018-01-10 13:30:01 -03:00
|
|
|
if os.path.exists(config_data_file_location):
|
|
|
|
from_file_config_data = yaml.safe_load(open(config_data_file_location))
|
|
|
|
config_data.update(from_file_config_data)
|
2017-05-03 17:00:03 -03:00
|
|
|
|
|
|
|
try:
|
2018-01-10 13:30:01 -03:00
|
|
|
with open(config_data_file_location, 'w') as FD:
|
|
|
|
yaml.dump(config_data, FD, default_flow_style=False)
|
2017-05-03 17:00:03 -03:00
|
|
|
FD.close()
|
|
|
|
except:
|
2018-01-10 13:30:01 -03:00
|
|
|
print "Unable to create/update config file " + config_data_file_location
|
2017-05-03 17:00:03 -03:00
|
|
|
e = sys.exc_info()[0]
|
|
|
|
print "The Error is " + str(e)
|
|
|
|
sys.exit(1)
|
2018-01-10 13:30:01 -03:00
|
|
|
return config_data
|
|
|
|
|
2017-05-03 17:00:03 -03:00
|
|
|
|
2018-01-10 13:30:01 -03:00
|
|
|
def save_config(config_data):
|
|
|
|
config_data_file_location = os.path.expanduser("~") + "/.esxi-vm.yml"
|
2017-05-03 17:00:03 -03:00
|
|
|
try:
|
2018-01-10 13:30:01 -03:00
|
|
|
with open(config_data_file_location, 'w') as FD:
|
|
|
|
yaml.dump(config_data, FD, default_flow_style=False)
|
2017-05-03 17:00:03 -03:00
|
|
|
FD.close()
|
|
|
|
except:
|
2018-01-10 13:30:01 -03:00
|
|
|
print "Unable to create/update config file " + config_data_file_location
|
2017-05-03 17:00:03 -03:00
|
|
|
e = sys.exc_info()[0]
|
|
|
|
print "The Error is " + str(e)
|
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2018-01-10 13:30:01 -03:00
|
|
|
def the_current_date_time():
|
2017-05-03 17:00:03 -03:00
|
|
|
i = datetime.datetime.now()
|
|
|
|
return str(i.isoformat())
|
|
|
|
|
|
|
|
|
|
|
|
unit_list = zip(['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'], [0, 0, 1, 2, 2, 2])
|
2018-01-10 13:30:01 -03:00
|
|
|
|
|
|
|
|
2017-05-03 17:00:03 -03:00
|
|
|
def float2human(num):
|
|
|
|
"""Integer to Human readable"""
|
|
|
|
if num > 1:
|
|
|
|
exponent = min(int(log(float(num), 1024)), len(unit_list) - 1)
|
|
|
|
quotient = float(num) / 1024**exponent
|
|
|
|
unit, num_decimals = unit_list[exponent]
|
2018-01-10 13:30:01 -03:00
|
|
|
format_string = '{:.%sf} {}' % num_decimals
|
2017-05-03 17:00:03 -03:00
|
|
|
return format_string.format(quotient, unit)
|
|
|
|
if num == 0:
|
|
|
|
return '0 bytes'
|
|
|
|
if num == 1:
|
|
|
|
return '1 byte'
|