]> git.ipfire.org Git - people/jschlag/pbs.git/blobdiff - src/buildservice/__init__.py
Redesign mastering repositories
[people/jschlag/pbs.git] / src / buildservice / __init__.py
index ad2790a6dac412cb1790bcf9def9d3b156bb5c31..b40527d89c25f5454a86626f3e5da594720482b6 100644 (file)
@@ -14,6 +14,9 @@ from . import builds
 from . import cache
 from . import database
 from . import distribution
+from . import geoip
+from . import jobqueue
+from . import jobs
 from . import keys
 from . import logs
 from . import messages
@@ -27,27 +30,31 @@ from . import updates
 from . import uploads
 from . import users
 
+log = logging.getLogger("backend")
+log.propagate = 1
+
+# Import version
+from .__version__ import VERSION as __version__
+
+from .decorators import *
 from .constants import *
 
 class Backend(object):
-       def __init__(self, config_file="pbs.conf"):
+       def __init__(self, config_file=None):
                # 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)
 
                self.arches      = arches.Arches(self)
                self.builds      = builds.Builds(self)
                self.cache       = cache.Cache(self)
-               self.geoip       = mirrors.GeoIP(self)
-               self.jobs        = builds.Jobs(self)
+               self.geoip       = geoip.GeoIP(self)
+               self.jobs        = jobs.Jobs(self)
                self.builders    = builders.Builders(self)
                self.distros     = distribution.Distributions(self)
+               self.jobqueue    = jobqueue.JobQueue(self)
                self.keys        = keys.Keys(self)
                self.messages    = messages.Messages(self)
                self.mirrors     = mirrors.Mirrors(self)
@@ -65,37 +72,77 @@ class Backend(object):
                # A pool to store strings (for comparison).
                self.pool        = pakfire.satsolver.Pool("dummy")
 
-       def __del__(self):
-               if self.db:
-                       self.db.close()
-                       del self.db
+       @lazy_property
+       def _environment_configuration(self):
+               env = {}
+
+               # Get database configuration
+               env["database"] = {
+                       "name"     : os.environ.get("PBS_DATABASE_NAME"),
+                       "hostname" : os.environ.get("PBS_DATABASE_HOSTNAME"),
+                       "user"     : os.environ.get("PBS_DATABASE_USER"),
+                       "password" : os.environ.get("PBS_DATABASE_PASSWORD"),
+               }
+
+               return env
 
        def read_config(self, path):
                c = ConfigParser.SafeConfigParser()
-               c.read(path)
+
+               # Import configuration from environment
+               for section in self._environment_configuration:
+                       c.add_section(section)
+
+                       for k in self._environment_configuration[section]:
+                               c.set(section, k, self._environment_configuration[section][k] or "")
+
+               # Load default configuration file first
+               paths = [
+                       os.path.join(CONFIGSDIR, "pbs.conf"),
+               ]
+
+               if path:
+                       paths.append(path)
+
+               # Load all configuration files
+               for path in paths:
+                       if os.path.exists(path):
+                               log.debug("Loading configuration from %s" % path)
+                               c.read(path)
+                       else:
+                               log.error("No such file %s" % 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")
+       @lazy_property
+       def db(self):
+               try:
+                       name     = self.config.get("database", "name")
+                       hostname = self.config.get("database", "hostname")
+                       user     = self.config.get("database", "user")
+                       password = self.config.get("database", "password")
+               except ConfigParser.Error as e:
+                       log.error("Error parsing the config: %s" % e.message)
+
+               log.debug("Connecting to database %s @ %s" % (name, hostname))
 
-               if self.config.has_option(section, "pass"):
-                       pw = self.config.get(section, "pass")
-               else:
-                       pw = None
+               return database.Connection(hostname, name, user=user, password=password)
 
-               return database.Connection(host, db, user=user, password=pw)
+       def delete_file(self, path, not_before=None):
+               self.db.execute("INSERT INTO queue_delete(path, not_before) \
+                       VALUES(%s, %s)", path, not_before)
 
        def cleanup_files(self):
-               query = self.db.query("SELECT * FROM queue_delete")
+               query = self.db.query("SELECT * FROM queue_delete \
+                       WHERE (not_before IS NULL OR not_before <= NOW())")
 
                for row in query:
                        if not row.path:
                                continue
 
-                       path = os.path.join(PACKAGES_DIR, row.path)
+                       path = row.path
+                       if not path.startswith("/"):
+                               path = os.path.join(PACKAGES_DIR, path)
 
                        try:
                                logging.debug("Removing %s..." % path)