-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.py
189 lines (155 loc) · 6.57 KB
/
install.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
#!/usr/bin/env python
#Copyright (c) 2006-2007 Citrix Systems, Inc.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import MySQLdb
import sys, time
import subprocess
try:
import XenAPI, provision
except:
print "Failed to import dependencies"
###########Database Connection Information##############
#Define Connection information for querying the DB.
hostname = "localhost"
username = ""
password = ""
database = ""
########################################################
#Connect to database and get templateid and hostid
db = MySQLdb.connect(hostname,username,password,database)
cursor = db.cursor()
cursor.execute("SELECT * FROM vms WHERE progress=0")
numrows = int(cursor.rowcount)
if numrows == 0:
print "Nothing for me to do..."
sys.exit(1)
result = cursor.fetchone()
vmid = result[0]
userid = result[1]
hostid = result[2]
vmname = result[6]
templateid = result[8]
#Change the progress to 10% to indicate the script is working on it.
cursor.execute("UPDATE vms SET progress='10' WHERE id='"+str(vmid)+"'")
#Use the hostid to get the connection information for the server.
db = MySQLdb.connect(hostname,username,password,database)
cursor = db.cursor()
cursor.execute("SELECT * from hosts where id="+str(hostid))
numrows = int(cursor.rowcount)
result = cursor.fetchone()
host_ip = result[1]
host_url = "https://"+str(result[1])
host_user = result[6]
host_pass = result[7]
host_mask = result[15]
host_gw = result[16]
host_dns1 = result[17]
host_dns2 = result[18]
#Fetch the assigned IP.
cursor.execute("SELECT ip from ips where vmid='"+str(vmid)+"'")
result = cursor.fetchone()
vm_ip = result[0]
#Create a session on the xenserver
session = XenAPI.Session(host_url)
try:
session.xenapi.login_with_password(host_user,host_pass)
except:
print "Unable to connect to XenServer."
cursor.execute("UPDATE vms SET progress='20' WHERE id='"+str(vmid)+"'")
# Choose the PIF with the alphabetically lowest device
try:
pifs = session.xenapi.PIF.get_all_records()
for pif in pifs:
device = pifs[pif]
if device['IP'] == host_ip:
network = session.xenapi.PIF.get_network(pif)
print "Chosen PIF is connected to network: ", session.xenapi.network.get_name_label(network)
except:
print "Error choosing PIF"
cursor.execute("UPDATE vms SET progress='30' WHERE id='"+str(vmid)+"'")
# List all the VM objects
vms = session.xenapi.VM.get_all_records()
print "Server has %d VM objects (this includes templates):" % (len(vms))
templates = []
for vm in vms:
record = vms[vm]
ty = "VM"
if record["is_a_template"]:
ty = "Template"
# Look for a template matching the given template ID
if record["name_label"].startswith("T-"+str(templateid)):
templates.append(vm)
print " Found %8s with name_label = %s" % (ty, record["name_label"])
cursor.execute("UPDATE vms SET progress='40' WHERE id='"+str(vmid)+"'")
print "Choosing a template to clone"
if templates == []:
print "Could not find a template. Exitting"
sys.exit(1)
cursor.execute("UPDATE vms SET progress='50' WHERE id='"+str(vmid)+"'")
template = templates[0]
print " Selected template: ", session.xenapi.VM.get_name_label(template)
print "Installing new VM from the template"
vm = session.xenapi.VM.clone(template, vmname)
print " New VM has name: "+vmname
print "Creating VIF"
vif = { 'device': '0',
'network': network,
'VM': vm,
'MAC': "",
'MTU': "1500",
"qos_algorithm_type": "",
"qos_algorithm_params": {},
"other_config": {} }
vif = session.xenapi.VIF.create(vif)
print "Adding noniteractive to the kernel commandline"
session.xenapi.VM.set_PV_args(vm, "noninteractive")
session.xenapi.VM.provision(vm)
cursor.execute("UPDATE vms SET progress='60' WHERE id='"+str(vmid)+"'")
#Get the MAC address of the new interface
vm_mac = session.xenapi.VIF.get_MAC(vif)
cursor.execute("UPDATE vms SET progress='70' WHERE id='"+str(vmid)+"'")
#SSH to the xenserver and append it to the dhcpd.conf file and restart the service.
sshConnect=host_user+"@"+host_ip
proc = subprocess.Popen(['ssh', sshConnect, 'echo "host '+str(vmid)+'{" >> /etc/dhcpd.conf;',
'echo "hardware ethernet '+vm_mac+';" >> /etc/dhcpd.conf;',
'echo "option routers '+host_gw+';" >> /etc/dhcpd.conf;',
'echo "option subnet-mask '+host_mask+';" >> /etc/dhcpd.conf;',
'echo "fixed-address '+vm_ip+';" >> /etc/dhcpd.conf;',
'echo "option domain-name-servers '+host_dns1+','+host_dns2+';" >> /etc/dhcpd.conf;',
'echo "}" >> /etc/dhcpd.conf;',
'service dhcpd restart',
],stdin=subprocess.PIPE)
print "Starting VM"
session.xenapi.VM.start(vm, False, True)
cursor.execute("UPDATE vms SET progress='80' WHERE id='"+str(vmid)+"'")
print " VM is booting"
print "Waiting for the installation to complete"
# Here we poll because we don't generate events for metrics objects currently
def read_ip_address(vm):
vgm = session.xenapi.VM.get_guest_metrics(vm)
try:
os = session.xenapi.VM_guest_metrics.get_networks(vgm)
if "0/ip" in os.keys():
return os["0/ip"]
cursor.execute("UPDATE vms SET progress='90' WHERE id='"+str(vmid)+"'")
return None
except:
return None
while read_ip_address(vm) == None: time.sleep(1)
print "Reported IP: ", read_ip_address(vm)
vm_uuid = session.xenapi.VM.get_uuid(vm)
print "UUID="+vm_uuid
cursor.execute("UPDATE vms set uuid='"+vm_uuid+"' WHERE id='"+str(vmid)+"'")
session.xenapi.session.logout()
cursor.execute("UPDATE vms SET progress='100' WHERE id='"+str(vmid)+"'")