~vcs-imports/buildbot/trunk

« back to all changes in this revision

Viewing changes to master/buildbot/scripts/create_master.py

  • Committer: Dustin J. Mitchell
  • Date: 2012-04-13 17:00:59 UTC
  • mfrom: (3062.1.32)
  • Revision ID: git-v1:4394e81b9919593494f51d95304ac6c84101c7e4
Merge branch 'scripts-refactor'

* scripts-refactor: (32 commits)
  always apply expanduser to basedir
  spell function name correctly
  reorder methods
  make change:changepw the default value for --auth
  factor required-option checks into SubcommandOptions
  move BasedirMixin to base.py
  remove unnecessarily separate DB_HELP
  include basedir in path join
  change spelling of createDb -> createDB
  remove backward-compatibility with Twisted-8.2.0
  use a template to generate buildbot.tac
  terminate directory traversal more gently
  use StdoutAssertionsMixin in the remaining useful places
  rename assertStdout to assertInStdout
  refactor run to use function names, and test Options and run
  cleanup, order tests to correspond to the source file
  tests for remaining Options subclasses
  move ForceOptions to words.py, where it's used
  refactor checkconfig
  refactor tryserver
  ...

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# This file is part of Buildbot.  Buildbot is free software: you can
 
2
# redistribute it and/or modify it under the terms of the GNU General Public
 
3
# License as published by the Free Software Foundation, version 2.
 
4
#
 
5
# This program is distributed in the hope that it will be useful, but WITHOUT
 
6
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
7
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
8
# details.
 
9
#
 
10
# You should have received a copy of the GNU General Public License along with
 
11
# this program; if not, write to the Free Software Foundation, Inc., 51
 
12
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
13
#
 
14
# Copyright Buildbot Team Members
 
15
 
 
16
import os
 
17
import jinja2
 
18
from twisted.python import util
 
19
from twisted.internet import defer
 
20
from buildbot.util import in_reactor
 
21
from buildbot.db import connector
 
22
from buildbot.master import BuildMaster
 
23
from buildbot import config as config_module
 
24
from buildbot import monkeypatches
 
25
 
 
26
def makeBasedir(config):
 
27
    if os.path.exists(config['basedir']):
 
28
        if not config['quiet']:
 
29
            print "updating existing installation"
 
30
        return
 
31
    if not config['quiet']:
 
32
        print "mkdir", config['basedir']
 
33
    os.mkdir(config['basedir'])
 
34
 
 
35
def makeTAC(config):
 
36
    # render buildbot_tac.tmpl using the config
 
37
    loader = jinja2.FileSystemLoader(os.path.dirname(__file__))
 
38
    env = jinja2.Environment(loader=loader, undefined=jinja2.StrictUndefined)
 
39
    env.filters['repr'] = repr
 
40
    tpl = env.get_template('buildbot_tac.tmpl')
 
41
    cxt = dict((k.replace('-', '_'), v) for k,v in config.iteritems())
 
42
    contents = tpl.render(cxt)
 
43
 
 
44
    tacfile = os.path.join(config['basedir'], "buildbot.tac")
 
45
    if os.path.exists(tacfile):
 
46
        with open(tacfile, "rt") as f:
 
47
            oldcontents = f.read()
 
48
        if oldcontents == contents:
 
49
            if not config['quiet']:
 
50
                print "buildbot.tac already exists and is correct"
 
51
            return
 
52
        if not config['quiet']:
 
53
            print "not touching existing buildbot.tac"
 
54
            print "creating buildbot.tac.new instead"
 
55
        tacfile += ".new"
 
56
    with open(tacfile, "wt") as f:
 
57
        f.write(contents)
 
58
 
 
59
def makeSampleConfig(config):
 
60
    source = util.sibpath(__file__, "sample.cfg")
 
61
    target = os.path.join(config['basedir'], "master.cfg.sample")
 
62
    if not config['quiet']:
 
63
        print "creating %s" % target
 
64
    with open(source, "rt") as f:
 
65
        config_sample = f.read()
 
66
    if config['db']:
 
67
        config_sample = config_sample.replace('sqlite:///state.sqlite',
 
68
                                                config['db'])
 
69
    with open(target, "wt") as f:
 
70
        f.write(config_sample)
 
71
    os.chmod(target, 0600)
 
72
 
 
73
def makePublicHtml(config):
 
74
    files = {
 
75
        'bg_gradient.jpg' : "../status/web/files/bg_gradient.jpg",
 
76
        'default.css' : "../status/web/files/default.css",
 
77
        'robots.txt' : "../status/web/files/robots.txt",
 
78
        'favicon.ico' : "../status/web/files/favicon.ico",
 
79
    }
 
80
    webdir = os.path.join(config['basedir'], "public_html")
 
81
    if os.path.exists(webdir):
 
82
        if not config['quiet']:
 
83
            print "public_html/ already exists: not replacing"
 
84
        return
 
85
    else:
 
86
        os.mkdir(webdir)
 
87
    if not config['quiet']:
 
88
        print "populating public_html/"
 
89
    for target, source in files.iteritems():
 
90
        source = util.sibpath(__file__, source)
 
91
        target = os.path.join(webdir, target)
 
92
        with open(target, "wt") as f:
 
93
            with open(source, "rt") as i:
 
94
                f.write(i.read())
 
95
 
 
96
def makeTemplatesDir(config):
 
97
    files = {
 
98
        'README.txt' : "../status/web/files/templates_readme.txt",
 
99
    }
 
100
    template_dir = os.path.join(config['basedir'], "templates")
 
101
    if os.path.exists(template_dir):
 
102
        if not config['quiet']:
 
103
            print "templates/ already exists: not replacing"
 
104
        return
 
105
    else:
 
106
        os.mkdir(template_dir)
 
107
    if not config['quiet']:
 
108
        print "populating templates/"
 
109
    for target, source in files.iteritems():
 
110
        source = util.sibpath(__file__, source)
 
111
        target = os.path.join(template_dir, target)
 
112
        with open(target, "wt") as f:
 
113
            with open(source, "rt") as i:
 
114
                f.write(i.read())
 
115
 
 
116
@defer.inlineCallbacks
 
117
def createDB(config, _noMonkey=False):
 
118
    # apply the db monkeypatches (and others - no harm)
 
119
    if not _noMonkey: # pragma: no cover
 
120
        monkeypatches.patch_all()
 
121
 
 
122
    # create a master with the default configuration, but with db_url
 
123
    # overridden
 
124
    master_cfg = config_module.MasterConfig()
 
125
    master_cfg.db['db_url'] = config['db']
 
126
    master = BuildMaster(config['basedir'])
 
127
    master.config = master_cfg
 
128
    db = connector.DBConnector(master, config['basedir'])
 
129
    yield db.setup(check_version=False, verbose=not config['quiet'])
 
130
    if not config['quiet']:
 
131
        print "creating database (%s)" % (master_cfg.db['db_url'],)
 
132
    yield db.model.upgrade()
 
133
 
 
134
 
 
135
@in_reactor
 
136
@defer.inlineCallbacks
 
137
def createMaster(config):
 
138
    makeBasedir(config)
 
139
    makeTAC(config)
 
140
    makeSampleConfig(config)
 
141
    makePublicHtml(config)
 
142
    makeTemplatesDir(config)
 
143
    yield createDB(config)
 
144
 
 
145
    if not config['quiet']:
 
146
        print "buildmaster configured in %s" % (config['basedir'],)
 
147
 
 
148
    defer.returnValue(0)