· 9 years ago · Sep 06, 2017, 03:24 PM
1#!/usr/local/bin/python
2
3import MySQLdb
4import sys
5import os
6import subprocess
7import csv
8import ipaddress
9from collections import OrderedDict
10from subprocess import Popen, PIPE
11from os import environ, path
12from optparse import OptionParser
13from configparser import ConfigParser
14
15
16class MysqlPython(object):
17
18 __instance = None
19 __host = None
20 __user = None
21 __password = None
22 __database = None
23 __session = None
24 __connection = None
25
26 def __new__(cls, *args, **kwargs):
27 if not cls.__instance or not cls.__database:
28 cls.__instance = super(MysqlPython, cls).__new__(cls, *args, **kwargs)
29 return cls.__instance
30 # End def __new__
31
32 def __init__(self, host='localhost', user='root', password='', database=''):
33 self.__host = host
34 self.__user = user
35 self.__password = password
36 self.__database = database
37 # End def __init__
38
39 def __open(self):
40 try:
41 cnx = MySQLdb.connect(self.__host, self.__user, self.__password, self.__database)
42 self.__connection = cnx
43 self.__session = cnx.cursor(MySQLdb.cursors.DictCursor)
44 except MySQLdb.Error as e:
45 print "Error %d: %s" % (e.args[0], e.args[1])
46 # End def __open
47
48 def __close(self):
49 self.__session.close()
50 self.__connection.close()
51 # End def __close
52
53 def select(self, query):
54 results = None
55 self.__open()
56 self.__session.execute(query)
57 number_rows = self.__session.rowcount
58 number_columns = len(self.__session.description)
59
60 if number_rows >= 1 and number_columns > 1:
61 results = [item for item in self.__session.fetchall()]
62 elif number_rows > 1 and number_columns == 1:
63 results = self.__session.fetchall()
64 else:
65 results = [item[0] for item in self.__session.fetchall()]
66
67 self.__close()
68
69 return results
70 # End def select
71
72 def select_one(self, query):
73 self.__open()
74 self.__session.execute(query)
75 number_rows = self.__session.rowcount
76 number_columns = len(self.__session.description)
77
78 if number_rows > 1 or number_columns > 1:
79 print "Error too mach results returned! \n"
80 result = None
81 else:
82 result = self.__session.fetchone()
83
84 self.__close()
85
86 return result
87 # End def select_one
88
89 def update(self, query):
90 self.__open()
91 self.__session.execute(query)
92 self.__connection.commit()
93
94 # Obtain rows affected
95 update_rows = self.__session.rowcount
96 self.__close()
97
98 return update_rows
99 # End function update
100
101 def insert(self, query):
102 self.__open()
103 self.__session.execute(query)
104 self.__connection.commit()
105
106 # Obtain last row id
107 insert_rowid = self.__session.lastrowid
108 self.__close()
109
110 return insert_rowid
111 ## End def insert
112
113 def delete(self, query, values):
114 self.__open()
115 self.__session.execute(query, values)
116 self.__connection.commit()
117
118 # Obtain rows affected
119 delete_rows = self.__session.rowcount
120 self.__close()
121
122 return delete_rows
123 ## End def delete
124
125 def create(self, create_query):
126 self.__open()
127 # Create table
128 self.__session.execute(create_query)
129 self.__close()
130
131 return
132 ## End def create
133
134 def drop(self, table):
135 self.__open()
136 # Drop table if exists"
137 drop_query = "DROP TABLE IF EXISTS %s " % (table)
138 self.__session.execute(drop_query)
139 self.__close()
140
141 return
142 ## End def drop
143
144# End class MysqlPython
145
146
147def source(script, update=True, clean=True):
148 global environ
149 if clean:
150 environ_back = dict(environ)
151 environ.clear()
152
153 pipe = Popen(". %s; env" % script, stdout=PIPE, shell=True)
154 data = pipe.communicate()[0]
155
156 env = dict((line.split("=", 1) for line in data.splitlines()))
157
158 if clean:
159 # remove unwanted minimal vars
160 env.pop('LINES', None)
161 env.pop('COLUMNS', None)
162 environ = dict(environ_back)
163
164 if update:
165 environ.update(env)
166
167 return env
168 # End def source
169
170
171def read_db_config(filename='my.cnf', section='mysql'):
172 # create parser and read database ini configuration file
173 parser = ConfigParser()
174 parser.read(filename)
175
176 # get section, default to mysql
177 db = {}
178 if parser.has_section(section):
179 items = parser.items(section)
180 for item in items:
181 db[item[0]] = item[1]
182 else:
183 raise Exception(
184 '{0} not found in the {1} file'.format(section, filename))
185
186 return db
187 # End def read_db_config
188
189
190def get_projectid(name, db_connect):
191 sql_query = """
192 SELECT id, name
193 FROM keystone.project
194 WHERE name='%s'
195 AND enabled=1
196 """ % (name)
197
198 projectid = db_connect.select(sql_query)[0]["id"]
199
200 return projectid
201 # End def get_projectid
202
203
204def check_router(r_name, tenant_id, db_connect):
205 # Check router on service tenant..
206 sql_query = """
207 SELECT id, name
208 FROM neutron.routers
209 WHERE tenant_id= '%s'
210 AND status='ACTIVE'
211 AND name='%s'
212 """ % (tenant_id, r_name)
213
214 if db_connect.select(sql_query):
215 router_name = db_connect.select(sql_query)[0]["name"]
216 else:
217 router_name = None
218
219 return router_name
220 # End def check_router
221
222
223def create_neutron_router(r_name, tenant_id, db_connect, env):
224 print
225 # Check router on service tenant and create one if it doesn't exist..
226 router = check_router(r_name, tenant_id, db_connect)
227 if router:
228 print "Found router '%s' on service tenant..\n" % router
229 else:
230 print "Neutron router not found, creating one..\n"
231 router_create = ("%s --tenant-id %s --distributed True" % (r_name, tenant_id))
232 subprocess.call(["neutron router-create %s" % (router_create)], env=env, shell=True)
233
234 # End def create_neutron_router
235
236
237def check_neutron(r_name, tenant_id, db_connect, env):
238 # Check basic neutron configuration..
239 # Check router on service tenant..
240 router = check_router(r_name, tenant_id, db_connect)
241 if router:
242 print "Found router '%s' on service tenant..\n" % (router)
243 else:
244 print "Neutron router '%s' not found!\n" % (r_name)
245
246 # Check networks..
247 sql_query = """
248 SELECT n.name AS n_name, p.name AS p_name
249 FROM neutron.networks n
250 JOIN keystone.project p
251 ON n.tenant_id=p.id
252 WHERE n.status='ACTIVE'
253 """
254
255 if db_connect.select(sql_query):
256 print "Found neutron networks..\n"
257 subprocess.call(["neutron", "net-list"], env=env)
258 else:
259 print "No neutron networks found!\n"
260
261 # Check subnets..
262 sql_query = """
263 SELECT p.name AS project, n.name AS network, s.name AS subnet, s.cidr, s.gateway_ip
264 FROM neutron.subnets s
265 JOIN keystone.project p
266 ON s.tenant_id=p.id
267 JOIN neutron.networks n
268 ON s.network_id=n.id
269 WHERE n.status='ACTIVE'
270 AND p.enabled='1'
271 """
272
273 if db_connect.select(sql_query):
274 print "Found neutron subnets..\n"
275 subprocess.call(["neutron", "subnet-list"], env=env)
276 else:
277 print "No neutron subnets found!\n"
278
279 # End def check_neutron
280
281
282def create_external_networks(router, ext_nets, t_service, env):
283 # From "ext_nets" configuration file, create one external network named "floating".
284 # Update these options if creating more than one external network.
285 with open(ext_nets) as csvfile:
286 reader = csv.DictReader(csvfile)
287 for row in reader:
288 net_create = ("--tenant-id %s --provider:physical_network external --provider:network_type vlan --provider:segmentation_id %s --router:external floating"
289 % (t_service, row['vlan_id']))
290
291 print
292 print "Creating external floating network vlan %s..\n" % (row['vlan_id'])
293 subprocess.call(["neutron net-create %s" % (net_create)], env=env, shell=True)
294
295 subnet_create = ("--tenant-id %s --name floating-subnet --disable-dhcp --allocation-pool start=%s,end=%s --dns-nameserver %s --dns-nameserver %s --gateway %s floating %s"
296 % (t_service, row['pool_start'], row['pool_end'], row['dns1'], row['dns2'], row['gateway'], row['network']))
297
298 print
299 print "Creating external subnet floating-subnet..\n"
300 subprocess.call(["neutron subnet-create %s" % (subnet_create)], env=env, shell=True)
301
302 print
303 print "Creating gateway for floating network..\n"
304 router_gateway = ("--fixed-ip ip_address=%s %s floating" % (row['v_gateway'], router))
305 subprocess.call(["neutron router-gateway-set %s" % (router_gateway)], env=env, shell=True)
306
307 # End def create_external_networks
308
309
310def get_network_id(filename='network.out'):
311 uuid = None
312 with open(filename) as f:
313 for line in f:
314 line = line.replace(' ', '')
315 if "|id|" in line:
316 uuid = line.split("|")[2]
317 break
318
319 return uuid
320
321 # End def get_network_id
322
323
324def create_internal_networks(db_connect, env):
325 print
326 sql_query = """
327 SELECT uuid, project_id, label, vlan, cidr, netmask, dhcp_start, gateway
328 FROM nova.networks
329 WHERE deleted_at IS NULL
330 """
331
332 for row in db_connect.select(sql_query):
333 print "Creating internal network '%s' vlan '%s'..\n" % (row['label'], row['vlan'])
334 net_create = ("--tenant-id %s --provider:physical_network internal --provider:network_type vlan --provider:segmentation_id %s %s"
335 % (row["project_id"], row['vlan'], row['label']))
336
337 with open('network.out', 'w') as f:
338 subprocess.call(["neutron net-create %s" % (net_create)], env=env, shell=True, stdout=f)
339
340 # getting the new neutron network 'id'
341 network_id = get_network_id()
342 if not network_id:
343 print "There was a problem getting the new network id..\n"
344 return 1
345
346 print "Updating uuid '%s' of the internal network '%s' vlan '%s' using existing uuid '%s'..\n" % (network_id, row['label'], row['vlan'], row["uuid"])
347 sql_update = ("UPDATE neutron.networks SET id = '%s' WHERE id = '%s'" % (row["uuid"], network_id))
348 db_connect.update(sql_update)
349
350 print "Finished creating neutron networks.."
351 subprocess.call(["neutron", "net-list"], env=env)
352
353 # End def create_internal_networks
354
355
356def create_temporary_data(int_nets, int_pools, pools_update, db_connect, env):
357 # Create network temporary table
358 sql_create = """
359 CREATE TABLE `nova`.`network_tmp` (
360 `id` int(11) NOT NULL AUTO_INCREMENT,
361 `vlan_id` int(11) NOT NULL,
362 `dns1` varchar(39) NOT NULL,
363 `dns2` varchar(39) NOT NULL,
364 `gateway` varchar(39) DEFAULT NULL,
365 PRIMARY KEY (`id`)
366 ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
367 """
368
369 db_connect.create(sql_create)
370
371 with open(int_nets) as csvfile:
372 reader = csv.DictReader(csvfile)
373 for row in reader:
374 sql_insert = """INSERT INTO `nova`.`network_tmp`
375 SET vlan_id = %d, dns1 = '%s', dns2 = '%s', gateway = '%s'
376 """ % (int(row['vlan_id']), row['dns1'], row['dns2'], row['gateway'])
377
378 db_connect.insert(sql_insert)
379
380 # Create network pools temporary table
381 sql_create = """
382 CREATE TABLE `nova`.`network_pools_tmp` (
383 `id` int(11) NOT NULL AUTO_INCREMENT,
384 `vlan_id` int(11) NOT NULL,
385 `pool_start` varchar(39) NOT NULL,
386 `pool_end` varchar(39) NOT NULL,
387 PRIMARY KEY (`id`)
388 ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
389 """
390 # CONSTRAINT `network_pools_tmp_fk` FOREIGN KEY (`vlan_id`) REFERENCES `nova`.`network_tmp` (`vlan_id`) ON DELETE CASCADE
391
392 db_connect.create(sql_create)
393
394 with open(int_pools) as csvfile:
395 reader = csv.DictReader(csvfile)
396 for row in reader:
397 sql_insert = """INSERT INTO `nova`.`network_pools_tmp`
398 SET vlan_id = %d, pool_start = '%s', pool_end = '%s'
399 """ % (int(row['vlan_id']), row['pool_start'], row['pool_end'])
400
401 db_connect.insert(sql_insert)
402
403 # Create network pools update temporary table
404 sql_create = """
405 CREATE TABLE `nova`.`network_pools_update_tmp` (
406 `id` int(11) NOT NULL AUTO_INCREMENT,
407 `vlan_id` int(11) NOT NULL,
408 `pool_start` varchar(39) NOT NULL,
409 `pool_end` varchar(39) NOT NULL,
410 PRIMARY KEY (`id`)
411 ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
412 """
413
414 db_connect.create(sql_create)
415
416 with open(pools_update) as csvfile:
417 reader = csv.DictReader(csvfile)
418 for row in reader:
419 sql_insert = """INSERT INTO `nova`.`network_pools_update_tmp`
420 SET vlan_id = %d, pool_start = '%s', pool_end = '%s'
421 """ % (int(row['vlan_id']), row['pool_start'], row['pool_end'])
422
423 db_connect.insert(sql_insert)
424
425 # End def create_temporary_data
426
427
428def create_internal_subnets(router, db_connect, env):
429 sql_query = """
430 SELECT nn.uuid, nn.project_id, nn.label, nn.vlan, nn.cidr, nn.gateway as v_gateway, nt.dns1, nt.dns2, nt.gateway as p_gateway
431 FROM nova.networks nn
432 JOIN nova.network_tmp nt
433 WHERE nn.vlan=nt.vlan_id
434 AND nn.deleted_at IS NULL
435 """
436
437 for row in db_connect.select(sql_query):
438
439 sql_query_pools = """
440 SELECT pool_start, pool_end
441 FROM nova.network_pools_tmp
442 WHERE vlan_id = %d
443 """ % int(row['vlan'])
444
445 allocation_pool = ""
446
447 for pool in db_connect.select(sql_query_pools):
448 allocation_pool += ("--allocation-pool start=%s,end=%s " % (pool["pool_start"], pool['pool_end']))
449
450 intf = ipaddress.ip_interface(u"%s" % (row['cidr']))
451
452 if intf.network.is_private:
453 if row['p_gateway']:
454 print
455 print "Creating internal subnet 'subnet%s' for private routed network '%s'..\n" % (row['vlan'], row['cidr'])
456 if row['dns1']:
457 subnet_create = ("--tenant-id %s --name subnet%s --enable-dhcp %s --dns-nameserver %s --dns-nameserver %s --gateway %s --host-route destination=134.158.0.0/16,nexthop=%s --host-route destination=193.48.96.0/20,nexthop=%s --host-route destination=193.48.80.0/20,nexthop=%s --host-route destination=193.48.112.0/23,nexthop=%s %s %s" % (row['project_id'], row['vlan'], allocation_pool, row['dns1'], row['dns2'], row['v_gateway'], row['p_gateway'], row['p_gateway'], row['p_gateway'], row['p_gateway'], row['label'], row['cidr']))
458 else:
459 subnet_create = ("--tenant-id %s --name subnet%s --enable-dhcp %s --gateway %s --host-route destination=134.158.0.0/16,nexthop=%s --host-route destination=193.48.96.0/20,nexthop=%s --host-route destination=193.48.80.0/20,nexthop=%s --host-route destination=193.48.112.0/23,nexthop=%s %s %s" % (row['project_id'], row['vlan'], allocation_pool, row['v_gateway'], row['p_gateway'], row['p_gateway'], row['p_gateway'], row['p_gateway'], row['label'], row['cidr']))
460
461 subprocess.call(["neutron subnet-create %s" % (subnet_create)], env=env, shell=True)
462
463 else:
464 print
465 print "Creating internal subnet 'subnet%s' for private not routed network '%s'..\n" % (row['vlan'], row['cidr'])
466 if row['dns1']:
467 subnet_create = ("--tenant-id %s --name subnet%s --enable-dhcp %s --dns-nameserver %s --dns-nameserver %s --gateway %s %s %s" % (row['project_id'], row['vlan'], allocation_pool, row['dns1'], row['dns2'], row['v_gateway'], row['label'], row['cidr']))
468 else:
469 subnet_create = ("--tenant-id %s --name subnet%s --enable-dhcp %s --gateway %s %s %s" % (row['project_id'], row['vlan'], allocation_pool, row['v_gateway'], row['label'], row['cidr']))
470
471 subprocess.call(["neutron subnet-create %s" % (subnet_create)], env=env, shell=True)
472
473 print
474 print "Creating gateway for subnet%s..\n" % (row['vlan'])
475 router_gateway = ("%s subnet%s" % (router, row['vlan']))
476
477 subprocess.call(["neutron router-interface-add %s" % (router_gateway)], env=env, shell=True)
478
479 else:
480 print
481 print "Creating internal subnet 'subnet%s' for public network '%s'..\n" % (row['vlan'], row['cidr'])
482 if row['dns1']:
483 subnet_create = ("--tenant-id %s --name subnet%s --enable-dhcp %s --dns-nameserver %s --dns-nameserver %s --no-gateway --host-route destination=0.0.0.0/0,nexthop=%s %s %s" % (row['project_id'], row['vlan'], allocation_pool, row['dns1'], row['dns2'], row['p_gateway'], row['label'], row['cidr']))
484 else:
485 subnet_create = ("--tenant-id %s --name subnet%s --enable-dhcp %s --no-gateway --host-route destination=0.0.0.0/0,nexthop=%s %s %s" % (row['project_id'], row['vlan'], allocation_pool, row['p_gateway'], row['label'], row['cidr']))
486
487 subprocess.call(["neutron subnet-create %s" % (subnet_create)], env=env, shell=True)
488
489 print
490 print "Updating ports quota for project '%s' (subnet 'subnet%s', network '%s')..\n" % (row['project_id'], row['vlan'], row['cidr'])
491 quota_udate = ("--port %s --tenant-id %s " % (intf.network.num_addresses, row['project_id']))
492 subprocess.call(["neutron quota-update %s" % (quota_udate)], env=env, shell=True)
493
494 quota_udate = ("--security-group %d --security-group-rule %d --tenant-id %s " % ((1), (4), row['project_id']))
495 subprocess.call(["neutron quota-update %s" % (quota_udate)], env=env, shell=True)
496
497
498 # End def create_internal_subnets
499
500def update_subnets(db_connect, env):
501 sql_query = """
502 SELECT distinct(vlan_id)
503 FROM nova.network_pools_update_tmp
504 """
505
506 for row in db_connect.select(sql_query):
507
508 sql_query_pools_update = """
509 SELECT pool_start, pool_end
510 FROM nova.network_pools_update_tmp
511 WHERE vlan_id = %d
512 """ % int(row['vlan_id'])
513
514 allocation_pool = ""
515
516 for pool in db_connect.select(sql_query_pools_update):
517 allocation_pool += ("--allocation-pool start=%s,end=%s " % (pool["pool_start"], pool['pool_end']))
518
519 subnet_pool_update = ("%s subnet%s" % (allocation_pool, row['vlan_id']))
520
521 print
522 print "Update allocation pool for subnet%s..\n" % (row['vlan_id'])
523
524 subprocess.call(["neutron subnet-update %s" % (subnet_pool_update)], env=env, shell=True)
525
526
527 # End def update_subnets
528
529
530def get_port_id(filename='port.out'):
531 uuid = None
532 with open(filename) as f:
533 for line in f:
534 line = line.replace(' ', '')
535 if "|id|" in line:
536 uuid = line.split("|")[2]
537 break
538
539 return uuid
540
541 # End def get_port_id
542
543def create_ports(db_connect, env):
544 sql_query = """
545 SELECT n.vlan, n.uuid as network_uuid, i.project_id, v.instance_uuid, i.hostname as instance_name, v.address as mac_address, f.address as ip_address, i.availability_zone, i.host
546 FROM nova.virtual_interfaces as v
547 JOIN (nova.networks as n, nova.instances as i, nova.fixed_ips as f)
548 WHERE v.network_id = n.id
549 AND v.instance_uuid = i.uuid
550 AND v.id = f.virtual_interface_id
551 AND v.deleted_at IS NULL
552 AND f.deleted_at IS NULL
553 ORDER BY i.host, n.vlan ASC
554 """
555
556 compute_name = None
557 for row in db_connect.select(sql_query):
558 port_create = ("--tenant-id %s --mac-address %s --fixed-ip subnet_id=subnet%s,ip_address=%s --device-owner compute:%s %s" % (row['project_id'], row['mac_address'], row['vlan'], row['ip_address'], row['availability_zone'], row['network_uuid']))
559
560 print
561 print "Creating neutron port for instance '%s' (mac=%s, ip=%s).. \n" % (row['instance_name'], row['mac_address'], row['ip_address'])
562
563 with open('port.out', 'w') as f:
564 subprocess.call(["neutron port-create %s" % (port_create)], env=env, shell=True, stdout=f)
565
566 # getting the neutron port 'id'
567 port_id = get_port_id()
568 if not port_id:
569 print "There was a problem getting the port id..\n"
570 return 1
571
572 print "Attach neutron port (id=%s, mac=%s, ip=%s) to instance '%s' .. \n" % (port_id, row['mac_address'], row['ip_address'],row['instance_name'])
573 #print "Instance '%s' must be in a normal state.. \n" % (row['instance_name'])
574
575 port_attach = ("--port-id %s %s" % (port_id, row['instance_uuid']))
576
577 print "Saving data to file attach_ports for later use.. \n"
578 with open('attach_ports', 'a') as p:
579 if row['host'] != compute_name:
580 p.write("######################################\n")
581 p.write("# Compute: %s \n" % (row['host']))
582 p.write("######################################\n")
583 p.write("#neutron port-create %s \n" % (port_create))
584 p.write("nova interface-attach %s \n" % (port_attach))
585
586 compute_name = row['host']
587
588 #print "Port attachment disabled during migration process.. \n"
589 #subprocess.call(["nova interface-attach %s" % (port_attach)], env=env, shell=True)
590
591 # End def create_ports
592
593
594def enable_dhcp(db_connect, env):
595 sql_query = """
596 SELECT id, name
597 FROM neutron.subnets
598 WHERE gateway_ip IS NULL
599 AND enable_dhcp=0
600 """
601 for row in db_connect.select(sql_query):
602 print
603 print "Adding dhcp support for subnet '%s'.. \n" % (row['name'])
604 subprocess.call(["neutron subnet-update --enable-dhcp %s" % (row['id'])], env=env, shell=True)
605 # End def enable_dhcp
606
607
608def get_ext_netid(ip_address, db_connect, env):
609 ext_netid = None
610 sql_query = """SELECT name, network_id, cidr
611 FROM neutron.subnets
612 """
613 #WHERE name like 'ext-%'
614 for row in db_connect.select(sql_query):
615 if ipaddress.ip_address(u"%s" % (ip_address)) in ipaddress.ip_network(u"%s" % (row['cidr'])):
616 print
617 print "Found network '%s' (%s) for floating ip '%s' \n" % (row['cidr'], row['name'], ip_address)
618 ext_netid = row['network_id']
619 break
620# else:
621# print "Floating ip '%s' is not part of network '%s' (%s) \n" % (ip_address, row['cidr'], row['name'])
622
623 return ext_netid
624
625 # End def get_ext_netid
626
627
628def create_floating_ips(db_connect, env):
629 sql_query = """
630 SELECT fl.project_id, fl.address AS floating_ip, fx.address AS fixed_ip, v.address AS fixed_mac, fx.instance_uuid
631 FROM nova.fixed_ips AS fx
632 JOIN (nova.floating_ips AS fl, nova.virtual_interfaces AS v)
633 WHERE fx.id = fl.fixed_ip_id
634 AND fx.virtual_interface_id = v.id
635 AND fx.deleted_at IS NULL
636 AND fl.deleted_at IS NULL
637 AND v.deleted_at IS NULL
638 """
639
640 for row in db_connect.select(sql_query):
641 sql_getport = """SELECT id
642 FROM neutron.ports
643 WHERE mac_address='%s'
644 AND tenant_id='%s'
645 """ % (row['fixed_mac'], row['project_id'])
646
647 port_id = db_connect.select_one(sql_getport)['id']
648 network_id = get_ext_netid(row['floating_ip'], db_connect, env)
649
650 sql_getinstance = """SELECT hostname
651 FROM nova.instances
652 WHERE uuid = '%s'
653 """ % (row['instance_uuid'])
654
655 instance_name = db_connect.select_one(sql_getinstance)['hostname']
656
657 print "creating floating ip '%s' for instance '%s' (uuid='%s', ip='%s').. \n" % (row['floating_ip'], instance_name, row['instance_uuid'], row['fixed_ip'])
658
659 floatingip_create = ("--tenant-id %s --port-id %s --floating-ip-address %s %s" % (row['project_id'], port_id, row['floating_ip'], network_id))
660 subprocess.call(["neutron floatingip-create %s" % (floatingip_create)], env=env, shell=True)
661
662 # End def create_floating_ips
663
664noop = False
665
666
667def main():
668 global noop
669 nova_creds, mysql_conf, ext_nets, int_nets, int_pools, pools_update, r_name, exec_type = parse_options()
670
671 # openstack creds and environment variables
672 if os.path.isfile(nova_creds) and os.access(nova_creds, os.R_OK):
673 env = {}
674 # env.update(os.environ)
675 env.update(source(nova_creds))
676 else:
677 print "Either creds file is missing or is not readable!\n"
678 return 1
679
680 # database connection
681 if os.path.isfile(mysql_conf) and os.access(mysql_conf, os.R_OK):
682 dsn = read_db_config(mysql_conf)
683 db_connect = MysqlPython(dsn['host'], dsn['user'], dsn['password'], dsn['database'])
684 else:
685 print "Either mysql configuration file is missing or is not readable!\n"
686 return 1
687
688 # service tenant_id
689 t_service = get_projectid('service', db_connect)
690
691 # Run exec types
692 if exec_type == 'test':
693 # work in progress
694 print "Checking Neutron configuration..\n"
695 check_neutron(r_name, t_service, db_connect, env)
696
697
698 elif exec_type == 'migration':
699
700 # delete temporary data
701 print "Removing temporary data before creation..\n"
702 db_connect.drop('`nova`.`network_tmp`')
703 db_connect.drop('`nova`.`network_pools_tmp`')
704 db_connect.drop('`nova`.`network_pools_update_tmp`')
705
706 # work in progress
707 print "Processig network migration..\n"
708
709 # create neutron router if it doesn't exist
710 create_neutron_router(r_name, t_service, db_connect, env)
711
712 # external networks configuration
713 if os.path.isfile(ext_nets) and os.access(ext_nets, os.R_OK):
714 print
715 print "Creating external networks..\n"
716 create_external_networks(r_name, ext_nets, t_service, env)
717 else:
718 print
719 print "Either external networks configuration file is missing or is not readable!\n"
720 return 1
721
722 # internal networks configuration
723 create_internal_networks(db_connect, env)
724
725 # create temporary data for internal subnets
726 if os.path.isfile(int_nets) and os.access(int_nets, os.R_OK):
727 if os.path.isfile(int_pools) and os.access(int_pools, os.R_OK):
728 print
729 print "Creating temporary data for internal subnets..\n"
730 create_temporary_data(int_nets, int_pools, pools_update, db_connect, env)
731 else:
732 print
733 print "Either internal network pools configuration file is missing or is not readable!\n"
734 return 1
735 else:
736 print
737 print "Either internal networks configuration file is missing or is not readable!\n"
738 return 1
739
740 # create internal subnets
741 print "Creating internal subnets..\n"
742 create_internal_subnets(r_name, db_connect, env)
743
744 # create ports
745 print "Creating ports..\n"
746 create_ports(db_connect, env)
747
748 # enable dhcp for routed public networks
749 enable_dhcp(db_connect, env)
750
751 # create floating ips
752 #print
753 #print "Creating floating ips..\n"
754 #create_floating_ips(db_connect, env)
755
756 # update subnet pools
757 if os.path.isfile(pools_update) and os.access(pools_update, os.R_OK):
758 print
759 print "Updating subnet pools..\n"
760 update_subnets(db_connect, env)
761 else:
762 print
763 print "Either pools_update configuration file is missing or is not readable!\n"
764 return 1
765
766 print "Removing temporary data after creation..\n"
767 db_connect.drop('`nova`.`network_tmp`')
768 db_connect.drop('`nova`.`network_pools_tmp`')
769 db_connect.drop('`nova`.`network_pools_update_tmp`')
770
771 # End main
772
773def parse_options():
774 global noop
775 parser = OptionParser(__doc__)
776 parser.add_option("-c", "--creds",
777 dest="nova_creds",
778 #default = os.path.expanduser('./creds'),
779 default="./creds",
780 help="path to nova_creds file (default to 'creds')")
781 parser.add_option("-m", "--mycnf",
782 dest="mysql_conf",
783 default="./mysql.ini",
784 help="path to mysql configuration file (default to 'mysql.ini')")
785 parser.add_option("-x", "--extnets",
786 dest="ext_nets",
787 default="./ext_nets",
788 help="""path to external networks configuration file (default to 'ext_nets')""")
789 parser.add_option("-i", "--intnets",
790 dest="int_nets",
791 default="./int_nets",
792 help="""path to internal networks configuration file (default to 'int_nets')""")
793 parser.add_option("-p", "--pools",
794 dest="int_pools",
795 default="./int_pools",
796 help="""path to internal network pools configuration file (default to 'int_pools')""")
797 parser.add_option("-u", "--update",
798 dest="pools_update",
799 default="./pools_update",
800 help="""path to network pools update configuration file (default to 'pools_update')""")
801 parser.add_option("-r", "--router",
802 dest="r_name",
803 default="router01",
804 help="Neutron router name (default to 'router01')")
805 parser.add_option("-e", "--exec",
806 type="choice",
807 action="store",
808 dest="exec_type",
809 choices=["migration", "test", ],
810 default="test",
811 help="""[migration]: Execute migration to Neutron,
812 [test]: Check Neutron configuration (default)""")
813 parser.add_option("-n", "--noop",
814 dest="noop",
815 action="store_true",
816 help="Take no actions (dry-run)")
817
818 (options, _) = parser.parse_args()
819 nova_creds, mysql_conf, ext_nets, int_nets, int_pools, pools_update, r_name, exec_type = options.nova_creds, options.mysql_conf, options.ext_nets, options.int_nets, options.int_pools, options.pools_update, options.r_name, options.exec_type
820 noop = options.noop
821 return nova_creds, mysql_conf, ext_nets, int_nets, int_pools, pools_update, r_name, exec_type
822 # End def parse_options
823
824if __name__ == "__main__":
825 main()