]>
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 | |
0742a8f2 | 9 | import shutil |
085af6d4 MT |
10 | |
11 | from . import arches | |
12 | from . import bugtracker | |
13 | from . import builders | |
14 | from . import builds | |
15 | from . import cache | |
16 | from . import database | |
17 | from . import distribution | |
d3e7a9fb | 18 | from . import geoip |
fd43d5e1 | 19 | from . import jobqueue |
2a1e9ce2 | 20 | from . import jobs |
085af6d4 MT |
21 | from . import keys |
22 | from . import logs | |
23 | from . import messages | |
24 | from . import mirrors | |
25 | from . import packages | |
26 | from . import repository | |
27 | from . import settings | |
28 | from . import sessions | |
29 | from . import sources | |
30 | from . import updates | |
31 | from . import uploads | |
32 | from . import users | |
33 | ||
565ca1d1 MT |
34 | log = logging.getLogger("backend") |
35 | log.propagate = 1 | |
36 | ||
455cd7d1 MT |
37 | # Import version |
38 | from .__version__ import VERSION as __version__ | |
39 | ||
5c8b4bfd | 40 | from .decorators import * |
085af6d4 MT |
41 | from .constants import * |
42 | ||
57859ebc | 43 | class Backend(object): |
565ca1d1 | 44 | def __init__(self, config_file=None): |
085af6d4 MT |
45 | # Read configuration file. |
46 | self.config = self.read_config(config_file) | |
47 | ||
085af6d4 MT |
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) | |
d3e7a9fb | 54 | self.geoip = geoip.GeoIP(self) |
2a1e9ce2 | 55 | self.jobs = jobs.Jobs(self) |
085af6d4 MT |
56 | self.builders = builders.Builders(self) |
57 | self.distros = distribution.Distributions(self) | |
fd43d5e1 | 58 | self.jobqueue = jobqueue.JobQueue(self) |
085af6d4 MT |
59 | self.keys = keys.Keys(self) |
60 | self.messages = messages.Messages(self) | |
61 | self.mirrors = mirrors.Mirrors(self) | |
62 | self.packages = packages.Packages(self) | |
63 | self.repos = repository.Repositories(self) | |
64 | self.sessions = sessions.Sessions(self) | |
65 | self.sources = sources.Sources(self) | |
66 | self.updates = updates.Updates(self) | |
67 | self.uploads = uploads.Uploads(self) | |
68 | self.users = users.Users(self) | |
69 | ||
70 | # Open a connection to bugzilla. | |
71 | self.bugzilla = bugtracker.Bugzilla(self) | |
72 | ||
73 | # A pool to store strings (for comparison). | |
74 | self.pool = pakfire.satsolver.Pool("dummy") | |
75 | ||
20325c11 MT |
76 | @lazy_property |
77 | def _environment_configuration(self): | |
78 | env = {} | |
79 | ||
80 | # Get database configuration | |
81 | env["database"] = { | |
82 | "name" : os.environ.get("PBS_DATABASE_NAME"), | |
83 | "hostname" : os.environ.get("PBS_DATABASE_HOSTNAME"), | |
84 | "user" : os.environ.get("PBS_DATABASE_USER"), | |
85 | "password" : os.environ.get("PBS_DATABASE_PASSWORD"), | |
86 | } | |
87 | ||
88 | return env | |
89 | ||
085af6d4 MT |
90 | def read_config(self, path): |
91 | c = ConfigParser.SafeConfigParser() | |
565ca1d1 | 92 | |
20325c11 MT |
93 | # Import configuration from environment |
94 | for section in self._environment_configuration: | |
95 | c.add_section(section) | |
96 | ||
97 | for k in self._environment_configuration[section]: | |
98 | c.set(section, k, self._environment_configuration[section][k] or "") | |
af3ece93 | 99 | |
565ca1d1 MT |
100 | # Load default configuration file first |
101 | paths = [ | |
102 | os.path.join(CONFIGSDIR, "pbs.conf"), | |
103 | ] | |
104 | ||
105 | if path: | |
106 | paths.append(path) | |
107 | ||
108 | # Load all configuration files | |
109 | for path in paths: | |
a2db7eea JS |
110 | if os.path.exists(path): |
111 | log.debug("Loading configuration from %s" % path) | |
112 | c.read(path) | |
113 | else: | |
114 | log.error("No such file %s" % path) | |
085af6d4 MT |
115 | |
116 | return c | |
117 | ||
5c8b4bfd MT |
118 | @lazy_property |
119 | def db(self): | |
c4a3df54 JS |
120 | try: |
121 | name = self.config.get("database", "name") | |
122 | hostname = self.config.get("database", "hostname") | |
123 | user = self.config.get("database", "user") | |
124 | password = self.config.get("database", "password") | |
125 | except ConfigParser.Error as e: | |
126 | log.error("Error parsing the config: %s" % e.message) | |
5c8b4bfd | 127 | |
fe1f59ca | 128 | log.debug("Connecting to database %s @ %s" % (name, hostname)) |
085af6d4 | 129 | |
fe1f59ca | 130 | return database.Connection(hostname, name, user=user, password=password) |
085af6d4 | 131 | |
5c9daa86 MT |
132 | def delete_file(self, path, not_before=None): |
133 | self.db.execute("INSERT INTO queue_delete(path, not_before) \ | |
134 | VALUES(%s, %s)", path, not_before) | |
135 | ||
085af6d4 | 136 | def cleanup_files(self): |
5c9daa86 | 137 | query = self.db.query("SELECT * FROM queue_delete \ |
d4597a4e | 138 | WHERE (not_before IS NULL OR not_before <= NOW())") |
085af6d4 MT |
139 | |
140 | for row in query: | |
612ee16e | 141 | path = row.path |
0742a8f2 | 142 | |
0742a8f2 MT |
143 | if not path or not paths.startswith("%s/" % PAKFIRE_DIR): |
144 | log.warning("Cannot delete file outside of the tree") | |
145 | continue | |
146 | ||
085af6d4 MT |
147 | try: |
148 | logging.debug("Removing %s..." % path) | |
0742a8f2 MT |
149 | shutil.rmtree(path) |
150 | except shutil.Error as e: | |
085af6d4 | 151 | logging.error("Could not remove %s: %s" % (path, e)) |
0742a8f2 | 152 | continue |
085af6d4 MT |
153 | |
154 | self.db.execute("DELETE FROM queue_delete WHERE id = %s", row.id) |