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 | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 9 | from core.models.user import User, UserDeployments |
| 10 | from util.logger import Logger, logging |
| 11 | |
| 12 | logger = Logger(level=logging.INFO) |
| 13 | |
| 14 | class 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 Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 21 | |
| 22 | deployments = Deployment.objects.all() |
| 23 | site_deployments = SiteDeployments.objects.all() |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 24 | site_deploy_lookup = defaultdict(list) |
| 25 | for site_deployment in site_deployments: |
| 26 | site_deploy_lookup[site_deployment.site].append(site_deployment.deployment) |
Tony Mack | 07f4976 | 2014-04-07 19:47:28 -0400 | [diff] [blame] | 27 | |
| 28 | user_deploy_lookup = defaultdict(list) |
| 29 | for user_deployment in UserDeployments.objects.all(): |
| 30 | user_deploy_lookup[user_deployment.user].append(user_deployment.deployment) |
| 31 | |
| 32 | user_deployments = [] |
Tony Mack | 1666502 | 2014-04-08 16:38:15 -0400 | [diff] [blame] | 33 | for user in User.objects.all(): |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 34 | if user.is_admin: |
| 35 | # admins should have an account at all deployments |
| 36 | expected_deployments = deployments |
| 37 | else: |
| 38 | # normal users should have an account at their site's deployments |
| 39 | expected_deployments = site_deploy_lookup[user.site] |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 40 | for expected_deployment in expected_deployments: |
Tony Mack | 07f4976 | 2014-04-07 19:47:28 -0400 | [diff] [blame] | 41 | if not user in user_deploy_lookup or \ |
| 42 | expected_deployment not in user_deploy_lookup[user]: |
| 43 | # add new record |
| 44 | ud = UserDeployments(user=user, deployment=expected_deployment) |
| 45 | user_deployments.append(ud) |
| 46 | #else: |
| 47 | # # update existing record |
| 48 | # ud = UserDeployments.objects.get(user=user, deployment=expected_deployment) |
| 49 | # user_deployments.append(ud) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 50 | |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 51 | return user_deployments |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 52 | |
| 53 | def sync_record(self, user_deployment): |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 54 | 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] | 55 | name = user_deployment.user.email[:user_deployment.user.email.find('@')] |
Tony Mack | f37bcfd | 2014-03-12 13:43:53 -0400 | [diff] [blame] | 56 | user_fields = {'name': user_deployment.user.email, |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 57 | 'email': user_deployment.user.email, |
| 58 | 'password': hashlib.md5(user_deployment.user.password).hexdigest()[:6], |
| 59 | 'enabled': True} |
| 60 | driver = self.driver.admin_driver(deployment=user_deployment.deployment.name) |
| 61 | if not user_deployment.kuser_id: |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 62 | keystone_user = driver.create_user(**user_fields) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 63 | user_deployment.kuser_id = keystone_user.id |
| 64 | else: |
| 65 | driver.update_user(user_deployment.kuser_id, user_fields) |
| 66 | |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 67 | # setup user deployment home site roles |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 68 | if user_deployment.user.site: |
| 69 | site_deployments = SiteDeployments.objects.filter(site=user_deployment.user.site, |
| 70 | deployment=user_deployment.deployment) |
| 71 | if site_deployments: |
| 72 | # need the correct tenant id for site at the deployment |
| 73 | tenant_id = site_deployments[0].tenant_id |
| 74 | driver.add_user_role(user_deployment.kuser_id, |
| 75 | tenant_id, 'user') |
| 76 | if user_deployment.user.is_admin: |
| 77 | driver.add_user_role(user_deployment.kuser_id, tenant_id, 'admin') |
| 78 | else: |
| 79 | # may have admin role so attempt to remove it |
| 80 | driver.delete_user_role(user_deployment.kuser_id, tenant_id, 'admin') |
| 81 | |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 82 | #if user_deployment.user.public_key: |
| 83 | # if not user_deployment.user.keyname: |
| 84 | # keyname = user_deployment.user.email.lower().replace('@', 'AT').replace('.', '') |
| 85 | # user_deployment.user.keyname = keyname |
| 86 | # user_deployment.user.save() |
| 87 | # |
| 88 | # user_driver = driver.client_driver(caller=user_deployment.user, |
| 89 | # tenant=user_deployment.user.site.login_base, |
| 90 | # deployment=user_deployment.deployment.name) |
| 91 | # key_fields = {'name': user_deployment.user.keyname, |
| 92 | # 'public_key': user_deployment.user.public_key} |
| 93 | # user_driver.create_keypair(**key_fields) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 94 | |
| 95 | user_deployment.save() |
Tony Mack | 07f4976 | 2014-04-07 19:47:28 -0400 | [diff] [blame] | 96 | user = User.objects.get(id=user_deployment.user.id) |
| 97 | user.save() |