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 | 39e8558 | 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 | 3f87969 | 2014-11-19 15:10:51 -0500 | [diff] [blame] | 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 | |
Sapan Bhatia | 475c597 | 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 | 3f87969 | 2014-11-19 15:10:51 -0500 | [diff] [blame] | 17 | class SyncUserDeployments(OpenStackSyncStep): |
| 18 | provides=[UserDeployments, 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 | 3f87969 | 2014-11-19 15:10:51 -0500 | [diff] [blame] | 24 | return UserDeployments.deleted_objects.all() |
Sapan Bhatia | ef3ae35 | 2014-07-23 09:43:20 -0400 | [diff] [blame] | 25 | else: |
Sapan Bhatia | 3f87969 | 2014-11-19 15:10:51 -0500 | [diff] [blame] | 26 | return UserDeployments.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 | 1e10f06 | 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 | 475c597 | 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 | 475c597 | 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 | 39e8558 | 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 | 475c597 | 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 | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 51 | roles.append('admin') |
| 52 | else: |
Sapan Bhatia | 39e8558 | 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 | 475c597 | 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 | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 58 | user_fields = {'endpoint':user_deployment.deployment.auth_url, |
| 59 | 'name': user_deployment.user.email, |
Sapan | 391eb3e | 2014-12-03 01:01:38 -0500 | [diff] [blame] | 60 | 'ansible_tag': '%s@%s'%(user_deployment.user.email,user_deployment.deployment.name), |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 61 | 'email': user_deployment.user.email, |
| 62 | 'password': hashlib.md5(user_deployment.user.password).hexdigest()[:6], |
| 63 | 'admin_user': user_deployment.deployment.admin_user, |
| 64 | 'admin_password': user_deployment.deployment.admin_password, |
| 65 | 'admin_tenant': 'admin', |
| 66 | 'roles':roles, |
| 67 | 'tenant':tenant_name} |
| 68 | |
| 69 | rendered = template.render(user_fields) |
Sapan | 5641981 | 2014-12-03 01:07:31 -0500 | [diff] [blame] | 70 | res = run_template('sync_user_deployments.yaml', user_fields, 'user_deployments') |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 71 | |
| 72 | # results is an array in which each element corresponds to an |
| 73 | # "ok" string received per operation. If we get as many oks as |
| 74 | # the number of operations we issued, that means a grand success. |
| 75 | # Otherwise, the number of oks tell us which operation failed. |
| 76 | expected_length = len(roles) + 1 |
| 77 | if (len(res)==expected_length): |
| 78 | user_deployment.save() |
| 79 | elif (len(res)): |
| 80 | raise Exception('Could not assign roles for user %s'%user_fields['name']) |
| 81 | else: |
| 82 | raise Exception('Could not create or update user %s'%user_fields['name']) |
Sapan Bhatia | ef3ae35 | 2014-07-23 09:43:20 -0400 | [diff] [blame] | 83 | |
| 84 | def delete_record(self, user_deployment): |
Tony Mack | 413e0d6 | 2014-09-24 10:22:31 -0400 | [diff] [blame] | 85 | if user_deployment.kuser_id: |
Sapan Bhatia | ef3ae35 | 2014-07-23 09:43:20 -0400 | [diff] [blame] | 86 | driver = self.driver.admin_driver(deployment=user_deployment.deployment.name) |
Tony Mack | 413e0d6 | 2014-09-24 10:22:31 -0400 | [diff] [blame] | 87 | driver.delete_user(user_deployment.kuser_id) |