Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -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 |
| 6 | from planetstack.config import Config |
| 7 | from observer.openstacksyncstep import OpenStackSyncStep |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 8 | from core.models.slice import Slice, ControllerSlice |
Tony Mack | 2656436 | 2015-01-06 17:49:25 -0500 | [diff] [blame] | 9 | from core.models.controlleruser import ControllerUser |
Tony Mack | 3375088 | 2015-01-06 18:27:48 -0500 | [diff] [blame] | 10 | from util.logger import Logger, logging, logger |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 11 | from observer.ansible import * |
Tony Mack | 3375088 | 2015-01-06 18:27:48 -0500 | [diff] [blame] | 12 | from openstack.driver import OpenStackDriver |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 13 | |
| 14 | logger = Logger(level=logging.INFO) |
| 15 | |
Tony Mack | 2656436 | 2015-01-06 17:49:25 -0500 | [diff] [blame] | 16 | class SyncControllerSlices(OpenStackSyncStep): |
Sapan Bhatia | e34efa0 | 2015-01-16 22:16:29 +0000 | [diff] [blame^] | 17 | provides=[ControllerSlice, Slice] |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 18 | requested_interval=0 |
| 19 | |
| 20 | def fetch_pending(self, deleted): |
| 21 | if (deleted): |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 22 | return ControllerSlice.deleted_objects.all() |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 23 | else: |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 24 | return ControllerSlice.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 25 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 26 | def sync_record(self, controller_slice): |
| 27 | logger.info("sync'ing slice controller %s" % controller_slice) |
| 28 | |
| 29 | if not controller_slice.controller.admin_user: |
| 30 | logger.info("controller %r has no admin_user, skipping" % controller_slice.controller) |
| 31 | return |
| 32 | |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 33 | controller_users = ControllerUser.objects.filter(user=controller_slice.slice.creator, |
Sapan Bhatia | f6db4db | 2014-12-22 11:25:31 -0500 | [diff] [blame] | 34 | controller=controller_slice.controller) |
| 35 | if not controller_users: |
| 36 | raise Exception("slice createor %s has not accout at controller %s" % (controller_slice.slice.creator, controller_slice.controller.name)) |
| 37 | else: |
| 38 | controller_user = controller_users[0] |
Tony Mack | 3375088 | 2015-01-06 18:27:48 -0500 | [diff] [blame] | 39 | roles = ['Admin'] |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 40 | |
Sapan Bhatia | f6db4db | 2014-12-22 11:25:31 -0500 | [diff] [blame] | 41 | max_instances=int(controller_slice.slice.max_slivers) |
| 42 | tenant_fields = {'endpoint':controller_slice.controller.auth_url, |
| 43 | 'admin_user': controller_slice.controller.admin_user, |
| 44 | 'admin_password': controller_slice.controller.admin_password, |
| 45 | 'admin_tenant': 'admin', |
| 46 | 'tenant': controller_slice.slice.name, |
| 47 | 'tenant_description': controller_slice.slice.description, |
| 48 | 'roles':roles, |
| 49 | 'name':controller_user.user.email, |
| 50 | 'ansible_tag':'%s@%s'%(controller_slice.slice.name,controller_slice.controller.name), |
| 51 | 'max_instances':max_instances} |
| 52 | |
| 53 | res = run_template('sync_controller_slices.yaml', tenant_fields, path='controller_slices') |
| 54 | expected_num = len(roles)+1 |
| 55 | if (len(res)!=expected_num): |
| 56 | raise Exception('Could not sync tenants for slice %s'%controller_slice.slice.name) |
| 57 | else: |
| 58 | tenant_id = res[0]['id'] |
| 59 | if (not controller_slice.tenant_id): |
| 60 | try: |
Tony Mack | 3375088 | 2015-01-06 18:27:48 -0500 | [diff] [blame] | 61 | driver = OpenStackDriver().admin_driver(controller=controller_slice.controller) |
| 62 | driver.shell.nova.quotas.update(tenant_id=controller_slice.tenant_id, instances=int(controller_slice.slice.max_slivers)) |
Sapan Bhatia | f6db4db | 2014-12-22 11:25:31 -0500 | [diff] [blame] | 63 | except: |
Tony Mack | 3375088 | 2015-01-06 18:27:48 -0500 | [diff] [blame] | 64 | logger.log_exc('Could not update quota for %s'%controller_slice.slice.name) |
Sapan Bhatia | f6db4db | 2014-12-22 11:25:31 -0500 | [diff] [blame] | 65 | raise Exception('Could not update quota for %s'%controller_slice.slice.name) |
Tony Mack | 3375088 | 2015-01-06 18:27:48 -0500 | [diff] [blame] | 66 | |
Sapan Bhatia | f6db4db | 2014-12-22 11:25:31 -0500 | [diff] [blame] | 67 | controller_slice.tenant_id = tenant_id |
| 68 | controller_slice.save() |
| 69 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 70 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 71 | def delete_record(self, controller_slice): |
| 72 | user = User.objects.get(id=controller_slice.slice.creator.id) |
| 73 | driver = OpenStackDriver().admin_driver(controller=controller_slice.controller.name) |
| 74 | client_driver = driver.client_driver(caller=user, |
| 75 | tenant=controller_slice.slice.name, |
| 76 | controller=controller_slice.controller.name) |
| 77 | |
| 78 | if controller_slice.router_id and controller_slice.subnet_id: |
| 79 | client_driver.delete_router_interface(controller_slice.router_id, controller_slice.subnet_id) |
| 80 | if controller_slice.subnet_id: |
| 81 | client_driver.delete_subnet(controller_slice.subnet_id) |
Sapan Bhatia | f6db4db | 2014-12-22 11:25:31 -0500 | [diff] [blame] | 82 | if controller_slice.router_id: |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 83 | client_driver.delete_router(controller_slice.router_id) |
| 84 | if controller_slice.network_id: |
| 85 | client_driver.delete_network(controller_slice.network_id) |
| 86 | if controller_slice.tenant_id: |
| 87 | driver.delete_tenant(controller_slice.tenant_id) |