~hopem/charms/precise/ci-configurator/relations-cleanup

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/canonical_ci/gerrit.py

[hopem,r=wolsen]

synced ~canonical-ci/charm-helpers/trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import logging
2
2
import os
 
3
import paramiko
3
4
import sys
4
5
import subprocess
5
6
import json
6
7
 
7
 
from charmhelpers.fetch import apt_install
8
8
from charmhelpers.core.hookenv import (
9
9
    log as _log,
10
10
    ERROR,
11
11
)
12
12
 
13
 
try:
14
 
    import paramiko
15
 
except ImportError:
16
 
    # NOTE: paramiko seems not be installed by default on trusty cloud images
17
 
    apt_install(['python-paramiko'])
18
 
 
19
 
 
20
13
_connection = None
21
14
GERRIT_DAEMON = "/etc/init.d/gerrit"
22
15
 
72
65
        self.ssh = get_ssh(host, user, port, key_file)
73
66
 
74
67
    def _run_cmd(self, cmd):
75
 
        stdin, stdout, stderr = self.ssh.exec_command(cmd)
 
68
        _, stdout, stderr = self.ssh.exec_command(cmd)
76
69
        return (stdout.read(), stderr.read())
77
70
 
78
71
    def create_user(self, user, name, group, ssh_key):
91
84
                sys.exit(1)
92
85
            else:
93
86
                # retrieve user id and update keys
94
 
                account_id = None  
95
 
                cmd = ('gerrit gsql --format json -c "SELECT account_id '
96
 
                    'FROM account_external_ids WHERE external_id=\'username:%s\'"'
97
 
                    % (user))
 
87
                account_id = None
 
88
                sql = ("SELECT account_id FROM account_external_ids WHERE "
 
89
                       "external_id='username:%s'" % (user))
 
90
                cmd = ('gerrit gsql --format json -c "%s"' % (sql))
98
91
                stdout, stderr = self._run_cmd(cmd)
99
92
                if not stderr:
100
93
                    # load and decode json, extract account id
101
94
                    lines = stdout.splitlines()
102
 
                    if len(lines)>0:
 
95
                    if len(lines) > 0:
103
96
                        res = json.loads(lines[0])
104
97
                        try:
105
98
                            account_id = res['columns']['account_id']
108
101
 
109
102
                # if found, update ssh keys
110
103
                if account_id:
111
 
                    cmd = ('gerrit gsql -c "DELETE FROM account_ssh_keys '
112
 
                           'WHERE account_id=%s' % account_id)
 
104
                    sql = ("DELETE FROM account_ssh_keys WHERE account_id=%s"
 
105
                           % account_id)
 
106
                    cmd = ('gerrit gsql -c "%s"' % (sql))
113
107
                    stdout, stderr = self._run_cmd(cmd)
114
108
 
115
109
                    # insert new key
116
 
                    cmd = ('gerrit gsql -c "INSERT INTO account_ssh_keys '
117
 
                        '(ssh_public_key, valid, account_id, seq) VALUES (\'%s\', \'Y\', '
118
 
                        '\'%s\', 0)" ' % (ssh_key, account_id))
 
110
                    sql = ("INSERT INTO account_ssh_keys (ssh_public_key, "
 
111
                           "valid, account_id, seq) VALUES ('%s', 'Y', "
 
112
                           "'%s', 0)" % (ssh_key, account_id))
 
113
                    cmd = ('gerrit gsql -c "%s"' % (sql))
119
114
                    stdout, stderr = self._run_cmd(cmd)
120
115
 
121
116
        # reboot gerrit to refresh accounts
132
127
            openid = user[4]
133
128
 
134
129
            cmd = (u'gerrit create-account %s --full-name "%s" '
135
 
                   u'--group "%s" --email "%s"' % 
 
130
                   u'--group "%s" --email "%s"' %
136
131
                   (login, name, group, email))
137
132
            stdout, stderr = self._run_cmd(cmd)
138
133
 
141
136
                    sys.exit(1)
142
137
 
143
138
            # retrieve user id
144
 
            account_id = None  
145
 
            cmd = ('gerrit gsql --format json -c "SELECT account_id '
146
 
                'FROM account_external_ids WHERE external_id=\'username:%s\'"'
147
 
                % (login))
 
139
            account_id = None
 
140
            sql = ("SELECT account_id FROM account_external_ids WHERE "
 
141
                   "external_id='username:%s'" % (login))
 
142
            cmd = ('gerrit gsql --format json -c "%s"' % (sql))
148
143
            stdout, stderr = self._run_cmd(cmd)
149
144
            if not stderr:
150
145
                # load and decode json, extract account id
151
146
                lines = stdout.splitlines()
152
 
                if len(lines)>0:
 
147
                if len(lines) > 0:
153
148
                    res = json.loads(lines[0])
154
149
                    try:
155
150
                        account_id = res['columns']['account_id']
159
154
            # if found, update ssh keys and openid
160
155
            if account_id:
161
156
                # remove old keys and add new
162
 
                if len(ssh)>0:
163
 
                    cmd = ('gerrit gsql -c "DELETE FROM account_ssh_keys '
164
 
                           'WHERE account_id=%s AND ssh_public_key NOT IN (%s)"' % 
165
 
                           (account_id, (', '.join('\''+item+'\'' for item in ssh)) ))
 
157
                if len(ssh) > 0:
 
158
                    sql = ("DELETE FROM account_ssh_keys WHERE account_id=%s "
 
159
                           "AND ssh_public_key NOT IN (%s)" %
 
160
                           (account_id,
 
161
                            (', '.join("'%s'" % item for item in ssh))))
 
162
                    cmd = ('gerrit gsql -c "%s"' % (sql))
166
163
                else:
167
164
                    cmd = ('gerrit gsql -c "DELETE FROM account_ssh_keys '
168
165
                           'WHERE account_id=%s' % account_id)
172
169
                num_key = 0
173
170
                for ssh_key in ssh:
174
171
                    # insert new keys
175
 
                    cmd = ('gerrit gsql -c "INSERT INTO account_ssh_keys '
176
 
                        '(ssh_public_key, valid, account_id, seq) SELECT '
177
 
                        '%(ssh_key)s, %(valid)s, %(account_id)s, %(num_key)s '
178
 
                        'WHERE NOT EXISTS (SELECT '
179
 
                        'account_id FROM account_ssh_keys WHERE '
180
 
                        'account_id=%(account_id)s AND ssh_public_key=%(ssh_key)s)"' %
181
 
                        {'ssh_key': '\''+ssh_key+'\'', 'valid':'\'Y\'',
182
 
                         'account_id': '\''+account_id+'\'', 'num_key': num_key})
183
 
                    num_key+=1
 
172
                    sql = ("INSERT INTO account_ssh_keys (ssh_public_key, "
 
173
                           "valid, account_id, seq) SELECT %(ssh_key)s, "
 
174
                           "%(valid)s, %(account_id)s, %(num_key)s WHERE NOT "
 
175
                           "EXISTS (SELECT account_id FROM account_ssh_keys "
 
176
                           "WHERE account_id=%(account_id)s AND "
 
177
                           "ssh_public_key=%(ssh_key)s)" %
 
178
                           {'ssh_key': "'%s'" % ssh_key,
 
179
                            'valid': "'Y'",
 
180
                            'account_id': "'%s'" % account_id,
 
181
                            'num_key': num_key})
 
182
                    cmd = ('gerrit gsql -c "%s"' % (sql))
 
183
                    num_key += 1
184
184
                    stdout, stderr = self._run_cmd(cmd)
185
185
 
186
186
                # replace external id
187
187
                if openid:
188
 
                    openid = openid.replace('login.launchpad.net', 'login.ubuntu.com')
189
 
                    cmd = ('gerrit gsql -c "DELETE FROM account_external_ids '
190
 
                           'WHERE account_id=%s AND external_id NOT IN (%s) AND '
191
 
                           'external_id LIKE \'http%%\'"' % (account_id, '\''+openid+'\''))
 
188
                    openid = openid.replace('login.launchpad.net',
 
189
                                            'login.ubuntu.com')
 
190
                    sql = ("DELETE FROM account_external_ids WHERE "
 
191
                           "account_id=%s AND external_id NOT IN (%s) AND "
 
192
                           "external_id LIKE 'http%%'" %
 
193
                           (account_id, "'%s'" % openid))
 
194
                    cmd = ('gerrit gsql -c "%s"' % (sql))
192
195
                    stdout, stderr = self._run_cmd(cmd)
193
196
 
194
197
                    # replace launchpad for ubuntu account
195
 
                    cmd = ('gerrit gsql -c "INSERT INTO account_external_ids '
196
 
                           '(account_id, email_address, external_id) SELECT '
197
 
                           '%(account_id)s, %(email_address)s, %(external_id)s WHERE '
198
 
                           'NOT EXISTS (SELECT account_id FROM account_external_ids '
199
 
                           'WHERE account_id=%(account_id)s AND external_id=%(external_id)s)"' % 
200
 
                           {'account_id':'\''+account_id+'\'', 
201
 
                           'email_address':'\''+str(email)+'\'', 
202
 
                           'external_id': '\''+openid+'\''})
 
198
                    sql = ("INSERT INTO account_external_ids "
 
199
                           "(account_id, email_address, external_id) SELECT "
 
200
                           "%(account_id)s, %(email_address)s, "
 
201
                           "%(external_id)s WHERE NOT EXISTS (SELECT "
 
202
                           "account_id FROM account_external_ids WHERE "
 
203
                           "account_id=%(account_id)s AND "
 
204
                           "external_id=%(external_id)s)" %
 
205
                           {'account_id': "'%s'" % account_id,
 
206
                            'email_address': "'%s'" % str(email),
 
207
                            'external_id': "'%s'" % openid})
 
208
                    cmd = ('gerrit gsql -c "%s"' % (sql))
203
209
                    stdout, stderr = self._run_cmd(cmd)
204
210
 
205
 
 
206
211
    def create_project(self, project):
207
212
        log('Creating gerrit project %s' % project)
208
213
        cmd = ('gerrit create-project %s' % project)
224
229
 
225
230
    def flush_cache(self):
226
231
        cmd = ('gerrit flush-caches')
227
 
        stdout, stderr = self._run_cmd(cmd)
 
232
        self._run_cmd(cmd)