Package buildbot :: Package scripts :: Module upgrade_master
[frames] | no frames]

Source Code for Module buildbot.scripts.upgrade_master

  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  from __future__ import with_statement 
 17   
 18  import os 
 19  import sys 
 20  import traceback 
 21  from twisted.internet import defer 
 22  from twisted.python import util, runtime 
 23  from buildbot import config as config_module 
 24  from buildbot import monkeypatches 
 25  from buildbot.db import connector 
 26  from buildbot.master import BuildMaster 
 27  from buildbot.util import in_reactor 
 28  from buildbot.scripts import base 
29 30 -def checkBasedir(config):
31 if not config['quiet']: 32 print "checking basedir" 33 34 if not base.isBuildmasterDir(config['basedir']): 35 return False 36 37 if runtime.platformType != 'win32': # no pids on win32 38 if not config['quiet']: 39 print "checking for running master" 40 pidfile = os.path.join(config['basedir'], 'twistd.pid') 41 if os.path.exists(pidfile): 42 print "'%s' exists - is this master still running?" % (pidfile,) 43 return False 44 45 return True
46
47 -def loadConfig(config, configFileName='master.cfg'):
48 if not config['quiet']: 49 print "checking %s" % configFileName 50 51 try: 52 master_cfg = config_module.MasterConfig.loadConfig( 53 config['basedir'], configFileName) 54 except config_module.ConfigErrors, e: 55 print "Errors loading configuration:" 56 for msg in e.errors: 57 print " " + msg 58 return 59 except: 60 print "Errors loading configuration:" 61 traceback.print_exc(file=sys.stdout) 62 return 63 64 return master_cfg
65
66 -def installFile(config, target, source, overwrite=False):
67 with open(source, "rt") as f: 68 new_contents = f.read() 69 if os.path.exists(target): 70 with open(target, "rt") as f: 71 old_contents = f.read() 72 if old_contents != new_contents: 73 if overwrite: 74 if not config['quiet']: 75 print "%s has old/modified contents" % target 76 print " overwriting it with new contents" 77 with open(target, "wt") as f: 78 f.write(new_contents) 79 else: 80 if not config['quiet']: 81 print "%s has old/modified contents" % target 82 print " writing new contents to %s.new" % target 83 with open(target + ".new", "wt") as f: 84 f.write(new_contents) 85 # otherwise, it's up to date 86 else: 87 if not config['quiet']: 88 print "creating %s" % target 89 with open(target, "wt") as f: 90 f.write(new_contents)
91
92 -def upgradeFiles(config):
93 if not config['quiet']: 94 print "upgrading basedir" 95 96 webdir = os.path.join(config['basedir'], "public_html") 97 if not os.path.exists(webdir): 98 if not config['quiet']: 99 print "creating public_html" 100 os.mkdir(webdir) 101 102 templdir = os.path.join(config['basedir'], "templates") 103 if not os.path.exists(templdir): 104 if not config['quiet']: 105 print "creating templates" 106 os.mkdir(templdir) 107 108 for file in ('bg_gradient.jpg', 'default.css', 109 'robots.txt', 'favicon.ico'): 110 source = util.sibpath(__file__, "../status/web/files/%s" % (file,)) 111 target = os.path.join(webdir, file) 112 try: 113 installFile(config, target, source) 114 except IOError: 115 print "Can't write '%s'." % (target,) 116 117 installFile(config, os.path.join(config['basedir'], "master.cfg.sample"), 118 util.sibpath(__file__, "sample.cfg"), overwrite=True) 119 120 # if index.html exists, use it to override the root page tempalte 121 index_html = os.path.join(webdir, "index.html") 122 root_html = os.path.join(templdir, "root.html") 123 if os.path.exists(index_html): 124 if os.path.exists(root_html): 125 print "Notice: %s now overrides %s" % (root_html, index_html) 126 print " as the latter is not used by buildbot anymore." 127 print " Decide which one you want to keep." 128 else: 129 try: 130 print "Notice: Moving %s to %s." % (index_html, root_html) 131 print " You can (and probably want to) remove it if " \ 132 "you haven't modified this file." 133 os.renames(index_html, root_html) 134 except Exception, e: 135 print "Error moving %s to %s: %s" % (index_html, root_html, 136 str(e))
137
138 @defer.inlineCallbacks 139 -def upgradeDatabase(config, master_cfg):
140 if not config['quiet']: 141 print "upgrading database (%s)" % (master_cfg.db['db_url']) 142 143 master = BuildMaster(config['basedir']) 144 master.config = master_cfg 145 db = connector.DBConnector(master, basedir=config['basedir']) 146 147 yield db.setup(check_version=False, verbose=not config['quiet']) 148 yield db.model.upgrade()
149
150 @in_reactor 151 @defer.inlineCallbacks 152 -def upgradeMaster(config, _noMonkey=False):
153 if not _noMonkey: # pragma: no cover 154 monkeypatches.patch_all() 155 156 if not checkBasedir(config): 157 defer.returnValue(1) 158 return 159 160 os.chdir(config['basedir']) 161 162 configFile = base.getConfigFileWithFallback(config['basedir']) 163 master_cfg = loadConfig(config, configFile) 164 if not master_cfg: 165 defer.returnValue(1) 166 return 167 168 upgradeFiles(config) 169 yield upgradeDatabase(config, master_cfg) 170 171 if not config['quiet']: 172 print "upgrade complete" 173 174 defer.returnValue(0)
175