aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/scripts/upgrade_master.py
blob: c471965d17f5caa7a468bb50fe84e8b4f322d14f (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# This file is part of Buildbot.  Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

from __future__ import with_statement

import os
import sys
import traceback
from twisted.internet import defer
from twisted.python import util, runtime
from buildbot import config as config_module
from buildbot import monkeypatches
from buildbot.db import connector
from buildbot.master import BuildMaster
from buildbot.util import in_reactor
from buildbot.scripts import base

def checkBasedir(config):
    if not config['quiet']:
        print "checking basedir"

    if not base.isBuildmasterDir(config['basedir']):
        return False

    if runtime.platformType != 'win32': # no pids on win32
        if not config['quiet']:
            print "checking for running master"
        pidfile = os.path.join(config['basedir'], 'twistd.pid')
        if os.path.exists(pidfile):
            print "'%s' exists - is this master still running?" % (pidfile,)
            return False

    return True

def loadConfig(config, configFileName='master.cfg'):
    if not config['quiet']:
        print "checking %s" % configFileName

    try:
        master_cfg = config_module.MasterConfig.loadConfig(
                                            config['basedir'], configFileName)
    except config_module.ConfigErrors, e:
        print "Errors loading configuration:"
        for msg in e.errors:
            print "  " + msg
        return
    except:
        print "Errors loading configuration:"
        traceback.print_exc(file=sys.stdout)
        return

    return master_cfg

def installFile(config, target, source, overwrite=False):
    with open(source, "rt") as f:
        new_contents = f.read()
    if os.path.exists(target):
        with open(target, "rt") as f:
            old_contents = f.read()
        if old_contents != new_contents:
            if overwrite:
                if not config['quiet']:
                    print "%s has old/modified contents" % target
                    print " overwriting it with new contents"
                with open(target, "wt") as f:
                    f.write(new_contents)
            else:
                if not config['quiet']:
                    print "%s has old/modified contents" % target
                    print " writing new contents to %s.new" % target
                with open(target + ".new", "wt") as f:
                    f.write(new_contents)
        # otherwise, it's up to date
    else:
        if not config['quiet']:
            print "creating %s" % target
        with open(target, "wt") as f:
            f.write(new_contents)

def upgradeFiles(config):
    if not config['quiet']:
        print "upgrading basedir"

    webdir = os.path.join(config['basedir'], "public_html")
    if not os.path.exists(webdir):
        if not config['quiet']:
            print "creating public_html"
        os.mkdir(webdir)

    templdir = os.path.join(config['basedir'], "templates")
    if not os.path.exists(templdir):
        if not config['quiet']:
            print "creating templates"
        os.mkdir(templdir)

    for file in ('bg_gradient.jpg', 'default.css',
                 'robots.txt', 'favicon.ico'):
        source = util.sibpath(__file__, "../status/web/files/%s" % (file,))
        target = os.path.join(webdir, file)
        try:
            installFile(config, target, source)
        except IOError:
            print "Can't write '%s'." % (target,)

    installFile(config, os.path.join(config['basedir'], "master.cfg.sample"),
                util.sibpath(__file__, "sample.cfg"), overwrite=True)

    # if index.html exists, use it to override the root page tempalte
    index_html = os.path.join(webdir, "index.html")
    root_html = os.path.join(templdir, "root.html")
    if os.path.exists(index_html):
        if os.path.exists(root_html):
            print "Notice: %s now overrides %s" % (root_html, index_html)
            print "        as the latter is not used by buildbot anymore."
            print "        Decide which one you want to keep."
        else:
            try:
                print "Notice: Moving %s to %s." % (index_html, root_html)
                print "        You can (and probably want to) remove it if " \
                              "you haven't modified this file."
                os.renames(index_html, root_html)
            except Exception, e:
                print "Error moving %s to %s: %s" % (index_html, root_html,
                                                     str(e))

@defer.inlineCallbacks
def upgradeDatabase(config, master_cfg):
    if not config['quiet']:
        print "upgrading database (%s)" % (master_cfg.db['db_url'])

    master = BuildMaster(config['basedir'])
    master.config = master_cfg
    db = connector.DBConnector(master, basedir=config['basedir'])

    yield db.setup(check_version=False, verbose=not config['quiet'])
    yield db.model.upgrade()

@in_reactor
@defer.inlineCallbacks
def upgradeMaster(config, _noMonkey=False):
    if not _noMonkey: # pragma: no cover
        monkeypatches.patch_all()

    if not checkBasedir(config):
        defer.returnValue(1)
        return

    os.chdir(config['basedir'])

    try:
        configFile = base.getConfigFileFromTac(config['basedir'])
    except (SyntaxError, ImportError), e:
        print "Unable to load 'buildbot.tac' from '%s':" % config['basedir']
        print e
        defer.returnValue(1)
        return
    master_cfg = loadConfig(config, configFile)
    if not master_cfg:
        defer.returnValue(1)
        return

    upgradeFiles(config)
    yield upgradeDatabase(config, master_cfg)

    if not config['quiet']:
        print "upgrade complete"

    defer.returnValue(0)