]> git.ipfire.org Git - people/jschlag/pbs.git/blame - src/buildservice/__init__.py
Redesign mastering repositories
[people/jschlag/pbs.git] / src / buildservice / __init__.py
CommitLineData
9137135a
MT
1#!/usr/bin/python
2
2c909128
MT
3from __future__ import absolute_import
4
085af6d4
MT
5import ConfigParser
6import logging
7import os
8import pakfire
9
10from . import arches
11from . import bugtracker
12from . import builders
13from . import builds
14from . import cache
15from . import database
16from . import distribution
d3e7a9fb 17from . import geoip
fd43d5e1 18from . import jobqueue
2a1e9ce2 19from . import jobs
085af6d4
MT
20from . import keys
21from . import logs
22from . import messages
23from . import mirrors
24from . import packages
25from . import repository
26from . import settings
27from . import sessions
28from . import sources
29from . import updates
30from . import uploads
31from . import users
32
565ca1d1
MT
33log = logging.getLogger("backend")
34log.propagate = 1
35
455cd7d1
MT
36# Import version
37from .__version__ import VERSION as __version__
38
5c8b4bfd 39from .decorators import *
085af6d4
MT
40from .constants import *
41
57859ebc 42class Backend(object):
565ca1d1 43 def __init__(self, config_file=None):
085af6d4
MT
44 # Read configuration file.
45 self.config = self.read_config(config_file)
46
085af6d4
MT
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)
d3e7a9fb 53 self.geoip = geoip.GeoIP(self)
2a1e9ce2 54 self.jobs = jobs.Jobs(self)
085af6d4
MT
55 self.builders = builders.Builders(self)
56 self.distros = distribution.Distributions(self)
fd43d5e1 57 self.jobqueue = jobqueue.JobQueue(self)
085af6d4
MT
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
20325c11
MT
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
085af6d4
MT
89 def read_config(self, path):
90 c = ConfigParser.SafeConfigParser()
565ca1d1 91
20325c11
MT
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 "")
af3ece93 98
565ca1d1
MT
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:
a2db7eea
JS
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)
085af6d4
MT
114
115 return c
116
5c8b4bfd
MT
117 @lazy_property
118 def db(self):
c4a3df54
JS
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)
5c8b4bfd 126
fe1f59ca 127 log.debug("Connecting to database %s @ %s" % (name, hostname))
085af6d4 128
fe1f59ca 129 return database.Connection(hostname, name, user=user, password=password)
085af6d4 130
5c9daa86
MT
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
085af6d4 135 def cleanup_files(self):
5c9daa86 136 query = self.db.query("SELECT * FROM queue_delete \
d4597a4e 137 WHERE (not_before IS NULL OR not_before <= NOW())")
085af6d4
MT
138
139 for row in query:
140 if not row.path:
141 continue
142
612ee16e
MT
143 path = row.path
144 if not path.startswith("/"):
145 path = os.path.join(PACKAGES_DIR, path)
085af6d4
MT
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)