-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcreate-ztpserver.py
executable file
·238 lines (200 loc) · 8.9 KB
/
create-ztpserver.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/python
####################################
# Automatically setup a ZTPServer VM
# Author: [email protected]
# Date: 20150113
####################################
import sys
import os
newPath = os.path.join(os.getcwd(), "lib")
sys.path.append(newPath)
from eosplusvnets import *
def createVM(hyper, hostOS, vmOS, vmName, vmSize, user, packerCmd, **kwargs):
d = datetime.datetime.now()
time = d.strftime("%Y%m%d_%H%M%S")
# Set packer logging variable
os.environ['PACKER_LOG'] = "enable"
os.environ['PACKER_LOG_PATH'] = "./packer-debug.log"
if vmName:
vmName = "%s-%s_%s" % (vmName, vmOS, time)
else:
vmName = "ztps-%s_%s" % (vmOS, time)
print "Using VM name %s" % vmName
print "Creating VM with user %s" % user
print bcolors.WARNING
print "##############################################"
print "WARNING: DO NOT TYPE IN VIRTUAL MACHINE WINDOW"
print "##############################################"
print bcolors.ENDC
if (hostOS == "windows" and hyper == "virtualbox"):
build = "--only=%s-windows-iso" % hyper
elif vmOS == "eos":
build = "--only=%s-iso-eos" % hyper
else:
build = "--only=%s-iso" % hyper
nameVar = "name=%s" % vmName
try:
opts = "-var name='%s' -var disk-size=%s" % (vmName, vmSize)
if vmOS == "fedora":
wkd = os.path.join(os.getcwd(), "Fedora")
builder_file = "ztps-fedora_20_x86_64.json"
if hyper == "esxi":
esxi = kwargs["esxi_info"]
opts += (" -var esxi-user='%s' -var esxi-passwd='%s' "
"-var esxi-host='%s' -var esxi-path='%s' "
"-var esxi-network='%s'") % (esxi['user'],
esxi['passwd'],
esxi['host'],
esxi['datastore'],
esxi['network'])
elif vmOS == "eos":
wkd = os.path.join(os.getcwd(), "Fedora")
builder_file = "ztps-fedora_20_i386.json"
if hyper == "esxi":
esxi = kwargs["esxi_info"]
opts += (" -var esxi-user='%s' -var esxi-passwd='%s' "
"-var esxi-host='%s' -var esxi-path='%s' "
"-var esxi-network='%s'") % (esxi['user'],
esxi['passwd'],
esxi['host'],
esxi['datastore'],
esxi['network'])
elif vmOS == "ubuntu":
wkd = os.path.join(os.getcwd(), "Ubuntu")
builder_file = "ztps-ubuntu-12.04.4_amd64.json"
if hyper == "esxi":
esxi = kwargs["esxi_info"]
opts += (" -var esxi-user='%s' -var esxi-passwd='%s' "
"-var esxi-host='%s' -var esxi-path='%s' "
"-var esxi-network='%s'") % (esxi['user'],
esxi['passwd'],
esxi['host'],
esxi['datastore'],
esxi['network'])
build_cmd = "%s build %s %s %s" % (packerCmd, build, opts, builder_file)
print build_cmd
rc = subprocess.call(build_cmd, shell=True, cwd=wkd)
print "Return code:%s" % rc
except OSError as e:
if e.errno == os.errno.ENOENT:
print "Unable to create Virtual Machine"
raise
else:
print "Something else went wrong"
raise
if rc == 0:
return vmName
elif rc > 0:
print "Packer install failed!!!"
print "Please copy error ouput and raise an issue at https://github.com/arista-eosplus/packer-ztpserver/issues with your console output."
exit(rc)
def registerVbox(hyper, libDir, vmName, vmOS):
#Import the VM into Vbox
if hyper == "virtualbox":
cmd = "%s/vboxmanage" % libDir
vmPath = "%s-vbox/%s.ovf" % (vmName, vmName)
if (vmOS == "fedora" or vmOS == "eos"):
path = "Fedora/"
else:
path = "Ubuntu/"
print "Path: %s" % path
print "VM: %s" % vmPath
subprocess.call([cmd, "import", "--options", "keepallmacs", vmPath], cwd=path)
return True
def main():
# Argument Variables
hypervisors = ["vmware", "esxi", "virtualbox"]
oses = ["fedora", "ubuntu", "eos"]
parser = argparse.ArgumentParser(description="Automatically install the ZTPServer Demo")
parser.add_argument("-H", "--hypervisor", required=True, choices=hypervisors, help="Hypervisor to create VM in")
parser.add_argument("-o", "--os", required=True, choices=oses, help="Desired OS to use for VM")
parser.add_argument("-n", "--vmname", help="The Virtual Machine name")
parser.add_argument("-d", "--disk-size", help="VM Disk size in MB", default=7000)
parser.add_argument("-u", "--esxi-user", help="The ESXi username")
parser.add_argument("-e", "--esxi-host", help="The IP or hostname of the ESXi host")
parser.add_argument("-p", "--datastore-path", help="The ESXi path to save the VM")
parser.add_argument("-i", "--esxi-network", help="vSphere network assigned to VM \
that allows communication with local \
builder")
args = parser.parse_args()
# Set install variables
user = getpass.getuser()
if user == "root" and os.getenv("SUDO_USER") != "root":
print bcolors.FAIL, "ERROR: DO NOT RUN THIS SCRIPT WITH SUDO", bcolors.ENDC
exit()
hyper = args.hypervisor
vmOS = args.os
if args.vmname:
vmName = args.vmname
else:
vmName = ""
vmSize = args.disk_size
if vmSize < 3000:
parser.error('3000 MB is minimum disk size for VM')
if vmOS == "eos":
vmSize = 4000
print "Changing disk size to 4000MB for eos"
if hyper == "esxi":
if not args.esxi_user or not args.esxi_host or not args.esxi_network or not args.datastore_path:
parser.error('esxi-host, datastore-path and esxi-network are all \
required when using the esxi hypervisor')
try:
print "Parsing arguments for ESXi installation:"
print " - Host:%s\n - Datastore Path:%s\n - VM Network:%s" % (args.esxi_host, args.datastore_path, args.esxi_network)
esxi = dict()
esxi["passwd"] = getpass.getpass("Enter ESXi host password:")
esxi["user"] = args.esxi_user
esxi["host"] = args.esxi_host
esxi["datastore"] = args.datastore_path
esxi["network"] = args.esxi_network
except:
raise Exception("Unable to get ESXi password from user")
# Get host machine information
hostOS = getHostOS()
hostArch = getHostArch()
print "Tailoring install for a %s bit %s environment" % (hostArch, hostOS)
print "Looking for hypervisor libraries"
if hyper == "vmware":
if hostOS == "darwin":
libDir = find("/Applications", "vmnet-cli")
elif hostOS == "windows":
libDir = find("C:\\", "vmware.exe")
elif hyper == "virtualbox":
if hostOS == "darwin":
libDir = find("/usr", "VBoxManage")
elif hostOS == "windows":
libDir = find("C:\\", "VBoxManage.exe")
# Test to see if Packer is installed
packerCmd = which("packer")
if not packerCmd:
print "Packer not found - install it"
packerCmd = installPacker(hostOS, hostArch)
# Setup Virtual Networks
if hyper == "virtualbox":
if createVBoxNets(hostOS, hostArch, libDir):
# Create the Virtual Machine
vmName = createVM(hyper, hostOS, vmOS, vmName, vmSize, user,
packerCmd)
if vmName:
if registerVbox(hyper, libDir, vmName, vmOS):
print "Successfully created VM %s!" % vmName
exit(0)
elif hyper == "vmware":
if createVmNets(hostOS, hostArch, libDir):
# Create the Virtual Machine
vmName = createVM(hyper, hostOS, vmOS, vmName, vmSize,
user, packerCmd)
if vmName:
print "Successfully created VM %s!" % vmName
exit(0)
elif hyper == "esxi":
vmName = createVM(hyper, hostOS, vmOS, vmName, vmSize, user, packerCmd,
esxi_info=esxi)
if vmName:
print "Successfully created VM %s!" % vmName
exit(0)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print "Exiting script..."