blob: 42aae56fdf337bddfbac36e1eb627620382b32cb [file] [log] [blame]
Tony Macke4be32f2014-03-11 20:45:25 -04001import os
2import base64
Tony Mack69f1bc32014-03-12 13:20:34 -04003import hashlib
Tony Macke4be32f2014-03-11 20:45:25 -04004from collections import defaultdict
5from django.db.models import F, Q
6from planetstack.config import Config
7from observer.openstacksyncstep import OpenStackSyncStep
Tony Mack69f1bc32014-03-12 13:20:34 -04008from core.models.site import SiteDeployments, Deployment
Tony Mack91463d92014-06-19 20:42:03 -04009from core.models.user import User
10from core.models.userdeployments import UserDeployments
Tony Macke4be32f2014-03-11 20:45:25 -040011from util.logger import Logger, logging
12
13logger = Logger(level=logging.INFO)
14
15class SyncUserDeployments(OpenStackSyncStep):
Sapan Bhatiaef3ae352014-07-23 09:43:20 -040016 provides=[UserDeployments, User]
Tony Macke4be32f2014-03-11 20:45:25 -040017 requested_interval=0
18
Sapan Bhatiaef3ae352014-07-23 09:43:20 -040019 def fetch_pending(self, deleted):
20
21 if (deleted):
22 return UserDeployments.deleted_objects.all()
Sapan Bhatiaef3ae352014-07-23 09:43:20 -040023 else:
Sapan Bhatiaef3ae352014-07-23 09:43:20 -040024 return UserDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Macke4be32f2014-03-11 20:45:25 -040025
26 def sync_record(self, user_deployment):
Tony Mack69f1bc32014-03-12 13:20:34 -040027 logger.info("sync'ing user %s at deployment %s" % (user_deployment.user, user_deployment.deployment.name))
Tony Macke4be32f2014-03-11 20:45:25 -040028 name = user_deployment.user.email[:user_deployment.user.email.find('@')]
Tony Mackf37bcfd2014-03-12 13:43:53 -040029 user_fields = {'name': user_deployment.user.email,
Tony Macke4be32f2014-03-11 20:45:25 -040030 'email': user_deployment.user.email,
31 'password': hashlib.md5(user_deployment.user.password).hexdigest()[:6],
32 'enabled': True}
33 driver = self.driver.admin_driver(deployment=user_deployment.deployment.name)
34 if not user_deployment.kuser_id:
Tony Mack69f1bc32014-03-12 13:20:34 -040035 keystone_user = driver.create_user(**user_fields)
Tony Macke4be32f2014-03-11 20:45:25 -040036 user_deployment.kuser_id = keystone_user.id
37 else:
38 driver.update_user(user_deployment.kuser_id, user_fields)
39
Tony Mackd1a17e12014-03-19 15:18:15 -040040 # setup user deployment home site roles
Tony Macke4be32f2014-03-11 20:45:25 -040041 if user_deployment.user.site:
42 site_deployments = SiteDeployments.objects.filter(site=user_deployment.user.site,
43 deployment=user_deployment.deployment)
44 if site_deployments:
45 # need the correct tenant id for site at the deployment
46 tenant_id = site_deployments[0].tenant_id
47 driver.add_user_role(user_deployment.kuser_id,
48 tenant_id, 'user')
49 if user_deployment.user.is_admin:
50 driver.add_user_role(user_deployment.kuser_id, tenant_id, 'admin')
51 else:
52 # may have admin role so attempt to remove it
53 driver.delete_user_role(user_deployment.kuser_id, tenant_id, 'admin')
54
Tony Mackd1a17e12014-03-19 15:18:15 -040055 #if user_deployment.user.public_key:
56 # if not user_deployment.user.keyname:
57 # keyname = user_deployment.user.email.lower().replace('@', 'AT').replace('.', '')
58 # user_deployment.user.keyname = keyname
59 # user_deployment.user.save()
60 #
61 # user_driver = driver.client_driver(caller=user_deployment.user,
62 # tenant=user_deployment.user.site.login_base,
63 # deployment=user_deployment.deployment.name)
64 # key_fields = {'name': user_deployment.user.keyname,
65 # 'public_key': user_deployment.user.public_key}
66 # user_driver.create_keypair(**key_fields)
Tony Macke4be32f2014-03-11 20:45:25 -040067
68 user_deployment.save()
Sapan Bhatiaef3ae352014-07-23 09:43:20 -040069
70 def delete_record(self, user_deployment):
71 if user_deployment.user.kuser_id:
72 driver = self.driver.admin_driver(deployment=user_deployment.deployment.name)
73 driver.delete_user(user_deployment.user.kuser_id)
74