Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 3 | from collections import defaultdict |
| 4 | from django.db.models import F, Q |
| 5 | from planetstack.config import Config |
| 6 | from observer.openstacksyncstep import OpenStackSyncStep |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 7 | from core.models.site import Controller, SiteDeployment, SiteDeployment |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 8 | from core.models.user import User |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 9 | from core.models.controllerusers import ControllerUser |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 10 | from util.logger import Logger, logging |
| 11 | |
| 12 | from observer.ansible import * |
| 13 | |
| 14 | logger = Logger(level=logging.INFO) |
| 15 | |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 16 | class SyncControllerUser(OpenStackSyncStep): |
| 17 | provides=[ControllerUser, User] |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 18 | requested_interval=0 |
| 19 | |
| 20 | def fetch_pending(self, deleted): |
| 21 | |
| 22 | if (deleted): |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 23 | return ControllerUser.deleted_objects.all() |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 24 | else: |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 25 | return ControllerUser.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 26 | |
| 27 | def sync_record(self, controller_user): |
| 28 | logger.info("sync'ing user %s at controller %s" % (controller_user.user, controller_user.controller)) |
| 29 | |
| 30 | if not controller_user.controller.admin_user: |
| 31 | logger.info("controller %r has no admin_user, skipping" % controller_user.controller) |
| 32 | return |
| 33 | |
| 34 | template = os_template_env.get_template('sync_controller_users.yaml') |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 35 | |
Tony Mack | cd5fded | 2015-01-03 15:21:50 -0500 | [diff] [blame] | 36 | # All users will have at least the 'user' role at their home site/tenant. |
| 37 | # We must also check if the user should have the admin role |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 38 | roles = ['user'] |
| 39 | if controller_user.user.is_admin: |
| 40 | roles.append('admin') |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 41 | |
| 42 | # setup user home site roles at controller |
| 43 | if not controller_user.user.site: |
Tony Mack | cd5fded | 2015-01-03 15:21:50 -0500 | [diff] [blame] | 44 | raise Exception('Siteless user %s'%controller_user.user.email) |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 45 | else: |
| 46 | # look up tenant id for the user's site at the controller |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 47 | #ctrl_site_deployments = SiteDeployment.objects.filter( |
Tony Mack | cd5fded | 2015-01-03 15:21:50 -0500 | [diff] [blame] | 48 | # site_deployment__site=controller_user.user.site, |
| 49 | # controller=controller_user.controller) |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 50 | |
Tony Mack | cd5fded | 2015-01-03 15:21:50 -0500 | [diff] [blame] | 51 | #if ctrl_site_deployments: |
| 52 | # # need the correct tenant id for site at the controller |
| 53 | # tenant_id = ctrl_site_deployments[0].tenant_id |
| 54 | # tenant_name = ctrl_site_deployments[0].site_deployment.site.login_base |
| 55 | user_fields = { |
| 56 | 'endpoint':controller_user.controller.auth_url, |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 57 | 'name': controller_user.user.email, |
| 58 | 'email': controller_user.user.email, |
Tony Mack | cd5fded | 2015-01-03 15:21:50 -0500 | [diff] [blame] | 59 | 'password': controller_user.user.remote_password, |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 60 | 'admin_user': controller_user.controller.admin_user, |
| 61 | 'admin_password': controller_user.controller.admin_password, |
Sapan Bhatia | b5bf2df | 2014-12-22 01:45:04 -0500 | [diff] [blame] | 62 | 'ansible_tag':'%s@%s'%(controller_user.user.email.replace('@','-at-'),controller_user.controller.name), |
Tony Mack | cd5fded | 2015-01-03 15:21:50 -0500 | [diff] [blame] | 63 | 'admin_tenant': controller_user.controller.admin_tenant, |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 64 | 'roles':roles, |
Tony Mack | cd5fded | 2015-01-03 15:21:50 -0500 | [diff] [blame] | 65 | 'tenant':controller_user.user.site.login_base} |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 66 | |
Tony Mack | cd5fded | 2015-01-03 15:21:50 -0500 | [diff] [blame] | 67 | rendered = template.render(user_fields) |
| 68 | res = run_template('sync_controller_users.yaml', user_fields,path='controller_users') |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 69 | |
Tony Mack | cd5fded | 2015-01-03 15:21:50 -0500 | [diff] [blame] | 70 | # results is an array in which each element corresponds to an |
| 71 | # "ok" string received per operation. If we get as many oks as |
| 72 | # the number of operations we issued, that means a grand success. |
| 73 | # Otherwise, the number of oks tell us which operation failed. |
| 74 | expected_length = len(roles) + 1 |
| 75 | if (len(res)==expected_length): |
| 76 | controller_user.kuser_id = res[0]['id'] |
| 77 | controller_user.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']) |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 82 | |
| 83 | def delete_record(self, controller_user): |
| 84 | if controller_user.kuser_id: |
| 85 | driver = self.driver.admin_driver(controller=controller_user.controller) |
| 86 | driver.delete_user(controller_user.kuser_id) |