blob: 47d1096e94c5e18ab4759749d0ac04c53b634a0c [file] [log] [blame]
Tony Mack336e0f92014-11-30 15:53:08 -05001import os
2import base64
Tony Mack336e0f92014-11-30 15:53:08 -05003from collections import defaultdict
4from django.db.models import F, Q
Scott Baker76a840e2015-02-11 21:38:09 -08005from xos.config import Config
Tony Mack336e0f92014-11-30 15:53:08 -05006from observer.openstacksyncstep import OpenStackSyncStep
Tony Macka7dbd422015-01-05 22:48:11 -05007from core.models.site import Controller, SiteDeployment, SiteDeployment
Tony Mack336e0f92014-11-30 15:53:08 -05008from core.models.user import User
Tony Mack26564362015-01-06 17:49:25 -05009from core.models.controlleruser import ControllerUser
Tony Mack336e0f92014-11-30 15:53:08 -050010from observer.ansible import *
Tony Mack08f82882015-03-29 08:32:21 -040011from util.logger import observer_logger as logger
Sapan Bhatia9028c9a2015-05-09 18:14:40 +020012import json
Tony Mack336e0f92014-11-30 15:53:08 -050013
Tony Mack26564362015-01-06 17:49:25 -050014class SyncControllerUsers(OpenStackSyncStep):
Sapan Bhatiab3048aa2015-01-27 03:52:19 +000015 provides=[User]
Tony Mack336e0f92014-11-30 15:53:08 -050016 requested_interval=0
Sapan Bhatia99f49682015-01-29 20:58:25 +000017 observes=ControllerUser
Tony Mack336e0f92014-11-30 15:53:08 -050018
19 def fetch_pending(self, deleted):
20
21 if (deleted):
Tony Macka7dbd422015-01-05 22:48:11 -050022 return ControllerUser.deleted_objects.all()
Tony Mack336e0f92014-11-30 15:53:08 -050023 else:
Tony Macka7dbd422015-01-05 22:48:11 -050024 return ControllerUser.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Mack336e0f92014-11-30 15:53:08 -050025
26 def sync_record(self, controller_user):
27 logger.info("sync'ing user %s at controller %s" % (controller_user.user, controller_user.controller))
28
Sapan Bhatia9028c9a2015-05-09 18:14:40 +020029 controller_register = json.loads(controller_user.controller.backend_register)
30 if (controller_register.get('disabled',False)):
31 raise Exception('Controller %s is disabled'%controller_user.controller.name)
32
Tony Mack336e0f92014-11-30 15:53:08 -050033 if not controller_user.controller.admin_user:
34 logger.info("controller %r has no admin_user, skipping" % controller_user.controller)
35 return
36
37 template = os_template_env.get_template('sync_controller_users.yaml')
Tony Mack336e0f92014-11-30 15:53:08 -050038
Tony Mackcd5fded2015-01-03 15:21:50 -050039 # All users will have at least the 'user' role at their home site/tenant.
40 # We must also check if the user should have the admin role
Tony Mack528d4222014-12-05 17:13:08 -050041 roles = ['user']
42 if controller_user.user.is_admin:
Tony Mack26564362015-01-06 17:49:25 -050043 roles.append('Admin')
Tony Mack528d4222014-12-05 17:13:08 -050044
45 # setup user home site roles at controller
46 if not controller_user.user.site:
Tony Mackcd5fded2015-01-03 15:21:50 -050047 raise Exception('Siteless user %s'%controller_user.user.email)
Tony Mack528d4222014-12-05 17:13:08 -050048 else:
49 # look up tenant id for the user's site at the controller
Tony Macka7dbd422015-01-05 22:48:11 -050050 #ctrl_site_deployments = SiteDeployment.objects.filter(
Tony Mackcd5fded2015-01-03 15:21:50 -050051 # site_deployment__site=controller_user.user.site,
52 # controller=controller_user.controller)
Tony Mack336e0f92014-11-30 15:53:08 -050053
Tony Mackcd5fded2015-01-03 15:21:50 -050054 #if ctrl_site_deployments:
55 # # need the correct tenant id for site at the controller
56 # tenant_id = ctrl_site_deployments[0].tenant_id
57 # tenant_name = ctrl_site_deployments[0].site_deployment.site.login_base
58 user_fields = {
59 'endpoint':controller_user.controller.auth_url,
Tony Mack336e0f92014-11-30 15:53:08 -050060 'name': controller_user.user.email,
61 'email': controller_user.user.email,
Tony Mackcd5fded2015-01-03 15:21:50 -050062 'password': controller_user.user.remote_password,
Tony Mack336e0f92014-11-30 15:53:08 -050063 'admin_user': controller_user.controller.admin_user,
64 'admin_password': controller_user.controller.admin_password,
Sapan Bhatiab5bf2df2014-12-22 01:45:04 -050065 'ansible_tag':'%s@%s'%(controller_user.user.email.replace('@','-at-'),controller_user.controller.name),
Tony Mackcd5fded2015-01-03 15:21:50 -050066 'admin_tenant': controller_user.controller.admin_tenant,
Tony Mack336e0f92014-11-30 15:53:08 -050067 'roles':roles,
Tony Mackcd5fded2015-01-03 15:21:50 -050068 'tenant':controller_user.user.site.login_base}
Tony Mack336e0f92014-11-30 15:53:08 -050069
Tony Mackcd5fded2015-01-03 15:21:50 -050070 rendered = template.render(user_fields)
Tony Mackcd5fded2015-01-03 15:21:50 -050071 expected_length = len(roles) + 1
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000072
73 res = run_template('sync_controller_users.yaml', user_fields,path='controller_users', expected_num=expected_length)
74
75 controller_user.kuser_id = res[0]['id']
Sapan Bhatia5851db42015-01-27 03:52:43 +000076 controller_user.backend_status = '1 - OK'
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000077 controller_user.save()
Tony Mack336e0f92014-11-30 15:53:08 -050078
79 def delete_record(self, controller_user):
Sapan Bhatia9028c9a2015-05-09 18:14:40 +020080 controller_register = json.loads(controller_user.controller.backend_register)
81 if (controller_register.get('disabled',False)):
82 raise Exception('Controller %s is disabled'%controller_user.controller.name)
83
Tony Mack336e0f92014-11-30 15:53:08 -050084 if controller_user.kuser_id:
85 driver = self.driver.admin_driver(controller=controller_user.controller)
86 driver.delete_user(controller_user.kuser_id)