Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | ''' |
| 3 | Module for handling gerrit calls. |
| 4 | |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 5 | :optdepends: - gerritlib/pygerrit Python adapter |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 6 | :configuration: This module is not usable until the following are specified |
| 7 | either in a pillar or in the minion's config file:: |
| 8 | |
| 9 | gerrit.host: localhost |
| 10 | gerrit.user: admin |
| 11 | gerrit.key: | |
| 12 | -----BEGIN RSA PRIVATE KEY----- |
| 13 | MIIEowIBAAKCAQEAs0Y8mxS3dfs5zG8Du5vdBkfOCOng1IEUmFZIirJ8oBgJOd54 |
| 14 | ... |
| 15 | jvMXms60iD/A5OpG33LWHNNzQBP486SxG75LB+Xs5sp5j2/b7VF5LJLhpGiJv9Mk |
| 16 | ydbuy8iuuvali2uF133kAlLqnrWfVTYQQI1OfW5glOv1L6kv94dU |
| 17 | -----END RSA PRIVATE KEY----- |
| 18 | |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 19 | Examples: |
| 20 | - gerrit_account: |
| 21 | username: Jenkins |
| 22 | fullname: Jenkins continuous integration tool |
| 23 | email: admin@example.com |
| 24 | groups: |
| 25 | - Non-Interactive Users |
| 26 | - Testers |
| 27 | gerrit_url: http://gerrit.example.com:8080/ |
| 28 | gerrit_admin_username: dicky |
| 29 | gerrit_admin_password: b0sst0nes |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 30 | ''' |
| 31 | |
| 32 | from __future__ import absolute_import |
| 33 | |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 34 | import json |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 35 | import logging |
| 36 | import os |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 37 | import urllib |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 38 | import requests.auth |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 39 | |
| 40 | LOG = logging.getLogger(__name__) |
| 41 | |
| 42 | # Import third party libs |
| 43 | HAS_GERRIT = False |
| 44 | try: |
| 45 | from gerritlib import gerrit |
Filip Pytloun | e8ce1f8 | 2018-01-10 13:41:12 +0100 | [diff] [blame] | 46 | try: |
Filip Pytloun | 323536e | 2018-01-10 18:14:20 +0100 | [diff] [blame] | 47 | from pygerrit2 import rest as pygerrit |
Filip Pytloun | e8ce1f8 | 2018-01-10 13:41:12 +0100 | [diff] [blame] | 48 | except ImportError: |
Filip Pytloun | 323536e | 2018-01-10 18:14:20 +0100 | [diff] [blame] | 49 | from pygerrit import rest as pygerrit |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 50 | HAS_GERRIT = True |
| 51 | except ImportError: |
| 52 | pass |
| 53 | |
| 54 | |
| 55 | def __virtual__(): |
| 56 | ''' |
| 57 | Only load this module if gerrit |
| 58 | is installed on this minion. |
| 59 | ''' |
| 60 | if HAS_GERRIT: |
| 61 | return 'gerrit' |
| 62 | return False |
| 63 | |
| 64 | __opts__ = {} |
| 65 | |
| 66 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 67 | # Common functions |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 68 | |
| 69 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 70 | def _get_boolean(gerrit, path): |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 71 | response = gerrit.get(path) |
| 72 | if response == 'ok': |
| 73 | value = True |
| 74 | elif response == '': |
| 75 | value = False |
| 76 | else: |
Filip Pytloun | 512baf4 | 2017-03-09 22:14:00 +0100 | [diff] [blame] | 77 | raise Exception( |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 78 | "Unexpected response for %s: %s" % (path, response)) |
| 79 | return value |
| 80 | |
| 81 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 82 | def _get_list(gerrit, path): |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 83 | values = gerrit.get(path) |
| 84 | return values |
| 85 | |
| 86 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 87 | def _get_string(gerrit, path): |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 88 | try: |
| 89 | value = gerrit.get(path) |
Filip Pytloun | 9c1b92f | 2017-02-28 19:26:49 +0100 | [diff] [blame] | 90 | except Exception as e: |
Filip Pytloun | 693ebbe | 2017-03-21 12:55:09 +0100 | [diff] [blame] | 91 | try: |
| 92 | if e.response.status_code == 404: |
| 93 | logging.debug("Ignoring exception %s", e) |
| 94 | logging.debug("Got %s", e.response.__dict__) |
| 95 | value = None |
| 96 | else: |
| 97 | raise |
| 98 | except Exception: |
| 99 | raise e |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 100 | return value |
| 101 | |
| 102 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 103 | def _set_boolean(gerrit, path, value): |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 104 | if value: |
| 105 | gerrit.put(path) |
| 106 | else: |
| 107 | gerrit.delete(path) |
| 108 | |
| 109 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 110 | def _set_string(gerrit, path, value, field_name=None): |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 111 | field_name = field_name or os.path.basename(path) |
| 112 | |
| 113 | # Setting to '' is equivalent to deleting, so we have no need for the |
| 114 | # DELETE method. |
| 115 | headers = {'content-type': 'application/json'} |
| 116 | data = json.dumps({field_name: value}) |
| 117 | gerrit.put(path, data=data, headers=headers) |
| 118 | |
| 119 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 120 | def _maybe_update_field(gerrit, path, field, gerrit_value, salt_value, |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 121 | type='str', gerrit_api_path=None): |
| 122 | |
| 123 | gerrit_api_path = gerrit_api_path or field |
| 124 | fullpath = path + '/' + gerrit_api_path |
| 125 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 126 | if gerrit_value == salt_value: |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 127 | logging.info("Not updating %s: same value specified: %s", fullpath, |
| 128 | gerrit_value) |
| 129 | value = gerrit_value |
| 130 | changed = False |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 131 | elif salt_value is None: |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 132 | logging.info("Not updating %s: no value specified, value stays as %s", |
| 133 | fullpath, gerrit_value) |
| 134 | value = gerrit_value |
| 135 | changed = False |
| 136 | else: |
| 137 | logging.info("Changing %s from %s to %s", fullpath, gerrit_value, |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 138 | salt_value) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 139 | if type == 'str': |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 140 | _set_string(gerrit, fullpath, salt_value, field_name=field) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 141 | elif type == 'bool': |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 142 | _set_boolean(gerrit, fullpath, salt_value) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 143 | else: |
| 144 | raise AssertionError("Unknown Ansible parameter type '%s'" % type) |
| 145 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 146 | value = salt_value |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 147 | changed = True |
| 148 | return value, changed |
| 149 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 150 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 151 | def _quote(name): |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 152 | return urllib.quote(name, safe="") |
| 153 | |
| 154 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 155 | def _account_name2id(gerrit, name=None): |
| 156 | # Although we could pass an AccountInput entry here to set details in one |
| 157 | # go, it's left up to the _update_group() function, to avoid having a |
| 158 | # totally separate code path for create vs. update. |
| 159 | info = gerrit.get('/accounts/%s' % _quote(name)) |
| 160 | return info['_account_id'] |
| 161 | |
| 162 | |
| 163 | def _group_name2id(gerrit, name=None): |
| 164 | # Although we could pass an AccountInput entry here to set details in one |
| 165 | # go, it's left up to the _update_group() function, to avoid having a |
| 166 | # totally separate code path for create vs. update. |
| 167 | info = gerrit.get('/groups/%s' % _quote(name)) |
| 168 | return info['id'] |
| 169 | |
| 170 | |
| 171 | def _create_group(gerrit, name=None): |
| 172 | # Although we could pass an AccountInput entry here to set details in one |
| 173 | # go, it's left up to the _update_group() function, to avoid having a |
| 174 | # totally separate code path for create vs. update. |
| 175 | group_info = gerrit.put('/groups/%s' % _quote(name)) |
| 176 | return group_info |
| 177 | |
| 178 | |
| 179 | def _create_account(gerrit, username=None): |
| 180 | # Although we could pass an AccountInput entry here to set details in one |
| 181 | # go, it's left up to the _update_account() function, to avoid having a |
| 182 | # totally separate code path for create vs. update. |
| 183 | account_info = gerrit.put('/accounts/%s' % _quote(username)) |
| 184 | return account_info |
| 185 | |
| 186 | |
| 187 | def _create_account_email(gerrit, account_id, email, preferred=False, |
| 188 | no_confirmation=False): |
| 189 | logging.info('Creating email %s for account %s', email, account_id) |
| 190 | |
| 191 | email_input = { |
| 192 | # Setting 'email' is optional (it's already in the URL) but it's good |
| 193 | # to double check that the email is encoded in the URL properly. |
| 194 | 'email': email, |
| 195 | 'preferred': preferred, |
| 196 | 'no_confirmation': no_confirmation, |
| 197 | } |
| 198 | logging.debug(email_input) |
| 199 | |
| 200 | path = 'accounts/%s/emails/%s' % (account_id, _quote(email)) |
| 201 | headers = {'content-type': 'application/json'} |
| 202 | gerrit.post(path, data=json.dumps(email_input), headers=headers) |
| 203 | |
| 204 | |
| 205 | def _create_account_ssh_key(gerrit, account_id, ssh_public_key): |
| 206 | logging.info('Creating SSH key %s for account %s', ssh_public_key, |
| 207 | account_id) |
| 208 | |
| 209 | import requests |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 210 | |
| 211 | path = 'accounts/%s/sshkeys' % (account_id) |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 212 | |
| 213 | kwargs = { |
| 214 | "data": ssh_public_key |
| 215 | } |
| 216 | kwargs.update(gerrit.kwargs.copy()) |
| 217 | |
Filip Pytloun | da91075 | 2017-08-03 12:13:57 +0200 | [diff] [blame] | 218 | requests.post(gerrit.make_url(path), **kwargs) |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 219 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 220 | |
| 221 | def _create_group_membership(gerrit, account_id, group_id): |
| 222 | logging.info('Creating membership of %s in group %s', account_id, group_id) |
| 223 | # group_id = _group_name2id(gerrit, group_id) |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 224 | path = 'groups/%s/members/%s' % (_quote(group_id), account_id) |
| 225 | gerrit.put(path, data=json.dumps({})) |
| 226 | |
| 227 | |
| 228 | def _ensure_only_member_of_these_groups(gerrit, account_id, salt_groups): |
| 229 | path = 'accounts/%s' % account_id |
| 230 | group_info_list = _get_list(gerrit, path + '/groups') |
| 231 | |
| 232 | changed = False |
| 233 | gerrit_groups = [] |
| 234 | for group_info in group_info_list: |
| 235 | if group_info['name'] in salt_groups: |
| 236 | logging.info("Preserving %s membership of group %s", path, |
| 237 | group_info) |
| 238 | gerrit_groups.append(group_info['name']) |
| 239 | else: |
| 240 | logging.info("Removing %s from group %s", path, group_info) |
| 241 | membership_path = 'groups/%s/members/%s' % ( |
| 242 | _quote(group_info['id']), account_id) |
| 243 | try: |
| 244 | gerrit.delete(membership_path) |
| 245 | changed = True |
Filip Pytloun | 9c1b92f | 2017-02-28 19:26:49 +0100 | [diff] [blame] | 246 | except Exception as e: |
Filip Pytloun | 693ebbe | 2017-03-21 12:55:09 +0100 | [diff] [blame] | 247 | try: |
| 248 | if e.response.status_code == 404: |
| 249 | # This is a kludge, it'd be better to work out in advance |
| 250 | # which groups the user is a member of only via membership |
| 251 | # in a different. That's not trivial though with the |
| 252 | # current API Gerrit provides. |
| 253 | logging.info( |
| 254 | "Ignored %s; assuming membership of this group is due " |
| 255 | "to membership of a group that includes it.", e) |
| 256 | else: |
| 257 | raise |
| 258 | except Exception: |
| 259 | raise e |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 260 | |
| 261 | # If the user gave group IDs instead of group names, this will |
| 262 | # needlessly recreate the membership. The only actual issue will be that |
| 263 | # Ansible reports 'changed' when nothing really did change, I think. |
| 264 | # |
| 265 | # We might receive [""] when the user tries to pass in an empty list, so |
| 266 | # handle that. |
| 267 | for new_group in set(salt_groups).difference(gerrit_groups): |
| 268 | if len(new_group) > 0: |
| 269 | _create_group_membership(gerrit, account_id, new_group) |
| 270 | gerrit_groups.append(new_group) |
| 271 | changed = True |
| 272 | |
| 273 | return gerrit_groups, changed |
| 274 | |
| 275 | |
| 276 | def _ensure_only_one_account_email(gerrit, account_id, email): |
| 277 | path = 'accounts/%s' % account_id |
| 278 | email_info_list = _get_list(gerrit, path + '/emails') |
| 279 | |
| 280 | changed = False |
| 281 | found_email = False |
| 282 | for email_info in email_info_list: |
| 283 | existing_email = email_info['email'] |
| 284 | if existing_email == email: |
| 285 | # Since we're deleting all emails except this one, there's no need |
| 286 | # to care whether it's the 'preferred' one. It soon will be! |
| 287 | logging.info("Keeping %s email %s", path, email) |
| 288 | found_email = True |
| 289 | else: |
| 290 | logging.info("Removing %s email %s", path, existing_email) |
| 291 | gerrit.delete(path + '/emails/%s' % _quote(existing_email)) |
| 292 | changed = True |
| 293 | |
| 294 | if len(email) > 0 and not found_email: |
| 295 | _create_account_email(gerrit, account_id, email, |
| 296 | preferred=True, no_confirmation=True) |
| 297 | changed = True |
| 298 | |
| 299 | return email, changed |
| 300 | |
| 301 | |
| 302 | def _ensure_only_one_account_ssh_key(gerrit, account_id, ssh_public_key): |
| 303 | path = 'accounts/%s' % account_id |
| 304 | ssh_key_info_list = _get_list(gerrit, path + '/sshkeys') |
| 305 | |
| 306 | changed = False |
| 307 | found_ssh_key = False |
| 308 | for ssh_key_info in ssh_key_info_list: |
| 309 | if ssh_key_info['ssh_public_key'] == ssh_public_key: |
| 310 | logging.info("Keeping %s SSH key %s", path, ssh_key_info) |
| 311 | found_ssh_key = True |
| 312 | else: |
| 313 | logging.info("Removing %s SSH key %s", path, ssh_key_info) |
| 314 | gerrit.delete(path + '/sshkeys/%i' % ssh_key_info['seq']) |
| 315 | changed = True |
| 316 | |
| 317 | if len(ssh_public_key) > 0 and not found_ssh_key: |
| 318 | _create_account_ssh_key(gerrit, account_id, ssh_public_key) |
| 319 | changed = True |
| 320 | |
| 321 | return ssh_public_key, changed |
| 322 | |
| 323 | |
| 324 | def _update_account(gerrit, username=None, **params): |
| 325 | change = False |
| 326 | |
| 327 | try: |
| 328 | account_info = gerrit.get('/accounts/%s' % _quote(username)) |
Filip Pytloun | 9c1b92f | 2017-02-28 19:26:49 +0100 | [diff] [blame] | 329 | except Exception as e: |
Filip Pytloun | 693ebbe | 2017-03-21 12:55:09 +0100 | [diff] [blame] | 330 | try: |
| 331 | if e.response.status_code == 404: |
| 332 | logging.info("Account %s not found, creating it.", username) |
| 333 | account_info = _create_account(gerrit, username) |
| 334 | change = True |
| 335 | else: |
| 336 | raise |
| 337 | except Exception: |
| 338 | raise e |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 339 | |
| 340 | logging.debug( |
| 341 | 'Existing account info for account %s: %s', username, |
| 342 | json.dumps(account_info, indent=4)) |
| 343 | |
| 344 | account_id = account_info['_account_id'] |
| 345 | path = 'accounts/%s' % account_id |
| 346 | |
| 347 | output = {} |
| 348 | output['username'] = username |
| 349 | output['id'] = account_id |
| 350 | |
| 351 | fullname, fullname_changed = _maybe_update_field( |
| 352 | gerrit, path, 'name', account_info.get('name'), params.get('fullname')) |
| 353 | output['fullname'] = fullname |
| 354 | change |= fullname_changed |
| 355 | |
| 356 | # Set the value of params that the user did not provide to None. |
| 357 | |
| 358 | if params.get('active') is not None: |
| 359 | active = _get_boolean(gerrit, path + '/active') |
| 360 | active, active_changed = _maybe_update_field( |
| 361 | gerrit, path, 'active', active, params['active'], type='bool') |
| 362 | output['active'] = active |
| 363 | change |= active_changed |
| 364 | |
| 365 | if params.get('email') is not None: |
| 366 | email, emails_changed = _ensure_only_one_account_email( |
| 367 | gerrit, account_id, params['email']) |
| 368 | output['email'] = email |
| 369 | change |= emails_changed |
| 370 | |
Filip Pytloun | 1a521b3 | 2017-03-28 12:06:50 +0200 | [diff] [blame] | 371 | if params.get('groups'): |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 372 | groups, groups_changed = _ensure_only_member_of_these_groups( |
| 373 | gerrit, account_info.get('name'), params['groups']) |
| 374 | output['groups'] = groups |
| 375 | change |= groups_changed |
| 376 | |
| 377 | if params.get('http_password') is not None: |
| 378 | http_password = _get_string(gerrit, path + '/password.http') |
| 379 | http_password, http_password_changed = _maybe_update_field( |
| 380 | gerrit, path, 'http_password', http_password, |
| 381 | params.get('http_password'), |
| 382 | gerrit_api_path='password.http') |
| 383 | output['http_password'] = http_password |
| 384 | change |= http_password_changed |
| 385 | |
| 386 | if params.get('ssh_key') is not None: |
| 387 | ssh_key, ssh_keys_changed = _ensure_only_one_account_ssh_key( |
Filip Pytloun | da91075 | 2017-08-03 12:13:57 +0200 | [diff] [blame] | 388 | gerrit, account_id, params['ssh_key']) |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 389 | output['ssh_key'] = ssh_key |
| 390 | change |= ssh_keys_changed |
| 391 | |
| 392 | return output, change |
| 393 | |
| 394 | |
| 395 | def _update_group(gerrit, name=None, **params): |
| 396 | change = False |
| 397 | |
| 398 | try: |
| 399 | group_info = gerrit.get('/groups/%s' % _quote(name)) |
Filip Pytloun | 9c1b92f | 2017-02-28 19:26:49 +0100 | [diff] [blame] | 400 | except Exception as e: |
Filip Pytloun | 693ebbe | 2017-03-21 12:55:09 +0100 | [diff] [blame] | 401 | try: |
| 402 | if e.response.status_code == 404: |
| 403 | logging.info("Group %s not found, creating it.", name) |
| 404 | group_info = _create_group(gerrit, name) |
| 405 | change = True |
| 406 | else: |
| 407 | raise |
| 408 | except Exception: |
| 409 | raise e |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 410 | |
| 411 | logging.debug( |
| 412 | 'Existing info for group %s: %s', name, |
| 413 | json.dumps(group_info, indent=4)) |
| 414 | |
| 415 | output = {group_info['name']: group_info} |
| 416 | |
| 417 | return output, change |
| 418 | |
| 419 | |
| 420 | # Gerrit client connectors |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 421 | |
| 422 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 423 | def _gerrit_ssh_connection(**connection_args): |
| 424 | ''' |
| 425 | Set up gerrit credentials |
| 426 | |
| 427 | Only intended to be used within gerrit-enabled modules |
| 428 | ''' |
| 429 | |
| 430 | prefix = "gerrit" |
| 431 | |
| 432 | # look in connection_args first, then default to config file |
| 433 | def get(key, default=None): |
| 434 | return connection_args.get('connection_' + key, |
| 435 | __salt__['config.get'](prefix, {})).get(key, default) |
| 436 | |
| 437 | host = get('host', 'localhost') |
| 438 | user = get('user', 'admin') |
| 439 | keyfile = get('keyfile', '/var/cache/salt/minion/gerrit_rsa') |
| 440 | |
| 441 | gerrit_client = gerrit.Gerrit(host, user, keyfile=keyfile) |
| 442 | |
| 443 | return gerrit_client |
| 444 | |
| 445 | |
| 446 | def _gerrit_http_connection(**connection_args): |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 447 | |
| 448 | prefix = "gerrit" |
| 449 | |
| 450 | # look in connection_args first, then default to config file |
| 451 | def get(key, default=None): |
| 452 | return connection_args.get( |
| 453 | 'connection_' + key, |
| 454 | __salt__['config.get'](prefix, {})).get(key, default) |
| 455 | |
| 456 | host = get('host', 'localhost') |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 457 | http_port = get('http_port', '8082') |
| 458 | protocol = get('protocol', 'http') |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 459 | username = get('user', 'admin') |
| 460 | password = get('password', 'admin') |
Filip Pytloun | b530616 | 2017-03-28 09:51:06 +0200 | [diff] [blame] | 461 | auth_method = get('auth_method', 'digest') |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 462 | |
Filip Pytloun | da91075 | 2017-08-03 12:13:57 +0200 | [diff] [blame] | 463 | url = protocol + "://" + str(host) + ':' + str(http_port) |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 464 | |
Filip Pytloun | b530616 | 2017-03-28 09:51:06 +0200 | [diff] [blame] | 465 | if auth_method == 'digest': |
| 466 | auth = requests.auth.HTTPDigestAuth(username, password) |
| 467 | elif auth_method == 'basic': |
| 468 | auth = requests.auth.HTTPBasicAuth(username, password) |
| 469 | else: |
| 470 | raise Exception("Unknown auth_method %s" % auth_method) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 471 | |
Filip Pytloun | 323536e | 2018-01-10 18:14:20 +0100 | [diff] [blame] | 472 | gerrit = pygerrit.GerritRestAPI( |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 473 | url=url, auth=auth) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 474 | |
| 475 | return gerrit |
| 476 | |
| 477 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 478 | # Salt modules |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 479 | |
| 480 | |
| 481 | def account_create(username, fullname=None, email=None, active=None, groups=[], ssh_key=None, http_password=None, **kwargs): |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 482 | ''' |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 483 | Create a gerrit account |
| 484 | |
| 485 | :param username: username |
| 486 | :param fullname: fullname |
| 487 | :param email: email |
| 488 | :param active: active |
| 489 | :param groups: array of strings |
| 490 | groups: |
| 491 | - Non-Interactive Users |
| 492 | - Testers |
| 493 | :param ssh_key: public ssh key |
| 494 | :param http_password: http password |
| 495 | |
| 496 | CLI Examples: |
| 497 | |
| 498 | .. code-block:: bash |
| 499 | |
| 500 | salt '*' gerrit.account_create username "full name" "mail@domain.com" |
| 501 | |
| 502 | ''' |
| 503 | gerrit_client = _gerrit_http_connection(**kwargs) |
| 504 | output, changed = _update_account( |
| 505 | gerrit_client, **{ |
| 506 | 'username': username, |
| 507 | 'fullname': fullname, |
| 508 | 'email': email, |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 509 | # 'active': active, |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 510 | 'groups': groups, |
| 511 | 'ssh_key': ssh_key, |
| 512 | 'http_password': http_password |
| 513 | }) |
| 514 | return output |
| 515 | |
| 516 | |
| 517 | def account_update(username, fullname=None, email=None, active=None, groups=[], ssh_key=None, http_password=None, **kwargs): |
| 518 | ''' |
| 519 | Create a gerrit account |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 520 | |
| 521 | :param username: username |
| 522 | :param fullname: fullname |
| 523 | :param email: email |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 524 | :param active: active |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 525 | :param groups: array of strings |
| 526 | groups: |
| 527 | - Non-Interactive Users |
| 528 | - Testers |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 529 | :param ssh_key: public ssh key |
| 530 | :param http_password: http password |
| 531 | |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 532 | CLI Examples: |
| 533 | |
| 534 | .. code-block:: bash |
| 535 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 536 | salt '*' gerrit.account_create username "full name" "mail@domain.com" |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 537 | |
| 538 | ''' |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 539 | gerrit_client = _gerrit_http_connection(**kwargs) |
| 540 | output, changed = _update_account( |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 541 | gerrit_client, **{ |
| 542 | 'username': username, |
| 543 | 'fullname': fullname, |
| 544 | 'email': email, |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 545 | # 'active': active, |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 546 | 'groups': groups, |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 547 | 'ssh_key': ssh_key, |
| 548 | 'http_password': http_password |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 549 | }) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 550 | return output |
| 551 | |
Filip Pytloun | da91075 | 2017-08-03 12:13:57 +0200 | [diff] [blame] | 552 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 553 | def account_list(**kwargs): |
| 554 | ''' |
| 555 | List gerrit accounts |
| 556 | |
| 557 | CLI Examples: |
| 558 | |
| 559 | .. code-block:: bash |
| 560 | |
| 561 | salt '*' gerrit.account_list |
| 562 | |
| 563 | ''' |
| 564 | gerrit_client = _gerrit_http_connection(**kwargs) |
| 565 | ret_list = gerrit_client.get('/accounts/?q=*&n=10000') |
| 566 | ret = {} |
| 567 | for item in ret_list: |
| 568 | ret[item['username']] = item |
| 569 | return ret |
| 570 | |
| 571 | |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 572 | def account_get(name, **kwargs): |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 573 | ''' |
| 574 | Get gerrit account |
| 575 | |
| 576 | CLI Examples: |
| 577 | |
| 578 | .. code-block:: bash |
| 579 | |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 580 | salt '*' gerrit.account_get name |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 581 | |
| 582 | ''' |
| 583 | gerrit_client = _gerrit_http_connection(**kwargs) |
Filip Pytloun | 1a521b3 | 2017-03-28 12:06:50 +0200 | [diff] [blame] | 584 | try: |
| 585 | ret = gerrit_client.get('/accounts/%s' % name) |
| 586 | except Exception: |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 587 | ret = {'Error': 'Error in retrieving account'} |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 588 | return ret |
| 589 | |
| 590 | |
| 591 | def group_list(**kwargs): |
| 592 | ''' |
| 593 | List gerrit groups |
| 594 | |
| 595 | CLI Examples: |
| 596 | |
| 597 | .. code-block:: bash |
| 598 | |
| 599 | salt '*' gerrit.group_list |
| 600 | |
| 601 | ''' |
| 602 | gerrit_client = _gerrit_http_connection(**kwargs) |
| 603 | return gerrit_client.get('/groups/') |
| 604 | |
| 605 | |
| 606 | def group_get(groupname, **kwargs): |
| 607 | ''' |
| 608 | Get gerrit group |
| 609 | |
| 610 | CLI Examples: |
| 611 | |
| 612 | .. code-block:: bash |
| 613 | |
| 614 | salt '*' gerrit.group_get groupname |
| 615 | |
| 616 | ''' |
| 617 | gerrit_client = _gerrit_http_connection(**kwargs) |
Filip Pytloun | 9c1b92f | 2017-02-28 19:26:49 +0100 | [diff] [blame] | 618 | try: |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 619 | item = gerrit_client.get('/groups/%s' % groupname) |
| 620 | ret = {item['name']: item} |
| 621 | except: |
| 622 | ret = {'Error': 'Error in retrieving account'} |
| 623 | return ret |
| 624 | |
| 625 | |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 626 | def group_create(name, description=None, **kwargs): |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 627 | ''' |
| 628 | Create a gerrit group |
| 629 | |
| 630 | :param name: name |
| 631 | |
| 632 | CLI Examples: |
| 633 | |
| 634 | .. code-block:: bash |
| 635 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 636 | salt '*' gerrit.group_create group-name description |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 637 | |
| 638 | ''' |
| 639 | gerrit_client = _gerrit_http_connection(**kwargs) |
| 640 | ret, changed = _update_group( |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 641 | gerrit_client, **{'name': name, 'description': description}) |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 642 | return ret |
| 643 | |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 644 | |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 645 | def project_create(name, **kwargs): |
| 646 | ''' |
| 647 | Create a gerrit project |
| 648 | |
| 649 | :param name: new project name |
| 650 | |
| 651 | CLI Examples: |
| 652 | |
| 653 | .. code-block:: bash |
| 654 | |
| 655 | salt '*' gerrit.project_create namespace/nova description='nova project' |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 656 | |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 657 | ''' |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 658 | gerrit_client = _gerrit_ssh_connection(**kwargs) |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 659 | |
| 660 | project = project_get(name, **kwargs) |
| 661 | |
Filip Pytloun | da91075 | 2017-08-03 12:13:57 +0200 | [diff] [blame] | 662 | if project and "Error" not in project: |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 663 | LOG.debug("Project {0} exists".format(name)) |
| 664 | return project |
| 665 | |
Filip Pytloun | da91075 | 2017-08-03 12:13:57 +0200 | [diff] [blame] | 666 | gerrit_client.createProject(name) |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 667 | return project_get(name, **kwargs) |
| 668 | |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 669 | |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 670 | def project_get(name, **kwargs): |
| 671 | ''' |
| 672 | Return a specific project |
| 673 | |
| 674 | CLI Examples: |
| 675 | |
| 676 | .. code-block:: bash |
| 677 | |
| 678 | salt '*' gerrit.project_get projectname |
| 679 | ''' |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 680 | gerrit_client = _gerrit_ssh_connection(**kwargs) |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 681 | ret = {} |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 682 | projects = gerrit_client.listProjects() |
Filip Pytloun | da91075 | 2017-08-03 12:13:57 +0200 | [diff] [blame] | 683 | if name not in projects: |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 684 | return {'Error': 'Error in retrieving project'} |
| 685 | ret[name] = {'name': name} |
| 686 | return ret |
| 687 | |
| 688 | |
| 689 | def project_list(**connection_args): |
| 690 | ''' |
| 691 | Return a list of available projects |
| 692 | |
| 693 | CLI Example: |
| 694 | |
| 695 | .. code-block:: bash |
| 696 | |
| 697 | salt '*' gerrit.project_list |
| 698 | ''' |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 699 | gerrit_client = _gerrit_ssh_connection(**connection_args) |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 700 | ret = {} |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 701 | projects = gerrit_client.listProjects() |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 702 | for project in projects: |
| 703 | ret[project] = { |
| 704 | 'name': project |
| 705 | } |
| 706 | return ret |
| 707 | |
| 708 | |
| 709 | def query(change, **kwargs): |
| 710 | ''' |
| 711 | Query gerrit |
| 712 | |
| 713 | :param change: Query content |
| 714 | |
| 715 | CLI Examples: |
| 716 | |
| 717 | .. code-block:: bash |
| 718 | |
| 719 | salt '*' gerrit.query 'status:open project:tools/gerrit limit:2' |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 720 | |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 721 | ''' |
| 722 | ret = {} |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 723 | gerrit_client = _gerrit_ssh_connection(**kwargs) |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 724 | msg = gerrit_client.query(change) |
| 725 | ret['query'] = msg |
| 726 | return ret |