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