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