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

« back to all changes in this revision

Viewing changes to hooks/local.py

  • Committer: Francesco Banconi
  • Date: 2012-02-07 14:37:13 UTC
  • mfrom: (11.1.8 dynamic-relationship)
  • Revision ID: francesco.banconi@canonical.com-20120207143713-7ycviq0xpp8roffy
Merged branch dynamic-relationship and added cleanup fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Shared functions for the buildbot master and slave"""
 
5
 
 
6
__metaclass__ = type
 
7
__all__ = [
 
8
    'buildbot_reconfig',
 
9
    'buildslave_start',
 
10
    'buildslave_stop',
 
11
    'config_json',
 
12
    'create_slave',
 
13
    'generate_string',
 
14
    'slave_json',
 
15
    ]
 
16
 
 
17
import os
 
18
import subprocess
 
19
import uuid
 
20
 
 
21
from helpers import (
 
22
    get_config,
 
23
    log,
 
24
    run,
 
25
    Serializer,
 
26
    )
 
27
 
 
28
 
 
29
def _get_buildbot_dir():
 
30
    config = get_config()
 
31
    return config.get('installdir')
 
32
 
 
33
 
 
34
def generate_string(prefix=""):
 
35
    """Generate a unique string and return it."""
 
36
    return prefix + uuid.uuid4().hex
 
37
 
 
38
 
 
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)
 
43
 
 
44
 
 
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()
 
49
        try:
 
50
            # Is buildbot running?
 
51
            run('kill', '-0', buildbot_pid)
 
52
        except subprocess.CalledProcessError:
 
53
            return False
 
54
        return True
 
55
    return False
 
56
 
 
57
 
 
58
def buildbot_stop():
 
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)
 
64
 
 
65
 
 
66
def buildbot_reconfig():
 
67
    buildbot_dir = _get_buildbot_dir()
 
68
    pidfile = os.path.join(buildbot_dir, 'twistd.pid')
 
69
    running = False
 
70
    if os.path.exists(pidfile):
 
71
        buildbot_pid = open(pidfile).read().strip()
 
72
        try:
 
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.
 
77
            pass
 
78
        else:
 
79
            # Buildbot is running, reconfigure it.
 
80
            log('--> Reconfiguring buildbot')
 
81
            # XXX as root! :-(
 
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')
 
87
            running = True
 
88
    if not running:
 
89
        # Buildbot isn't running so start afresh.
 
90
        if os.path.exists(os.path.join(buildbot_dir, 'master.cfg')):
 
91
            log('--> Starting buildbot')
 
92
            # XXX as root! :-(
 
93
            run('buildbot', 'start', buildbot_dir)
 
94
            log('<-- Starting buildbot')
 
95
 
 
96
 
 
97
def _get_tac_filename(buildbot_dir):
 
98
    return os.path.join(buildbot_dir, 'buildbot.tac')
 
99
 
 
100
 
 
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):
 
107
        os.remove(tac_file)
 
108
    return exit_code
 
109
 
 
110
 
 
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])
 
115
 
 
116
 
 
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])
 
124
 
 
125
 
 
126
slave_json = Serializer('/tmp/slave_info.json')
 
127
config_json = Serializer('/tmp/config.json')