]> git.ipfire.org Git - people/jschlag/pbs.git/blob - src/buildservice/__init__.py
Catch errors when we try to parse our config
[people/jschlag/pbs.git] / src / buildservice / __init__.py
1 #!/usr/bin/python
2
3 from __future__ import absolute_import
4
5 import ConfigParser
6 import logging
7 import os
8 import pakfire
9
10 from . import arches
11 from . import bugtracker
12 from . import builders
13 from . import builds
14 from . import cache
15 from . import database
16 from . import distribution
17 from . import geoip
18 from . import jobqueue
19 from . import keys
20 from . import logs
21 from . import messages
22 from . import mirrors
23 from . import packages
24 from . import repository
25 from . import settings
26 from . import sessions
27 from . import sources
28 from . import updates
29 from . import uploads
30 from . import users
31
32 log = logging.getLogger("backend")
33 log.propagate = 1
34
35 # Import version
36 from .__version__ import VERSION as __version__
37
38 from .decorators import *
39 from .constants import *
40
41 class Backend(object):
42 def __init__(self, config_file=None):
43 # Read configuration file.
44 self.config = self.read_config(config_file)
45
46 # Global pakfire settings (from database).
47 self.settings = settings.Settings(self)
48
49 self.arches = arches.Arches(self)
50 self.builds = builds.Builds(self)
51 self.cache = cache.Cache(self)
52 self.geoip = geoip.GeoIP(self)
53 self.jobs = builds.Jobs(self)
54 self.builders = builders.Builders(self)
55 self.distros = distribution.Distributions(self)
56 self.jobqueue = jobqueue.JobQueue(self)
57 self.keys = keys.Keys(self)
58 self.messages = messages.Messages(self)
59 self.mirrors = mirrors.Mirrors(self)
60 self.packages = packages.Packages(self)
61 self.repos = repository.Repositories(self)
62 self.sessions = sessions.Sessions(self)
63 self.sources = sources.Sources(self)
64 self.updates = updates.Updates(self)
65 self.uploads = uploads.Uploads(self)
66 self.users = users.Users(self)
67
68 # Open a connection to bugzilla.
69 self.bugzilla = bugtracker.Bugzilla(self)
70
71 # A pool to store strings (for comparison).
72 self.pool = pakfire.satsolver.Pool("dummy")
73
74 def read_config(self, path):
75 c = ConfigParser.SafeConfigParser()
76
77 # Load default configuration file first
78 paths = [
79 os.path.join(CONFIGSDIR, "pbs.conf"),
80 ]
81
82 if path:
83 paths.append(path)
84
85 # Load all configuration files
86 for path in paths:
87 if os.path.exists(path):
88 log.debug("Loading configuration from %s" % path)
89 c.read(path)
90 else:
91 log.error("No such file %s" % path)
92
93 return c
94
95 @lazy_property
96 def db(self):
97 try:
98 name = self.config.get("database", "name")
99 hostname = self.config.get("database", "hostname")
100 user = self.config.get("database", "user")
101 password = self.config.get("database", "password")
102 except ConfigParser.Error as e:
103 log.error("Error parsing the config: %s" % e.message)
104
105 log.debug("Connecting to database %s @ %s" % (name, hostname))
106
107 return database.Connection(hostname, name, user=user, password=password)
108
109 def cleanup_files(self):
110 query = self.db.query("SELECT * FROM queue_delete")
111
112 for row in query:
113 if not row.path:
114 continue
115
116 path = os.path.join(PACKAGES_DIR, row.path)
117
118 try:
119 logging.debug("Removing %s..." % path)
120 os.unlink(path)
121 except OSError, e:
122 logging.error("Could not remove %s: %s" % (path, e))
123
124 while True:
125 path = os.path.dirname(path)
126
127 # Stop if we are running outside of the tree.
128 if not path.startswith(PACKAGES_DIR):
129 break
130
131 # If the directory is not empty, we cannot remove it.
132 if os.path.exists(path) and os.listdir(path):
133 break
134
135 try:
136 logging.debug("Removing %s..." % path)
137 os.rmdir(path)
138 except OSError, e:
139 logging.error("Could not remove %s: %s" % (path, e))
140 break
141
142 self.db.execute("DELETE FROM queue_delete WHERE id = %s", row.id)