Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | import hashlib |
| 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 |
| 8 | from core.models.site import SiteDeployments, Deployment |
| 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. |
| 21 | |
| 22 | deployments = Deployment.objects.all() |
| 23 | site_deployments = SiteDeployments.objects.all() |
| 24 | site_deploy_lookup = defaultdict(list) |
| 25 | for site_deployment in site_deployments: |
| 26 | site_deploy_lookup[site_deployment.site].append(site_deployment.deployment) |
| 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 | all_deployments = Deployment.objects.filter() |
| 33 | for user in User.objects.all(): |
| 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] |
| 40 | # users are added to all deployments for now |
| 41 | expected_deployments = deployments |
| 42 | for expected_deployment in expected_deployments: |
| 43 | if not user in user_deploy_lookup or \ |
| 44 | expected_deployment not in user_deploy_lookup[user]: |
| 45 | # add new record |
| 46 | ud = UserDeployments(user=user, deployment=expected_deployment) |
| 47 | ud.save() |
| 48 | #user_deployments.append(ud) |
| 49 | #else: |
| 50 | # # update existing record |
| 51 | # ud = UserDeployments.objects.get(user=user, deployment=expected_deployment) |
| 52 | # user_deployments.append(ud) |
| 53 | |
| 54 | return UserDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 55 | |
| 56 | def sync_record(self, user_deployment): |
| 57 | logger.info("sync'ing user %s at deployment %s" % (user_deployment.user, user_deployment.deployment.name)) |
| 58 | name = user_deployment.user.email[:user_deployment.user.email.find('@')] |
| 59 | user_fields = {'name': user_deployment.user.email, |
| 60 | 'email': user_deployment.user.email, |
| 61 | 'password': hashlib.md5(user_deployment.user.password).hexdigest()[:6], |
| 62 | 'enabled': True} |
| 63 | driver = self.driver.admin_driver(deployment=user_deployment.deployment.name) |
| 64 | if not user_deployment.kuser_id: |
| 65 | keystone_user = driver.create_user(**user_fields) |
| 66 | user_deployment.kuser_id = keystone_user.id |
| 67 | else: |
| 68 | driver.update_user(user_deployment.kuser_id, user_fields) |
| 69 | |
| 70 | # setup user deployment home site roles |
| 71 | if user_deployment.user.site: |
| 72 | site_deployments = SiteDeployments.objects.filter(site=user_deployment.user.site, |
| 73 | deployment=user_deployment.deployment) |
| 74 | if site_deployments: |
| 75 | # need the correct tenant id for site at the deployment |
| 76 | tenant_id = site_deployments[0].tenant_id |
| 77 | driver.add_user_role(user_deployment.kuser_id, |
| 78 | tenant_id, 'user') |
| 79 | if user_deployment.user.is_admin: |
| 80 | driver.add_user_role(user_deployment.kuser_id, tenant_id, 'admin') |
| 81 | else: |
| 82 | # may have admin role so attempt to remove it |
| 83 | driver.delete_user_role(user_deployment.kuser_id, tenant_id, 'admin') |
| 84 | |
| 85 | #if user_deployment.user.public_key: |
| 86 | # if not user_deployment.user.keyname: |
| 87 | # keyname = user_deployment.user.email.lower().replace('@', 'AT').replace('.', '') |
| 88 | # user_deployment.user.keyname = keyname |
| 89 | # user_deployment.user.save() |
| 90 | # |
| 91 | # user_driver = driver.client_driver(caller=user_deployment.user, |
| 92 | # tenant=user_deployment.user.site.login_base, |
| 93 | # deployment=user_deployment.deployment.name) |
| 94 | # key_fields = {'name': user_deployment.user.keyname, |
| 95 | # 'public_key': user_deployment.user.public_key} |
| 96 | # user_driver.create_keypair(**key_fields) |
| 97 | |
| 98 | user_deployment.save() |