]> git.ipfire.org Git - pbs.git/blob - src/buildservice/__init__.py
builders: Add some basic integration with AWS
[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 aws
12 from . import bugtracker
13 from . import builders
14 from . import builds
15 from . import cache
16 from . import database
17 from . import distribution
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 version = __version__
44
45 def __init__(self, config_file=None):
46 # Read configuration file.
47 self.config = self.read_config(config_file)
48
49 # Global pakfire settings (from database).
50 self.settings = settings.Settings(self)
51
52 self.arches = arches.Arches(self)
53 self.aws = aws.AWS(self)
54 self.builds = builds.Builds(self)
55 self.cache = cache.Cache(self)
56 self.jobs = jobs.Jobs(self)
57 self.builders = builders.Builders(self)
58 self.distros = distribution.Distributions(self)
59 self.jobqueue = jobqueue.JobQueue(self)
60 self.keys = keys.Keys(self)
61 self.messages = messages.Messages(self)
62 self.mirrors = mirrors.Mirrors(self)
63 self.packages = packages.Packages(self)
64 self.repos = repository.Repositories(self)
65 self.sessions = sessions.Sessions(self)
66 self.sources = sources.Sources(self)
67 self.updates = updates.Updates(self)
68 self.uploads = uploads.Uploads(self)
69 self.users = users.Users(self)
70
71 # Open a connection to bugzilla.
72 self.bugzilla = bugtracker.Bugzilla(self)
73
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
88 def read_config(self, path):
89 c = configparser.SafeConfigParser()
90
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 "")
97
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:
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)
113
114 return c
115
116 @lazy_property
117 def db(self):
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)
125
126 log.debug("Connecting to database %s @ %s" % (name, hostname))
127
128 return database.Connection(hostname, name, user=user, password=password)
129
130 def delete_file(self, path, not_before=None):
131 self.db.execute("INSERT INTO queue_delete(path, not_before) \
132 VALUES(%s, %s)", path, not_before)
133
134 def cleanup_files(self):
135 query = self.db.query("SELECT * FROM queue_delete \
136 WHERE (not_before IS NULL OR not_before <= NOW())")
137
138 for row in query:
139 if not row.path:
140 continue
141
142 path = row.path
143
144 if not path or not path.startswith("%s/" % PAKFIRE_DIR):
145 log.warning("Cannot delete file outside of the tree")
146 continue
147
148 try:
149 logging.debug("Removing %s..." % path)
150 os.unlink(path)
151 except OSError as e:
152 logging.error("Could not remove %s: %s" % (path, e))
153
154 while True:
155 path = os.path.dirname(path)
156
157 # Stop if we are running outside of the tree.
158 if not path.startswith(PAKFIRE_DIR):
159 break
160
161 # If the directory is not empty, we cannot remove it.
162 if os.path.exists(path) and os.listdir(path):
163 break
164
165 try:
166 logging.debug("Removing %s..." % path)
167 os.rmdir(path)
168 except OSError as e:
169 logging.error("Could not remove %s: %s" % (path, e))
170 break
171
172 self.db.execute("DELETE FROM queue_delete WHERE id = %s", row.id)