Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [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 |
Scott Baker | 76a840e | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 6 | from xos.config import Config |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 7 | from observer.openstacksyncstep import OpenStackSyncStep |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 8 | from observer.syncstep import * |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 9 | from core.models.network import * |
| 10 | from core.models.slice import * |
Tony Mack | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 11 | from core.models.instance import Instance |
Tony Mack | 08f8288 | 2015-03-29 08:32:21 -0400 | [diff] [blame] | 12 | from util.logger import observer_logger as logger |
Sapan Bhatia | 6c1cb84 | 2014-12-22 01:42:18 -0500 | [diff] [blame] | 13 | from observer.ansible import * |
S.Çağlar Onur | ad135a9 | 2015-02-18 09:59:55 -0500 | [diff] [blame] | 14 | from openstack.driver import OpenStackDriver |
Sapan Bhatia | 9028c9a | 2015-05-09 18:14:40 +0200 | [diff] [blame] | 15 | import json |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 16 | |
Sapan Bhatia | 99f4968 | 2015-01-29 20:58:25 +0000 | [diff] [blame] | 17 | import pdb |
| 18 | |
Tony Mack | dd8746b | 2015-01-07 12:48:37 -0500 | [diff] [blame] | 19 | class SyncControllerNetworks(OpenStackSyncStep): |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 20 | requested_interval = 0 |
Sapan Bhatia | b3048aa | 2015-01-27 03:52:19 +0000 | [diff] [blame] | 21 | provides=[Network] |
Sapan Bhatia | 99f4968 | 2015-01-29 20:58:25 +0000 | [diff] [blame] | 22 | observes=ControllerNetwork |
Sapan Bhatia | 321b70e | 2015-08-19 12:20:47 -0400 | [diff] [blame] | 23 | playbook='sync_controller_networks.yaml' |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 24 | |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 25 | def alloc_subnet(self, uuid): |
Sapan Bhatia | d99b112 | 2015-01-23 16:22:12 +0000 | [diff] [blame] | 26 | # 16 bits only |
| 27 | uuid_masked = uuid & 0xffff |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 28 | a = 10 |
Sapan Bhatia | d99b112 | 2015-01-23 16:22:12 +0000 | [diff] [blame] | 29 | b = uuid_masked >> 8 |
| 30 | c = uuid_masked & 0xff |
| 31 | d = 0 |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 32 | |
Sapan Bhatia | d99b112 | 2015-01-23 16:22:12 +0000 | [diff] [blame] | 33 | cidr = '%d.%d.%d.%d/24'%(a,b,c,d) |
| 34 | return cidr |
| 35 | |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 36 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 37 | def save_controller_network(self, controller_network): |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 38 | network_name = controller_network.network.name |
| 39 | subnet_name = '%s-%d'%(network_name,controller_network.pk) |
| 40 | cidr = self.alloc_subnet(controller_network.pk) |
Sapan Bhatia | 70a3e14 | 2015-09-16 19:14:41 +0200 | [diff] [blame] | 41 | self.cidr=cidr |
Scott Baker | 1ebe12d | 2015-08-21 16:12:33 -0700 | [diff] [blame] | 42 | slice = controller_network.network.owner |
Sapan Bhatia | 6c1cb84 | 2014-12-22 01:42:18 -0500 | [diff] [blame] | 43 | |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 44 | network_fields = {'endpoint':controller_network.controller.auth_url, |
Tony Mack | 09a2f07 | 2015-09-14 00:53:39 +0000 | [diff] [blame] | 45 | 'endpoint_v3': controller_network.controller.auth_url_v3, |
Scott Baker | 1ebe12d | 2015-08-21 16:12:33 -0700 | [diff] [blame] | 46 | 'admin_user':slice.creator.email, |
| 47 | 'tenant_name':slice.name, |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 48 | 'admin_password':slice.creator.remote_password, |
Scott Baker | 4a20ce6 | 2015-09-21 20:24:45 -0700 | [diff] [blame] | 49 | 'domain': controller_network.controller.domain, |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 50 | 'name':network_name, |
| 51 | 'subnet_name':subnet_name, |
| 52 | '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] | 53 | 'cidr':cidr, |
| 54 | 'delete':False |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 55 | } |
Sapan Bhatia | 321b70e | 2015-08-19 12:20:47 -0400 | [diff] [blame] | 56 | return network_fields |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 57 | |
Sapan Bhatia | 321b70e | 2015-08-19 12:20:47 -0400 | [diff] [blame] | 58 | def map_sync_outputs(self, controller_network,res): |
Tony Mack | 3944db2 | 2015-09-30 18:40:16 +0000 | [diff] [blame] | 59 | network_id = res[0]['id'] |
| 60 | subnet_id = res[1]['id'] |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 61 | controller_network.net_id = network_id |
Sapan Bhatia | 70a3e14 | 2015-09-16 19:14:41 +0200 | [diff] [blame] | 62 | controller_network.subnet = self.cidr |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 63 | controller_network.subnet_id = subnet_id |
Sapan Bhatia | 5851db4 | 2015-01-27 03:52:43 +0000 | [diff] [blame] | 64 | controller_network.backend_status = '1 - OK' |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 65 | controller_network.save() |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 66 | |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 67 | |
Sapan Bhatia | 321b70e | 2015-08-19 12:20:47 -0400 | [diff] [blame] | 68 | def map_sync_inputs(self, controller_network): |
Scott Baker | 3aaa76b | 2015-12-09 22:55:19 -0800 | [diff] [blame] | 69 | # XXX This check should really be made from booleans, rather than using hardcoded network names |
| 70 | if (controller_network.network.template.name not in ['Private', 'Private-Indirect', 'Private-Direct']): |
Scott Baker | 1ebe12d | 2015-08-21 16:12:33 -0700 | [diff] [blame] | 71 | logger.info("skipping network controller %s because it is not private" % controller_network) |
Sapan Bhatia | db631b2 | 2015-05-27 17:23:58 +0200 | [diff] [blame] | 72 | # We only sync private networks |
Scott Baker | 1a36d66 | 2015-10-12 18:28:00 -0700 | [diff] [blame] | 73 | return SyncStep.SYNC_WITHOUT_RUNNING |
Sapan Bhatia | db631b2 | 2015-05-27 17:23:58 +0200 | [diff] [blame] | 74 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 75 | if not controller_network.controller.admin_user: |
| 76 | logger.info("controller %r has no admin_user, skipping" % controller_network.controller) |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 77 | return |
| 78 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 79 | if controller_network.network.owner and controller_network.network.owner.creator: |
Sapan Bhatia | 321b70e | 2015-08-19 12:20:47 -0400 | [diff] [blame] | 80 | return self.save_controller_network(controller_network) |
| 81 | else: |
| 82 | raise Exception('Could not save network controller %s'%controller_network) |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 83 | |
Sapan Bhatia | 321b70e | 2015-08-19 12:20:47 -0400 | [diff] [blame] | 84 | def map_delete_inputs(self, controller_network): |
Scott Baker | 3aaa76b | 2015-12-09 22:55:19 -0800 | [diff] [blame] | 85 | # XXX This check should really be made from booleans, rather than using hardcoded network names |
| 86 | 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] | 87 | # We only sync private networks |
| 88 | return |
Sapan Bhatia | 53a0e9b | 2015-05-09 18:16:24 +0200 | [diff] [blame] | 89 | try: |
| 90 | slice = controller_network.network.owner # XXX: FIXME!! |
| 91 | except: |
| 92 | raise Exception('Could not get slice for Network %s'%controller_network.network.name) |
| 93 | |
| 94 | network_name = controller_network.network.name |
| 95 | subnet_name = '%s-%d'%(network_name,controller_network.pk) |
| 96 | cidr = controller_network.subnet |
| 97 | network_fields = {'endpoint':controller_network.controller.auth_url, |
| 98 | 'admin_user':slice.creator.email, # XXX: FIXME |
| 99 | 'tenant_name':slice.name, # XXX: FIXME |
| 100 | 'admin_password':slice.creator.remote_password, |
| 101 | 'name':network_name, |
| 102 | 'subnet_name':subnet_name, |
| 103 | 'ansible_tag':'%s-%s@%s'%(network_name,slice.slicename,controller_network.controller.name), |
| 104 | 'cidr':cidr, |
| 105 | 'delete':True |
| 106 | } |
| 107 | |
Sapan Bhatia | 321b70e | 2015-08-19 12:20:47 -0400 | [diff] [blame] | 108 | return network_fields |
Sapan Bhatia | 53a0e9b | 2015-05-09 18:16:24 +0200 | [diff] [blame] | 109 | |
| 110 | """ |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 111 | driver = OpenStackDriver().client_driver(caller=controller_network.network.owner.creator, |
| 112 | tenant=controller_network.network.owner.name, |
| 113 | controller=controller_network.controller.name) |
| 114 | if (controller_network.router_id) and (controller_network.subnet_id): |
| 115 | driver.delete_router_interface(controller_network.router_id, controller_network.subnet_id) |
| 116 | if controller_network.subnet_id: |
| 117 | driver.delete_subnet(controller_network.subnet_id) |
| 118 | if controller_network.router_id: |
| 119 | driver.delete_router(controller_network.router_id) |
| 120 | if controller_network.net_id: |
| 121 | driver.delete_network(controller_network.net_id) |
Sapan Bhatia | 53a0e9b | 2015-05-09 18:16:24 +0200 | [diff] [blame] | 122 | """ |