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): |
Sapan Bhatia | ef3ae35 | 2014-07-23 09:43:20 -0400 | [diff] [blame] | 16 | provides=[UserDeployments, User] |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 17 | requested_interval=0 |
| 18 | |
Sapan Bhatia | ef3ae35 | 2014-07-23 09:43:20 -0400 | [diff] [blame] | 19 | def fetch_pending(self, deleted): |
| 20 | |
| 21 | if (deleted): |
| 22 | return UserDeployments.deleted_objects.all() |
Sapan Bhatia | ef3ae35 | 2014-07-23 09:43:20 -0400 | [diff] [blame] | 23 | else: |
Sapan Bhatia | ef3ae35 | 2014-07-23 09:43:20 -0400 | [diff] [blame] | 24 | return UserDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 25 | |
| 26 | def sync_record(self, user_deployment): |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 27 | logger.info("sync'ing user %s at deployment %s" % (user_deployment.user, user_deployment.deployment.name)) |
Scott Baker | 9ae9e4e | 2014-09-04 15:13:40 -0700 | [diff] [blame] | 28 | |
| 29 | if not user_deployment.deployment.admin_user: |
| 30 | logger.info("deployment %r has no admin_user, skipping" % user_deployment.deployment) |
| 31 | return |
| 32 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 33 | name = user_deployment.user.email[:user_deployment.user.email.find('@')] |
Tony Mack | f37bcfd | 2014-03-12 13:43:53 -0400 | [diff] [blame] | 34 | user_fields = {'name': user_deployment.user.email, |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 35 | 'email': user_deployment.user.email, |
| 36 | 'password': hashlib.md5(user_deployment.user.password).hexdigest()[:6], |
| 37 | 'enabled': True} |
| 38 | driver = self.driver.admin_driver(deployment=user_deployment.deployment.name) |
| 39 | if not user_deployment.kuser_id: |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 40 | keystone_user = driver.create_user(**user_fields) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 41 | user_deployment.kuser_id = keystone_user.id |
| 42 | else: |
| 43 | driver.update_user(user_deployment.kuser_id, user_fields) |
| 44 | |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 45 | # setup user deployment home site roles |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 46 | if user_deployment.user.site: |
| 47 | site_deployments = SiteDeployments.objects.filter(site=user_deployment.user.site, |
| 48 | deployment=user_deployment.deployment) |
| 49 | if site_deployments: |
| 50 | # need the correct tenant id for site at the deployment |
| 51 | tenant_id = site_deployments[0].tenant_id |
| 52 | driver.add_user_role(user_deployment.kuser_id, |
| 53 | tenant_id, 'user') |
| 54 | if user_deployment.user.is_admin: |
| 55 | driver.add_user_role(user_deployment.kuser_id, tenant_id, 'admin') |
| 56 | else: |
| 57 | # may have admin role so attempt to remove it |
| 58 | driver.delete_user_role(user_deployment.kuser_id, tenant_id, 'admin') |
| 59 | |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 60 | #if user_deployment.user.public_key: |
| 61 | # if not user_deployment.user.keyname: |
| 62 | # keyname = user_deployment.user.email.lower().replace('@', 'AT').replace('.', '') |
| 63 | # user_deployment.user.keyname = keyname |
| 64 | # user_deployment.user.save() |
| 65 | # |
| 66 | # user_driver = driver.client_driver(caller=user_deployment.user, |
| 67 | # tenant=user_deployment.user.site.login_base, |
| 68 | # deployment=user_deployment.deployment.name) |
| 69 | # key_fields = {'name': user_deployment.user.keyname, |
| 70 | # 'public_key': user_deployment.user.public_key} |
| 71 | # user_driver.create_keypair(**key_fields) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 72 | |
| 73 | user_deployment.save() |
Sapan Bhatia | ef3ae35 | 2014-07-23 09:43:20 -0400 | [diff] [blame] | 74 | |
| 75 | def delete_record(self, user_deployment): |
Tony Mack | 9592528 | 2014-09-24 10:22:31 -0400 | [diff] [blame] | 76 | if user_deployment.kuser_id: |
Sapan Bhatia | ef3ae35 | 2014-07-23 09:43:20 -0400 | [diff] [blame] | 77 | driver = self.driver.admin_driver(deployment=user_deployment.deployment.name) |
Tony Mack | 9592528 | 2014-09-24 10:22:31 -0400 | [diff] [blame] | 78 | driver.delete_user(user_deployment.kuser_id) |
Sapan Bhatia | ef3ae35 | 2014-07-23 09:43:20 -0400 | [diff] [blame] | 79 | |