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: |
| 23 | # network deployments are not visible to users. We must ensure |
Scott Baker | f3e7151 | 2014-08-12 18:05:04 -0700 | [diff] [blame^] | 24 | # networks are deployed at all deploymets available to their slices. |
Sapan Bhatia | 881f62e | 2014-07-23 09:45:23 -0400 | [diff] [blame] | 25 | slice_deployments = SliceDeployments.objects.all() |
| 26 | slice_deploy_lookup = defaultdict(list) |
| 27 | for slice_deployment in slice_deployments: |
| 28 | slice_deploy_lookup[slice_deployment.slice].append(slice_deployment.deployment) |
Scott Baker | f3e7151 | 2014-08-12 18:05:04 -0700 | [diff] [blame^] | 29 | |
Sapan Bhatia | 881f62e | 2014-07-23 09:45:23 -0400 | [diff] [blame] | 30 | network_deployments = NetworkDeployments.objects.all() |
| 31 | network_deploy_lookup = defaultdict(list) |
| 32 | for network_deployment in network_deployments: |
| 33 | network_deploy_lookup[network_deployment.network].append(network_deployment.deployment) |
Tony Mack | 6793de4 | 2014-04-08 16:36:52 -0400 | [diff] [blame] | 34 | |
Sapan Bhatia | 881f62e | 2014-07-23 09:45:23 -0400 | [diff] [blame] | 35 | for network in Network.objects.filter(): |
| 36 | # ignore networks that have |
| 37 | # template.visibility = private and translation = none |
| 38 | if network.template.visibility == 'private' and not network.template.translation == 'none': |
Scott Baker | f3e7151 | 2014-08-12 18:05:04 -0700 | [diff] [blame^] | 39 | continue |
Sapan Bhatia | 881f62e | 2014-07-23 09:45:23 -0400 | [diff] [blame] | 40 | expected_deployments = slice_deploy_lookup[network.owner] |
| 41 | for expected_deployment in expected_deployments: |
| 42 | if network not in network_deploy_lookup or \ |
| 43 | expected_deployment not in network_deploy_lookup[network]: |
| 44 | nd = NetworkDeployments(network=network, deployment=expected_deployment) |
| 45 | nd.save() |
| 46 | return NetworkDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | 6793de4 | 2014-04-08 16:36:52 -0400 | [diff] [blame] | 47 | |
| 48 | def get_next_subnet(self, deployment=None): |
| 49 | # limit ourself to 10.0.x.x for now |
| 50 | valid_subnet = lambda net: net.startswith('10.0') |
| 51 | driver = self.driver.admin_driver(deployment=deployment) |
| 52 | subnets = driver.shell.quantum.list_subnets()['subnets'] |
| 53 | ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \ |
| 54 | if valid_subnet(subnet['cidr'])] |
| 55 | ints.sort() |
| 56 | if ints: |
| 57 | last_ip = IPAddress(ints[-1]) |
| 58 | else: |
Tony Mack | b84fccc | 2014-04-12 10:28:52 -0400 | [diff] [blame] | 59 | last_ip = IPAddress('10.0.0.0') |
Tony Mack | 6793de4 | 2014-04-08 16:36:52 -0400 | [diff] [blame] | 60 | last_network = IPNetwork(str(last_ip) + "/24") |
| 61 | next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24") |
| 62 | return next_network |
| 63 | |
| 64 | def save_network_deployment(self, network_deployment): |
Scott Baker | f3e7151 | 2014-08-12 18:05:04 -0700 | [diff] [blame^] | 65 | if not network_deployment.net_id and network_deployment.network.template.sharedNetworkName: |
| 66 | # It's a shared network, try to find the shared network id |
| 67 | |
| 68 | quantum_networks = self.driver.shell.quantum.list_networks(name=network_deployment.network.template.sharedNetworkName)["networks"] |
| 69 | if quantum_networks: |
| 70 | logger.info("set shared network id %s" % quantum_networks[0]["id"]) |
| 71 | network_deployment.net_id = quantum_networks[0]["id"] |
| 72 | else: |
| 73 | logger.info("failed to find shared network id for deployment") |
| 74 | return |
| 75 | |
| 76 | # At this point, it must be a private network, so create it if it does |
| 77 | # not exist. |
Tony Mack | 6793de4 | 2014-04-08 16:36:52 -0400 | [diff] [blame] | 78 | |
| 79 | if not network_deployment.net_id: |
| 80 | network_name = network_deployment.network.name |
| 81 | |
| 82 | # create network |
| 83 | os_network = self.driver.create_network(network_name, shared=True) |
| 84 | network_deployment.net_id = os_network['id'] |
| 85 | |
| 86 | # create router |
Tony Mack | eb786e0 | 2014-04-18 15:32:45 -0400 | [diff] [blame] | 87 | #router = self.driver.create_router(network_name) |
| 88 | #network_deployment.router_id = router['id'] |
Tony Mack | 6793de4 | 2014-04-08 16:36:52 -0400 | [diff] [blame] | 89 | |
| 90 | # create subnet |
| 91 | next_subnet = self.get_next_subnet(deployment=network_deployment.deployment.name) |
| 92 | cidr = str(next_subnet.cidr) |
| 93 | ip_version = next_subnet.version |
| 94 | start = str(next_subnet[2]) |
| 95 | end = str(next_subnet[-2]) |
| 96 | subnet = self.driver.create_subnet(name=network_name, |
| 97 | network_id = network_deployment.net_id, |
| 98 | cidr_ip = cidr, |
| 99 | ip_version = ip_version, |
| 100 | start = start, |
| 101 | end = end) |
| 102 | network_deployment.subnet = cidr |
| 103 | network_deployment.subnet_id = subnet['id'] |
| 104 | # add subnet as interface to slice's router |
Tony Mack | eb786e0 | 2014-04-18 15:32:45 -0400 | [diff] [blame] | 105 | #self.driver.add_router_interface(router['id'], subnet['id']) |
Tony Mack | 6793de4 | 2014-04-08 16:36:52 -0400 | [diff] [blame] | 106 | # add external route |
Tony Mack | eb786e0 | 2014-04-18 15:32:45 -0400 | [diff] [blame] | 107 | #self.driver.add_external_route(subnet) |
Tony Mack | 6793de4 | 2014-04-08 16:36:52 -0400 | [diff] [blame] | 108 | 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^] | 109 | |
| 110 | # Now, figure out the subnet and subnet_id for the network. This works |
| 111 | # for both private and shared networks. |
| 112 | |
| 113 | if (not network_deployment.subnet_id) or (not network_deployment.subnet): |
Tony Mack | 6793de4 | 2014-04-08 16:36:52 -0400 | [diff] [blame] | 114 | (network_deployment.subnet_id, network_deployment.subnet) = self.driver.get_network_subnet(network_deployment.net_id) |
| 115 | logger.info("sync'ed subnet (%s) for network: %s" % (network_deployment.subnet, network_deployment.network)) |
| 116 | |
Scott Baker | f3e7151 | 2014-08-12 18:05:04 -0700 | [diff] [blame^] | 117 | if (not network_deployment.subnet): |
| 118 | # this will generate a non-null database constraint error |
| 119 | # ... which in turn leads to transaction errors |
| 120 | # it's probably caused by networks that no longer exist at the |
| 121 | # quantum level. |
| 122 | |
| 123 | logger.info("null subnet for network %s, skipping save" % network_deployment.network) |
| 124 | return |
| 125 | |
Tony Mack | 6793de4 | 2014-04-08 16:36:52 -0400 | [diff] [blame] | 126 | network_deployment.save() |
| 127 | |
| 128 | def sync_record(self, network_deployment): |
| 129 | if network_deployment.network.owner and network_deployment.network.owner.creator: |
| 130 | try: |
| 131 | # update manager context |
| 132 | real_driver = self.driver |
Scott Baker | f3e7151 | 2014-08-12 18:05:04 -0700 | [diff] [blame^] | 133 | self.driver = self.driver.client_driver(caller=network_deployment.network.owner.creator, |
Tony Mack | 6793de4 | 2014-04-08 16:36:52 -0400 | [diff] [blame] | 134 | tenant=network_deployment.network.owner.name, |
| 135 | deployment=network_deployment.deployment.name) |
| 136 | self.save_network_deployment(network_deployment) |
| 137 | self.driver = real_driver |
| 138 | logger.info("saved network deployment: %s" % (network_deployment)) |
| 139 | except Exception,e: |
| 140 | logger.log_exc("save network deployment failed: %s" % network_deployment) |
Scott Baker | f3e7151 | 2014-08-12 18:05:04 -0700 | [diff] [blame^] | 141 | raise e |
| 142 | |
| 143 | |
| 144 | def delete_record(self, network_deployment): |
Sapan Bhatia | 881f62e | 2014-07-23 09:45:23 -0400 | [diff] [blame] | 145 | driver = OpenStackDriver().client_driver(caller=network_deployment.network.owner.creator, |
| 146 | tenant=network_deployment.network.owner.name, |
| 147 | deployment=network_deployment.deployment.name) |
| 148 | if (network_deployment.router_id) and (network_deployment.subnet_id): |
| 149 | driver.delete_router_interface(network_deployment.router_id, network_deployment.subnet_id) |
| 150 | if network_deployment.subnet_id: |
| 151 | driver.delete_subnet(network_deployment.subnet_id) |
| 152 | if network_deployment.router_id: |
| 153 | driver.delete_router(network_deployment.router_id) |
| 154 | if network_deployment.net_id: |
| 155 | driver.delete_network(network_deployment.net_id) |