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