]>
Commit | Line | Data |
---|---|---|
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 cleanup_files(self): | |
132 | query = self.db.query("SELECT * FROM queue_delete") | |
133 | ||
134 | for row in query: | |
135 | if not row.path: | |
136 | continue | |
137 | ||
138 | path = os.path.join(PACKAGES_DIR, row.path) | |
139 | ||
140 | try: | |
141 | logging.debug("Removing %s..." % path) | |
142 | os.unlink(path) | |
143 | except OSError, e: | |
144 | logging.error("Could not remove %s: %s" % (path, e)) | |
145 | ||
146 | while True: | |
147 | path = os.path.dirname(path) | |
148 | ||
149 | # Stop if we are running outside of the tree. | |
150 | if not path.startswith(PACKAGES_DIR): | |
151 | break | |
152 | ||
153 | # If the directory is not empty, we cannot remove it. | |
154 | if os.path.exists(path) and os.listdir(path): | |
155 | break | |
156 | ||
157 | try: | |
158 | logging.debug("Removing %s..." % path) | |
159 | os.rmdir(path) | |
160 | except OSError, e: | |
161 | logging.error("Could not remove %s: %s" % (path, e)) | |
162 | break | |
163 | ||
164 | self.db.execute("DELETE FROM queue_delete WHERE id = %s", row.id) |