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 * |
Tony Mack | ea2fb4f | 2014-04-21 17:07:06 -0400 | [diff] [blame] | 10 | from core.models.slice 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): |
Tony Mack | 490f6fa | 2014-04-10 12:20:33 -0400 | [diff] [blame] | 16 | requested_interval = 0 |
Tony Mack | ea2fb4f | 2014-04-21 17:07:06 -0400 | [diff] [blame] | 17 | provides=[Networ, NetworkDeployments, Sliver] |
Tony Mack | 6793de4 | 2014-04-08 16:36:52 -0400 | [diff] [blame] | 18 | |
| 19 | def fetch_pending(self): |
| 20 | # network deployments are not visible to users. We must ensure |
| 21 | # networks are deployed at all deploymets available to their slices. |
| 22 | slice_deployments = SliceDeployments.objects.all() |
| 23 | slice_deploy_lookup = defaultdict(list) |
| 24 | for slice_deployment in slice_deployments: |
| 25 | slice_deploy_lookup[slice_deployment.slice].append(slice_deployment.deployment) |
| 26 | |
| 27 | network_deployments = NetworkDeployments.objects.all() |
| 28 | network_deploy_lookup = defaultdict(list) |
| 29 | for network_deployment in network_deployments: |
| 30 | network_deploy_lookup[network_deployment.network].append(network_deployment.deployment) |
| 31 | |
| 32 | for network in Network.objects.filter(): |
Tony Mack | 3a50a6c | 2014-04-16 07:49:15 -0400 | [diff] [blame] | 33 | # ignore networks that have |
| 34 | # template.visibility = private and template.translation = none |
Tony Mack | 078f668 | 2014-04-17 21:57:16 -0400 | [diff] [blame] | 35 | if network.template.visibility == 'private' and not network.template.translation == 'none': |
| 36 | continue |
Tony Mack | 6793de4 | 2014-04-08 16:36:52 -0400 | [diff] [blame] | 37 | expected_deployments = slice_deploy_lookup[network.owner] |
| 38 | for expected_deployment in expected_deployments: |
| 39 | if network not in network_deploy_lookup or \ |
| 40 | expected_deployment not in network_deploy_lookup[network]: |
| 41 | nd = NetworkDeployments(network=network, deployment=expected_deployment) |
| 42 | nd.save() |
| 43 | return NetworkDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 44 | |
| 45 | def get_next_subnet(self, deployment=None): |
| 46 | # limit ourself to 10.0.x.x for now |
| 47 | valid_subnet = lambda net: net.startswith('10.0') |
| 48 | driver = self.driver.admin_driver(deployment=deployment) |
| 49 | subnets = driver.shell.quantum.list_subnets()['subnets'] |
| 50 | ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \ |
| 51 | if valid_subnet(subnet['cidr'])] |
| 52 | ints.sort() |
| 53 | if ints: |
| 54 | last_ip = IPAddress(ints[-1]) |
| 55 | else: |
Tony Mack | b84fccc | 2014-04-12 10:28:52 -0400 | [diff] [blame] | 56 | last_ip = IPAddress('10.0.0.0') |
Tony Mack | 6793de4 | 2014-04-08 16:36:52 -0400 | [diff] [blame] | 57 | last_network = IPNetwork(str(last_ip) + "/24") |
| 58 | next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24") |
| 59 | return next_network |
| 60 | |
| 61 | def save_network_deployment(self, network_deployment): |
| 62 | if not network_deployment.network_id and network_deployment.network.template.sharedNetworkName: |
| 63 | network_deployment.network_id = network_deployment.network.template.sharedNetworkId |
| 64 | |
| 65 | if not network_deployment.net_id: |
| 66 | network_name = network_deployment.network.name |
| 67 | |
| 68 | # create network |
| 69 | os_network = self.driver.create_network(network_name, shared=True) |
| 70 | network_deployment.net_id = os_network['id'] |
| 71 | |
| 72 | # create router |
Tony Mack | eb786e0 | 2014-04-18 15:32:45 -0400 | [diff] [blame] | 73 | #router = self.driver.create_router(network_name) |
| 74 | #network_deployment.router_id = router['id'] |
Tony Mack | 6793de4 | 2014-04-08 16:36:52 -0400 | [diff] [blame] | 75 | |
| 76 | # create subnet |
| 77 | next_subnet = self.get_next_subnet(deployment=network_deployment.deployment.name) |
| 78 | cidr = str(next_subnet.cidr) |
| 79 | ip_version = next_subnet.version |
| 80 | start = str(next_subnet[2]) |
| 81 | end = str(next_subnet[-2]) |
| 82 | subnet = self.driver.create_subnet(name=network_name, |
| 83 | network_id = network_deployment.net_id, |
| 84 | cidr_ip = cidr, |
| 85 | ip_version = ip_version, |
| 86 | start = start, |
| 87 | end = end) |
| 88 | network_deployment.subnet = cidr |
| 89 | network_deployment.subnet_id = subnet['id'] |
| 90 | # add subnet as interface to slice's router |
Tony Mack | eb786e0 | 2014-04-18 15:32:45 -0400 | [diff] [blame] | 91 | #self.driver.add_router_interface(router['id'], subnet['id']) |
Tony Mack | 6793de4 | 2014-04-08 16:36:52 -0400 | [diff] [blame] | 92 | # add external route |
Tony Mack | eb786e0 | 2014-04-18 15:32:45 -0400 | [diff] [blame] | 93 | #self.driver.add_external_route(subnet) |
Tony Mack | 6793de4 | 2014-04-08 16:36:52 -0400 | [diff] [blame] | 94 | logger.info("created private subnet (%s) for network: %s" % (cidr, network_deployment.network)) |
| 95 | else: |
| 96 | (network_deployment.subnet_id, network_deployment.subnet) = self.driver.get_network_subnet(network_deployment.net_id) |
| 97 | logger.info("sync'ed subnet (%s) for network: %s" % (network_deployment.subnet, network_deployment.network)) |
| 98 | |
| 99 | network_deployment.save() |
| 100 | |
| 101 | def sync_record(self, network_deployment): |
| 102 | if network_deployment.network.owner and network_deployment.network.owner.creator: |
| 103 | try: |
| 104 | # update manager context |
| 105 | real_driver = self.driver |
| 106 | self.driver = self.driver.client_driver(caller=network_deployment.network.owner.creator, |
| 107 | tenant=network_deployment.network.owner.name, |
| 108 | deployment=network_deployment.deployment.name) |
| 109 | self.save_network_deployment(network_deployment) |
| 110 | self.driver = real_driver |
| 111 | logger.info("saved network deployment: %s" % (network_deployment)) |
| 112 | except Exception,e: |
| 113 | logger.log_exc("save network deployment failed: %s" % network_deployment) |
| 114 | raise e |
| 115 | |
| 116 | |
| 117 | |