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