]> git.ipfire.org Git - people/jschlag/pbs.git/blob - src/buildservice/__init__.py
Redesign mastering repositories
[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 jobs
20 from . import keys
21 from . import logs
22 from . import messages
23 from . import mirrors
24 from . import packages
25 from . import repository
26 from . import settings
27 from . import sessions
28 from . import sources
29 from . import updates
30 from . import uploads
31 from . import users
32
33 log = logging.getLogger("backend")
34 log.propagate = 1
35
36 # Import version
37 from .__version__ import VERSION as __version__
38
39 from .decorators import *
40 from .constants import *
41
42 class Backend(object):
43 def __init__(self, config_file=None):
44 # Read configuration file.
45 self.config = self.read_config(config_file)
46
47 # Global pakfire settings (from database).
48 self.settings = settings.Settings(self)
49
50 self.arches = arches.Arches(self)
51 self.builds = builds.Builds(self)
52 self.cache = cache.Cache(self)
53 self.geoip = geoip.GeoIP(self)
54 self.jobs = jobs.Jobs(self)
55 self.builders = builders.Builders(self)
56 self.distros = distribution.Distributions(self)
57 self.jobqueue = jobqueue.JobQueue(self)
58 self.keys = keys.Keys(self)
59 self.messages = messages.Messages(self)
60 self.mirrors = mirrors.Mirrors(self)
61 self.packages = packages.Packages(self)
62 self.repos = repository.Repositories(self)
63 self.sessions = sessions.Sessions(self)
64 self.sources = sources.Sources(self)
65 self.updates = updates.Updates(self)
66 self.uploads = uploads.Uploads(self)
67 self.users = users.Users(self)
68
69 # Open a connection to bugzilla.
70 self.bugzilla = bugtracker.Bugzilla(self)
71
72 # A pool to store strings (for comparison).
73 self.pool = pakfire.satsolver.Pool("dummy")
74
75 @lazy_property
76 def _environment_configuration(self):
77 env = {}
78
79 # Get database configuration
80 env["database"] = {
81 "name" : os.environ.get("PBS_DATABASE_NAME"),
82 "hostname" : os.environ.get("PBS_DATABASE_HOSTNAME"),
83 "user" : os.environ.get("PBS_DATABASE_USER"),
84 "password" : os.environ.get("PBS_DATABASE_PASSWORD"),
85 }
86
87 return env
88
89 def read_config(self, path):
90 c = ConfigParser.SafeConfigParser()
91
92 # Import configuration from environment
93 for section in self._environment_configuration:
94 c.add_section(section)
95
96 for k in self._environment_configuration[section]:
97 c.set(section, k, self._environment_configuration[section][k] or "")
98
99 # Load default configuration file first
100 paths = [
101 os.path.join(CONFIGSDIR, "pbs.conf"),
102 ]
103
104 if path:
105 paths.append(path)
106
107 # Load all configuration files
108 for path in paths:
109 if os.path.exists(path):
110 log.debug("Loading configuration from %s" % path)
111 c.read(path)
112 else:
113 log.error("No such file %s" % path)
114
115 return c
116
117 @lazy_property
118 def db(self):
119 try:
120 name = self.config.get("database", "name")
121 hostname = self.config.get("database", "hostname")
122 user = self.config.get("database", "user")
123 password = self.config.get("database", "password")
124 except ConfigParser.Error as e:
125 log.error("Error parsing the config: %s" % e.message)
126
127 log.debug("Connecting to database %s @ %s" % (name, hostname))
128
129 return database.Connection(hostname, name, user=user, password=password)
130
131 def delete_file(self, path, not_before=None):
132 self.db.execute("INSERT INTO queue_delete(path, not_before) \
133 VALUES(%s, %s)", path, not_before)
134
135 def cleanup_files(self):
136 query = self.db.query("SELECT * FROM queue_delete \
137 WHERE (not_before IS NULL OR not_before <= NOW())")
138
139 for row in query:
140 if not row.path:
141 continue
142
143 path = row.path
144 if not path.startswith("/"):
145 path = os.path.join(PACKAGES_DIR, path)
146
147 try:
148 logging.debug("Removing %s..." % path)
149 os.unlink(path)
150 except OSError, e:
151 logging.error("Could not remove %s: %s" % (path, e))
152
153 while True:
154 path = os.path.dirname(path)
155
156 # Stop if we are running outside of the tree.
157 if not path.startswith(PACKAGES_DIR):
158 break
159
160 # If the directory is not empty, we cannot remove it.
161 if os.path.exists(path) and os.listdir(path):
162 break
163
164 try:
165 logging.debug("Removing %s..." % path)
166 os.rmdir(path)
167 except OSError, e:
168 logging.error("Could not remove %s: %s" % (path, e))
169 break
170
171 self.db.execute("DELETE FROM queue_delete WHERE id = %s", row.id)