-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcreate-veos.py
executable file
·184 lines (157 loc) · 6.47 KB
/
create-veos.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
#!/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 *
ABOOT_FN = "Aboot-vEOS.iso"
VMDK_FN = "vEOS.vmdk"
HYPERVISORS = ["vmware", "virtualbox"]
VEOS_NODES = ["vEOS1", "vEOS2", "vEOS3", "vEOS4"]
def createVM(hyper, hostOS, nodes, vmName, boottime, user, packerCmd):
d = datetime.datetime.now()
time = d.strftime("%Y%m%d_%H%M%S")
if vmName:
vmName = "%s_%s" % (time, vmName)
else:
vmName = "%s" % time
print "Using VM name prefix %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 "all" in nodes:
OPTS = ""
else:
OPTS = "--only=%s" % ','.join(nodes)
OPTS += " "
OPTS += "-var boot_time=%s -var name='%s'" % (boottime, vmName)
try:
if (hyper == "virtualbox" and hostOS=="windows"):
cmd = "%s build --parallel=false %s vEOS-windows.json" % (packerCmd, OPTS)
print "Executing command:%s" % cmd
rc = subprocess.call(cmd, shell=True, cwd=hyper)
else:
cmd = "%s build --parallel=false %s vEOS.json" % (packerCmd, OPTS)
print "Executing command:%s" % cmd
rc = subprocess.call(cmd, shell=True, cwd=hyper)
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 raise an issue at https://github.com/arista-eosplus/packer-ztpserver/issues with your console output."
exit(rc)
def registerVbox(hyper, libDir, vmName, nodes):
#Import the VM into Vbox
if hyper == "virtualbox":
cmd = "%s/vboxmanage" % libDir
if "all" in nodes:
for n in VEOS_NODES:
vmPath = "%s-%s/%s-%s.ovf" % (vmName, n, vmName, n)
print "Importing VM: %s" % vmPath
subprocess.call([ cmd, "import", "--options", "keepallmacs", vmPath ], cwd=hyper)
return True
else:
for n in nodes:
vmPath = "%s-%s/%s-%s.ovf" % (vmName, n, vmName, n)
print "Importing VM: %s" % vmPath
subprocess.call([ cmd, "import", "--options", "keepallmacs", vmPath ], cwd=hyper)
return True
def main():
parser = argparse.ArgumentParser(description="Automatically install the vEOS Demo Nodes")
parser.add_argument("-H", "--hypervisor", required=True, choices=HYPERVISORS,
help="Hypervisor to create VM in")
parser.add_argument("-n", "--nodes", choices=VEOS_NODES, nargs='+',
default=["all"], help="Space-separated list of nodes to build OR omit to build all")
# parser.add_argument("-a", "--aboot", help="Location of Aboot ISO")
# parser.add_argument("-d", "--disk", help="Location of vEOS VMDK")
parser.add_argument("-b", "--boottime", default="2m30s",
help="This is the time Packer will wait before it sends commands over VNC")
parser.add_argument("-N", "--vmname", help="The Virtual Machine name prefix")
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
nodes = args.nodes
boottime = args.boottime
# Check for aboot and vmdk file existance
print "Looking for required source files..."
sourcePath = os.path.join( os.getcwd(), hyper, "source/")
aboot = os.path.join( sourcePath, ABOOT_FN )
vmdk = os.path.join( sourcePath, VMDK_FN )
if os.path.isfile(aboot):
print " - %s found in %s" % (ABOOT_FN, sourcePath)
else:
print bcolors.FAIL
print " - ERROR:Cannot find '%s' in '%s'" % (ABOOT_FN, sourcePath)
print " - Please make sure the file is named '%s' and located in '%s'" % (ABOOT_FN, sourcePath)
print bcolors.ENDC
exit(1)
if os.path.isfile(vmdk):
print " - %s found in %s" % (VMDK_FN, sourcePath)
else:
print bcolors.FAIL
print " - ERROR:Cannot find '%s' in '%s'" % (VMDK_FN, sourcePath)
print " - Please make sure the file is named '%s' and located in '%s'" % (VMDK_FN, sourcePath)
print bcolors.ENDC
exit(1)
if args.vmname:
vmName = args.vmname
else:
vmName = ""
# 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 vnets then create VM
if hyper == "virtualbox":
if createVBoxNets(hostOS, hostArch, libDir):
# Create the Virtual Machine
vmName = createVM(hyper, hostOS, nodes, vmName, boottime, user, packerCmd)
if vmName:
if registerVbox(hyper, libDir, vmName, nodes):
print "Successfully created VM %s!" % vmName
exit(0)
elif hyper == "vmware":
if createVmNets(hostOS, hostArch, libDir):
# Create the Virtual Machine
vmName = createVM(hyper, hostOS, nodes, vmName, boottime, user, packerCmd)
if vmName:
print "Successfully created VM %s!" % vmName
exit(0)
if __name__ == "__main__":
main()