blob: 2e8256f7a58063fc27ba53cee3fcbccc04d40d3b [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 Bhatia39e85582014-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 Bhatia3f879692014-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 Bhatia475c5972014-11-05 10:32:41 -050013from observer.ansible import *
14
Tony Macke4be32f2014-03-11 20:45:25 -040015logger = Logger(level=logging.INFO)
16
Sapan Bhatia3f879692014-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 Bhatia3f879692014-11-19 15:10:51 -050024 return UserDeployments.deleted_objects.all()
Sapan Bhatiaef3ae352014-07-23 09:43:20 -040025 else:
Sapan Bhatia3f879692014-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 Baker1e10f062014-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 Bhatia475c5972014-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 Bhatia475c5972014-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 Bhatia39e85582014-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 Bhatia475c5972014-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 Bhatia475c5972014-11-05 10:32:41 -050051 roles.append('admin')
52 else:
Sapan Bhatia39e85582014-11-19 15:07:36 -050053 raise Exception('Internal error. Missing SiteDeployments for user %s'%user_deployment.user.email)
Sapan Bhatia475c5972014-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 Bhatia475c5972014-11-05 10:32:41 -050058 user_fields = {'endpoint':user_deployment.deployment.auth_url,
59 'name': user_deployment.user.email,
60 'email': user_deployment.user.email,
61 'password': hashlib.md5(user_deployment.user.password).hexdigest()[:6],
62 'admin_user': user_deployment.deployment.admin_user,
63 'admin_password': user_deployment.deployment.admin_password,
64 'admin_tenant': 'admin',
65 'roles':roles,
66 'tenant':tenant_name}
67
68 rendered = template.render(user_fields)
69 res = run_template('sync_user_deployments.yaml', user_fields)
70
71 # results is an array in which each element corresponds to an
72 # "ok" string received per operation. If we get as many oks as
73 # the number of operations we issued, that means a grand success.
74 # Otherwise, the number of oks tell us which operation failed.
75 expected_length = len(roles) + 1
76 if (len(res)==expected_length):
77 user_deployment.save()
78 elif (len(res)):
79 raise Exception('Could not assign roles for user %s'%user_fields['name'])
80 else:
81 raise Exception('Could not create or update user %s'%user_fields['name'])
Sapan Bhatiaef3ae352014-07-23 09:43:20 -040082
83 def delete_record(self, user_deployment):
Tony Mack413e0d62014-09-24 10:22:31 -040084 if user_deployment.kuser_id:
Sapan Bhatiaef3ae352014-07-23 09:43:20 -040085 driver = self.driver.admin_driver(deployment=user_deployment.deployment.name)
Tony Mack413e0d62014-09-24 10:22:31 -040086 driver.delete_user(user_deployment.kuser_id)