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