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