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 |
Scott Baker | 76a840e | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 6 | from xos.config import Config |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -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 * |
Sapan Bhatia | 9028c9a | 2015-05-09 18:14:40 +0200 | [diff] [blame] | 9 | from core.models import * |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 10 | from observer.ansible import * |
Tony Mack | 3375088 | 2015-01-06 18:27:48 -0500 | [diff] [blame] | 11 | from openstack.driver import OpenStackDriver |
Tony Mack | 08f8288 | 2015-03-29 08:32:21 -0400 | [diff] [blame] | 12 | from util.logger import observer_logger as logger |
Sapan Bhatia | 9028c9a | 2015-05-09 18:14:40 +0200 | [diff] [blame] | 13 | import json |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 14 | |
Tony Mack | 2656436 | 2015-01-06 17:49:25 -0500 | [diff] [blame] | 15 | class SyncControllerSlices(OpenStackSyncStep): |
Sapan Bhatia | b3048aa | 2015-01-27 03:52:19 +0000 | [diff] [blame] | 16 | provides=[Slice] |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 17 | requested_interval=0 |
Sapan Bhatia | 99f4968 | 2015-01-29 20:58:25 +0000 | [diff] [blame] | 18 | observes=ControllerSlice |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 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 | |
Sapan Bhatia | 9028c9a | 2015-05-09 18:14:40 +0200 | [diff] [blame] | 29 | controller_register = json.loads(controller_slice.controller.backend_register) |
| 30 | if (controller_register.get('disabled',False)): |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 31 | raise InnocuousException('Controller %s is disabled'%controller_slice.controller.name) |
Sapan Bhatia | 9028c9a | 2015-05-09 18:14:40 +0200 | [diff] [blame] | 32 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 33 | if not controller_slice.controller.admin_user: |
| 34 | logger.info("controller %r has no admin_user, skipping" % controller_slice.controller) |
| 35 | return |
| 36 | |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 37 | controller_users = ControllerUser.objects.filter(user=controller_slice.slice.creator, |
Sapan Bhatia | f6db4db | 2014-12-22 11:25:31 -0500 | [diff] [blame] | 38 | controller=controller_slice.controller) |
| 39 | if not controller_users: |
| 40 | raise Exception("slice createor %s has not accout at controller %s" % (controller_slice.slice.creator, controller_slice.controller.name)) |
| 41 | else: |
| 42 | controller_user = controller_users[0] |
Andy Bavier | 196a37a | 2015-08-03 15:54:30 -0400 | [diff] [blame] | 43 | roles = ['admin'] |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 44 | |
Sapan Bhatia | f6db4db | 2014-12-22 11:25:31 -0500 | [diff] [blame] | 45 | max_instances=int(controller_slice.slice.max_slivers) |
| 46 | tenant_fields = {'endpoint':controller_slice.controller.auth_url, |
| 47 | 'admin_user': controller_slice.controller.admin_user, |
| 48 | 'admin_password': controller_slice.controller.admin_password, |
| 49 | 'admin_tenant': 'admin', |
| 50 | 'tenant': controller_slice.slice.name, |
| 51 | 'tenant_description': controller_slice.slice.description, |
| 52 | 'roles':roles, |
| 53 | 'name':controller_user.user.email, |
| 54 | 'ansible_tag':'%s@%s'%(controller_slice.slice.name,controller_slice.controller.name), |
| 55 | 'max_instances':max_instances} |
| 56 | |
Sapan Bhatia | f6db4db | 2014-12-22 11:25:31 -0500 | [diff] [blame] | 57 | expected_num = len(roles)+1 |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 58 | res = run_template('sync_controller_slices.yaml', tenant_fields, path='controller_slices', expected_num=expected_num) |
| 59 | tenant_id = res[0]['id'] |
| 60 | if (not controller_slice.tenant_id): |
| 61 | try: |
Sapan Bhatia | 6218c4d | 2015-05-09 18:17:39 +0200 | [diff] [blame] | 62 | driver = OpenStackDriver().admin_driver(controller=controller_slice.controller) |
| 63 | driver.shell.nova.quotas.update(tenant_id=controller_slice.tenant_id, instances=int(controller_slice.slice.max_slivers)) |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 64 | except: |
Sapan Bhatia | 6218c4d | 2015-05-09 18:17:39 +0200 | [diff] [blame] | 65 | logger.log_exc('Could not update quota for %s'%controller_slice.slice.name) |
| 66 | raise Exception('Could not update quota for %s'%controller_slice.slice.name) |
| 67 | |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 68 | controller_slice.tenant_id = tenant_id |
Sapan Bhatia | 5851db4 | 2015-01-27 03:52:43 +0000 | [diff] [blame] | 69 | controller_slice.backend_status = '1 - OK' |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 70 | controller_slice.save() |
Sapan Bhatia | f6db4db | 2014-12-22 11:25:31 -0500 | [diff] [blame] | 71 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 72 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 73 | def delete_record(self, controller_slice): |
Sapan Bhatia | 6218c4d | 2015-05-09 18:17:39 +0200 | [diff] [blame] | 74 | controller_register = json.loads(controller_slice.controller.backend_register) |
| 75 | if (controller_register.get('disabled',False)): |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 76 | raise InnocuousException('Controller %s is disabled'%controller_slice.controller.name) |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 77 | |
Sapan Bhatia | 6218c4d | 2015-05-09 18:17:39 +0200 | [diff] [blame] | 78 | controller_users = ControllerUser.objects.filter(user=controller_slice.slice.creator, |
| 79 | controller=controller_slice.controller) |
| 80 | if not controller_users: |
| 81 | raise Exception("slice createor %s has not accout at controller %s" % (controller_slice.slice.creator, controller_slice.controller.name)) |
| 82 | else: |
| 83 | controller_user = controller_users[0] |
| 84 | |
| 85 | tenant_fields = {'endpoint':controller_slice.controller.auth_url, |
| 86 | 'admin_user': controller_slice.controller.admin_user, |
| 87 | 'admin_password': controller_slice.controller.admin_password, |
| 88 | 'admin_tenant': 'admin', |
| 89 | 'tenant': controller_slice.slice.name, |
| 90 | 'tenant_description': controller_slice.slice.description, |
| 91 | 'name':controller_user.user.email, |
| 92 | 'ansible_tag':'%s@%s'%(controller_slice.slice.name,controller_slice.controller.name), |
| 93 | 'delete': True} |
| 94 | |
| 95 | expected_num = 1 |
| 96 | run_template('sync_controller_slices.yaml', tenant_fields, path='controller_slices', expected_num=expected_num) |