esxi-vm/esxi_vm_functions.py

136 lines
3.8 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
2018-01-11 11:56:43 -03:00
import re # For regex
import paramiko # For remote ssh
2017-05-03 17:00:03 -03:00
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())
2018-01-11 11:56:43 -03:00
def exec_ssh_command(message, command, ssh, verbose):
if verbose:
if message:
print(message)
print("SSH: " + command)
return ssh.exec_command(command)
def get_esxi_version(ssh, verbose):
try:
(stdin, stdout, stderr) = exec_ssh_command("Get ESXi version", "esxcli system version get |grep Version",
ssh, verbose)
if re.match("Version", str(stdout.readlines())) is not None:
print("Unable to determine if this is a ESXi Host")
2018-01-11 11:56:43 -03:00
sys.exit(1)
except Exception as e:
print("Unable to get ESXi version")
print("The Error is {}".format(e))
sys.exit(1)
def connect_to_esxi(host, port, user, password, key, verbose):
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
if key:
ssh.connect(host, port=port, username=user, key_filename=key)
else:
ssh.connect(host, port=port, username=user, password=password)
2018-01-11 11:56:43 -03:00
get_esxi_version(ssh, verbose)
return ssh
except Exception as e:
print("Unable to access ESXi Host: {}, port: {}, username: {}, key: {}".format(host, port, user, key))
print("The Error is {}".format(e))
sys.exit(1)