forked from josenk/esxi-vm-create
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esxi_vm_functions.py
executable file
·117 lines (91 loc) · 2.93 KB
/
esxi_vm_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import os.path
import yaml
import datetime # For current Date/Time
import paramiko # For remote ssh
from math import log
#
#
# Functions
#
#
def setup_config():
#
# System wide defaults
#
ConfigData = dict(
# Your logfile
LOG= os.path.expanduser("~") + "/esxi-vm.log",
# Enable/Disable dryrun by default
isDryRun=False,
# Enable/Disable Verbose output by default
isVerbose=False,
# Enable/Disable exit summary by default
isSummary=False,
# ESXi host/IP, root login & password
HOST="esxi",
USER="root",
PASSWORD="",
# Default number of vCPU's, GB Mem, & GB boot disk
CPU=2,
MEM=4,
HDISK=20,
# Default Disk format thin, zeroedthick, eagerzeroedthick
DISKFORMAT="thin",
# Virtual Disk device type
VIRTDEV="pvscsi",
# Specify default Disk store to "LeastUsed"
STORE="LeastUsed",
# Default Network Interface (vswitch)
NET="None",
# Default ISO
ISO="None",
# Default GuestOS type. (See VMware documentation for all available options)
GUESTOS="centos-64",
# Extra VMX options
VMXOPTS=""
)
ConfigDataFileLocation = os.path.expanduser("~") + "/.esxi-vm.yml"
#
# Get ConfigData from ConfigDataFile, then merge.
#
if os.path.exists(ConfigDataFileLocation):
FromFileConfigData = yaml.safe_load(open(ConfigDataFileLocation))
ConfigData.update(FromFileConfigData)
try:
with open(ConfigDataFileLocation, 'w') as FD:
yaml.dump(ConfigData, FD, default_flow_style=False)
FD.close()
except:
print "Unable to create/update config file " + ConfigDataFileLocation
e = sys.exc_info()[0]
print "The Error is " + str(e)
sys.exit(1)
return ConfigData
def SaveConfig(ConfigData):
ConfigDataFileLocation = os.path.expanduser("~") + "/.esxi-vm.yml"
try:
with open(ConfigDataFileLocation, 'w') as FD:
yaml.dump(ConfigData, FD, default_flow_style=False)
FD.close()
except:
print "Unable to create/update config file " + ConfigDataFileLocation
e = sys.exc_info()[0]
print "The Error is " + str(e)
return 1
return 0
def theCurrDateTime():
i = datetime.datetime.now()
return str(i.isoformat())
unit_list = zip(['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'], [0, 0, 1, 2, 2, 2])
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]
format_string = '{:.%sf} {}' % (num_decimals)
return format_string.format(quotient, unit)
if num == 0:
return '0 bytes'
if num == 1:
return '1 byte'