blob: a6995ab48d4a2b3d506bf6ccc56921664c331f4e [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 Mack91463d92014-06-19 20:42:03 -04009from core.models.user import User
10from core.models.userdeployments import UserDeployments
Tony Macke4be32f2014-03-11 20:45:25 -040011from util.logger import Logger, logging
12
13logger = Logger(level=logging.INFO)
14
15class SyncUserDeployments(OpenStackSyncStep):
16 provides=[User, UserDeployments]
17 requested_interval=0
18
19 def fetch_pending(self):
20 # user deployments are not visible to users. We must ensure
21 # user are deployed at all deploymets available to their sites.
Tony Mack69f1bc32014-03-12 13:20:34 -040022
23 deployments = Deployment.objects.all()
24 site_deployments = SiteDeployments.objects.all()
Tony Macke4be32f2014-03-11 20:45:25 -040025 site_deploy_lookup = defaultdict(list)
26 for site_deployment in site_deployments:
27 site_deploy_lookup[site_deployment.site].append(site_deployment.deployment)
Tony Mack07f49762014-04-07 19:47:28 -040028
29 user_deploy_lookup = defaultdict(list)
30 for user_deployment in UserDeployments.objects.all():
31 user_deploy_lookup[user_deployment.user].append(user_deployment.deployment)
32
Tony Mack80021662014-04-10 12:25:46 -040033 all_deployments = Deployment.objects.filter()
Tony Mack16665022014-04-08 16:38:15 -040034 for user in User.objects.all():
Tony Mack69f1bc32014-03-12 13:20:34 -040035 if user.is_admin:
36 # admins should have an account at all deployments
37 expected_deployments = deployments
38 else:
39 # normal users should have an account at their site's deployments
Tony Mack80021662014-04-10 12:25:46 -040040 #expected_deployments = site_deploy_lookup[user.site]
41 # users are added to all deployments for now
42 expected_deployments = deployments
Tony Macke4be32f2014-03-11 20:45:25 -040043 for expected_deployment in expected_deployments:
Tony Mack07f49762014-04-07 19:47:28 -040044 if not user in user_deploy_lookup or \
45 expected_deployment not in user_deploy_lookup[user]:
46 # add new record
47 ud = UserDeployments(user=user, deployment=expected_deployment)
Tony Mackd4a64292014-04-16 19:53:21 -040048 ud.save()
49 #user_deployments.append(ud)
Tony Mack07f49762014-04-07 19:47:28 -040050 #else:
51 # # update existing record
52 # ud = UserDeployments.objects.get(user=user, deployment=expected_deployment)
53 # user_deployments.append(ud)
Tony Macke4be32f2014-03-11 20:45:25 -040054
Tony Mackd4a64292014-04-16 19:53:21 -040055 return UserDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Macke4be32f2014-03-11 20:45:25 -040056
57 def sync_record(self, user_deployment):
Tony Mack69f1bc32014-03-12 13:20:34 -040058 logger.info("sync'ing user %s at deployment %s" % (user_deployment.user, user_deployment.deployment.name))
Tony Macke4be32f2014-03-11 20:45:25 -040059 name = user_deployment.user.email[:user_deployment.user.email.find('@')]
Tony Mackf37bcfd2014-03-12 13:43:53 -040060 user_fields = {'name': user_deployment.user.email,
Tony Macke4be32f2014-03-11 20:45:25 -040061 'email': user_deployment.user.email,
62 'password': hashlib.md5(user_deployment.user.password).hexdigest()[:6],
63 'enabled': True}
64 driver = self.driver.admin_driver(deployment=user_deployment.deployment.name)
65 if not user_deployment.kuser_id:
Tony Mack69f1bc32014-03-12 13:20:34 -040066 keystone_user = driver.create_user(**user_fields)
Tony Macke4be32f2014-03-11 20:45:25 -040067 user_deployment.kuser_id = keystone_user.id
68 else:
69 driver.update_user(user_deployment.kuser_id, user_fields)
70
Tony Mackd1a17e12014-03-19 15:18:15 -040071 # setup user deployment home site roles
Tony Macke4be32f2014-03-11 20:45:25 -040072 if user_deployment.user.site:
73 site_deployments = SiteDeployments.objects.filter(site=user_deployment.user.site,
74 deployment=user_deployment.deployment)
75 if site_deployments:
76 # need the correct tenant id for site at the deployment
77 tenant_id = site_deployments[0].tenant_id
78 driver.add_user_role(user_deployment.kuser_id,
79 tenant_id, 'user')
80 if user_deployment.user.is_admin:
81 driver.add_user_role(user_deployment.kuser_id, tenant_id, 'admin')
82 else:
83 # may have admin role so attempt to remove it
84 driver.delete_user_role(user_deployment.kuser_id, tenant_id, 'admin')
85
Tony Mackd1a17e12014-03-19 15:18:15 -040086 #if user_deployment.user.public_key:
87 # if not user_deployment.user.keyname:
88 # keyname = user_deployment.user.email.lower().replace('@', 'AT').replace('.', '')
89 # user_deployment.user.keyname = keyname
90 # user_deployment.user.save()
91 #
92 # user_driver = driver.client_driver(caller=user_deployment.user,
93 # tenant=user_deployment.user.site.login_base,
94 # deployment=user_deployment.deployment.name)
95 # key_fields = {'name': user_deployment.user.keyname,
96 # 'public_key': user_deployment.user.public_key}
97 # user_driver.create_keypair(**key_fields)
Tony Macke4be32f2014-03-11 20:45:25 -040098
99 user_deployment.save()