From: Michael Tremer Date: Sun, 25 Nov 2012 11:58:17 +0000 (+0100) Subject: Move database configuration into a configuration file. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4585a44d168963f464782aff5c20fd8491c04278;p=people%2Fjschlag%2Fpbs.git Move database configuration into a configuration file. --- diff --git a/.gitignore b/.gitignore index edc24a4..cb99eec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.py[co] *.mo + +pbs.conf diff --git a/backend/main.py b/backend/main.py index 9eee852..e7aaf41 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,5 +1,6 @@ #!/usr/bin/python +import ConfigParser import logging import os import pakfire @@ -25,15 +26,14 @@ import users from constants import * -# Database access. -MYSQL_SERVER = "mysql-master.ipfire.org" -MYSQL_USER = "pakfire" -MYSQL_DB = "pakfire" -MYSQL_GEOIP_DB = "geoip" - class Pakfire(object): - def __init__(self): - self.db = database.Connection(MYSQL_SERVER, MYSQL_DB, user=MYSQL_USER) + def __init__(self, config_file): + # Read configuration file. + self.config = self.read_config(config_file) + + # Connect to databases. + self.db = self.connect_database() + self.geoip_db = self.connect_database("geoip-database") # Global pakfire settings (from database). self.settings = settings.Settings(self) @@ -41,8 +41,7 @@ class Pakfire(object): self.arches = arches.Arches(self) self.builds = builds.Builds(self) self.cache = cache.Cache(self) - self.geoip = mirrors.GeoIP(self, MYSQL_SERVER, MYSQL_GEOIP_DB, - user=MYSQL_USER) + self.geoip = mirrors.GeoIP(self) self.jobs = builds.Jobs(self) self.builders = builders.Builders(self) self.distros = distribution.Distributions(self) @@ -67,6 +66,24 @@ class Pakfire(object): self.db.close() del self.db + def read_config(self, path): + c = ConfigParser.SafeConfigParser() + c.read(path) + + return c + + def connect_database(self, section="database"): + db = self.config.get(section, "db") + host = self.config.get(section, "host") + user = self.config.get(section, "user") + + if self.config.has_option(section, "pass"): + pw = self.config.get(section, "pass") + else: + pw = None + + return database.Connection(host, db, user=user, password=pw) + def cleanup_files(self): query = self.db.query("SELECT * FROM queue_delete") diff --git a/backend/mirrors.py b/backend/mirrors.py index fb05f01..261682b 100644 --- a/backend/mirrors.py +++ b/backend/mirrors.py @@ -9,17 +9,10 @@ import base import logs class GeoIP(object): - db = None - - def __init__(self, pakfire, server, name, user): + def __init__(self, pakfire): self.pakfire = pakfire - if self.db is None: - self.db = tornado.database.Connection( - server, name, user=user - ) - - logging.info("Creating database connection to GeoIP database.") + self.db = self.pakfire.geoip_db @property def cache(self): diff --git a/hub/__init__.py b/hub/__init__.py index 484d9a3..748ff5f 100644 --- a/hub/__init__.py +++ b/hub/__init__.py @@ -44,7 +44,9 @@ class Application(tornado.web.Application): @property def pakfire(self): if self.__pakfire is None: - self.__pakfire = backend.Pakfire() + config_file = os.path.join(BASEDIR, "..", "pbs.conf") + + self.__pakfire = backend.Pakfire(config_file=config_file) return self.__pakfire diff --git a/pakfire-manager b/pakfire-manager index 4a6bd89..956f9c5 100644 --- a/pakfire-manager +++ b/pakfire-manager @@ -1,6 +1,7 @@ #!/usr/bin/python import logging +import os.path import time import tornado.ioloop import tornado.options @@ -10,13 +11,16 @@ from backend.managers import * tornado.options.parse_command_line() +BASEDIR = os.path.dirname(__file__) + class Daemon(object): def __init__(self): self._managers = [] self.ioloop.set_blocking_log_threshold(300) - self.pakfire = backend.Pakfire() + config_file = os.path.join(BASEDIR, "pbs.conf") + self.pakfire = backend.Pakfire(config_file=config_file) @property def ioloop(self): diff --git a/pbs.conf.example b/pbs.conf.example new file mode 100644 index 0000000..373cbd2 --- /dev/null +++ b/pbs.conf.example @@ -0,0 +1,15 @@ +[database] +; Credentials to the pakfire build service database. + +host = mysql-master.ipfire.org +user = pakfire +pass = pakfire +db = pakfire + +[geoip-database] +; Credentials to the geoip database. + +host = mysql-master.ipfire.org +user = pakfire +pass = pakfire +db = geoip diff --git a/web/__init__.py b/web/__init__.py index f298500..b3d6d95 100644 --- a/web/__init__.py +++ b/web/__init__.py @@ -226,7 +226,9 @@ class Application(tornado.web.Application): @property def pakfire(self): if self.__pakfire is None: - self.__pakfire = backend.Pakfire() + config_file = os.path.join(BASEDIR, "..", "pbs.conf") + + self.__pakfire = backend.Pakfire(config_file=config_file) return self.__pakfire