Skip to content

Commit

Permalink
add dbconfig
Browse files Browse the repository at this point in the history
DbConfig is an utility for master.cfg to get easy key/value storage in the buildbot database

DbConfig can get and store any jsonable object to the db for use by other masters or separate ui plugins to edit them.

The design is voluntary simplistic, the focus is on the easy use.

Example ::

    from buildbot.plugins import util, buildslave
    dbConfig = util.DbConfig(c['db']['db_url'], basedir)
    slaves = dbConfig.get("slaves")
    c['slaves'] = [
        buildslave.BuildSlave(slave['name'], slave['passwd'],
                              properties=slave.get('properties')),
        for slave in slaves
    ]
  • Loading branch information
Pierre Tardy committed Feb 19, 2015
1 parent a3c21f3 commit 7b29083
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 2 deletions.
68 changes: 68 additions & 0 deletions master/buildbot/db/dbconfig.py
@@ -0,0 +1,68 @@
# 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 buildbot.db import enginestrategy
from buildbot.db import model
from buildbot.db import state


class FakeDBConnector(object):
pass


class FakeCacheManager(object):

def get_cache(self, cache_name, miss_fn):
return None


class FakeMaster(object):
pass


class FakePool(object):
pass


class DbConfig(object):

def __init__(self, db_url, basedir, name="config"):
self.db_url = db_url
self.basedir = basedir
self.name = name

def getDb(self):
db_engine = enginestrategy.create_engine(self.db_url,
basedir=self.basedir)
db = FakeDBConnector()
db.master = FakeMaster()
db.pool = FakePool()
db.pool.engine = db_engine
db.master.caches = FakeCacheManager()
db.model = model.Model(db)
db.state = state.StateConnectorComponent(db)
self.objectid = db.state.thdGetObjectId(db_engine, self.name, "DbConfig")['id']
return db

def get(self, name, default=state.StateConnectorComponent.Thunk):
db = self.getDb()
ret = db.state.thdGetState(db.pool.engine, self.objectid, name, default=default)
db.pool.engine.close()
return ret

def set(self, name, value):
db = self.getDb()
db.state.thdSetState(db.pool.engine, self.objectid, name, value)
db.pool.engine.close()
44 changes: 44 additions & 0 deletions master/buildbot/test/unit/test_db_dbconfig.py
@@ -0,0 +1,44 @@
# 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 buildbot.db import dbconfig
from buildbot.test.util import db
from twisted.internet import defer
from twisted.internet import threads
from twisted.trial import unittest


class TestDbConfig(db.RealDatabaseMixin, unittest.TestCase):

@defer.inlineCallbacks
def setUp(self):
# as we will open the db twice, we can't use in memory sqlite
yield self.setUpRealDatabase(table_names=['objects', 'object_state'], sqlite_memory=False)
yield threads.deferToThread(self.createDbConfig)

def createDbConfig(self):
self.dbConfig = dbconfig.DbConfig(self.db_url, self.basedir)

def tearDown(self):
return self.tearDownRealDatabase()

def test_basic(self):
def thd():
slavesInDB = ['foo', 'bar']
self.dbConfig.set(u"slaves", slavesInDB)
slaves = self.dbConfig.get(u"slaves")
self.assertEqual(slaves, slavesInDB)

return threads.deferToThread(thd)
2 changes: 1 addition & 1 deletion master/buildbot/test/util/db.py
Expand Up @@ -119,7 +119,7 @@ def setUpRealDatabase(self, table_names=[], basedir='basedir',
os.makedirs(basedir)

self.db_url = os.environ.get('BUILDBOT_TEST_DB_URL', default)

self.basedir = basedir
self.db_engine = enginestrategy.create_engine(self.db_url,
basedir=basedir)
# if the caller does not want a pool, we're done.
Expand Down
2 changes: 2 additions & 0 deletions master/docs/developer/database.rst
Expand Up @@ -1251,6 +1251,8 @@ state
Set the state value for ``name`` for the object with id ``objectid``,
overwriting any existing value.

Those 3 methods have their threaded equivalent, ``thdGetObjectId``, ``thdGetState``, ``thdSetState`` that are intended to run in synchronous code, (e.g master.cfg environnement)

users
~~~~~

Expand Down
21 changes: 21 additions & 0 deletions master/docs/manual/cfg-dbconfig.rst
@@ -0,0 +1,21 @@
.. bb:cfg:: dbconfig
DbConfig
--------

DbConfig is an utility for master.cfg to get easy key/value storage in the buildbot database

DbConfig can get and store any jsonable object to the db for use by other masters or separate ui plugins to edit them.

The design is voluntary simplistic, the focus is on the easy use.

Example ::

from buildbot.plugins import util, buildslave
dbConfig = util.DbConfig(c['db']['db_url'], basedir)
slaves = dbConfig.get("slaves")
c['slaves'] = [
buildslave.BuildSlave(slave['name'], slave['passwd'],
properties=slave.get('properties')),
for slave in slaves
]
1 change: 1 addition & 0 deletions master/docs/manual/configuration.rst
Expand Up @@ -25,3 +25,4 @@ The next section, :doc:`customization`, describes this approach, with frequent r
cfg-statustargets
cfg-www
cfg-services
cfg-dbconfig
4 changes: 3 additions & 1 deletion master/setup.py
Expand Up @@ -334,7 +334,9 @@ def define_plugin_entries(groups):
'UserPasswordAuth', 'HTPasswdAuth', 'RemoteUserAuth']),
('buildbot.www.ldapuserinfos', ['LdapUserInfo']),
('buildbot.www.oauth2', [
'GoogleAuth', 'GitHubAuth'])
'GoogleAuth', 'GitHubAuth']),
('buildbot.db.state', [
'DbConfig'])
])
])
}
Expand Down

0 comments on commit 7b29083

Please sign in to comment.