blob: 5c04003849d757be82af8ac736559e68d9708543 [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
Tony Mack69f1bc32014-03-12 13:20:34 -04008from core.models.site import SiteDeployments, Deployment
Tony Macke4be32f2014-03-11 20:45:25 -04009from core.models.user import User, UserDeployments
10from util.logger import Logger, logging
11
12logger = Logger(level=logging.INFO)
13
14class SyncUserDeployments(OpenStackSyncStep):
15 provides=[User, UserDeployments]
16 requested_interval=0
17
18 def fetch_pending(self):
19 # user deployments are not visible to users. We must ensure
20 # user are deployed at all deploymets available to their sites.
Tony Mack69f1bc32014-03-12 13:20:34 -040021
22 deployments = Deployment.objects.all()
23 site_deployments = SiteDeployments.objects.all()
Tony Macke4be32f2014-03-11 20:45:25 -040024 site_deploy_lookup = defaultdict(list)
25 for site_deployment in site_deployments:
26 site_deploy_lookup[site_deployment.site].append(site_deployment.deployment)
27
Tony Mackd1a17e12014-03-19 15:18:15 -040028 user_deployments = []
29 for user in User.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)):
Tony Mack69f1bc32014-03-12 13:20:34 -040030 if user.is_admin:
31 # admins should have an account at all deployments
32 expected_deployments = deployments
33 else:
34 # normal users should have an account at their site's deployments
35 expected_deployments = site_deploy_lookup[user.site]
Tony Macke4be32f2014-03-11 20:45:25 -040036 for expected_deployment in expected_deployments:
Tony Mackd1a17e12014-03-19 15:18:15 -040037 ud = UserDeployments(user=user, deployment=expected_deployment)
38 user_deployments.append(ud)
Tony Macke4be32f2014-03-11 20:45:25 -040039
Tony Mackd1a17e12014-03-19 15:18:15 -040040 return user_deployments
Tony Macke4be32f2014-03-11 20:45:25 -040041
42 def sync_record(self, user_deployment):
Tony Mack69f1bc32014-03-12 13:20:34 -040043 logger.info("sync'ing user %s at deployment %s" % (user_deployment.user, user_deployment.deployment.name))
Tony Macke4be32f2014-03-11 20:45:25 -040044 name = user_deployment.user.email[:user_deployment.user.email.find('@')]
Tony Mackf37bcfd2014-03-12 13:43:53 -040045 user_fields = {'name': user_deployment.user.email,
Tony Macke4be32f2014-03-11 20:45:25 -040046 'email': user_deployment.user.email,
47 'password': hashlib.md5(user_deployment.user.password).hexdigest()[:6],
48 'enabled': True}
49 driver = self.driver.admin_driver(deployment=user_deployment.deployment.name)
50 if not user_deployment.kuser_id:
Tony Mack69f1bc32014-03-12 13:20:34 -040051 keystone_user = driver.create_user(**user_fields)
Tony Macke4be32f2014-03-11 20:45:25 -040052 user_deployment.kuser_id = keystone_user.id
53 else:
54 driver.update_user(user_deployment.kuser_id, user_fields)
55
Tony Mackd1a17e12014-03-19 15:18:15 -040056 # setup user deployment home site roles
Tony Macke4be32f2014-03-11 20:45:25 -040057 if user_deployment.user.site:
58 site_deployments = SiteDeployments.objects.filter(site=user_deployment.user.site,
59 deployment=user_deployment.deployment)
60 if site_deployments:
61 # need the correct tenant id for site at the deployment
62 tenant_id = site_deployments[0].tenant_id
63 driver.add_user_role(user_deployment.kuser_id,
64 tenant_id, 'user')
65 if user_deployment.user.is_admin:
66 driver.add_user_role(user_deployment.kuser_id, tenant_id, 'admin')
67 else:
68 # may have admin role so attempt to remove it
69 driver.delete_user_role(user_deployment.kuser_id, tenant_id, 'admin')
70
Tony Mackd1a17e12014-03-19 15:18:15 -040071 #if user_deployment.user.public_key:
72 # if not user_deployment.user.keyname:
73 # keyname = user_deployment.user.email.lower().replace('@', 'AT').replace('.', '')
74 # user_deployment.user.keyname = keyname
75 # user_deployment.user.save()
76 #
77 # user_driver = driver.client_driver(caller=user_deployment.user,
78 # tenant=user_deployment.user.site.login_base,
79 # deployment=user_deployment.deployment.name)
80 # key_fields = {'name': user_deployment.user.keyname,
81 # 'public_key': user_deployment.user.public_key}
82 # user_driver.create_keypair(**key_fields)
Tony Macke4be32f2014-03-11 20:45:25 -040083
84 user_deployment.save()