~charmers/charms/precise/buildbot-master/trunk

« back to all changes in this revision

Viewing changes to hooks/config-changed

  • Committer: Gary Poster
  • Date: 2012-02-06 00:06:32 UTC
  • mto: (11.3.1 dynamic-relationship)
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: gary.poster@canonical.com-20120206000632-eyfolr3dlmlkpfgt
incremental step: buildbot starts with demo Pyflakes environment.  See README.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
 
 
3
import base64
 
4
import json
 
5
import os
 
6
import os.path
 
7
import shutil
 
8
import StringIO
 
9
from subprocess import CalledProcessError
 
10
import sys
 
11
 
3
12
from helpers import (
4
13
    command,
5
14
    DictDiffer,
6
15
    get_config,
 
16
    generate_string,
7
17
    install_extra_repository,
8
18
    load_pickle,
9
19
    save_pickle,
10
20
    run,
11
21
    )
12
 
from subprocess import CalledProcessError
13
 
import base64
14
 
import os
15
 
import os.path
16
 
import sys
17
 
 
18
 
# config_file is changed via juju like:
19
 
# juju set buildbot-master config-file=`uuencode master.cfg`
 
22
from local import (
 
23
    buildbot_reconfig,
 
24
    get_slave_info_path,
 
25
    set_slave_info,
 
26
    )
20
27
 
21
28
CONFIG_PICKLE = "config.pkl"
22
29
REQUIRED_KEYS = [
85
92
    log("{} is an unsupported transport".format(config_transport))
86
93
    sys.exit(1)
87
94
 
 
95
master_cfg_path = os.path.join(buildbot_dir, 'master.cfg')
 
96
 
88
97
# Write the buildbot config to disk (fetching it if necessary).
89
 
if config_file and 'config-file' not in diff.unchanged:
90
 
    if not os.path.exists(buildbot_dir):
91
 
        os.makedirs(buildbot_dir)
92
 
    with open(os.path.join(buildbot_dir, 'master.cfg', 'w')) as f:
93
 
        f.write(base64.decode(config_file))
 
98
if config_file and 'config-file' in diff.added_or_changed:
 
99
    if not os.path.exists(os.path.join(buildbot_dir, 'buildbot.tac')):
 
100
        run('buildbot', 'create-master', buildbot_dir)
 
101
    # This file will be moved to master.cfg.original below.
 
102
    with open(master_cfg_path, 'w') as f:
 
103
        base64.decode(StringIO.StringIO(config_file), f)
94
104
    log('config_file decoded and written.')
95
105
    restart_required = True
96
106
elif (config_transport == 'bzr' and config_url and
112
122
        with open(os.path.join(ssh_dir, 'lp_key', w)) as f:
113
123
            f.write(base64.decode(private_key))
114
124
    bzr('branch', '--use-existing-dir', config_url, buildbot_dir)
115
 
    run('chown', '-R', 'ubuntu:ubuntu', buildbot_dir)
116
125
    log('configuration fetched from {}'.format(config_url))
117
126
    restart_required = True
118
127
else:
119
128
    # Configuration file specifiers are unchanged or unrecognized.
120
129
    pass
121
130
 
122
 
# Restart buildbot if it is running.
123
 
if restart_required:
124
 
    pidfile = os.path.join(buildbot_dir, 'twistd.pid')
125
 
    if os.path.exists(pidfile):
126
 
        buildbot_pid = open(pidfile).read().strip()
127
 
        try:
128
 
            # Is buildbot running?
129
 
            run('kill', '-0', buildbot_pid)
130
 
        except CalledProcessError:
131
 
            # Buildbot isn't running, so no need to reconfigure it.
132
 
            pass
133
 
        else:
134
 
            # Buildbot is running, reconfigure it.
135
 
            log('Reconfiguring buildbot')
136
 
            run('buildbot', 'reconfig', buildbot_dir)
137
 
            log('Buildbot reconfigured')
138
 
    else:
139
 
        # Buildbot isn't running so start afresh but only if properly
140
 
        # configured.
141
 
        if os.path.exists(os.path.join(buildbot_dir, 'master.cfg')):
142
 
            run('buildbot', 'start', buildbot_dir)
 
131
# Initialize the buildbot directory and (re)start buildbot.
 
132
if restart_required and os.path.exists(master_cfg_path):
 
133
    shutil.move(master_cfg_path, master_cfg_path + '.original')
 
134
    shutil.copy(
 
135
        os.path.join(os.path.dirname(__file__), 'master.cfg'), master_cfg_path)
 
136
    if not os.path.exists(get_slave_info_path()):
 
137
        set_slave_info({})
 
138
    placeholder_path = os.path.join(buildbot_dir, 'placeholder.json')
 
139
    if not os.path.exists(placeholder_path):
 
140
        with open(placeholder_path, 'w') as f:
 
141
            json.dump(generate_string("temporary-placeholder-"), f)
 
142
    run('chown', '-R', 'ubuntu:ubuntu', buildbot_dir)
 
143
    buildbot_reconfig()
143
144
else:
144
145
    log("Configuration changed but didn't require restarting.")
145
146