esxi-vm/esxi_vm_functions.py

97 lines
2.4 KiB
Python
Raw Normal View History

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
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,
# ESXi host/IP, port, root login & password
2017-05-03 17:00:03 -03:00
HOST="esxi",
PORT=22,
2017-05-03 17:00:03 -03:00
USER="root",
PASSWORD="",
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)
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 Exception as e:
2018-01-10 16:06:33 -03:00
print("Unable to create/update config file {}".format(config_data_file_location))
print("The Error is {}".format(e))
2017-05-03 17:00:03 -03:00
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 Exception as e:
2018-01-10 16:06:33 -03:00
print("Unable to create/update config file {}".format(config_data_file_location))
print("The Error is {}".format(e))
2017-05-03 17:00:03 -03:00
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())