-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnailgun.py
executable file
·334 lines (255 loc) · 11.5 KB
/
nailgun.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/python
"""
Usage: python nailgun.py test_config.cfg FUEL_IP
Requirements installation:
- sudo apt-get install -y python-dev python-pip libxslt-dev gcc make
- sudo pip install pip -U
- sudo pip install netaddr python-keystoneclient
Authors: Vadim Rovachev ([email protected])
Sergey Kolekonov ([email protected])
"""
from ConfigParser import SafeConfigParser
from sys import argv
import time
import json
import urllib2
from netaddr import *
import pprint
import python.nailgun_client as fuel
def delete_environment():
# Clean Fuel cluster
client = fuel.NailgunClient(str(fuel_ip))
for cluster in client.list_clusters():
client.delete_cluster(cluster['id'])
while True:
try:
client.get_cluster(cluster['id'])
except urllib2.HTTPError as e:
if str(e) == "HTTP Error 404: Not Found":
break
else:
raise
except Exception:
raise
time.sleep(1)
def create_environment():
# Connect to Fuel Main Server
client = fuel.NailgunClient(str(fuel_ip))
# Create cluster
release_id = client.get_release_id(cluster_settings['release_name'])
data = {"name": cluster_settings['env_name'], "release": release_id,
"mode": cluster_settings['config_mode'],
"net_provider": cluster_settings['net_provider']}
if cluster_settings.get('net_segment_type'):
data['net_segment_type'] = cluster_settings['net_segment_type']
client.create_cluster(data)
# Update cluster configuration
cluster_id = client.get_cluster_id(cluster_settings['env_name'])
attributes = client.get_cluster_attributes(cluster_id)
# settings = json.loads(cluster_settings['settings'])
settings = generate_components_config()
for option in settings:
section = False
if option in ('sahara', 'murano', 'ceilometer'):
section = 'additional_components'
if option in ('volumes_ceph', 'images_ceph', 'ephemeral_ceph',
'objects_ceph', 'osd_pool_size', 'volumes_lvm'):
section = 'storage'
if section:
attributes['editable'][section][option]['value'] = settings[option]
hpv_data = attributes['editable']['common']['libvirt_type']
hpv_data['value'] = str(cluster_settings['virt_type'])
debug = cluster_settings.get('debug', 'false')
auto_assign = cluster_settings.get('auto_assign_floating_ip', 'false')
nova_quota = cluster_settings.get('nova_quota', 'false')
attributes['editable']['common']['debug']['value'] = json.loads(debug)
attributes['editable']['common'][
'auto_assign_floating_ip']['value'] = json.loads(auto_assign)
attributes['editable']['common']['nova_quota']['value'] = \
json.loads(nova_quota)
client.update_cluster_attributes(cluster_id, attributes)
counter = 0
while True:
actual_kvm_count = len([k for k in client.list_nodes()
if not k['cluster'] and k['online']
and k['status'] == 'discover'])
if actual_kvm_count >= int(kvm_count):
break
counter += 5
if counter > 600:
raise RuntimeError
time.sleep(5)
# Add all available nodes to cluster
# for node_name, params in json.loads(cluster_settings['node_roles']).items():
for node_name, params in json.loads(generate_nodes_config()).items():
node = next(k for k in client.list_nodes()
if not k['cluster'] and k['online'])
data = {"cluster_id": str(cluster_id),
"pending_roles": params['roles'],
"pending_addition": True,
"name": node_name,
}
client.update_node(node['id'], data)
# Move networks on interfaces
for node in client.list_cluster_nodes(cluster_id):
# networks_dict = json.loads(cluster_settings['interfaces'])
networks_dict = generate_interfaces_config()
update_node_networks(client, node['id'], networks_dict)
# Update network
default_networks = client.get_networks(cluster_id)
# networks = json.loads(cluster_settings['networks'])
networks = generate_network_config()
change_dict = networks.get('networking_parameters', {})
for key, value in change_dict.items():
default_networks['networking_parameters'][key] = value
for net in default_networks['networks']:
change_dict = networks.get(net['name'], {})
for key, value in change_dict.items():
net[key] = value
client.update_network(cluster_id,
default_networks['networking_parameters'],
default_networks['networks'])
def deploy_environment():
client = fuel.NailgunClient(str(fuel_ip))
cluster_id = client.get_cluster_id(cluster_settings['env_name'])
client.deploy_cluster_changes(cluster_id)
def update_node_networks(client, node_id, interfaces_dict, raw_data=None):
# interfaces_dict['eth0'] = interfaces_dict.get('eth0', [])
# if 'fuelweb_admin' not in interfaces_dict['eth0']:
# interfaces_dict['eth0'].append('fuelweb_admin')
interfaces = client.get_node_interfaces(node_id)
if raw_data:
interfaces.append(raw_data)
all_networks = dict()
for interface in interfaces:
all_networks.update(
{net['name']: net for net in interface['assigned_networks']})
for interface in interfaces:
name = interface["name"]
interface['assigned_networks'] = \
[all_networks[i] for i in interfaces_dict.get(name, [])]
client.put_node_interfaces([{'id': node_id, 'interfaces': interfaces}])
def await_deploy():
client = fuel.NailgunClient(str(fuel_ip))
cluster_id = client.get_cluster_id(cluster_settings['env_name'])
notif_count = 0
done_deploy = 0
list_notification = []
while True:
try:
list_notification = client.get_notifications()
except urllib2.URLError:
pass
if len(list_notification) > notif_count:
log_file = open('await_deploy.log', 'aw')
log_file.write("{}\n{}\n".format(notif_count,
list_notification[notif_count:]))
log_file.close()
for notification in list_notification[notif_count:]:
if notification['cluster'] == cluster_id:
if notification['topic'] == 'error':
raise RuntimeError(notification['message'])
if notification['topic'] == 'done':
print notification['message']
return
notif_count = len(list_notification)
time.sleep(10)
def return_controller_ip(config, fuel_ip):
parser = SafeConfigParser()
parser.read(config)
cluster_settings = dict(parser.items('cluster'))
client = fuel.NailgunClient(str(fuel_ip))
cluster_id = client.get_cluster_id(cluster_settings['env_name'])
notification = [q for q in client.get_notifications()
if q['topic'] == "done" and q['cluster'] == cluster_id]
print [word for word in notification[0][
'message'].split() if word.startswith('http://')][0][7:-1]
def generate_nodes_config():
controller = ["controller"]
compute = ["compute"]
ceph_controller = int(cluster_settings.get('ceph_controller'))
ceph_compute = int(cluster_settings.get('ceph_compute'))
d = {}
ceph = False
cnt_count = int(cluster_settings.get('controller_count'))
cmp_count = int(cluster_settings.get('compute_count'))
if cluster_settings.get('ceilometer', 'false') == 'true':
controller.append("mongo")
for option in cluster_settings.viewkeys():
if "ceph" in option:
if cluster_settings.get(option) == "true":
ceph = True
for i in xrange(cnt_count):
s = "controller_%d" % i
d[s] = {"manufacturer": "QEMU"}
d[s]["roles"] = controller
for i in xrange(cmp_count):
s = "compute_%d" % i
d[s] = {"manufacturer": "QEMU"}
d[s]["roles"] = compute
if ceph:
for i in xrange(ceph_compute):
s = "compute_%d" % i
d[s]["roles"] = compute + ["ceph-osd"]
for i in xrange(ceph_controller):
s = "controller_%d" % i
d[s]["roles"] = controller + ["ceph-osd"]
if cluster_settings.get('volumes_lvm') == "true":
controller.append("cinder")
return str(json.dumps(d))
def generate_interfaces_config():
interfaces = dict(parser.items('interfaces'))
netmap = {}
for key in interfaces.keys():
if interfaces[key] != "":
netmap["%s" % key] = interfaces[key].split(',')
return netmap
def generate_network_config():
networks = {}
cidr = cluster_settings.get("cidr")
mask = IPNetwork(cidr).netmask
net_size = int(IPNetwork(cidr).prefixlen)
public_start = cluster_settings.get("public_range").split('-')[0]
public_end = cluster_settings.get("public_range").split('-')[1]
floating_start = cluster_settings.get("floating_range").split('-')[0]
floating_end = cluster_settings.get("floating_range").split('-')[1]
networks["public"] = {"network_size": net_size,
"netmask": "%s" % mask,
"ip_ranges": [["%s" % public_start, "%s" % public_end]],
"cidr": "%s" % cidr,
"gateway": cluster_settings.get("gateway")}
networks["management"] = {"vlan_start": int(cluster_settings.get("management_vlan"))}
networks["storage"] = {"vlan_start": int(cluster_settings.get("storage_vlan"))}
networks["networking_parameters"] = {"floating_ranges": [["%s" % floating_start, "%s" % floating_end]]}
if cluster_settings.get("net_segment_type") == "vlan":
floating_vlan_start = cluster_settings.get("floating_vlan_range").split('-')[0]
floating_vlan_end = cluster_settings.get("floating_vlan_range").split('-')[1]
networks["networking_parameters"]["vlan_range"] = [int(floating_vlan_start), int(floating_vlan_end)]
if cluster_settings.get("net_provider") == "nova_network":
nn_floating_vlan = cluster_settings.get("nn_floating_vlan")
# networks["fixed"] = {"vlan_start": int(cluster_settings.get("fixed_vlan"))}
networks["networking_parameters"]["fixed_networks_vlan_start"] = int(nn_floating_vlan)
return networks
def generate_components_config():
settings = {}
settings["sahara"] = s2b(cluster_settings.get('sahara', 'false'))
settings["murano"] = s2b(cluster_settings.get('murano', 'false'))
settings["ceilometer"] = s2b(cluster_settings.get('ceilometer', 'false'))
settings["volumes_lvm"] = s2b(cluster_settings.get('volumes_lvm', 'false'))
settings["volumes_ceph"] = s2b(cluster_settings.get('volumes_ceph', 'false'))
settings["images_ceph"] = s2b(cluster_settings.get('images_ceph', 'false'))
settings["ephemeral_ceph"] = s2b(cluster_settings.get('ephemeral_ceph', 'false'))
settings["osd_pool_size"] = cluster_settings.get('osd_pool_size', 1)
return settings
def s2b(v):
return v.lower() in ("yes", "true", "t", "1")
if __name__ == '__main__':
parser = SafeConfigParser()
parser.read(argv[1])
cluster_settings = dict(parser.items('cluster'))
fuel_ip = argv[2]
kvm_count = int(cluster_settings.get('node_count'))
delete_environment()
create_environment()
deploy_environment()
await_deploy()