]>
Commit | Line | Data |
---|---|---|
9137135a MT |
1 | #!/usr/bin/python |
2 | ||
2c909128 MT |
3 | from __future__ import absolute_import |
4 | ||
085af6d4 MT |
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 | |
d3e7a9fb | 17 | from . import geoip |
fd43d5e1 | 18 | from . import jobqueue |
085af6d4 MT |
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 | ||
565ca1d1 MT |
32 | log = logging.getLogger("backend") |
33 | log.propagate = 1 | |
34 | ||
455cd7d1 MT |
35 | # Import version |
36 | from .__version__ import VERSION as __version__ | |
37 | ||
5c8b4bfd | 38 | from .decorators import * |
085af6d4 MT |
39 | from .constants import * |
40 | ||
57859ebc | 41 | class Backend(object): |
565ca1d1 | 42 | def __init__(self, config_file=None): |
085af6d4 MT |
43 | # Read configuration file. |
44 | self.config = self.read_config(config_file) | |
45 | ||
085af6d4 MT |
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) | |
d3e7a9fb | 52 | self.geoip = geoip.GeoIP(self) |
085af6d4 MT |
53 | self.jobs = builds.Jobs(self) |
54 | self.builders = builders.Builders(self) | |
55 | self.distros = distribution.Distributions(self) | |
fd43d5e1 | 56 | self.jobqueue = jobqueue.JobQueue(self) |
085af6d4 MT |
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 | ||
20325c11 MT |
74 | @lazy_property |
75 | def _environment_configuration(self): | |
76 | env = {} | |
77 | ||
78 | # Get database configuration | |
79 | env["database"] = { | |
80 | "name" : os.environ.get("PBS_DATABASE_NAME"), | |
81 | "hostname" : os.environ.get("PBS_DATABASE_HOSTNAME"), | |
82 | "user" : os.environ.get("PBS_DATABASE_USER"), | |
83 | "password" : os.environ.get("PBS_DATABASE_PASSWORD"), | |
84 | } | |
85 | ||
86 | return env | |
87 | ||
085af6d4 MT |
88 | def read_config(self, path): |
89 | c = ConfigParser.SafeConfigParser() | |
565ca1d1 | 90 | |
20325c11 MT |
91 | # Import configuration from environment |
92 | for section in self._environment_configuration: | |
93 | c.add_section(section) | |
94 | ||
95 | for k in self._environment_configuration[section]: | |
96 | c.set(section, k, self._environment_configuration[section][k] or "") | |
af3ece93 | 97 | |
565ca1d1 MT |
98 | # Load default configuration file first |
99 | paths = [ | |
100 | os.path.join(CONFIGSDIR, "pbs.conf"), | |
101 | ] | |
102 | ||
103 | if path: | |
104 | paths.append(path) | |
105 | ||
106 | # Load all configuration files | |
107 | for path in paths: | |
a2db7eea JS |
108 | if os.path.exists(path): |
109 | log.debug("Loading configuration from %s" % path) | |
110 | c.read(path) | |
111 | else: | |
112 | log.error("No such file %s" % path) | |
085af6d4 MT |
113 | |
114 | return c | |
115 | ||
5c8b4bfd MT |
116 | @lazy_property |
117 | def db(self): | |
c4a3df54 JS |
118 | try: |
119 | name = self.config.get("database", "name") | |
120 | hostname = self.config.get("database", "hostname") | |
121 | user = self.config.get("database", "user") | |
122 | password = self.config.get("database", "password") | |
123 | except ConfigParser.Error as e: | |
124 | log.error("Error parsing the config: %s" % e.message) | |
5c8b4bfd | 125 | |
fe1f59ca | 126 | log.debug("Connecting to database %s @ %s" % (name, hostname)) |
085af6d4 | 127 | |
fe1f59ca | 128 | return database.Connection(hostname, name, user=user, password=password) |
085af6d4 MT |
129 | |
130 | def cleanup_files(self): | |
131 | query = self.db.query("SELECT * FROM queue_delete") | |
132 | ||
133 | for row in query: | |
134 | if not row.path: | |
135 | continue | |
136 | ||
137 | path = os.path.join(PACKAGES_DIR, row.path) | |
138 | ||
139 | try: | |
140 | logging.debug("Removing %s..." % path) | |
141 | os.unlink(path) | |
142 | except OSError, e: | |
143 | logging.error("Could not remove %s: %s" % (path, e)) | |
144 | ||
145 | while True: | |
146 | path = os.path.dirname(path) | |
147 | ||
148 | # Stop if we are running outside of the tree. | |
149 | if not path.startswith(PACKAGES_DIR): | |
150 | break | |
151 | ||
152 | # If the directory is not empty, we cannot remove it. | |
153 | if os.path.exists(path) and os.listdir(path): | |
154 | break | |
155 | ||
156 | try: | |
157 | logging.debug("Removing %s..." % path) | |
158 | os.rmdir(path) | |
159 | except OSError, e: | |
160 | logging.error("Could not remove %s: %s" % (path, e)) | |
161 | break | |
162 | ||
163 | self.db.execute("DELETE FROM queue_delete WHERE id = %s", row.id) |