]>
git.ipfire.org Git - pbs.git/blob - src/buildservice/__init__.py
11 from . import bugtracker
12 from . import builders
15 from . import database
16 from . import distribution
17 from . import jobqueue
21 from . import messages
23 from . import packages
24 from . import repository
25 from . import settings
26 from . import sessions
32 log
= logging
.getLogger("backend")
36 from .__version
__ import VERSION
as __version__
38 from .decorators
import *
39 from .constants
import *
41 class Backend(object):
44 def __init__(self
, config_file
=None):
45 # Read configuration file.
46 self
.config
= self
.read_config(config_file
)
48 # Global pakfire settings (from database).
49 self
.settings
= settings
.Settings(self
)
51 self
.aws
= aws
.AWS(self
)
52 self
.builds
= builds
.Builds(self
)
53 self
.cache
= cache
.Cache(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
)
69 # Open a connection to bugzilla.
70 self
.bugzilla
= bugtracker
.Bugzilla(self
)
73 def _environment_configuration(self
):
76 # Get database configuration
78 "name" : os
.environ
.get("PBS_DATABASE_NAME"),
79 "hostname" : os
.environ
.get("PBS_DATABASE_HOSTNAME"),
80 "user" : os
.environ
.get("PBS_DATABASE_USER"),
81 "password" : os
.environ
.get("PBS_DATABASE_PASSWORD"),
86 def read_config(self
, path
):
87 c
= configparser
.SafeConfigParser()
89 # Import configuration from environment
90 for section
in self
._environment
_configuration
:
91 c
.add_section(section
)
93 for k
in self
._environment
_configuration
[section
]:
94 c
.set(section
, k
, self
._environment
_configuration
[section
][k
] or "")
96 # Load default configuration file first
98 os
.path
.join(CONFIGSDIR
, "pbs.conf"),
104 # Load all configuration files
106 if os
.path
.exists(path
):
107 log
.debug("Loading configuration from %s" % path
)
110 log
.error("No such file %s" % path
)
117 name
= self
.config
.get("database", "name")
118 hostname
= self
.config
.get("database", "hostname")
119 user
= self
.config
.get("database", "user")
120 password
= self
.config
.get("database", "password")
121 except configparser
.Error
as e
:
122 log
.error("Error parsing the config: %s" % e
.message
)
124 log
.debug("Connecting to database %s @ %s" % (name
, hostname
))
126 return database
.Connection(hostname
, name
, user
=user
, password
=password
)
128 def pakfire(self
, config
, offline
=True, **kwargs
):
130 Launches a new Pakfire instance with the given configuration
132 log
.debug("Launching pakfire with configuration:\n%s" % config
)
134 # Write configuration to file
135 t
= self
._write
_tempfile
(config
)
137 # Launch a new Pakfire instance
139 return pakfire
.Pakfire(conf
=t
, logger
=log
.log
, offline
=offline
, **kwargs
)
142 # Delete the configuration file
145 def _write_tempfile(self
, content
):
147 Writes the content to a temporary file and returns its path
149 t
= tempfile
.NamedTemporaryFile(delete
=False)
152 t
.write(content
.encode())
157 async def open(self
, path
):
159 Opens a package and returns the archive
161 return await asyncio
.to_thread(self
._open
, path
)
163 def _open(self
, path
):
164 # Create a dummy Pakfire instance
165 p
= pakfire
.Pakfire(offline
=True)
170 def delete_file(self
, path
, not_before
=None):
171 self
.db
.execute("INSERT INTO queue_delete(path, not_before) \
172 VALUES(%s, %s)", path
, not_before
)
174 def cleanup_files(self
):
175 query
= self
.db
.query("SELECT * FROM queue_delete \
176 WHERE (not_before IS NULL OR not_before <= NOW())")
184 if not path
or not path
.startswith("%s/" % PAKFIRE_DIR
):
185 log
.warning("Cannot delete file outside of the tree")
189 logging
.debug("Removing %s..." % path
)
192 logging
.error("Could not remove %s: %s" % (path
, e
))
195 path
= os
.path
.dirname(path
)
197 # Stop if we are running outside of the tree.
198 if not path
.startswith(PAKFIRE_DIR
):
201 # If the directory is not empty, we cannot remove it.
202 if os
.path
.exists(path
) and os
.listdir(path
):
206 logging
.debug("Removing %s..." % path
)
209 logging
.error("Could not remove %s: %s" % (path
, e
))
212 self
.db
.execute("DELETE FROM queue_delete WHERE id = %s", row
.id)