Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | from django.db.models import F, Q |
| 4 | from planetstack.config import Config |
| 5 | from observer.openstacksyncstep import OpenStackSyncStep |
| 6 | from core.models.site import * |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 7 | from observer.ansible import * |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 8 | |
Sapan Bhatia | 39e8558 | 2014-11-19 15:07:36 -0500 | [diff] [blame] | 9 | class SyncSiteDeployments(OpenStackSyncStep): |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 10 | requested_interval=0 |
Sapan Bhatia | 39e8558 | 2014-11-19 15:07:36 -0500 | [diff] [blame] | 11 | provides=[SiteDeployments, Site] |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 12 | |
| 13 | def sync_record(self, site_deployment): |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 14 | |
| 15 | template = os_template_env.get_template('sync_site_deployments.yaml') |
| 16 | tenant_fields = {'endpoint':site_deployment.deployment.auth_url, |
| 17 | 'admin_user': site_deployment.deployment.admin_user, |
| 18 | 'admin_password': site_deployment.deployment.admin_password, |
| 19 | 'admin_tenant': 'admin', |
| 20 | 'tenant': site_deployment.site.login_base, |
Sapan | 5bfab5e | 2014-12-03 00:04:58 -0500 | [diff] [blame] | 21 | 'ansible_tag': '%s@%s'%(site_deployment.site.login_base,site_deployment.deployment.name), # name of ansible playbook |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 22 | 'tenant_description': site_deployment.site.name} |
| 23 | |
| 24 | rendered = template.render(tenant_fields) |
Sapan | 5bfab5e | 2014-12-03 00:04:58 -0500 | [diff] [blame] | 25 | res = run_template('sync_site_deployments.yaml', tenant_fields, path='site_deployments') |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 26 | |
| 27 | if (len(res)==1): |
| 28 | site_deployment.tenant_id = res[0]['id'] |
| 29 | site_deployment.save() |
| 30 | elif (len(res)): |
| 31 | raise Exception('Could not assign roles for user %s'%tenant_fields['tenant']) |
| 32 | else: |
| 33 | raise Exception('Could not create or update user %s'%tenant_fields['tenant']) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 34 | |
Sapan Bhatia | 663626e | 2014-07-23 09:50:33 -0400 | [diff] [blame] | 35 | def delete_record(self, site_deployment): |
| 36 | if site_deployment.tenant_id: |
| 37 | driver = self.driver.admin_driver(deployment=site_deployment.deployment.name) |
| 38 | driver.delete_tenant(site_deployment.tenant_id) |