~lutostag/charms/trusty/ci-configurator/next

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Greg Lutostanski
  • Date: 2016-09-14 05:43:16 UTC
  • Revision ID: gregory.lutostanski@canonical.com-20160914054316-9rl91mfbzb86w4hl
update to latest with various git fixes, and vault relation and cron fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
import os
7
7
import sys
 
8
import shlex
 
9
import shutil
 
10
import subprocess
8
11
 
9
12
import gerrit
10
13
import jjb
26
29
    INFO,
27
30
    relation_ids,
28
31
    related_units,
 
32
    relation_get,
29
33
    relation_set,
30
34
    Hooks,
31
35
    UnregisteredHookError,
 
36
    local_unit
32
37
)
 
38
from charmhelpers.core.host import mkdir
 
39
from charmhelpers.core.templating import render
33
40
 
34
41
hooks = Hooks()
35
42
 
66
73
def config_changed():
67
74
    # setup identity to reach private LP resources
68
75
    common.ensure_user()
69
 
    common.install_ssh_keys()
70
 
 
 
76
    common.install_ssh_keys() 
71
77
    lp_user = config('lp-login')
72
78
    if lp_user:
73
79
        cmd = ['bzr', 'launchpad-login', lp_user]
149
155
        log('CI not yet configured - skipping zuul update', level=INFO)
150
156
 
151
157
 
 
158
@hooks.hook('vault-relation-changed')
 
159
def vault_relation_changed(rid=None):
 
160
    content = {'host': None, 'port': None, 'token': None}
 
161
    for rid in relation_ids('vault'):
 
162
        for unit in related_units(rid):
 
163
            for key in content:
 
164
                value = relation_get(key, rid=rid, unit=unit)
 
165
                if value:
 
166
                    content[key] = value
 
167
    if not all(content.values()):
 
168
        return
 
169
    shutil.copyfile(
 
170
        '{}/files/vault-0.5.0'.format(charm_dir()),
 
171
        '/tmp/vault')
 
172
    mkdir('/usr/local/bin')
 
173
    shutil.move('/tmp/vault', '/usr/local/bin/vault')
 
174
    os.chmod('/usr/local/bin/vault', 0o755)
 
175
 
 
176
    render('vault-client', '/usr/local/bin/vault-client', content, perms=0o755)
 
177
    user = 'jenkins'
 
178
    cmd = "/bin/su -c '/usr/local/bin/vault-client auth %s' %s" % \
 
179
        (content['token'], user)
 
180
    subprocess.check_call(shlex.split(cmd))
 
181
 
 
182
 
 
183
@hooks.hook('vault-relation-joined')
 
184
def vault_relation_joined(rid=None):
 
185
    relation_set(relation_id=rid, token=local_unit())
 
186
 
 
187
 
152
188
def main():
153
189
    try:
154
190
        hooks.execute(sys.argv)