Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 3 | import hashlib |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 4 | from collections import defaultdict |
| 5 | from django.db.models import F, Q |
| 6 | from planetstack.config import Config |
| 7 | from observer.openstacksyncstep import OpenStackSyncStep |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 8 | from core.models.site import SiteDeployments, Deployment |
Tony Mack | 91463d9 | 2014-06-19 20:42:03 -0400 | [diff] [blame] | 9 | from core.models.user import User |
| 10 | from core.models.userdeployments import UserDeployments |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 11 | from util.logger import Logger, logging |
| 12 | |
| 13 | logger = Logger(level=logging.INFO) |
| 14 | |
| 15 | class 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 Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 22 | |
| 23 | deployments = Deployment.objects.all() |
| 24 | site_deployments = SiteDeployments.objects.all() |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 25 | site_deploy_lookup = defaultdict(list) |
| 26 | for site_deployment in site_deployments: |
| 27 | site_deploy_lookup[site_deployment.site].append(site_deployment.deployment) |
Tony Mack | 07f4976 | 2014-04-07 19:47:28 -0400 | [diff] [blame] | 28 | |
| 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 Mack | 8002166 | 2014-04-10 12:25:46 -0400 | [diff] [blame] | 33 | all_deployments = Deployment.objects.filter() |
Tony Mack | 1666502 | 2014-04-08 16:38:15 -0400 | [diff] [blame] | 34 | for user in User.objects.all(): |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 35 | 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 Mack | 8002166 | 2014-04-10 12:25:46 -0400 | [diff] [blame] | 40 | #expected_deployments = site_deploy_lookup[user.site] |
| 41 | # users are added to all deployments for now |
| 42 | expected_deployments = deployments |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 43 | for expected_deployment in expected_deployments: |
Tony Mack | 07f4976 | 2014-04-07 19:47:28 -0400 | [diff] [blame] | 44 | 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 Mack | d4a6429 | 2014-04-16 19:53:21 -0400 | [diff] [blame] | 48 | ud.save() |
| 49 | #user_deployments.append(ud) |
Tony Mack | 07f4976 | 2014-04-07 19:47:28 -0400 | [diff] [blame] | 50 | #else: |
| 51 | # # update existing record |
| 52 | # ud = UserDeployments.objects.get(user=user, deployment=expected_deployment) |
| 53 | # user_deployments.append(ud) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 54 | |
Tony Mack | d4a6429 | 2014-04-16 19:53:21 -0400 | [diff] [blame] | 55 | return UserDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 56 | |
| 57 | def sync_record(self, user_deployment): |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 58 | logger.info("sync'ing user %s at deployment %s" % (user_deployment.user, user_deployment.deployment.name)) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 59 | name = user_deployment.user.email[:user_deployment.user.email.find('@')] |
Tony Mack | f37bcfd | 2014-03-12 13:43:53 -0400 | [diff] [blame] | 60 | user_fields = {'name': user_deployment.user.email, |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 61 | '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 Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 66 | keystone_user = driver.create_user(**user_fields) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 67 | user_deployment.kuser_id = keystone_user.id |
| 68 | else: |
| 69 | driver.update_user(user_deployment.kuser_id, user_fields) |
| 70 | |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 71 | # setup user deployment home site roles |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 72 | 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 Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 86 | #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 Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 98 | |
| 99 | user_deployment.save() |