blob: 907325a9d6344edb838aca19415db63d914f7cbf [file] [log] [blame]
Tony Mack336e0f92014-11-30 15:53:08 -05001import os
2import base64
3from django.db.models import F, Q
4from planetstack.config import Config
5from observer.openstacksyncstep import OpenStackSyncStep
6from core.models.site import *
7from observer.ansible import *
8
Tony Mack528d4222014-12-05 17:13:08 -05009class SyncControllerSiteDeployments(OpenStackSyncStep):
Tony Mack336e0f92014-11-30 15:53:08 -050010 requested_interval=0
Tony Mack7f542c62015-01-03 17:26:06 -050011 provides=[SiteDeployments]
Tony Mack336e0f92014-11-30 15:53:08 -050012
Tony Mackb469d242015-01-03 19:37:39 -050013 def fetch_pending(self, deleted=False):
14 pending = super(OpenStackSyncStep, self).fetch_pending(deleted)
15 return pending.filter(controller__isnull=False)
16
Tony Mack7f542c62015-01-03 17:26:06 -050017 def sync_record(self, site_deployment):
Tony Mack336e0f92014-11-30 15:53:08 -050018
19 template = os_template_env.get_template('sync_controller_site_deployments.yaml')
Tony Mack7f542c62015-01-03 17:26:06 -050020 tenant_fields = {'endpoint':site_deployment.controller.auth_url,
21 'admin_user': site_deployment.controller.admin_user,
22 'admin_password': site_deployment.controller.admin_password,
23 'admin_tenant': site_deployment.controller.admin_tenant,
Tony Mackb469d242015-01-03 19:37:39 -050024 'ansible_tag': '%s@%s'%(site_deployment.site.login_base,site_deployment.deployment.name), # name of ansible playbook
Tony Mack7f542c62015-01-03 17:26:06 -050025 'tenant': site_deployment.site.login_base,
26 'tenant_description': site_deployment.site.name}
Tony Mack336e0f92014-11-30 15:53:08 -050027
28 rendered = template.render(tenant_fields)
Tony Mack7f542c62015-01-03 17:26:06 -050029 res = run_template('sync_controller_site_deployments.yaml', tenant_fields, path='controller_site_deployments')
Tony Mack336e0f92014-11-30 15:53:08 -050030
31 if (len(res)==1):
Tony Mack7f542c62015-01-03 17:26:06 -050032 site_deployment.tenant_id = res[0]['id']
33 site_deployment.save()
Tony Mack336e0f92014-11-30 15:53:08 -050034 elif (len(res)):
35 raise Exception('Could not assign roles for user %s'%tenant_fields['tenant'])
36 else:
37 raise Exception('Could not create or update user %s'%tenant_fields['tenant'])
38
Tony Mack7f542c62015-01-03 17:26:06 -050039 def delete_record(self, site_deployment):
40 if site_deployment.tenant_id:
41 driver = self.driver.admin_driver(controller=site_deployment.controller)
42 driver.delete_tenant(site_deployment.tenant_id)
Sapan Bhatiaf2378a82014-12-21 02:33:52 -050043
44 """
45 Ansible does not support tenant deletion yet
46
47 import pdb
48 pdb.set_trace()
Tony Mack7f542c62015-01-03 17:26:06 -050049 template = os_template_env.get_template('delete_site_deployments.yaml')
50 tenant_fields = {'endpoint':site_deployment.controller.auth_url,
51 'admin_user': site_deployment.controller.admin_user,
52 'admin_password': site_deployment.controller.admin_password,
Sapan Bhatiaf2378a82014-12-21 02:33:52 -050053 'admin_tenant': 'admin',
Tony Mack7f542c62015-01-03 17:26:06 -050054 'ansible_tag': 'site_deployments/%s@%s'%(site_deployment.site_deployment.site.login_base,site_deployment.site_deployment.deployment.name), # name of ansible playbook
55 'tenant': site_deployment.site_deployment.site.login_base,
Sapan Bhatiaf2378a82014-12-21 02:33:52 -050056 'delete': True}
57
58 rendered = template.render(tenant_fields)
Tony Mack7f542c62015-01-03 17:26:06 -050059 res = run_template('sync_site_deployments.yaml', tenant_fields)
Sapan Bhatiaf2378a82014-12-21 02:33:52 -050060
61 if (len(res)!=1):
62 raise Exception('Could not assign roles for user %s'%tenant_fields['tenant'])
63 """