]> git.ipfire.org Git - pbs.git/blob - src/buildservice/__init__.py
Drop pool
[pbs.git] / src / buildservice / __init__.py
1 #!/usr/bin/python
2
3
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 jobqueue
18 from . import jobs
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 version = __version__
43
44 def __init__(self, config_file=None):
45 # Read configuration file.
46 self.config = self.read_config(config_file)
47
48 # Global pakfire settings (from database).
49 self.settings = settings.Settings(self)
50
51 self.arches = arches.Arches(self)
52 self.builds = builds.Builds(self)
53 self.cache = cache.Cache(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 @lazy_property
73 def _environment_configuration(self):
74 env = {}
75
76 # Get database configuration
77 env["database"] = {
78 "name" : os.environ.get("PBS_DATABASE_NAME"),
79 "hostname" : os.environ.get("PBS_DATABASE_HOSTNAME"),
80 "user" : os.environ.get("PBS_DATABASE_USER"),
81 "password" : os.environ.get("PBS_DATABASE_PASSWORD"),
82 }
83
84 return env
85
86 def read_config(self, path):
87 c = configparser.SafeConfigParser()
88
89 # Import configuration from environment
90 for section in self._environment_configuration:
91 c.add_section(section)
92
93 for k in self._environment_configuration[section]:
94 c.set(section, k, self._environment_configuration[section][k] or "")
95
96 # Load default configuration file first
97 paths = [
98 os.path.join(CONFIGSDIR, "pbs.conf"),
99 ]
100
101 if path:
102 paths.append(path)
103
104 # Load all configuration files
105 for path in paths:
106 if os.path.exists(path):
107 log.debug("Loading configuration from %s" % path)
108 c.read(path)
109 else:
110 log.error("No such file %s" % path)
111
112 return c
113
114 @lazy_property
115 def db(self):
116 try:
117 name = self.config.get("database", "name")
118 hostname = self.config.get("database", "hostname")
119 user = self.config.get("database", "user")
120 password = self.config.get("database", "password")
121 except configparser.Error as e:
122 log.error("Error parsing the config: %s" % e.message)
123
124 log.debug("Connecting to database %s @ %s" % (name, hostname))
125
126 return database.Connection(hostname, name, user=user, password=password)
127
128 def delete_file(self, path, not_before=None):
129 self.db.execute("INSERT INTO queue_delete(path, not_before) \
130 VALUES(%s, %s)", path, not_before)
131
132 def cleanup_files(self):
133 query = self.db.query("SELECT * FROM queue_delete \
134 WHERE (not_before IS NULL OR not_before <= NOW())")
135
136 for row in query:
137 if not row.path:
138 continue
139
140 path = row.path
141
142 if not path or not path.startswith("%s/" % PAKFIRE_DIR):
143 log.warning("Cannot delete file outside of the tree")
144 continue
145
146 try:
147 logging.debug("Removing %s..." % path)
148 os.unlink(path)
149 except OSError as e:
150 logging.error("Could not remove %s: %s" % (path, e))
151
152 while True:
153 path = os.path.dirname(path)
154
155 # Stop if we are running outside of the tree.
156 if not path.startswith(PAKFIRE_DIR):
157 break
158
159 # If the directory is not empty, we cannot remove it.
160 if os.path.exists(path) and os.listdir(path):
161 break
162
163 try:
164 logging.debug("Removing %s..." % path)
165 os.rmdir(path)
166 except OSError as e:
167 logging.error("Could not remove %s: %s" % (path, e))
168 break
169
170 self.db.execute("DELETE FROM queue_delete WHERE id = %s", row.id)