Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | from django.db.models import F, Q |
Scott Baker | 76a840e | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 4 | from xos.config import Config |
Tony Mack | 8f30ebe | 2015-01-06 15:08:20 -0500 | [diff] [blame] | 5 | from openstack_observer.openstacksyncstep import OpenStackSyncStep |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 6 | from core.models.site import * |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 7 | from observer.syncstep import * |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 8 | from observer.ansible import * |
Tony Mack | 08f8288 | 2015-03-29 08:32:21 -0400 | [diff] [blame] | 9 | from util.logger import observer_logger as logger |
Sapan Bhatia | 9028c9a | 2015-05-09 18:14:40 +0200 | [diff] [blame] | 10 | import json |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 11 | |
Tony Mack | 8f30ebe | 2015-01-06 15:08:20 -0500 | [diff] [blame] | 12 | class SyncControllerSites(OpenStackSyncStep): |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 13 | requested_interval=0 |
Sapan Bhatia | b3048aa | 2015-01-27 03:52:19 +0000 | [diff] [blame] | 14 | provides=[Site] |
Sapan Bhatia | 99f4968 | 2015-01-29 20:58:25 +0000 | [diff] [blame] | 15 | observes=ControllerSite |
Sapan Bhatia | 321b70e | 2015-08-19 12:20:47 -0400 | [diff] [blame] | 16 | playbook = 'sync_controller_sites.yaml' |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 17 | |
| 18 | def fetch_pending(self, deleted=False): |
Sapan Bhatia | 8347581 | 2015-09-16 16:40:36 +0200 | [diff] [blame] | 19 | lobjs = ControllerSite.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None),Q(lazy_blocked=False),Q(controller__isnull=False)) |
| 20 | return lobjs |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 21 | |
Sapan Bhatia | 321b70e | 2015-08-19 12:20:47 -0400 | [diff] [blame] | 22 | def map_sync_inputs(self, controller_site): |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 23 | tenant_fields = {'endpoint':controller_site.controller.auth_url, |
Tony Mack | 09a2f07 | 2015-09-14 00:53:39 +0000 | [diff] [blame] | 24 | 'endpoint_v3': controller_site.controller.auth_url_v3, |
| 25 | 'domain': controller_site.controller.domain, |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 26 | 'admin_user': controller_site.controller.admin_user, |
| 27 | 'admin_password': controller_site.controller.admin_password, |
| 28 | 'admin_tenant': controller_site.controller.admin_tenant, |
Tony Mack | 09a2f07 | 2015-09-14 00:53:39 +0000 | [diff] [blame] | 29 | 'ansible_tag': '%s@%s'%(controller_site.site.login_base,controller_site.controller.name), # name of ansible playbook |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 30 | 'tenant': controller_site.site.login_base, |
| 31 | 'tenant_description': controller_site.site.name} |
Sapan Bhatia | 321b70e | 2015-08-19 12:20:47 -0400 | [diff] [blame] | 32 | return tenant_fields |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 33 | |
Sapan Bhatia | 321b70e | 2015-08-19 12:20:47 -0400 | [diff] [blame] | 34 | def map_sync_outputs(self, controller_site, res): |
Tony Mack | f3c17e1 | 2015-10-01 00:05:56 +0000 | [diff] [blame] | 35 | controller_site.tenant_id = res[0]['id'] |
Sapan Bhatia | 5851db4 | 2015-01-27 03:52:43 +0000 | [diff] [blame] | 36 | controller_site.backend_status = '1 - OK' |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 37 | controller_site.save() |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 38 | |
| 39 | def delete_record(self, controller_site): |
Sapan Bhatia | 9028c9a | 2015-05-09 18:14:40 +0200 | [diff] [blame] | 40 | controller_register = json.loads(controller_site.controller.backend_register) |
| 41 | if (controller_register.get('disabled',False)): |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 42 | raise InnocuousException('Controller %s is disabled'%controller_site.controller.name) |
Sapan Bhatia | 9028c9a | 2015-05-09 18:14:40 +0200 | [diff] [blame] | 43 | |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 44 | if controller_site.tenant_id: |
| 45 | driver = self.driver.admin_driver(controller=controller_site.controller) |
| 46 | driver.delete_tenant(controller_site.tenant_id) |
| 47 | |
| 48 | """ |
| 49 | Ansible does not support tenant deletion yet |
| 50 | |
| 51 | import pdb |
| 52 | pdb.set_trace() |
| 53 | template = os_template_env.get_template('delete_controller_sites.yaml') |
| 54 | tenant_fields = {'endpoint':controller_site.controller.auth_url, |
| 55 | 'admin_user': controller_site.controller.admin_user, |
| 56 | 'admin_password': controller_site.controller.admin_password, |
| 57 | 'admin_tenant': 'admin', |
| 58 | 'ansible_tag': 'controller_sites/%s@%s'%(controller_site.controller_site.site.login_base,controller_site.controller_site.deployment.name), # name of ansible playbook |
| 59 | 'tenant': controller_site.controller_site.site.login_base, |
| 60 | 'delete': True} |
| 61 | |
| 62 | rendered = template.render(tenant_fields) |
| 63 | res = run_template('sync_controller_sites.yaml', tenant_fields) |
| 64 | |
| 65 | if (len(res)!=1): |
| 66 | raise Exception('Could not assign roles for user %s'%tenant_fields['tenant']) |
| 67 | """ |