blob: 5ff654b940ba1b3de2247d2a88298d8ca8339135 [file] [log] [blame]
Monty Taylorf45f6ca2012-05-01 17:11:48 -04001#! /usr/bin/env python
2# Copyright (C) 2012 OpenStack, LLC.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16# Synchronize Gerrit users from Launchpad.
17# TODO items:
18# 1. add a temporary (instance level) object store for the launchpad class
19# 2. split out the two classes into separate files to be used as a library
20
21import os
22import ConfigParser
23import StringIO
24import paramiko
25import json
26import logging
27import uuid
28from launchpadlib.launchpad import Launchpad
29from launchpadlib.uris import LPNET_SERVICE_ROOT
30
31from datetime import datetime
32
33from openid.consumer import consumer
34from openid.cryptutil import randomString
35
36GERRIT_USER = os.environ.get('GERRIT_USER', 'launchpadsync')
37GERRIT_CONFIG = os.environ.get('GERRIT_CONFIG',
38 '/home/gerrit2/review_site/etc/gerrit.config')
39GERRIT_SECURE_CONFIG = os.environ.get('GERRIT_SECURE_CONFIG',
40 '/home/gerrit2/review_site/etc/secure.config')
41GERRIT_SSH_KEY = os.environ.get('GERRIT_SSH_KEY',
42 '/home/gerrit2/.ssh/launchpadsync_rsa')
43GERRIT_CACHE_DIR = os.path.expanduser(os.environ.get('GERRIT_CACHE_DIR',
44 '~/.launchpadlib/cache'))
45GERRIT_CREDENTIALS = os.path.expanduser(os.environ.get('GERRIT_CREDENTIALS',
46 '~/.launchpadlib/creds'))
47GERRIT_BACKUP_PATH = os.environ.get('GERRIT_BACKUP_PATH',
48 '/home/gerrit2/dbupdates')
49
50logging.basicConfig(format='%(asctime)-6s: %(name)s - %(levelname)s - %(message)s', filename='/var/log/gerrit/update_users.log')
51logger= logging.getLogger('update_users')
52logger.setLevel(logging.INFO)
53
54for check_path in (os.path.dirname(GERRIT_CACHE_DIR),
55 os.path.dirname(GERRIT_CREDENTIALS),
56 GERRIT_BACKUP_PATH):
57 if not os.path.exists(check_path):
58 os.makedirs(check_path)
59
60def get_broken_config(filename):
61 """ gerrit config ini files are broken and have leading tabs """
62 text = ""
63 with open(filename,"r") as conf:
64 for line in conf.readlines():
65 text = "%s%s" % (text, line.lstrip())
66
67 fp = StringIO.StringIO(text)
68 c=ConfigParser.ConfigParser()
69 c.readfp(fp)
70 return c
71
72gerrit_config = get_broken_config(GERRIT_CONFIG)
73secure_config = get_broken_config(GERRIT_SECURE_CONFIG)
74
75DB_USER = gerrit_config.get("database", "username")
76DB_PASS = secure_config.get("database","password")
77DB_DB = gerrit_config.get("database","database")
78
79def make_db_backup():
80 db_backup_file = "%s.%s.sql" % (DB_DB, datetime.isoformat(datetime.now()))
81 db_backup_path = os.path.join(GERRIT_BACKUP_PATH, db_backup_file)
David Shrewsbury54a63902012-05-03 09:27:14 -040082 retval = os.system("mysqldump --opt -u%s -p%s %s | gzip -9 > %s.gz" %
Monty Taylorf45f6ca2012-05-01 17:11:48 -040083 (DB_USER, DB_PASS, DB_DB, db_backup_path))
84 if retval != 0:
85 logger.error("Problem taking a db dump, aborting db update")
86 sys.exit(retval)
87
88class LaunchpadAction(object):
89 def __init__(self):
90 logger.info('Connecting to Launchpad')
91 self.launchpad= Launchpad.login_with('Gerrit User Sync', LPNET_SERVICE_ROOT,
92 GERRIT_CACHE_DIR,
93 credentials_file = GERRIT_CREDENTIALS)
94
95 logger.info('Getting Launchpad teams')
96 self.lp_teams= self.get_all_sub_teams('openstack', [])
97
98 def get_all_sub_teams(self, team, have_teams):
99 for sub_team in self.launchpad.people[team].sub_teams:
100 if sub_team.name not in have_teams:
101 have_teams = self.get_all_sub_teams(sub_team.name, have_teams)
102 have_teams.append(team)
103 return have_teams
104
105 def get_sub_teams(self, team):
106 sub_teams= []
107 for sub_team in self.launchpad.people[team].sub_teams:
108 sub_teams.append(sub_team.name)
109 return sub_teams
110
111 def get_teams(self):
112 return self.lp_teams
113
114 def get_all_users(self):
115 logger.info('Getting Launchpad users')
116 users= []
117 for team in self.lp_teams:
118 for detail in self.launchpad.people[team].members_details:
119 if (detail.status == 'Approved' or detail.status == 'Administrator'):
120 name= detail.self_link.split('/')[-1]
121 if ((users.count(name) == 0) and (name not in self.lp_teams)):
122 users.append(name)
123 return users
124
125 def get_user_data(self, user):
126 return self.launchpad.people[user]
127
128 def get_team_members(self, team, gerrit):
129 users= []
130 for detail in self.launchpad.people[team].members_details:
131 if (detail.status == 'Approved' or detail.status == 'Administrator'):
132 name= detail.self_link.split('/')[-1]
133 # if we found a subteam
134 if name in self.lp_teams:
135 # check subteam for implied subteams
136 for implied_group in gerrit.get_implied_groups(name):
137 if implied_group in self.lp_teams:
138 users.extend(self.get_team_members(implied_group, gerrit))
139 users.extend(self.get_team_members(name, gerrit))
140 continue
141 users.append(name)
142 # check team for implied teams
143 for implied_group in gerrit.get_implied_groups(team):
144 if implied_group in self.lp_teams:
145 users.extend(self.get_team_members(implied_group, gerrit))
146 # filter out dupes
147 users= list(set(users))
148 return users
149
150 def get_team_watches(self, team):
151 users= []
152 for detail in self.launchpad.people[team].members_details:
153 if (detail.status == 'Approved' or detail.status == 'Administrator'):
154 name= detail.self_link.split('/')[-1]
155 if name in self.lp_teams:
156 continue
157 if users.count(name) == 0:
158 users.append(name)
159 return users
160
161 def get_team_display_name(self, team):
162 team_data = self.launchpad.people[team]
163 return team_data.display_name
164
165class GerritAction(object):
166 def __init__(self):
167 logger.info('Connecting to Gerrit')
168 self.ssh= paramiko.SSHClient()
169 self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
170 self.ssh.connect('localhost', username=GERRIT_USER, port=29418, key_filename=GERRIT_SSH_KEY)
171
172 def cleanup(self):
173 logger.info('Closing connection to Gerrit')
174 self.ssh.close()
175
176 def run_query(self, query):
177 command= 'gerrit gsql --format JSON -c "{0}"'.format(query)
178 stdin, stdout, stderr= self.ssh.exec_command(command)
179# trying to get stdout return code or stderr can hang with large result sets
180# for line in stderr:
181# logger.error(line)
182 return stdout
183
184 def get_groups(self):
185 logger.info('Getting Gerrit groups')
186 groups= []
187 query= "select name from account_groups"
188 stdout= self.run_query(query)
189 for line in stdout:
190 row= json.loads(line)
191 if row['type'] == 'row':
192 group= row['columns']['name']
193 groups.append(group)
194 return groups
195
196 def get_users(self):
197 logger.info('Getting Gerrit users')
198 users= []
199 query= "select external_id from account_external_ids"
200 stdout= self.run_query(query)
201 for line in stdout:
202 row= json.loads(line)
203 if row['type'] == 'row':
204 user= row['columns']['external_id'].replace('username:','')
205 users.append(user)
206 return users
207
208 def get_group_id(self, group_name):
209 query= "select group_id from account_groups where name='{0}'".format(group_name)
210 stdout= self.run_query(query)
211 line= stdout.readline()
212 row= json.loads(line)
213 if row['type'] == 'row':
214 return row['columns']['group_id']
215 else:
216 return 0
217
218 def get_user_id(self, user_name):
219 query= "select account_id from account_external_ids where external_id='username:{0}'".format(user_name)
220 stdout= self.run_query(query)
221 line= stdout.readline()
222 row= json.loads(line)
223 return row['columns']['account_id']
224
225 def get_users_from_group(self, group_name):
226 logger.info('Getting Gerrit users from group %s', group_name)
227 users= []
228 gid= self.get_group_id(group_name)
229
230 query= "select external_id from account_external_ids join account_group_members on account_group_members.account_id=account_external_ids.account_id where account_group_members.group_id={0} and external_id like 'username%%'".format(gid)
231 stdout= self.run_query(query)
232 for line in stdout:
233 row= json.loads(line)
234 if row['type'] == 'row':
235 user= row['columns']['external_id'].replace('username:','')
236 users.append(user)
237 return users
238
239 def get_users_from_watches(self, group_name):
240 logger.info('Getting Gerrit users from watch list %s', group_name)
241 users= []
242 if group_name.endswith("-core"):
243 group_name = group_name[:-5]
244 group_name = "openstack/{0}".format(group_name)
245
246 query= "select external_id from account_external_ids join account_project_watches on account_project_watches.account_id=account_external_ids.account_id where account_project_watches.project_name like '{0}' and external_id like 'username%%'".format(group_name)
247 stdout= self.run_query(query)
248 for line in stdout:
249 row= json.loads(line)
250 if row['type'] == 'row':
251 user= row['columns']['external_id'].replace('username:','')
252 users.append(user)
253 return users
254
255
256 def get_implied_groups(self, group_name):
257 gid= self.get_group_id(group_name)
258 groups= []
259 query= "select name from account_groups join account_group_includes on account_group_includes.include_id=account_groups.group_id where account_group_includes.group_id={0}".format(gid)
260 stdout= self.run_query(query)
261 for line in stdout:
262 row= json.loads(line)
263 if row['type'] == 'row':
264 group= row['columns']['name']
265 groups.append(group)
266 return groups
267
268 def add_group(self, group_name, group_display_name):
269 logger.info('New group %s (%s)', group_display_name, group)
270 query= "insert into account_group_id (s) values (NULL)"
271 stdout= self.run_query(query)
272 row= json.loads(stdout.readline())
273 if row['rowCount'] is not 1:
274 print "Could not get a new account group ID"
275 raise
276 query= "select max(s) from account_group_id"
277 stdout= self.run_query(query)
278 row= json.loads(stdout.readline())
279 gid= row['columns']['max(s)']
280 full_uuid= "{0}{1}".format(uuid.uuid4().hex, uuid.uuid4().hex[:8])
281 query= "insert into account_groups (group_id, group_type, owner_group_id, name, description, group_uuid) values ({0}, 'INTERNAL', 1, '{1}', '{2}', '{3}')". format(gid, group_name, group_display_name, full_uuid)
282 self.run_query(query)
283 query= "insert into account_group_names (group_id, name) values ({0}, '{1}')".format(gid, group_name)
284 self.run_query(query)
285
286 def add_user(self, user_name, user_data):
287 logger.info("Adding Gerrit user %s", user_name)
288 openid_consumer = consumer.Consumer(dict(id=randomString(16, '0123456789abcdef')), None)
289 openid_request = openid_consumer.begin("https://launchpad.net/~%s" % user_data.name)
290 user_openid_external_id = openid_request.endpoint.getLocalID()
291 query= "select account_id from account_external_ids where external_id in ('{0}')".format(user_openid_external_id)
292 stdout= self.run_query(query)
293 row= json.loads(stdout.readline())
294 if row['type'] == 'row':
295 # we have a result so this is an updated user name
296 account_id= row['columns']['account_id']
297 query= "update account_external_ids set external_id='{0}' where external_id like 'username%%' and account_id = {1}".format('username:%s' % user_name, account_id)
298 self.run_query(query)
299 else:
300 # we really do have a new user
301 user_ssh_keys= ["%s %s %s" % ('ssh-%s' % key.keytype.lower(), key.keytext, key.comment) for key in user_data.sshkeys]
302 user_email= None
303 try:
304 email = user_data.preferred_email_address.email
305 except ValueError:
306 pass
307 query= "insert into account_id (s) values (NULL)"
308 self.run_query(query)
309 query= "select max(s) from account_id"
310 stdout= self.run_query(query)
311 row= json.loads(stdout.readline())
312 uid= row['columns']['max(s)']
313 query= "insert into accounts (account_id, full_name, preferred_email) values ({0}, '{1}', '{2}')".format(uid, user_name, user_email)
314 self.run_query(query)
315 keyno= 1
316 for key in user_ssh_keys:
317 query= "insert into account_ssh_keys (ssh_public_key, valid, account_id, seq) values ('{0}', 'Y', {1}, {2})".format(key.strip(), uid, keyno)
318 self.run_query(query)
319 keyno = keyno + 1
320 query= "insert into account_external_ids (account_id, email_address, external_id) values ({0}, '{1}', '{2}')".format(uid, user_email, user_openid_external_id)
321 self.run_query(query)
322 query= "insert into account_external_ids (account_id, external_id) values ({0}, '{1}')".format(uid, "username:%s" % user_name)
323 self.run_query(query)
324 if user_email is not None:
325 query= "insert into account_external_ids (account_id, email_address, external_id) values ({0}. '{1}', '{2}')".format(uid, user_email, "mailto:%s" % user_email)
326 return None
327
328 def add_user_to_group(self, user_name, group_name):
329 logger.info("Adding Gerrit user %s to group %s", user_name, group_name)
330 uid= self.get_user_id(user_name)
331 gid= self.get_group_id(group_name)
332 if gid is 0:
333 print "Trying to add user {0} to non-existent group {1}".format(user_name, group_name)
334 raise
335 query= "insert into account_group_members (account_id, group_id) values ({0}, {1})".format(uid, gid)
336 self.run_query(query)
337
338 def add_user_to_watch(self, user_name, group_name):
339 logger.info("Adding Gerrit user %s to watch group %s", user_name, group_name)
340 uid= self.get_user_id(user_name)
341 if group_name.endswith("-core"):
342 group_name = group_name[:-5]
343 group_name = "openstack/{0}".format(group_name)
344 query= "insert into account_project_watches VALUES ('Y', 'N', 'N', {0}, '{1}', '*')". format(uid, group_name)
345 self.run_query(query)
346
347
348 def del_user_from_group(self, user_name, group_name):
349 logger.info("Deleting Gerrit user %s from group %s", user_name, group_name)
350 uid= self.get_user_id(user_name)
351 gid= self.get_group_id(group_name)
352 query= "delete from account_group_members where account_id = {0} and group_id = {1}".format(uid, gid)
353 self.run_query(query)
354 if group_name.endswith("-core"):
355 group_name = group_name[:-5]
356 group_name= "openstack/{0}".format(group_name)
357 query= "delete from account_project_watches where account_id = {0} and project_name= '{1}'".format(uid, group_name)
358 self.run_query(query)
359
360 def rebuild_sub_groups(self, group, sub_groups):
361 gid= self.get_group_id(group)
362 for sub_group in sub_groups:
363 sgid= self.get_group_id(sub_group)
364 query= "select group_id from account_group_includes where group_id={0} and include_id={1}".format(gid, sgid)
365 stdout= self.run_query(query)
366 row= json.loads(stdout.readline())
367 if row['type'] != 'row':
368 logger.info('Adding implied group %s to group %s', group, sub_group)
369 query= "insert into account_group_includes (group_id, include_id) values ({0}, {1})".format(gid, sgid)
370 self.run_query(query)
371
372
373# Actual work starts here!
374
375lp= LaunchpadAction()
376gerrit= GerritAction()
377
378logger.info('Making DB backup')
379make_db_backup()
380
381logger.info('Starting group reconcile')
382lp_groups= lp.get_teams()
383gerrit_groups= gerrit.get_groups()
384
385group_diff= filter(lambda a: a not in gerrit_groups, lp_groups)
386for group in group_diff:
387 group_display_name= lp.get_team_display_name(group)
388 gerrit.add_group(group, group_display_name)
389
390for group in lp_groups:
391 sub_group= lp.get_sub_teams(group)
392 if sub_group:
393 gerrit.rebuild_sub_groups(group, sub_group)
394
395logger.info('End group reconcile')
396
397logger.info('Starting user reconcile')
398lp_users= lp.get_all_users()
399gerrit_users= gerrit.get_users()
400
401user_diff= filter(lambda a: a not in gerrit_users, lp_users)
402for user in user_diff:
403 gerrit.add_user(user, lp.get_user_data(user))
404
405logger.info('End user reconcile')
406
407logger.info('Starting user to group reconcile')
408lp_groups= lp.get_teams()
409for group in lp_groups:
410 # First find users to attach to groups
411 gerrit_group_users= gerrit.get_users_from_group(group)
412 lp_group_users= lp.get_team_members(group, gerrit)
413
414 group_diff= filter(lambda a: a not in gerrit_group_users, lp_group_users)
415 for user in group_diff:
416 gerrit.add_user_to_group(user, group)
417 # Second find users to attach to watches
418 lp_group_watches= lp.get_team_watches(group)
419 gerrit_group_watches= gerrit.get_users_from_watches(group)
420 group_diff= filter(lambda a: a not in gerrit_group_watches, lp_group_watches)
421 for user in group_diff:
422 gerrit.add_user_to_watch(user, group)
423 # Third find users to remove from groups/watches
424 group_diff= filter(lambda a: a not in lp_group_users, gerrit_group_users)
425 for user in group_diff:
426 gerrit.del_user_from_group(user, group)
427
428logger.info('Ending user to group reconcile')
429
430gerrit.cleanup()