blob: 505ba877e94b8a25043d1f34f213b967475337e1 [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
Sapan Bhatiae6376de2015-05-13 15:51:03 +02007from observer.syncstep import *
Tony Macka7dbd422015-01-05 22:48:11 -05008from core.models.site import Controller, SiteDeployment, SiteDeployment
Tony Mack336e0f92014-11-30 15:53:08 -05009from core.models.user import User
Tony Mack26564362015-01-06 17:49:25 -050010from core.models.controlleruser import ControllerUser
Tony Mack336e0f92014-11-30 15:53:08 -050011from observer.ansible import *
Tony Mack08f82882015-03-29 08:32:21 -040012from util.logger import observer_logger as logger
Sapan Bhatia9028c9a2015-05-09 18:14:40 +020013import json
Tony Mack336e0f92014-11-30 15:53:08 -050014
Tony Mack26564362015-01-06 17:49:25 -050015class SyncControllerUsers(OpenStackSyncStep):
Sapan Bhatiab3048aa2015-01-27 03:52:19 +000016 provides=[User]
Tony Mack336e0f92014-11-30 15:53:08 -050017 requested_interval=0
Sapan Bhatia99f49682015-01-29 20:58:25 +000018 observes=ControllerUser
Tony Mack336e0f92014-11-30 15:53:08 -050019
20 def fetch_pending(self, deleted):
21
22 if (deleted):
Tony Macka7dbd422015-01-05 22:48:11 -050023 return ControllerUser.deleted_objects.all()
Tony Mack336e0f92014-11-30 15:53:08 -050024 else:
Andy Bavier6eb12a42015-08-03 15:55:16 -040025 return ControllerUser.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Mack336e0f92014-11-30 15:53:08 -050026
27 def sync_record(self, controller_user):
28 logger.info("sync'ing user %s at controller %s" % (controller_user.user, controller_user.controller))
29
Andy Bavier6eb12a42015-08-03 15:55:16 -040030 controller_register = json.loads(controller_user.controller.backend_register)
Sapan Bhatia9028c9a2015-05-09 18:14:40 +020031 if (controller_register.get('disabled',False)):
Andy Bavier6eb12a42015-08-03 15:55:16 -040032 raise InnocuousException('Controller %s is disabled'%controller_user.controller.name)
Sapan Bhatia9028c9a2015-05-09 18:14:40 +020033
Tony Mack336e0f92014-11-30 15:53:08 -050034 if not controller_user.controller.admin_user:
35 logger.info("controller %r has no admin_user, skipping" % controller_user.controller)
36 return
37
Andy Bavier6eb12a42015-08-03 15:55:16 -040038 template = os_template_env.get_template('sync_controller_users.yaml')
Tony Mack336e0f92014-11-30 15:53:08 -050039
Tony Mackcd5fded2015-01-03 15:21:50 -050040 # All users will have at least the 'user' role at their home site/tenant.
Andy Bavier6eb12a42015-08-03 15:55:16 -040041 # We must also check if the user should have the admin role
42 roles = ['user']
Tony Mack528d4222014-12-05 17:13:08 -050043 if controller_user.user.is_admin:
Andy Bavier6eb12a42015-08-03 15:55:16 -040044 roles.append('admin')
45
46 # setup user home site roles at controller
Tony Mack528d4222014-12-05 17:13:08 -050047 if not controller_user.user.site:
Tony Mackcd5fded2015-01-03 15:21:50 -050048 raise Exception('Siteless user %s'%controller_user.user.email)
Tony Mack528d4222014-12-05 17:13:08 -050049 else:
50 # look up tenant id for the user's site at the controller
Tony Macka7dbd422015-01-05 22:48:11 -050051 #ctrl_site_deployments = SiteDeployment.objects.filter(
Tony Mackcd5fded2015-01-03 15:21:50 -050052 # site_deployment__site=controller_user.user.site,
53 # controller=controller_user.controller)
Tony Mack336e0f92014-11-30 15:53:08 -050054
Tony Mackcd5fded2015-01-03 15:21:50 -050055 #if ctrl_site_deployments:
56 # # need the correct tenant id for site at the controller
Andy Bavier6eb12a42015-08-03 15:55:16 -040057 # tenant_id = ctrl_site_deployments[0].tenant_id
Tony Mackcd5fded2015-01-03 15:21:50 -050058 # tenant_name = ctrl_site_deployments[0].site_deployment.site.login_base
59 user_fields = {
Andy Bavier6eb12a42015-08-03 15:55:16 -040060 'endpoint':controller_user.controller.auth_url,
Tony Mack09a2f072015-09-14 00:53:39 +000061 'endpoint_v3': controller_user.controller.auth_url_v3,
62 'domain': controller_user.controller.domain,
Andy Bavier6eb12a42015-08-03 15:55:16 -040063 'name': controller_user.user.email,
64 'email': controller_user.user.email,
65 'password': controller_user.user.remote_password,
66 'admin_user': controller_user.controller.admin_user,
67 'admin_password': controller_user.controller.admin_password,
68 'ansible_tag':'%s@%s'%(controller_user.user.email.replace('@','-at-'),controller_user.controller.name),
69 'admin_tenant': controller_user.controller.admin_tenant,
70 'roles':roles,
71 'tenant':controller_user.user.site.login_base
72 }
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000073
Andy Bavier6eb12a42015-08-03 15:55:16 -040074 rendered = template.render(user_fields)
75 expected_length = len(roles) + 1
76
77 res = run_template('sync_controller_users.yaml', user_fields,path='controller_users', expected_num=expected_length)
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000078
79 controller_user.kuser_id = res[0]['id']
Sapan Bhatia5851db42015-01-27 03:52:43 +000080 controller_user.backend_status = '1 - OK'
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000081 controller_user.save()
Tony Mack336e0f92014-11-30 15:53:08 -050082
83 def delete_record(self, controller_user):
Andy Bavier6eb12a42015-08-03 15:55:16 -040084 controller_register = json.loads(controller_user.controller.backend_register)
Sapan Bhatia9028c9a2015-05-09 18:14:40 +020085 if (controller_register.get('disabled',False)):
Andy Bavier6eb12a42015-08-03 15:55:16 -040086 raise InnocuousException('Controller %s is disabled'%controller_user.controller.name)
Sapan Bhatia9028c9a2015-05-09 18:14:40 +020087
Tony Mack336e0f92014-11-30 15:53:08 -050088 if controller_user.kuser_id:
89 driver = self.driver.admin_driver(controller=controller_user.controller)
90 driver.delete_user(controller_user.kuser_id)