Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | import struct |
| 4 | import socket |
| 5 | from collections import defaultdict |
| 6 | from netaddr import IPAddress, IPNetwork |
| 7 | from django.db.models import F, Q |
| 8 | from xos.config import Config |
Scott Baker | 8b75e85 | 2016-08-16 15:04:59 -0700 | [diff] [blame] | 9 | from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 10 | from synchronizers.base.syncstep import * |
| 11 | from core.models.network import * |
| 12 | from core.models.slice import * |
| 13 | from core.models.instance import Instance |
| 14 | from xos.logger import observer_logger as logger |
| 15 | from synchronizers.base.ansible import * |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 16 | from xos.config import Config |
| 17 | import json |
| 18 | |
| 19 | import pdb |
| 20 | |
| 21 | class SyncControllerNetworks(OpenStackSyncStep): |
| 22 | requested_interval = 0 |
| 23 | provides=[Network] |
| 24 | observes=ControllerNetwork |
| 25 | playbook='sync_controller_networks.yaml' |
| 26 | |
| 27 | def alloc_subnet(self, uuid): |
| 28 | # 16 bits only |
| 29 | uuid_masked = uuid & 0xffff |
| 30 | a = 10 |
| 31 | b = uuid_masked >> 8 |
| 32 | c = uuid_masked & 0xff |
| 33 | d = 0 |
| 34 | |
| 35 | cidr = '%d.%d.%d.%d/24'%(a,b,c,d) |
| 36 | return cidr |
| 37 | |
| 38 | def alloc_gateway(self, subnet): |
| 39 | # given a CIDR, allocate a default gateway using the .1 address within |
| 40 | # the subnet. |
| 41 | # 10.123.0.0/24 --> 10.123.0.1 |
| 42 | # 207.141.192.128/28 --> 207.141.192.129 |
| 43 | (network, bits) = subnet.split("/") |
| 44 | network=network.strip() |
| 45 | bits=int(bits.strip()) |
| 46 | netmask = (~(pow(2,32-bits)-1) & 0xFFFFFFFF) |
| 47 | ip = struct.unpack("!L", socket.inet_aton(network))[0] |
| 48 | ip = ip & netmask | 1 |
| 49 | return socket.inet_ntoa(struct.pack("!L", ip)) |
| 50 | |
Scott Baker | 11cb69f | 2016-08-25 16:17:52 -0700 | [diff] [blame] | 51 | def get_segmentation_id(self, controller_network): |
Scott Baker | 7bd1cd4 | 2016-08-29 15:30:45 -0700 | [diff] [blame] | 52 | driver = self.driver.admin_driver(controller = controller_network.controller) |
Scott Baker | 11cb69f | 2016-08-25 16:17:52 -0700 | [diff] [blame] | 53 | neutron_network = driver.shell.neutron.list_networks(controller_network.network_id)["networks"][0] |
| 54 | if "provider:segmentation_id" in neutron_network:
|
| 55 | return neutron_network["provider:segmentation_id"] |
| 56 | else: |
| 57 | return None |
| 58 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 59 | def save_controller_network(self, controller_network): |
| 60 | network_name = controller_network.network.name |
| 61 | subnet_name = '%s-%d'%(network_name,controller_network.pk) |
| 62 | if controller_network.subnet and controller_network.subnet.strip(): |
| 63 | # If a subnet is already specified (pass in by the creator), then |
| 64 | # use that rather than auto-generating one. |
| 65 | cidr = controller_network.subnet.strip() |
| 66 | print "CIDR_MS", cidr |
| 67 | else: |
| 68 | cidr = self.alloc_subnet(controller_network.pk) |
| 69 | print "CIDR_AMS", cidr |
| 70 | |
| 71 | if controller_network.network.start_ip and controller_network.network.start_ip.strip(): |
| 72 | start_ip = controller_network.network.start_ip.strip() |
| 73 | else: |
| 74 | start_ip = None |
| 75 | |
| 76 | if controller_network.network.end_ip and controller_network.network.end_ip.strip(): |
| 77 | end_ip = controller_network.network.end_ip.strip() |
| 78 | else: |
| 79 | end_ip = None |
| 80 | |
| 81 | self.cidr=cidr |
| 82 | slice = controller_network.network.owner |
| 83 | |
Scott Baker | 11cb69f | 2016-08-25 16:17:52 -0700 | [diff] [blame] | 84 | controller_network.gateway = self.alloc_gateway(cidr) |
| 85 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 86 | network_fields = {'endpoint':controller_network.controller.auth_url, |
| 87 | 'endpoint_v3': controller_network.controller.auth_url_v3, |
| 88 | 'admin_user':slice.creator.email, |
| 89 | 'admin_password':slice.creator.remote_password, |
| 90 | 'admin_project':slice.name, |
| 91 | 'domain': controller_network.controller.domain, |
| 92 | 'name':network_name, |
| 93 | 'subnet_name':subnet_name, |
| 94 | 'ansible_tag':'%s-%s@%s'%(network_name,slice.slicename,controller_network.controller.name), |
| 95 | 'cidr':cidr, |
Scott Baker | 11cb69f | 2016-08-25 16:17:52 -0700 | [diff] [blame] | 96 | 'gateway': controller_network.gateway, |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 97 | 'start_ip':start_ip, |
| 98 | 'end_ip':end_ip, |
| 99 | 'use_vtn':getattr(Config(), "networking_use_vtn", False), |
| 100 | 'delete':False |
| 101 | } |
| 102 | return network_fields |
| 103 | |
| 104 | def map_sync_outputs(self, controller_network,res): |
| 105 | network_id = res[0]['network']['id'] |
| 106 | subnet_id = res[1]['subnet']['id'] |
| 107 | controller_network.net_id = network_id |
| 108 | controller_network.subnet = self.cidr |
| 109 | controller_network.subnet_id = subnet_id |
| 110 | controller_network.backend_status = '1 - OK' |
Scott Baker | 11cb69f | 2016-08-25 16:17:52 -0700 | [diff] [blame] | 111 | if not controller_network.segmentation_id: |
| 112 | controller_network.segmentation_id = self.get_segmentation_id(controller_network) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 113 | controller_network.save() |
| 114 | |
| 115 | |
| 116 | def map_sync_inputs(self, controller_network): |
Scott Baker | 3de3d58 | 2016-09-09 15:26:43 -0700 | [diff] [blame] | 117 | # make sure to not sync a shared network |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 118 | if (controller_network.network.template.shared_network_name or controller_network.network.template.shared_network_id): |
| 119 | return SyncStep.SYNC_WITHOUT_RUNNING |
Scott Baker | 3de3d58 | 2016-09-09 15:26:43 -0700 | [diff] [blame] | 120 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 121 | if not controller_network.controller.admin_user: |
| 122 | logger.info("controller %r has no admin_user, skipping" % controller_network.controller) |
| 123 | return |
| 124 | |
| 125 | if controller_network.network.owner and controller_network.network.owner.creator: |
| 126 | return self.save_controller_network(controller_network) |
| 127 | else: |
| 128 | raise Exception('Could not save network controller %s'%controller_network) |
| 129 | |
| 130 | def map_delete_inputs(self, controller_network): |
Scott Baker | 3de3d58 | 2016-09-09 15:26:43 -0700 | [diff] [blame] | 131 | # make sure to not delete a shared network |
| 132 | if (controller_network.network.template.shared_network_name or controller_network.network.template.shared_network_id): |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 133 | return |
Scott Baker | 3de3d58 | 2016-09-09 15:26:43 -0700 | [diff] [blame] | 134 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 135 | try: |
| 136 | slice = controller_network.network.owner # XXX: FIXME!! |
| 137 | except: |
| 138 | raise Exception('Could not get slice for Network %s'%controller_network.network.name) |
| 139 | |
| 140 | network_name = controller_network.network.name |
| 141 | subnet_name = '%s-%d'%(network_name,controller_network.pk) |
| 142 | cidr = controller_network.subnet |
| 143 | network_fields = {'endpoint':controller_network.controller.auth_url, |
| 144 | 'admin_user':slice.creator.email, # XXX: FIXME |
| 145 | 'tenant_name':slice.name, # XXX: FIXME |
| 146 | 'admin_password':slice.creator.remote_password, |
| 147 | 'name':network_name, |
| 148 | 'subnet_name':subnet_name, |
| 149 | 'ansible_tag':'%s-%s@%s'%(network_name,slice.slicename,controller_network.controller.name), |
| 150 | 'cidr':cidr, |
| 151 | 'delete':True |
| 152 | } |
| 153 | |
| 154 | return network_fields |
| 155 | |