Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame^] | 1 | import os |
| 2 | import base64 |
| 3 | import hashlib |
| 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 |
| 8 | from core.models.site import ControllerSites, Controller |
| 9 | from core.models.user import User |
| 10 | from core.models.usercontrollers import ControllerUsers |
| 11 | from util.logger import Logger, logging |
| 12 | |
| 13 | from observer.ansible import * |
| 14 | |
| 15 | logger = Logger(level=logging.INFO) |
| 16 | |
| 17 | class SyncControllerUsers(OpenStackSyncStep): |
| 18 | provides=[ControllerUsers, User] |
| 19 | requested_interval=0 |
| 20 | |
| 21 | def fetch_pending(self, deleted): |
| 22 | |
| 23 | if (deleted): |
| 24 | return ControllerUsers.deleted_objects.all() |
| 25 | else: |
| 26 | return ControllerUsers.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 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') |
| 36 | |
| 37 | name = controller_user.user.email[:controller_user.user.email.find('@')] |
| 38 | |
| 39 | roles = [] |
| 40 | # setup user controller home site roles |
| 41 | if controller_user.user.site: |
| 42 | site_controllers = ControllerSites.objects.filter(site=controller_user.user.site, |
| 43 | controller=controller_user.controller) |
| 44 | if site_controllers: |
| 45 | # need the correct tenant id for site at the controller |
| 46 | tenant_id = site_controllers[0].tenant_id |
| 47 | tenant_name = site_controllers[0].site.login_base |
| 48 | |
| 49 | roles.append('user') |
| 50 | if controller_user.user.is_admin: |
| 51 | roles.append('admin') |
| 52 | else: |
| 53 | raise Exception('Internal error. Missing ControllerSites for user %s'%controller_user.user.email) |
| 54 | else: |
| 55 | raise Exception('Siteless user %s'%controller_user.user.email) |
| 56 | |
| 57 | |
| 58 | user_fields = {'endpoint':controller_user.controller.auth_url, |
| 59 | 'name': controller_user.user.email, |
| 60 | 'email': controller_user.user.email, |
| 61 | 'password': hashlib.md5(controller_user.user.password).hexdigest()[:6], |
| 62 | 'admin_user': controller_user.controller.admin_user, |
| 63 | 'admin_password': controller_user.controller.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_controller_users.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 | 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']) |
| 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) |