~fginther/charms/precise/ubuntu-ci-services-itself/restish-fix-master

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Andy Doan
  • Date: 2013-12-05 23:22:18 UTC
  • Revision ID: andy.doan@canonical.com-20131205232218-aswezy10f3oins0w
first try for a restish deployer

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
import os
 
4
import json
 
5
import subprocess
 
6
import sys
 
7
 
 
8
 
 
9
def juju_info(msg):
 
10
    subprocess.check_call(['juju-log', '-l', 'INFO', msg])
 
11
    pass
 
12
 
 
13
 
 
14
def _config():
 
15
    output = subprocess.check_output(['config-get', '--format=json'])
 
16
    return json.loads(output)
 
17
 
 
18
 
 
19
def _mkdir(dirname, owner="root", group="root", mode=0700):
 
20
    subprocess.check_call(
 
21
        ['install', '-o', owner, '-g', group, '-m', oct(mode), '-d', dirname])
 
22
 
 
23
 
 
24
def install(config):
 
25
    pkgs = [x for x in config['packages'].split(' ') if x]
 
26
    pkgs.append('python-pip')  # required for restish?
 
27
 
 
28
    juju_info('installing apt packages...')
 
29
    subprocess.check_call(['apt-get', 'install', '-y', 'q'] + pkgs)
 
30
 
 
31
    juju_info('installing restish...')
 
32
    restish = 'restish==%s' % config['restish_version']
 
33
    subprocess.check_call(['pip', 'install', restish])
 
34
 
 
35
    juju_info('grabbing service from bzr...')
 
36
    args = ['bzr', 'branch']
 
37
    rev = config['revno'].strip()
 
38
    if rev:
 
39
        args.extend(['-r', rev])
 
40
    args.append(config['branch'])
 
41
    args.append(config['install_root'])
 
42
    _mkdir(config['install_root'])
 
43
    subprocess.check_call(args)
 
44
 
 
45
 
 
46
def start(config):
 
47
    service = 'restish'
 
48
    conf = '/etc/init/%s.sh' % service
 
49
    if os.path.exists(conf):
 
50
        try:
 
51
            subprocess.check_call(['service', 'restart', service])
 
52
        except subprocess.CalledProcessError:
 
53
            # if it wasn't running, restart fails, so just try to start
 
54
            subprocess.check_call(['service', 'start', service])
 
55
 
 
56
 
 
57
def stop(config):
 
58
    service = 'restish'
 
59
    conf = '/etc/init/%s.sh' % service
 
60
    if os.path.exists(conf):
 
61
        subprocess.check_call(['service', 'stop', service])
 
62
 
 
63
 
 
64
def main():
 
65
    hook = os.path.basename(sys.argv[0])
 
66
    juju_info("Running hook: %s" % hook)
 
67
 
 
68
    hook_py = hook.replace('-', '_')
 
69
    funcs = globals()
 
70
    if hook_py not in funcs:
 
71
        print("Unknown hook: %s" % hook)
 
72
        return 1
 
73
 
 
74
    config = _config()
 
75
    return funcs[hook_py](config)
 
76
 
 
77
 
 
78
if __name__ == '__main__':
 
79
    exit(main())