1
# Copyright 2012 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Shared functions for the buildbot master and slave"""
29
def _get_buildbot_dir():
31
return config.get('installdir')
34
def generate_string(prefix=""):
35
"""Generate a unique string and return it."""
36
return prefix + uuid.uuid4().hex
39
def buildbot_create(buildbot_dir):
40
"""Create a buildbot instance in `buildbot_dir`."""
41
if not os.path.exists(os.path.join(buildbot_dir, 'buildbot.tac')):
42
return run('buildbot', 'create-master', buildbot_dir)
45
def buildbot_running(buildbot_dir):
46
pidfile = os.path.join(buildbot_dir, 'twistd.pid')
47
if os.path.exists(pidfile):
48
buildbot_pid = open(pidfile).read().strip()
50
# Is buildbot running?
51
run('kill', '-0', buildbot_pid)
52
except subprocess.CalledProcessError:
59
buildbot_dir = _get_buildbot_dir()
60
if buildbot_running(buildbot_dir):
61
# Buildbot is running, stop it.
62
log('Stopping buildbot')
63
run('buildbot', 'stop', buildbot_dir)
66
def buildbot_reconfig():
67
buildbot_dir = _get_buildbot_dir()
68
pidfile = os.path.join(buildbot_dir, 'twistd.pid')
70
if os.path.exists(pidfile):
71
buildbot_pid = open(pidfile).read().strip()
73
# Is buildbot running?
74
run('kill', '-0', buildbot_pid)
75
except subprocess.CalledProcessError:
76
# Buildbot isn't running, so no need to reconfigure it.
79
# Buildbot is running, reconfigure it.
80
log('--> Reconfiguring buildbot')
82
# reconfig is broken in 0.8.3 (Oneiric)
83
# run('buildbot', 'reconfig', buildbot_dir)
84
run('buildbot', 'stop', buildbot_dir)
85
run('buildbot', 'start', buildbot_dir)
86
log('<-- Reconfiguring buildbot')
89
# Buildbot isn't running so start afresh.
90
if os.path.exists(os.path.join(buildbot_dir, 'master.cfg')):
91
log('--> Starting buildbot')
93
run('buildbot', 'start', buildbot_dir)
94
log('<-- Starting buildbot')
97
def _get_tac_filename(buildbot_dir):
98
return os.path.join(buildbot_dir, 'buildbot.tac')
101
def buildslave_stop(buildbot_dir=None):
102
if buildbot_dir is None:
103
buildbot_dir = _get_buildbot_dir()
104
exit_code = subprocess.call(['buildslave', 'stop', buildbot_dir])
105
tac_file = _get_tac_filename(buildbot_dir)
106
if os.path.exists(tac_file):
111
def buildslave_start(buildbot_dir=None):
112
if buildbot_dir is None:
113
buildbot_dir = _get_buildbot_dir()
114
return subprocess.call(['buildslave', 'start', buildbot_dir])
117
def create_slave(name, passwd, host='localhost', buildbot_dir=None):
118
if buildbot_dir is None:
119
buildbot_dir = _get_buildbot_dir()
120
if not os.path.exists(buildbot_dir):
121
os.makedirs(buildbot_dir)
122
return subprocess.call([
123
'buildslave', 'create-slave', buildbot_dir, host, name, passwd])
126
slave_json = Serializer('/tmp/slave_info.json')
127
config_json = Serializer('/tmp/config.json')