Package buildbot :: Package db :: Module connector
[frames] | no frames]

Source Code for Module buildbot.db.connector

  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 textwrap 
 17  from twisted.internet import defer 
 18  from twisted.python import log 
 19  from twisted.application import internet, service 
 20  from buildbot import config 
 21  from buildbot.db import enginestrategy 
 22  from buildbot.db import pool, model, changes, schedulers, sourcestamps, sourcestampsets 
 23  from buildbot.db import state, buildsets, buildrequests, builds, users 
 24   
25 -class DatabaseNotReadyError(Exception):
26 pass
27 28 upgrade_message = textwrap.dedent("""\ 29 30 The Buildmaster database needs to be upgraded before this version of 31 buildbot can run. Use the following command-line 32 33 buildbot upgrade-master path/to/master 34 35 to upgrade the database, and try starting the buildmaster again. You may 36 want to make a backup of your buildmaster before doing so. If you are 37 using MySQL, you must specify the connector string on the upgrade-master 38 command line: 39 40 buildbot upgrade-master --db=<db-url> path/to/master 41 42 """).strip() 43
44 -class DBConnector(config.ReconfigurableServiceMixin, service.MultiService):
45 # The connection between Buildbot and its backend database. This is 46 # generally accessible as master.db, but is also used during upgrades. 47 # 48 # Most of the interesting operations available via the connector are 49 # implemented in connector components, available as attributes of this 50 # object, and listed below. 51 52 # Period, in seconds, of the cleanup task. This master will perform 53 # periodic cleanup actions on this schedule. 54 CLEANUP_PERIOD = 3600 55
56 - def __init__(self, master, basedir):
57 service.MultiService.__init__(self) 58 self.setName('db') 59 self.master = master 60 self.basedir = basedir 61 62 # not configured yet - we don't build an engine until the first 63 # reconfig 64 self.configured_url = None 65 66 # set up components 67 self._engine = None # set up in reconfigService 68 self.pool = None # set up in reconfigService 69 self.model = model.Model(self) 70 self.changes = changes.ChangesConnectorComponent(self) 71 self.schedulers = schedulers.SchedulersConnectorComponent(self) 72 self.sourcestamps = sourcestamps.SourceStampsConnectorComponent(self) 73 self.sourcestampsets = sourcestampsets.SourceStampSetsConnectorComponent(self) 74 self.buildsets = buildsets.BuildsetsConnectorComponent(self) 75 self.buildrequests = buildrequests.BuildRequestsConnectorComponent(self) 76 self.state = state.StateConnectorComponent(self) 77 self.builds = builds.BuildsConnectorComponent(self) 78 self.users = users.UsersConnectorComponent(self) 79 80 self.cleanup_timer = internet.TimerService(self.CLEANUP_PERIOD, 81 self._doCleanup) 82 self.cleanup_timer.setServiceParent(self)
83 84
85 - def setup(self, check_version=True, verbose=True):
86 db_url = self.configured_url = self.master.config.db['db_url'] 87 88 log.msg("Setting up database with URL %r" % (db_url,)) 89 90 # set up the engine and pool 91 self._engine = enginestrategy.create_engine(db_url, 92 basedir=self.basedir) 93 self.pool = pool.DBThreadPool(self._engine, verbose=verbose) 94 95 # make sure the db is up to date, unless specifically asked not to 96 if check_version: 97 d = self.model.is_current() 98 def check_current(res): 99 if not res: 100 for l in upgrade_message.split('\n'): 101 log.msg(l) 102 raise DatabaseNotReadyError()
103 d.addCallback(check_current) 104 else: 105 d = defer.succeed(None) 106 107 return d
108 109
110 - def reconfigService(self, new_config):
111 # double-check -- the master ensures this in config checks 112 assert self.configured_url == new_config.db['db_url'] 113 114 return config.ReconfigurableServiceMixin.reconfigService(self, 115 new_config)
116 117
118 - def _doCleanup(self):
119 """ 120 Perform any periodic database cleanup tasks. 121 122 @returns: Deferred 123 """ 124 # pass on this if we're not configured yet 125 if not self.configured_url: 126 return 127 128 d = self.changes.pruneChanges(self.master.config.changeHorizon) 129 d.addErrback(log.err, 'while pruning changes') 130 return d
131