Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | from collections import defaultdict |
| 4 | from netaddr import IPAddress, IPNetwork |
| 5 | from django.db.models import F, Q |
| 6 | from planetstack.config import Config |
| 7 | from observer.openstacksyncstep import OpenStackSyncStep |
| 8 | from core.models.network import * |
| 9 | from core.models.slice import * |
| 10 | from core.models.sliver import Sliver |
| 11 | from util.logger import Logger, logging |
Sapan Bhatia | 6c1cb84 | 2014-12-22 01:42:18 -0500 | [diff] [blame] | 12 | from observer.ansible import * |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 13 | |
| 14 | logger = Logger(level=logging.INFO) |
| 15 | |
Tony Mack | dd8746b | 2015-01-07 12:48:37 -0500 | [diff] [blame] | 16 | class SyncControllerNetworks(OpenStackSyncStep): |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 17 | requested_interval = 0 |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 18 | provides=[Network, ControllerNetwork, Sliver] |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 19 | |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 20 | def alloc_subnet(self, uuid): |
| 21 | a = 10 |
| 22 | b = uuid >> 32 |
| 23 | c = uuid & 0xffffffff |
| 24 | d = 0 |
| 25 | |
| 26 | cidr = '%d.%d.%d.%d/24'%(a,b,c,d) |
| 27 | return cidr |
| 28 | |
| 29 | |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 30 | def fetch_pending(self, deleted): |
| 31 | if (deleted): |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 32 | return ControllerNetwork.deleted_objects.all() |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 33 | else: |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 34 | return ControllerNetwork.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 35 | |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 36 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 37 | def save_controller_network(self, controller_network): |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 38 | network_name = controller_network.network.name |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 39 | subnet_name = '%s-%d'%(network_name,controller_network.pk) |
| 40 | cidr = self.alloc_subnet(controller_network.pk) |
Sapan Bhatia | 6c1cb84 | 2014-12-22 01:42:18 -0500 | [diff] [blame] | 41 | slice = controller_network.network.slices.all()[0] # XXX: FIXME!! |
| 42 | |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 43 | network_fields = {'endpoint':controller_network.controller.auth_url, |
Sapan Bhatia | 6c1cb84 | 2014-12-22 01:42:18 -0500 | [diff] [blame] | 44 | 'admin_user':slice.creator.email, # XXX: FIXME |
Tony Mack | dd8746b | 2015-01-07 12:48:37 -0500 | [diff] [blame] | 45 | 'tenant_name':slice.name, # XXX: FIXME |
| 46 | 'admin_password':slice.creator.remote_password, |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 47 | 'name':network_name, |
| 48 | 'subnet_name':subnet_name, |
Sapan Bhatia | 6c1cb84 | 2014-12-22 01:42:18 -0500 | [diff] [blame] | 49 | 'ansible_tag':'%s-%s@%s'%(network_name,slice.slicename,controller_network.controller.name), |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 50 | 'cidr':cidr |
| 51 | } |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 52 | |
Sapan Bhatia | 6c1cb84 | 2014-12-22 01:42:18 -0500 | [diff] [blame] | 53 | res = run_template('sync_controller_networks.yaml', network_fields, path = 'controller_networks') |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 54 | |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 55 | if (len(res)!=2): |
| 56 | raise Exception('Could not sync network %s'%controller_network.network.name) |
| 57 | else: |
| 58 | network_id = res[0]['id'] |
| 59 | subnet_id = res[1]['id'] |
| 60 | controller_network.net_id = network_id |
| 61 | controller_network.subnet = cidr |
| 62 | controller_network.subnet_id = subnet_id |
| 63 | controller_network.save() |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 64 | |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 65 | logger.info("sync'ed subnet (%s) for network: %s" % (controller_network.subnet, controller_network.network)) |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 66 | |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 67 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 68 | def sync_record(self, controller_network): |
| 69 | logger.info("sync'ing network controller %s for network %s slice %s controller %s" % (controller_network, controller_network.network, str(controller_network.network.owner), controller_network.controller)) |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 70 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 71 | if not controller_network.controller.admin_user: |
| 72 | logger.info("controller %r has no admin_user, skipping" % controller_network.controller) |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 73 | return |
| 74 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 75 | if controller_network.network.owner and controller_network.network.owner.creator: |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 76 | try: |
| 77 | # update manager context |
| 78 | # Bring back |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 79 | self.save_controller_network(controller_network) |
| 80 | logger.info("saved network controller: %s" % (controller_network)) |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 81 | except Exception,e: |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 82 | logger.log_exc("save network controller failed: %s" % controller_network) |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 83 | raise e |
| 84 | |
| 85 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 86 | def delete_record(self, controller_network): |
| 87 | driver = OpenStackDriver().client_driver(caller=controller_network.network.owner.creator, |
| 88 | tenant=controller_network.network.owner.name, |
| 89 | controller=controller_network.controller.name) |
| 90 | if (controller_network.router_id) and (controller_network.subnet_id): |
| 91 | driver.delete_router_interface(controller_network.router_id, controller_network.subnet_id) |
| 92 | if controller_network.subnet_id: |
| 93 | driver.delete_subnet(controller_network.subnet_id) |
| 94 | if controller_network.router_id: |
| 95 | driver.delete_router(controller_network.router_id) |
| 96 | if controller_network.net_id: |
| 97 | driver.delete_network(controller_network.net_id) |