Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Scott Baker | 3ba6b5d | 2016-03-06 12:03:56 -0800 | [diff] [blame] | 3 | import struct |
| 4 | import socket |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 5 | from collections import defaultdict |
| 6 | from netaddr import IPAddress, IPNetwork |
| 7 | from django.db.models import F, Q |
Scott Baker | 76a840e | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 8 | from xos.config import Config |
Sapan Bhatia | 67ea0d7 | 2016-01-14 11:41:38 -0500 | [diff] [blame] | 9 | from synchronizers.base.openstacksyncstep import OpenStackSyncStep |
Sapan Bhatia | 003e84c | 2016-01-15 11:05:52 -0500 | [diff] [blame] | 10 | from synchronizers.base.syncstep import * |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 11 | from core.models.network import * |
| 12 | from core.models.slice import * |
Tony Mack | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 13 | from core.models.instance import Instance |
Scott Baker | 0ae266e | 2016-01-14 15:30:20 -0800 | [diff] [blame] | 14 | from xos.logger import observer_logger as logger |
Sapan Bhatia | 003e84c | 2016-01-15 11:05:52 -0500 | [diff] [blame] | 15 | from synchronizers.base.ansible import * |
S.Çağlar Onur | ad135a9 | 2015-02-18 09:59:55 -0500 | [diff] [blame] | 16 | from openstack.driver import OpenStackDriver |
Scott Baker | 287137c | 2016-01-04 22:50:28 -0800 | [diff] [blame] | 17 | from xos.config import Config |
Sapan Bhatia | 9028c9a | 2015-05-09 18:14:40 +0200 | [diff] [blame] | 18 | import json |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 19 | |
Sapan Bhatia | 99f4968 | 2015-01-29 20:58:25 +0000 | [diff] [blame] | 20 | import pdb |
| 21 | |
Tony Mack | dd8746b | 2015-01-07 12:48:37 -0500 | [diff] [blame] | 22 | class SyncControllerNetworks(OpenStackSyncStep): |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 23 | requested_interval = 0 |
Sapan Bhatia | b3048aa | 2015-01-27 03:52:19 +0000 | [diff] [blame] | 24 | provides=[Network] |
Sapan Bhatia | 99f4968 | 2015-01-29 20:58:25 +0000 | [diff] [blame] | 25 | observes=ControllerNetwork |
Sapan Bhatia | 321b70e | 2015-08-19 12:20:47 -0400 | [diff] [blame] | 26 | playbook='sync_controller_networks.yaml' |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 27 | |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 28 | def alloc_subnet(self, uuid): |
Sapan Bhatia | d99b112 | 2015-01-23 16:22:12 +0000 | [diff] [blame] | 29 | # 16 bits only |
| 30 | uuid_masked = uuid & 0xffff |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 31 | a = 10 |
Sapan Bhatia | d99b112 | 2015-01-23 16:22:12 +0000 | [diff] [blame] | 32 | b = uuid_masked >> 8 |
| 33 | c = uuid_masked & 0xff |
| 34 | d = 0 |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 35 | |
Sapan Bhatia | d99b112 | 2015-01-23 16:22:12 +0000 | [diff] [blame] | 36 | cidr = '%d.%d.%d.%d/24'%(a,b,c,d) |
| 37 | return cidr |
| 38 | |
Scott Baker | 49a1b4d | 2016-02-08 16:02:52 -0800 | [diff] [blame] | 39 | def alloc_gateway(self, subnet): |
Scott Baker | 21dbaf6 | 2016-03-06 11:17:16 -0800 | [diff] [blame] | 40 | # given a CIDR, allocate a default gateway using the .1 address within |
| 41 | # the subnet. |
| 42 | # 10.123.0.0/24 --> 10.123.0.1 |
| 43 | # 207.141.192.128/28 --> 207.141.192.129 |
| 44 | (network, bits) = subnet.split("/") |
| 45 | network=network.strip() |
| 46 | bits=int(bits.strip()) |
| 47 | netmask = (~(pow(2,32-bits)-1) & 0xFFFFFFFF) |
| 48 | ip = struct.unpack("!L", socket.inet_aton(network))[0] |
| 49 | ip = ip & netmask | 1 |
| 50 | return socket.inet_ntoa(struct.pack("!L", ip)) |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 51 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 52 | def save_controller_network(self, controller_network): |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 53 | network_name = controller_network.network.name |
| 54 | subnet_name = '%s-%d'%(network_name,controller_network.pk) |
Scott Baker | 49a1b4d | 2016-02-08 16:02:52 -0800 | [diff] [blame] | 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 | else: |
| 60 | cidr = self.alloc_subnet(controller_network.pk) |
Sapan Bhatia | 70a3e14 | 2015-09-16 19:14:41 +0200 | [diff] [blame] | 61 | self.cidr=cidr |
Scott Baker | 1ebe12d | 2015-08-21 16:12:33 -0700 | [diff] [blame] | 62 | slice = controller_network.network.owner |
Sapan Bhatia | 6c1cb84 | 2014-12-22 01:42:18 -0500 | [diff] [blame] | 63 | |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 64 | network_fields = {'endpoint':controller_network.controller.auth_url, |
Tony Mack | 09a2f07 | 2015-09-14 00:53:39 +0000 | [diff] [blame] | 65 | 'endpoint_v3': controller_network.controller.auth_url_v3, |
Scott Baker | 1ebe12d | 2015-08-21 16:12:33 -0700 | [diff] [blame] | 66 | 'admin_user':slice.creator.email, |
| 67 | 'tenant_name':slice.name, |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 68 | 'admin_password':slice.creator.remote_password, |
Scott Baker | 4a20ce6 | 2015-09-21 20:24:45 -0700 | [diff] [blame] | 69 | 'domain': controller_network.controller.domain, |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 70 | 'name':network_name, |
| 71 | 'subnet_name':subnet_name, |
| 72 | 'ansible_tag':'%s-%s@%s'%(network_name,slice.slicename,controller_network.controller.name), |
Sapan Bhatia | 1b4394a | 2015-05-09 18:18:56 +0200 | [diff] [blame] | 73 | 'cidr':cidr, |
Scott Baker | 49a1b4d | 2016-02-08 16:02:52 -0800 | [diff] [blame] | 74 | 'gateway':self.alloc_gateway(cidr), |
Scott Baker | 287137c | 2016-01-04 22:50:28 -0800 | [diff] [blame] | 75 | 'use_vtn':getattr(Config(), "networking_use_vtn", False), |
Scott Baker | 49a1b4d | 2016-02-08 16:02:52 -0800 | [diff] [blame] | 76 | 'delete':False |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 77 | } |
Sapan Bhatia | 321b70e | 2015-08-19 12:20:47 -0400 | [diff] [blame] | 78 | return network_fields |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 79 | |
Sapan Bhatia | 321b70e | 2015-08-19 12:20:47 -0400 | [diff] [blame] | 80 | def map_sync_outputs(self, controller_network,res): |
Tony Mack | 3944db2 | 2015-09-30 18:40:16 +0000 | [diff] [blame] | 81 | network_id = res[0]['id'] |
| 82 | subnet_id = res[1]['id'] |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 83 | controller_network.net_id = network_id |
Sapan Bhatia | 70a3e14 | 2015-09-16 19:14:41 +0200 | [diff] [blame] | 84 | controller_network.subnet = self.cidr |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 85 | controller_network.subnet_id = subnet_id |
Sapan Bhatia | 5851db4 | 2015-01-27 03:52:43 +0000 | [diff] [blame] | 86 | controller_network.backend_status = '1 - OK' |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 87 | controller_network.save() |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 88 | |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 89 | |
Sapan Bhatia | 321b70e | 2015-08-19 12:20:47 -0400 | [diff] [blame] | 90 | def map_sync_inputs(self, controller_network): |
Scott Baker | 3aaa76b | 2015-12-09 22:55:19 -0800 | [diff] [blame] | 91 | # XXX This check should really be made from booleans, rather than using hardcoded network names |
Scott Baker | bb650ed | 2016-02-03 18:30:08 -0800 | [diff] [blame] | 92 | #if (controller_network.network.template.name not in ['Private', 'Private-Indirect', 'Private-Direct', 'management_template'): |
| 93 | # logger.info("skipping network controller %s because it is not private" % controller_network) |
| 94 | # # We only sync private networks |
| 95 | # return SyncStep.SYNC_WITHOUT_RUNNING |
| 96 | |
| 97 | # hopefully a better approach than above |
| 98 | if (controller_network.network.template.shared_network_name or controller_network.network.template.shared_network_id): |
Scott Baker | 1a36d66 | 2015-10-12 18:28:00 -0700 | [diff] [blame] | 99 | return SyncStep.SYNC_WITHOUT_RUNNING |
Sapan Bhatia | db631b2 | 2015-05-27 17:23:58 +0200 | [diff] [blame] | 100 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 101 | if not controller_network.controller.admin_user: |
| 102 | logger.info("controller %r has no admin_user, skipping" % controller_network.controller) |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 103 | return |
| 104 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 105 | if controller_network.network.owner and controller_network.network.owner.creator: |
Sapan Bhatia | 321b70e | 2015-08-19 12:20:47 -0400 | [diff] [blame] | 106 | return self.save_controller_network(controller_network) |
| 107 | else: |
| 108 | raise Exception('Could not save network controller %s'%controller_network) |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 109 | |
Sapan Bhatia | 321b70e | 2015-08-19 12:20:47 -0400 | [diff] [blame] | 110 | def map_delete_inputs(self, controller_network): |
Scott Baker | 3aaa76b | 2015-12-09 22:55:19 -0800 | [diff] [blame] | 111 | # XXX This check should really be made from booleans, rather than using hardcoded network names |
| 112 | if (controller_network.network.template.name not in ['Private', 'Private-Indirect', 'Private-Direct']): |
Sapan Bhatia | db631b2 | 2015-05-27 17:23:58 +0200 | [diff] [blame] | 113 | # We only sync private networks |
| 114 | return |
Sapan Bhatia | 53a0e9b | 2015-05-09 18:16:24 +0200 | [diff] [blame] | 115 | try: |
| 116 | slice = controller_network.network.owner # XXX: FIXME!! |
| 117 | except: |
| 118 | raise Exception('Could not get slice for Network %s'%controller_network.network.name) |
| 119 | |
| 120 | network_name = controller_network.network.name |
| 121 | subnet_name = '%s-%d'%(network_name,controller_network.pk) |
| 122 | cidr = controller_network.subnet |
| 123 | network_fields = {'endpoint':controller_network.controller.auth_url, |
| 124 | 'admin_user':slice.creator.email, # XXX: FIXME |
| 125 | 'tenant_name':slice.name, # XXX: FIXME |
| 126 | 'admin_password':slice.creator.remote_password, |
| 127 | 'name':network_name, |
| 128 | 'subnet_name':subnet_name, |
| 129 | 'ansible_tag':'%s-%s@%s'%(network_name,slice.slicename,controller_network.controller.name), |
| 130 | 'cidr':cidr, |
| 131 | 'delete':True |
| 132 | } |
| 133 | |
Sapan Bhatia | 321b70e | 2015-08-19 12:20:47 -0400 | [diff] [blame] | 134 | return network_fields |
Sapan Bhatia | 53a0e9b | 2015-05-09 18:16:24 +0200 | [diff] [blame] | 135 | |
| 136 | """ |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 137 | driver = OpenStackDriver().client_driver(caller=controller_network.network.owner.creator, |
| 138 | tenant=controller_network.network.owner.name, |
| 139 | controller=controller_network.controller.name) |
| 140 | if (controller_network.router_id) and (controller_network.subnet_id): |
| 141 | driver.delete_router_interface(controller_network.router_id, controller_network.subnet_id) |
| 142 | if controller_network.subnet_id: |
| 143 | driver.delete_subnet(controller_network.subnet_id) |
| 144 | if controller_network.router_id: |
| 145 | driver.delete_router(controller_network.router_id) |
| 146 | if controller_network.net_id: |
| 147 | driver.delete_network(controller_network.net_id) |
Sapan Bhatia | 53a0e9b | 2015-05-09 18:16:24 +0200 | [diff] [blame] | 148 | """ |