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