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