~arosales/charms/precise/buildbot-master/icon-categories-add

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/python

# Copyright 2012 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

import os
import shutil
import subprocess


def run(*args):
    """Run the command with the given arguments.

    The first argument is the path to the command to run, subsequent arguments
    are command-line arguments to be passed.
    """
    process = subprocess.Popen(args, stdout=subprocess.PIPE,
        stderr=subprocess.PIPE, close_fds=True)
    stdout, stderr = process.communicate()
    if process.returncode:
        raise subprocess.CalledProcessError(
            process.returncode, repr(args), output=stdout+stderr)
    return stdout


def command(*base_args):
    """Return a callable that will run the given command with any arguments.

    The first argument is the path to the command to run, subsequent arguments
    are command-line arguments to "bake into" the returned callable.

    The callable runs the given executable and also takes arguments that will
    be appeneded to the "baked in" arguments.

    For example, this code will list a file named "foo" (if it exists):

        ls_foo = command('/bin/ls', 'foo')
        ls_foo()

    While this invocation will list "foo" and "bar" (assuming they exist):

        ls_foo('bar')
    """
    def callable_command(*args):
        all_args = base_args + args
        return run(*all_args)

    return callable_command


log = command('juju-log')


def install_extra_repository(extra_repository):
    try:
        run('apt-add-repository', extra_repository)
        run('apt-get', 'update')
    except subprocess.CalledProcessError as e:
        log('Error adding repository: ' + extra_repository)
        log(e)
        raise


def install_packages():
    apt_get_install = command('apt-get', 'install', '-y', '--force-yes')
    apt_get_install('bzr', 'python-boto')
    install_extra_repository('ppa:yellow/ppa')
    apt_get_install('python-shell-toolbox')


install_packages()

# These modules depend on shelltoolbox being installed so they must not be
# imported until that package is available.
from helpers import (
    get_config,
    log_entry,
    log_exit,
    )
from local import (
    config_json,
    slave_json,
    )


def cleanup(buildbot_dir):
    # Since we may be installing into a pre-existing service, ensure the
    # buildbot directory is removed.
    if os.path.exists(buildbot_dir):
        try:
            run('buildbot', 'stop', buildbot_dir)
        except (subprocess.CalledProcessError, OSError):
            # This usually happens because buildbot hasn't been
            # installed yet, or that it wasn't running; just
            # ignore the error.
            pass
        shutil.rmtree(buildbot_dir)
    # Initialize the cached config so that old configs don't hang around
    # after the service is torn down.
    config_json.set({})
    # Remove the slaves JSON file if it exists.
    if os.path.exists(slave_json.path):
        os.remove(slave_json.path)


def main():
    config = get_config()
    log("config:")
    log(str(config))
    old_config = config_json.get()
    buildbot_dir = old_config.get('installdir', config['installdir'])
    cleanup(buildbot_dir)


if __name__ == '__main__':

    log_entry()
    try:
        main()
    finally:
        log_exit()