blob: b9e7b03e11c980045d7b4c79a1eb88977befdfc1 [file] [log] [blame]
Tony Mack336e0f92014-11-30 15:53:08 -05001import os
2import base64
3import hashlib
4from collections import defaultdict
5from django.db.models import F, Q
6from planetstack.config import Config
7from observer.openstacksyncstep import OpenStackSyncStep
Tony Mack528d4222014-12-05 17:13:08 -05008from core.models.site import Controller, SiteDeployments
Tony Mack336e0f92014-11-30 15:53:08 -05009from core.models.user import User
Sapan Bhatia708f2822014-12-19 13:24:00 -050010from core.models.controllerusers import ControllerUsers
Tony Mack336e0f92014-11-30 15:53:08 -050011from util.logger import Logger, logging
12
13from observer.ansible import *
14
15logger = Logger(level=logging.INFO)
16
17class 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
Tony Mack528d4222014-12-05 17:13:08 -050039 roles = ['user']
40 if controller_user.user.is_admin:
41 roles.append('admin')
42 else:
43 raise Exception('Internal error. Missing controller for user %s'%controller_user.user.email)
44
45 # setup user home site roles at controller
46 if not controller_user.user.site:
Tony Mack336e0f92014-11-30 15:53:08 -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
50 ctrl_site_deployments = ControllerSiteDeployments.objects.filter(
51 site_deployment__site=controller_user.user.site,
52 controller=controller_user.controller)
Tony Mack336e0f92014-11-30 15:53:08 -050053
Tony Mack528d4222014-12-05 17:13:08 -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_deployment[0].site_deployment.site.login_base
Tony Mack336e0f92014-11-30 15:53:08 -050058
Tony Mack528d4222014-12-05 17:13:08 -050059 user_fields = {'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,
62 'password': hashlib.md5(controller_user.user.password).hexdigest()[:6],
63 'admin_user': controller_user.controller.admin_user,
64 'admin_password': controller_user.controller.admin_password,
65 'admin_tenant': 'admin',
66 'roles':roles,
67 'tenant':tenant_name}
68
Tony Mack528d4222014-12-05 17:13:08 -050069 rendered = template.render(user_fields)
70 res = run_template('sync_controller_users.yaml', user_fields)
Tony Mack336e0f92014-11-30 15:53:08 -050071
Tony Mack528d4222014-12-05 17:13:08 -050072 # results is an array in which each element corresponds to an
73 # "ok" string received per operation. If we get as many oks as
74 # the number of operations we issued, that means a grand success.
75 # Otherwise, the number of oks tell us which operation failed.
76 expected_length = len(roles) + 1
77 if (len(res)==expected_length):
78 controller_user.kuser_id = res[0]['id']
79 controller_user.save()
80 elif (len(res)):
81 raise Exception('Could not assign roles for user %s'%user_fields['name'])
82 else:
83 raise Exception('Could not create or update user %s'%user_fields['name'])
Tony Mack336e0f92014-11-30 15:53:08 -050084
85 def delete_record(self, controller_user):
86 if controller_user.kuser_id:
87 driver = self.driver.admin_driver(controller=controller_user.controller)
88 driver.delete_user(controller_user.kuser_id)