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 |
Scott Baker | 76a840e | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 5 | from xos.config import Config |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 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 | 2656436 | 2015-01-06 17:49:25 -0500 | [diff] [blame] | 9 | from core.models.controlleruser 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 | 2656436 | 2015-01-06 17:49:25 -0500 | [diff] [blame] | 16 | class SyncControllerUsers(OpenStackSyncStep): |
Sapan Bhatia | b3048aa | 2015-01-27 03:52:19 +0000 | [diff] [blame] | 17 | provides=[User] |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 18 | requested_interval=0 |
Sapan Bhatia | 99f4968 | 2015-01-29 20:58:25 +0000 | [diff] [blame] | 19 | observes=ControllerUser |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 20 | |
| 21 | def fetch_pending(self, deleted): |
| 22 | |
| 23 | if (deleted): |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 24 | return ControllerUser.deleted_objects.all() |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 25 | else: |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 26 | return ControllerUser.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 27 | |
| 28 | def sync_record(self, controller_user): |
| 29 | logger.info("sync'ing user %s at controller %s" % (controller_user.user, controller_user.controller)) |
| 30 | |
| 31 | if not controller_user.controller.admin_user: |
| 32 | logger.info("controller %r has no admin_user, skipping" % controller_user.controller) |
| 33 | return |
| 34 | |
| 35 | template = os_template_env.get_template('sync_controller_users.yaml') |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 36 | |
Tony Mack | cd5fded | 2015-01-03 15:21:50 -0500 | [diff] [blame] | 37 | # All users will have at least the 'user' role at their home site/tenant. |
| 38 | # We must also check if the user should have the admin role |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 39 | roles = ['user'] |
| 40 | if controller_user.user.is_admin: |
Tony Mack | 2656436 | 2015-01-06 17:49:25 -0500 | [diff] [blame] | 41 | roles.append('Admin') |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 42 | |
| 43 | # setup user home site roles at controller |
| 44 | if not controller_user.user.site: |
Tony Mack | cd5fded | 2015-01-03 15:21:50 -0500 | [diff] [blame] | 45 | raise Exception('Siteless user %s'%controller_user.user.email) |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 46 | else: |
| 47 | # look up tenant id for the user's site at the controller |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 48 | #ctrl_site_deployments = SiteDeployment.objects.filter( |
Tony Mack | cd5fded | 2015-01-03 15:21:50 -0500 | [diff] [blame] | 49 | # site_deployment__site=controller_user.user.site, |
| 50 | # controller=controller_user.controller) |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 51 | |
Tony Mack | cd5fded | 2015-01-03 15:21:50 -0500 | [diff] [blame] | 52 | #if ctrl_site_deployments: |
| 53 | # # need the correct tenant id for site at the controller |
| 54 | # tenant_id = ctrl_site_deployments[0].tenant_id |
| 55 | # tenant_name = ctrl_site_deployments[0].site_deployment.site.login_base |
| 56 | user_fields = { |
| 57 | 'endpoint':controller_user.controller.auth_url, |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 58 | 'name': controller_user.user.email, |
| 59 | 'email': controller_user.user.email, |
Tony Mack | cd5fded | 2015-01-03 15:21:50 -0500 | [diff] [blame] | 60 | 'password': controller_user.user.remote_password, |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 61 | 'admin_user': controller_user.controller.admin_user, |
| 62 | 'admin_password': controller_user.controller.admin_password, |
Sapan Bhatia | b5bf2df | 2014-12-22 01:45:04 -0500 | [diff] [blame] | 63 | '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] | 64 | 'admin_tenant': controller_user.controller.admin_tenant, |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 65 | 'roles':roles, |
Tony Mack | cd5fded | 2015-01-03 15:21:50 -0500 | [diff] [blame] | 66 | 'tenant':controller_user.user.site.login_base} |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 67 | |
Tony Mack | cd5fded | 2015-01-03 15:21:50 -0500 | [diff] [blame] | 68 | rendered = template.render(user_fields) |
Tony Mack | cd5fded | 2015-01-03 15:21:50 -0500 | [diff] [blame] | 69 | expected_length = len(roles) + 1 |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 70 | |
| 71 | res = run_template('sync_controller_users.yaml', user_fields,path='controller_users', expected_num=expected_length) |
| 72 | |
| 73 | controller_user.kuser_id = res[0]['id'] |
Sapan Bhatia | 5851db4 | 2015-01-27 03:52:43 +0000 | [diff] [blame] | 74 | controller_user.backend_status = '1 - OK' |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 75 | controller_user.save() |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 76 | |
| 77 | def delete_record(self, controller_user): |
| 78 | if controller_user.kuser_id: |
| 79 | driver = self.driver.admin_driver(controller=controller_user.controller) |
| 80 | driver.delete_user(controller_user.kuser_id) |