Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [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 xos.config import Config |
| 7 | from synchronizers.base.openstacksyncstep import OpenStackSyncStep |
| 8 | from synchronizers.base.syncstep import * |
| 9 | from core.models import * |
| 10 | from synchronizers.base.ansible import * |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 11 | from xos.logger import observer_logger as logger |
| 12 | import json |
| 13 | |
| 14 | class SyncControllerSlices(OpenStackSyncStep): |
| 15 | provides=[Slice] |
| 16 | requested_interval=0 |
| 17 | observes=ControllerSlice |
| 18 | playbook='sync_controller_slices.yaml' |
| 19 | |
| 20 | def map_sync_inputs(self, controller_slice): |
| 21 | logger.info("sync'ing slice controller %s" % controller_slice) |
| 22 | |
| 23 | if not controller_slice.controller.admin_user: |
| 24 | logger.info("controller %r has no admin_user, skipping" % controller_slice.controller) |
| 25 | return |
| 26 | |
| 27 | controller_users = ControllerUser.objects.filter(user=controller_slice.slice.creator, |
| 28 | controller=controller_slice.controller) |
| 29 | if not controller_users: |
| 30 | raise Exception("slice createor %s has not accout at controller %s" % (controller_slice.slice.creator, controller_slice.controller.name)) |
| 31 | else: |
| 32 | controller_user = controller_users[0] |
Scott Baker | 04a37f5 | 2016-08-11 10:52:21 -0700 | [diff] [blame^] | 33 | driver = self.driver.admin_driver(controller=controller_slice.controller) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 34 | roles = [driver.get_admin_role().name] |
| 35 | |
| 36 | max_instances=int(controller_slice.slice.max_instances) |
| 37 | tenant_fields = {'endpoint':controller_slice.controller.auth_url, |
| 38 | 'endpoint_v3': controller_slice.controller.auth_url_v3, |
| 39 | 'domain': controller_slice.controller.domain, |
| 40 | 'admin_user': controller_slice.controller.admin_user, |
| 41 | 'admin_password': controller_slice.controller.admin_password, |
| 42 | 'admin_tenant': 'admin', |
| 43 | 'tenant': controller_slice.slice.name, |
| 44 | 'tenant_description': controller_slice.slice.description, |
| 45 | 'roles':roles, |
| 46 | 'name':controller_user.user.email, |
| 47 | 'ansible_tag':'%s@%s'%(controller_slice.slice.name,controller_slice.controller.name), |
| 48 | 'max_instances':max_instances} |
| 49 | |
| 50 | return tenant_fields |
| 51 | |
| 52 | def map_sync_outputs(self, controller_slice, res): |
| 53 | tenant_id = res[0]['id'] |
| 54 | if (not controller_slice.tenant_id): |
| 55 | try: |
Scott Baker | 04a37f5 | 2016-08-11 10:52:21 -0700 | [diff] [blame^] | 56 | driver = self.driver.admin_driver(controller=controller_slice.controller) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 57 | driver.shell.nova.quotas.update(tenant_id=tenant_id, instances=int(controller_slice.slice.max_instances)) |
| 58 | except: |
| 59 | logger.log_exc('Could not update quota for %s'%controller_slice.slice.name) |
| 60 | raise Exception('Could not update quota for %s'%controller_slice.slice.name) |
| 61 | |
| 62 | controller_slice.tenant_id = tenant_id |
| 63 | controller_slice.backend_status = '1 - OK' |
| 64 | controller_slice.save() |
| 65 | |
| 66 | |
| 67 | def map_delete_inputs(self, controller_slice): |
| 68 | controller_users = ControllerUser.objects.filter(user=controller_slice.slice.creator, |
| 69 | controller=controller_slice.controller) |
| 70 | if not controller_users: |
| 71 | raise Exception("slice createor %s has not accout at controller %s" % (controller_slice.slice.creator, controller_slice.controller.name)) |
| 72 | else: |
| 73 | controller_user = controller_users[0] |
| 74 | |
| 75 | tenant_fields = {'endpoint':controller_slice.controller.auth_url, |
| 76 | 'admin_user': controller_slice.controller.admin_user, |
| 77 | 'admin_password': controller_slice.controller.admin_password, |
| 78 | 'admin_tenant': 'admin', |
| 79 | 'tenant': controller_slice.slice.name, |
| 80 | 'tenant_description': controller_slice.slice.description, |
| 81 | 'name':controller_user.user.email, |
| 82 | 'ansible_tag':'%s@%s'%(controller_slice.slice.name,controller_slice.controller.name), |
| 83 | 'delete': True} |
| 84 | return tenant_fields |