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 |
Sapan Bhatia | b55f4a3 | 2014-11-19 15:07:36 -0500 | [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 |
Sapan Bhatia | bfe716a | 2014-11-11 21:44:35 -0500 | [diff] [blame] | 10 | from core.models.userdeployments import UserDeployment |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 11 | from util.logger import Logger, logging |
| 12 | |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 13 | from observer.ansible import * |
| 14 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 15 | logger = Logger(level=logging.INFO) |
| 16 | |
Sapan Bhatia | bfe716a | 2014-11-11 21:44:35 -0500 | [diff] [blame] | 17 | class SyncUserDeployment(OpenStackSyncStep): |
| 18 | provides=[UserDeployment, User] |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 19 | requested_interval=0 |
| 20 | |
Sapan Bhatia | ef3ae35 | 2014-07-23 09:43:20 -0400 | [diff] [blame] | 21 | def fetch_pending(self, deleted): |
| 22 | |
| 23 | if (deleted): |
Sapan Bhatia | bfe716a | 2014-11-11 21:44:35 -0500 | [diff] [blame] | 24 | return UserDeployment.deleted_objects.all() |
Sapan Bhatia | ef3ae35 | 2014-07-23 09:43:20 -0400 | [diff] [blame] | 25 | else: |
Sapan Bhatia | bfe716a | 2014-11-11 21:44:35 -0500 | [diff] [blame] | 26 | return UserDeployment.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 27 | |
| 28 | def sync_record(self, user_deployment): |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 29 | 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] | 30 | |
| 31 | if not user_deployment.deployment.admin_user: |
| 32 | logger.info("deployment %r has no admin_user, skipping" % user_deployment.deployment) |
| 33 | return |
| 34 | |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 35 | template = os_template_env.get_template('sync_user_deployments.yaml') |
| 36 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 37 | name = user_deployment.user.email[:user_deployment.user.email.find('@')] |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 38 | |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 39 | roles = [] |
| 40 | # setup user deployment home site roles |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 41 | if user_deployment.user.site: |
Sapan Bhatia | b55f4a3 | 2014-11-19 15:07:36 -0500 | [diff] [blame] | 42 | site_deployments = SiteDeployments.objects.filter(site=user_deployment.user.site, |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 43 | deployment=user_deployment.deployment) |
| 44 | if site_deployments: |
| 45 | # need the correct tenant id for site at the deployment |
| 46 | tenant_id = site_deployments[0].tenant_id |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 47 | tenant_name = site_deployments[0].site.login_base |
| 48 | |
| 49 | roles.append('user') |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 50 | if user_deployment.user.is_admin: |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 51 | roles.append('admin') |
| 52 | else: |
Sapan Bhatia | b55f4a3 | 2014-11-19 15:07:36 -0500 | [diff] [blame] | 53 | raise Exception('Internal error. Missing SiteDeployments for user %s'%user_deployment.user.email) |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 54 | else: |
| 55 | raise Exception('Siteless user %s'%user_deployment.user.email) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 56 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 57 | |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 58 | user_fields = {'endpoint':user_deployment.deployment.auth_url, |
| 59 | 'name': user_deployment.user.email, |
| 60 | 'email': user_deployment.user.email, |
| 61 | 'password': hashlib.md5(user_deployment.user.password).hexdigest()[:6], |
| 62 | 'admin_user': user_deployment.deployment.admin_user, |
| 63 | 'admin_password': user_deployment.deployment.admin_password, |
| 64 | 'admin_tenant': 'admin', |
| 65 | 'roles':roles, |
| 66 | 'tenant':tenant_name} |
| 67 | |
| 68 | rendered = template.render(user_fields) |
| 69 | res = run_template('sync_user_deployments.yaml', user_fields) |
| 70 | |
| 71 | # results is an array in which each element corresponds to an |
| 72 | # "ok" string received per operation. If we get as many oks as |
| 73 | # the number of operations we issued, that means a grand success. |
| 74 | # Otherwise, the number of oks tell us which operation failed. |
| 75 | expected_length = len(roles) + 1 |
| 76 | if (len(res)==expected_length): |
| 77 | user_deployment.save() |
| 78 | elif (len(res)): |
| 79 | raise Exception('Could not assign roles for user %s'%user_fields['name']) |
| 80 | else: |
| 81 | raise Exception('Could not create or update user %s'%user_fields['name']) |
Sapan Bhatia | ef3ae35 | 2014-07-23 09:43:20 -0400 | [diff] [blame] | 82 | |
| 83 | def delete_record(self, user_deployment): |
Tony Mack | 9592528 | 2014-09-24 10:22:31 -0400 | [diff] [blame] | 84 | if user_deployment.kuser_id: |
Sapan Bhatia | ef3ae35 | 2014-07-23 09:43:20 -0400 | [diff] [blame] | 85 | driver = self.driver.admin_driver(deployment=user_deployment.deployment.name) |
Tony Mack | 9592528 | 2014-09-24 10:22:31 -0400 | [diff] [blame] | 86 | driver.delete_user(user_deployment.kuser_id) |