blob: 821e67e430b68c0f4e3fac765e0498473cbbf7df [file] [log] [blame]
Tony Macke4be32f2014-03-11 20:45:25 -04001import os
2import base64
Tony Mack69f1bc32014-03-12 13:20:34 -04003import hashlib
Tony Macke4be32f2014-03-11 20:45:25 -04004from collections import defaultdict
5from django.db.models import F, Q
6from planetstack.config import Config
7from observer.openstacksyncstep import OpenStackSyncStep
Sapan Bhatiab55f4a32014-11-19 15:07:36 -05008from core.models.site import SiteDeployments, Deployment
Tony Mack91463d92014-06-19 20:42:03 -04009from core.models.user import User
Sapan Bhatiae20777d2014-11-19 15:10:51 -050010from core.models.userdeployments import UserDeployments
Tony Macke4be32f2014-03-11 20:45:25 -040011from util.logger import Logger, logging
12
Sapan Bhatia14356b72014-11-05 10:32:41 -050013from observer.ansible import *
14
Tony Macke4be32f2014-03-11 20:45:25 -040015logger = Logger(level=logging.INFO)
16
Sapan Bhatiae20777d2014-11-19 15:10:51 -050017class SyncUserDeployments(OpenStackSyncStep):
18 provides=[UserDeployments, User]
Tony Macke4be32f2014-03-11 20:45:25 -040019 requested_interval=0
20
Sapan Bhatiaef3ae352014-07-23 09:43:20 -040021 def fetch_pending(self, deleted):
22
23 if (deleted):
Sapan Bhatiae20777d2014-11-19 15:10:51 -050024 return UserDeployments.deleted_objects.all()
Sapan Bhatiaef3ae352014-07-23 09:43:20 -040025 else:
Sapan Bhatiae20777d2014-11-19 15:10:51 -050026 return UserDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Macke4be32f2014-03-11 20:45:25 -040027
28 def sync_record(self, user_deployment):
Tony Mack69f1bc32014-03-12 13:20:34 -040029 logger.info("sync'ing user %s at deployment %s" % (user_deployment.user, user_deployment.deployment.name))
Scott Baker9ae9e4e2014-09-04 15:13:40 -070030
31 if not user_deployment.deployment.admin_user:
32 logger.info("deployment %r has no admin_user, skipping" % user_deployment.deployment)
33 return
34
Sapan Bhatia14356b72014-11-05 10:32:41 -050035 template = os_template_env.get_template('sync_user_deployments.yaml')
36
Tony Macke4be32f2014-03-11 20:45:25 -040037 name = user_deployment.user.email[:user_deployment.user.email.find('@')]
Tony Macke4be32f2014-03-11 20:45:25 -040038
Sapan Bhatia14356b72014-11-05 10:32:41 -050039 roles = []
40 # setup user deployment home site roles
Tony Macke4be32f2014-03-11 20:45:25 -040041 if user_deployment.user.site:
Sapan Bhatiab55f4a32014-11-19 15:07:36 -050042 site_deployments = SiteDeployments.objects.filter(site=user_deployment.user.site,
Tony Macke4be32f2014-03-11 20:45:25 -040043 deployment=user_deployment.deployment)
44 if site_deployments:
45 # need the correct tenant id for site at the deployment
46 tenant_id = site_deployments[0].tenant_id
Sapan Bhatia14356b72014-11-05 10:32:41 -050047 tenant_name = site_deployments[0].site.login_base
48
49 roles.append('user')
Tony Macke4be32f2014-03-11 20:45:25 -040050 if user_deployment.user.is_admin:
Sapan Bhatia14356b72014-11-05 10:32:41 -050051 roles.append('admin')
52 else:
Sapan Bhatiab55f4a32014-11-19 15:07:36 -050053 raise Exception('Internal error. Missing SiteDeployments for user %s'%user_deployment.user.email)
Sapan Bhatia14356b72014-11-05 10:32:41 -050054 else:
55 raise Exception('Siteless user %s'%user_deployment.user.email)
Tony Macke4be32f2014-03-11 20:45:25 -040056
Tony Macke4be32f2014-03-11 20:45:25 -040057
Sapan Bhatia14356b72014-11-05 10:32:41 -050058 user_fields = {'endpoint':user_deployment.deployment.auth_url,
59 'name': user_deployment.user.email,
Sapan03018872014-12-03 01:01:38 -050060 'ansible_tag': '%s@%s'%(user_deployment.user.email,user_deployment.deployment.name),
Sapan Bhatia14356b72014-11-05 10:32:41 -050061 'email': user_deployment.user.email,
62 'password': hashlib.md5(user_deployment.user.password).hexdigest()[:6],
63 'admin_user': user_deployment.deployment.admin_user,
64 'admin_password': user_deployment.deployment.admin_password,
65 'admin_tenant': 'admin',
66 'roles':roles,
67 'tenant':tenant_name}
68
69 rendered = template.render(user_fields)
Sapanbd9bc252014-12-03 01:07:31 -050070 res = run_template('sync_user_deployments.yaml', user_fields, 'user_deployments')
Sapan Bhatia14356b72014-11-05 10:32:41 -050071
72 # results is an array in which each element corresponds to an
73 # "ok" string received per operation. If we get as many oks as
74 # the number of operations we issued, that means a grand success.
75 # Otherwise, the number of oks tell us which operation failed.
76 expected_length = len(roles) + 1
77 if (len(res)==expected_length):
78 user_deployment.save()
79 elif (len(res)):
80 raise Exception('Could not assign roles for user %s'%user_fields['name'])
81 else:
82 raise Exception('Could not create or update user %s'%user_fields['name'])
Sapan Bhatiaef3ae352014-07-23 09:43:20 -040083
84 def delete_record(self, user_deployment):
Tony Mack95925282014-09-24 10:22:31 -040085 if user_deployment.kuser_id:
Sapan Bhatiaef3ae352014-07-23 09:43:20 -040086 driver = self.driver.admin_driver(deployment=user_deployment.deployment.name)
Tony Mack95925282014-09-24 10:22:31 -040087 driver.delete_user(user_deployment.kuser_id)