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