Tony Mack | 51c4a7d | 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 |
| 12 | |
| 13 | logger = Logger(level=logging.INFO) |
| 14 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 15 | class SyncControllerNetworks(OpenStackSyncStep): |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 16 | requested_interval = 0 |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 17 | provides=[Network, ControllerNetworks, Sliver] |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 18 | |
| 19 | def fetch_pending(self, deleted): |
| 20 | if (deleted): |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 21 | return ControllerNetworks.deleted_objects.all() |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 22 | else: |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 23 | return ControllerNetworks.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 24 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 25 | def get_next_subnet(self, controller=None): |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 26 | # limit ourself to 10.0.x.x for now |
| 27 | valid_subnet = lambda net: net.startswith('10.0') |
| 28 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 29 | driver = self.driver.admin_driver(controller=controller,tenant='admin') |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 30 | subnets = driver.shell.quantum.list_subnets()['subnets'] |
| 31 | ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \ |
| 32 | if valid_subnet(subnet['cidr'])] |
| 33 | ints.sort() |
| 34 | if ints: |
| 35 | last_ip = IPAddress(ints[-1]) |
| 36 | else: |
| 37 | last_ip = IPAddress('10.0.0.0') |
| 38 | last_network = IPNetwork(str(last_ip) + "/24") |
| 39 | next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24") |
| 40 | return next_network |
| 41 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 42 | def save_controller_network(self, controller_network): |
| 43 | if (not controller_network.net_id) and controller_network.network.template.sharedNetworkName: |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 44 | # It's a shared network, try to find the shared network id |
| 45 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 46 | quantum_networks = self.driver.shell.quantum.list_networks(name=controller_network.network.template.sharedNetworkName)["networks"] |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 47 | if quantum_networks: |
| 48 | logger.info("set shared network id %s" % quantum_networks[0]["id"]) |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 49 | controller_network.net_id = quantum_networks[0]["id"] |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 50 | else: |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 51 | logger.info("failed to find shared network id for controller") |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 52 | return |
| 53 | |
| 54 | # At this point, it must be a private network, so create it if it does |
| 55 | # not exist. |
| 56 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 57 | if not controller_network.net_id: |
| 58 | network_name = controller_network.network.name |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 59 | |
| 60 | # create network |
| 61 | os_network = self.driver.create_network(network_name, shared=True) |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 62 | controller_network.net_id = os_network['id'] |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 63 | |
| 64 | # create router |
| 65 | #router = self.driver.create_router(network_name) |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 66 | #controller_network.router_id = router['id'] |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 67 | |
| 68 | # create subnet |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 69 | next_subnet = self.get_next_subnet(controller=controller_network.controller.name) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 70 | cidr = str(next_subnet.cidr) |
| 71 | ip_version = next_subnet.version |
| 72 | start = str(next_subnet[2]) |
| 73 | end = str(next_subnet[-2]) |
| 74 | subnet = self.driver.create_subnet(name=network_name, |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 75 | network_id = controller_network.net_id, |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 76 | cidr_ip = cidr, |
| 77 | ip_version = ip_version, |
| 78 | start = start, |
| 79 | end = end) |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 80 | controller_network.subnet = cidr |
| 81 | controller_network.subnet_id = subnet['id'] |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 82 | # add subnet as interface to slice's router |
| 83 | #self.driver.add_router_interface(router['id'], subnet['id']) |
| 84 | # add external route |
| 85 | #self.driver.add_external_route(subnet) |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 86 | logger.info("created private subnet (%s) for network: %s" % (cidr, controller_network.network)) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 87 | |
| 88 | # Now, figure out the subnet and subnet_id for the network. This works |
| 89 | # for both private and shared networks. |
| 90 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 91 | if (not controller_network.subnet_id) or (not controller_network.subnet): |
| 92 | (controller_network.subnet_id, controller_network.subnet) = self.driver.get_network_subnet(controller_network.net_id) |
| 93 | logger.info("sync'ed subnet (%s) for network: %s" % (controller_network.subnet, controller_network.network)) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 94 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 95 | if (not controller_network.subnet): |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 96 | # this will generate a non-null database constraint error |
| 97 | # ... which in turn leads to transaction errors |
| 98 | # it's probably caused by networks that no longer exist at the |
| 99 | # quantum level. |
| 100 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 101 | logger.info("null subnet for network %s, skipping save" % controller_network.network) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 102 | return |
| 103 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 104 | controller_network.save() |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 105 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 106 | def sync_record(self, controller_network): |
| 107 | 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 | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 108 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 109 | if not controller_network.controller.admin_user: |
| 110 | logger.info("controller %r has no admin_user, skipping" % controller_network.controller) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 111 | return |
| 112 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 113 | self.driver = self.driver.admin_driver(controller=controller_network.controller,tenant='admin') |
| 114 | if controller_network.network.owner and controller_network.network.owner.creator: |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 115 | try: |
| 116 | # update manager context |
| 117 | # Bring back |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 118 | self.save_controller_network(controller_network) |
| 119 | logger.info("saved network controller: %s" % (controller_network)) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 120 | except Exception,e: |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 121 | logger.log_exc("save network controller failed: %s" % controller_network) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 122 | raise e |
| 123 | |
| 124 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 125 | def delete_record(self, controller_network): |
| 126 | driver = OpenStackDriver().client_driver(caller=controller_network.network.owner.creator, |
| 127 | tenant=controller_network.network.owner.name, |
| 128 | controller=controller_network.controller.name) |
| 129 | if (controller_network.router_id) and (controller_network.subnet_id): |
| 130 | driver.delete_router_interface(controller_network.router_id, controller_network.subnet_id) |
| 131 | if controller_network.subnet_id: |
| 132 | driver.delete_subnet(controller_network.subnet_id) |
| 133 | if controller_network.router_id: |
| 134 | driver.delete_router(controller_network.router_id) |
| 135 | if controller_network.net_id: |
| 136 | driver.delete_network(controller_network.net_id) |