/
boot.py
341 lines (305 loc) · 12.1 KB
/
boot.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
335
336
337
338
339
340
import logging
import configparser
from novaclient import client as novaclient
from neutronclient.v2_0 import client as neutronclient
import utils
from neutronapi import NeutronIF
logging.basicConfig(level=logging.INFO)
class Booter():
"""
Boots the corresponding topology.
"""
def __init__(self, topo='fw_lb', *args, **kwargs):
"""
Read from config.ini the configuration for the topologies.
"""
self.topo = topo
self.args = args
self.kwargs = kwargs
self.config = configparser.ConfigParser()
self.config.read('config.ini')
def up(self):
"""
Call the booter to the corresponding topology class.
"""
if self.topo == 'fw_lb':
config = utils.parse_config(self.config['FwLbTopo'])
fw_config = utils.parse_fw_config(self.config['FWaaS'])
FwLbTopo(opts=config,
fw_opts=fw_config,
session=self.kwargs['session'],
token=self.kwargs['token'],
neutron_endpoint=self.kwargs['neutron_endpoint']).up()
class FwLbTopo():
"""
Implements the Firewall and LoadBalancer topology:
- Two or three networks
- FaaS and LbaaS
- n instances as servers
- m instances as storage nodes
"""
def __init__(self,
opts=None,
fw_opts=None,
net_names=['net0','net1'],
net_prefixes=['10.0.0.0/24','10.0.1.0/24'],
subnet_names=['subnet0','subnet1'],
dns_nameservers=['8.8.8.8'],
router_name='r',
router_ports=['port0','port1'],
flavors=['tiny_personalizada','tiny_personalizada'],
images=['trusty-server-cloudimg-amd64-cnvr','trusty-server-cloudimg-amd64-cnvr'],
secgroups=['default','default'],
key_names=['my_key','my_key'],
instances=[3,3],
userdata=['userdata/balancer-ud.txt',
'userdata/server-ud.txt',
'userdata/storage-ud.txt'],
fixed_ips=None,
session=None,
token=None,
neutron_endpoint=None,
*args, **kwargs):
"""
Initialize the topology. Authorization with nova and neutron using
a session from keystone for nova and a token for neutron.
"""
self.fw_opts = fw_opts
self.net_names = opts.get('net_names', net_names)
self.net_prefixes = opts.get('net_prefixes', net_prefixes)
self.subnet_names = opts.get('subnet_names', subnet_names)
self.dns_nameservers = opts.get('dns_nameservers', dns_nameservers)
self.router_name = opts.get('router_name', router_name)
self.router_ports = opts.get('router_ports', router_ports)
self.flavors = opts.get('flavors', flavors)
self.images = opts.get('images', images)
self.secgroups = opts.get('secgroups', secgroups)
self.key_names = opts.get('key_names', key_names)
self.instances = opts.get('instances', instances)
self.userdata = opts.get('userdata', userdata)
self.fixed_ips = opts.get('fixed_ips')
if session is None:
raise ValueError('No session provided')
if token is None:
raise ValueError('No token provided')
if neutron_endpoint is None:
raise ValueError('No neutron_endpoint provided')
self.session = session
self.token = token
self.neutron_endpoint = neutron_endpoint
self.nets = []
self.subnets = []
self.servers = []
def _create_net(self,neutron=None, net_name=None):
"""
Create the network using the neutronclient.
"""
net_body = {'network':{
'name': net_name,
'admin_state_up': True
}
}
net = neutron.create_network(body=net_body)
return net['network']
def _create_subnet(self, neutron=None, subnet_name=None,
subnet_prefix=None, net_id=None):
"""
Create the subnet attached to a network using the
neutronclient.
"""
subnet_body = {'subnets':[{
'cidr': subnet_prefix,
'ip_version': 4,
'network_id': net_id,
'name': subnet_name
}
]}
subnet = neutron.create_subnet(body=subnet_body)
body = {'subnet':{
'dns_nameservers': self.dns_nameservers
}}
neutron.update_subnet(subnet['subnets'][0]['id'],
body)
return subnet['subnets'][0]
def _create_router(self, neutron=None, nova=None, net_name=None,
router_name=None, port_names=None):
"""
Create router and add a port to the subnet in the net
specified by the name
"""
if isinstance(router_name, list):
router_name = router_name[0]
net_ids = list()
if isinstance(net_name, list):
for net in net_name:
net = nova.networks.find(label=net)
net_id = net.id
net_ids.append(net_id)
else:
net = nova.networks.find(label=net_name)
net_id = net.id
net_ids.append(net_id)
ext_net = nova.networks.find(label='ExtNet')
ext_net_id = ext_net.id
# net_ids = [net_id, ext_net_id]
request = {'router': {'name': router_name,
'admin_state_up': True}}
router = neutron.create_router(request)
router_id = router['router']['id']
neutron.add_gateway_router(router_id, {'network_id': ext_net_id})
for net_id in net_ids:
subnet_id = None
for subnet in neutron.list_subnets()['subnets']:
if subnet['network_id'] == net_id:
subnet_id = subnet['id']
neutron.add_interface_router(router_id,
{'subnet_id': subnet_id})
def _boot_instance(self, nova=None, image=None, flavor=None, nets=None,
key_name=None, secgroups=None, name=None, userdata=None, fixed_ip=None,
count=1):
"""
Boot the instace/s using the novaclient.
"""
image = nova.images.find(name=image)
flavor = nova.flavors.find(name=flavor)
secgroups = [secgroups]
nics = []
if not isinstance(nets,list):
nets = [nets]
for net in nets:
net = nova.networks.find(label=net)
nics.append({'net-id':net.id})
if not fixed_ip is None:
for i, nic in enumerate(nics):
nic['v4-fixed-ip'] = fixed_ip[i]
if userdata is None:
f_userdata = None
else:
f_userdata = open(userdata, 'r')
instance = nova.servers.create(name=name, image=image, flavor=flavor,
key_name=key_name, nics=nics, max_count=count,
min_count=count, security_groups=secgroups,
userdata=f_userdata)
def up(self):
"""
Set up the topology.
"""
nova = novaclient.Client('2', session=self.session)
neutron = neutronclient.Client(endpoint_url=self.neutron_endpoint,
token=self.token)
neutron_if = NeutronIF()
# Create nets
logging.info('Creating networks...')
try:
for i in range(len(self.instances)):
self.nets.append(self._create_net(neutron=neutron,
net_name=self.net_names[i]))
except Exception as e:
logging.error('ERROR at creating networks:')
logging.error(e)
else:
logging.info('Success!')
# Create subnets into the created nets
logging.info('Creating subnetworks...')
try:
for i in range(len(self.instances)):
self.subnets.append(self._create_subnet(neutron=neutron,
subnet_name=self.subnet_names[i],
subnet_prefix=self.net_prefixes[i],
net_id=self.nets[i]['id']))
except Exception as e:
logging.error('ERROR at creating subnetworks')
logging.error(e)
else:
logging.info('Success!')
# Create router and connect to net
logging.info('Creating router...')
try:
self._create_router(neutron=neutron, nova=nova,
net_name=self.net_names[0],
router_name=self.router_name[0],
port_names=self.router_ports)
self._create_router(neutron=neutron, nova=nova,
net_name=self.net_names[1:],
router_name=self.router_name[1])
except Exception as e:
logging.error('ERROR at creating router')
logging.error(e)
else:
logging.info('Success!')
# Boot the load balancer instance
logging.info('Booting load balancer instance')
self._boot_instance(nova=nova, image=self.images[0],
flavor=self.flavors[0], nets=self.net_names[:2],
key_name=self.key_names[0],
secgroups=self.secgroups[0],
name='loadbalancer', userdata=self.userdata[0],
count=self.instances[0])
logging.info('Success!')
# Boot the server instances
logging.info("Booting server instances")
try:
self._boot_instance(nova=nova, image=self.images[1],
flavor=self.flavors[1], nets=self.net_names[1:],
key_name=self.key_names[1],
secgroups=self.secgroups[1],
name='server', userdata=self.userdata[1],
count=self.instances[1] )
except Exception as e:
logging.error('ERROR when creating servers')
logging.error(e)
else:
logging.info('Success!')
# Boot the storage instances
logging.info('Booting storage instances')
try:
self._boot_instance(nova=nova, image=self.images[2],
flavor=self.flavors[2], nets=self.net_names[2],
key_name=self.key_names[2],
secgroups=self.secgroups[2],
name='persist_1', count=1,
userdata=self.userdata[2],
fixed_ip=[self.fixed_ips[2]])
self._boot_instance(nova=nova, image=self.images[2],
flavor=self.flavors[2], nets=self.net_names[2],
key_name=self.key_names[2],
secgroups=self.secgroups[2],
name='persist_2', count=1,
userdata=self.userdata[3],
fixed_ip=[self.fixed_ips[3]])
except Exception as e:
logging.error('ERROR when creating storage instances')
logging.error(e)
else:
logging.info('Success!')
# Allocate a floating IP and associate to balancer instance
logging.info('Allocating IP')
try:
for server in nova.servers.list():
if server.name == 'loadbalancer':
id_ = server.id
nova.floating_ips.create(pool='ExtNet')
floating_ips = nova.floating_ips.list()
floating_ip = floating_ips[0].ip
nova.servers.add_floating_ip(server=id_,
address=floating_ip)
except Exception as e:
logging.error('ERROR when allocating IP')
logging.error(e)
else:
logging.info('Success!')
# Create Firewall
logging.info('Creating FWaaS')
try:
for fw_rule in self.fw_opts['rules']:
neutron_if.firewall_rule_create(fw_rule)
neutron_if.firewall_policy_create(name=self.fw_opts['policy_name'],
fw_rules=self.fw_opts['policy_rules'])
neutron_if.firewall_create(name=self.fw_opts['fw_name'],
fw_policy=self.fw_opts['policy_name'],
router=self.fw_opts['fw_router'])
except Exception as e:
logging.error('ERROR at creating FWaaS')
logging.error(e)
else:
logging.info('Success!')