class CliDaemon(Cli):
- def __init__(self):
- self.parser = argparse.ArgumentParser(
- description = _("Pakfire daemon command line interface."),
+ def parse_cli(self):
+ parser = argparse.ArgumentParser(
+ description = _("Pakfire daemon command line interface"),
)
- self._add_common_arguments(self.parser, offline_switch=True)
+ self._add_common_arguments(parser, offline_switch=False)
- # Finally parse all arguments from the command line and save them.
- self.args = self.parser.parse_args()
+ # There is only one default action
+ parser.set_defaults(func=self.handle_run)
- def run(self):
+ return parser.parse_args()
+
+ def handle_run(self, ns):
"""
- Runs the pakfire daemon with provided settings.
+ Runs the pakfire daemon
"""
- # Create daemon instance.
- d = daemon.PakfireDaemon(self.config)
+ d = daemon.PakfireDaemon()
+
try:
d.run()
- # We cannot just kill the daemon, it needs a smooth shutdown.
+ # We cannot just kill the daemon, it needs a smooth shutdown
except (SystemExit, KeyboardInterrupt):
d.shutdown()
import pakfire.base
import pakfire.builder
-import pakfire.config
-import pakfire.system
import pakfire.util
-from pakfire.system import system
+
+from .system import system
from . import base
+from . import config
from . import http
-from . import transport
+from . import hub
from pakfire.constants import *
from pakfire.i18n import _
class PakfireDaemon(object):
- def __init__(self, config):
- self.config = config
+ def __init__(self, config_file="daemon.conf"):
+ self.config = config.Config(config_file)
+
+ # Connect to the Pakfire Hub
+ self.hub = self.connect_to_hub()
# Indicates if this daemon is in running mode.
self.__running = True
# Create daemon that sends keep-alive messages.
- self.keepalive = PakfireDaemonKeepalive(self.config)
+ self.keepalive = PakfireDaemonKeepalive(self.hub)
# List of worker processes.
self.__workers = [self.keepalive]
# Number of running workers.
self.max_running = system.cpu_count * 2
+ def connect_to_hub(self):
+ huburl = self.config.get("daemon", "server", PAKFIRE_HUB)
+
+ # Host Credentials
+ hostname = self.config.get("daemon", "hostname", system.hostname)
+ password = self.config.get("daemon", "secret")
+
+ if not (hostname and password):
+ raise RuntimeError("Host credentials are not set")
+
+ # Create connection to the hub
+ return hub.Hub(huburl, hostname, password)
+
def run(self, heartbeat=30):
"""
Main loop.
pass
# Create a new process and start it.
- self.keepalive = PakfireDaemonKeepalive(self.config)
+ self.keepalive = PakfireDaemonKeepalive(self.hub)
self.keepalive.start()
# Add the process to the process list.
"""
Spawns a new worker process.
"""
- worker = PakfireWorker(config=self.config, *args, **kwargs)
+ worker = PakfireWorker(self.hub, *args, **kwargs)
worker.start()
log.debug("Spawned new worker process: %s" % worker)
class PakfireDaemonKeepalive(multiprocessing.Process):
- def __init__(self, config):
+ def __init__(self, hub):
multiprocessing.Process.__init__(self)
-
- # Save config.
- self.config = config
+ self.hub = hub
def run(self, heartbeat=30):
# Register signal handlers.
self.register_signal_handlers()
- # Create connection to the hub.
- self.transport = transport.PakfireHubTransport(self.config)
-
# Send our profile to the hub.
self.send_builder_info()
# Pakfire + OS
"pakfire_version" : PAKFIRE_VERSION,
- "host_key" : self.config.get("signatures", "host_key", None),
"os_name" : system.distro.pretty_name,
# Supported arches
"supported_arches" : ",".join(system.supported_arches),
}
- self.transport.post("/builders/info", data=data)
+ self.hub._request("/builders/info", method="POST", data=data)
def send_keepalive(self):
log.debug("Sending keepalive message to hub...")
# Disk space
"space_free" : self.free_space,
}
- self.transport.post("/builders/keepalive", data=data)
+
+ self.hub._request("/builders/keepalive", method="POST", data=data)
@property
def free_space(self):
class PakfireWorker(multiprocessing.Process):
- def __init__(self, config, waiting=None):
+ def __init__(self, hub, waiting=None):
multiprocessing.Process.__init__(self)
-
- # Save config.
- self.config = config
+ self.hub = hub
# Waiting event. Clear if this worker is running a build.
self.waiting = multiprocessing.Event()
# Register signal handlers.
self.register_signal_handlers()
- # Create connection to the hub.
- self.transport = transport.PakfireHubTransport(self.config)
-
while self.__running:
# Check if the build root is file.
if not self.check_buildroot():
def get_new_build_job(self, timeout=600):
log.debug("Requesting new job...")
+
try:
- job = self.transport.get_json("/builders/jobs/queue",
+ job = self.hub._request("/builders/jobs/queue", method="GET", decode="json",
data={ "timeout" : timeout, }, timeout=timeout)
if job:
"message" : message or "",
}
- self.transport.post("/builders/jobs/%s/state/%s" % (job.id, state),
- data=data)
+ self.hub._request("/builders/jobs/%s/state/%s" % (job.id, state),
+ method="POST", data=data)
def upload_file(self, job, filename, type):
assert os.path.exists(filename)
assert type in ("package", "log")
# First upload the file data and save the upload_id.
- upload_id = self.transport.upload_file(filename)
+ upload_id = self.hub.upload_file(filename)
data = {
"type" : type,
}
# Add the file to the build.
- self.transport.post("/builders/jobs/%s/addfile/%s" % (job.id, upload_id),
- data=data)
+ self.hub._request("/builders/jobs/%s/addfile/%s" % (job.id, upload_id),
+ method="POST", data=data)
def upload_buildroot(self, job, installed_packages):
pkgs = []
data = { "buildroot" : json.dumps(pkgs) }
- self.transport.post("/builders/jobs/%s/buildroot" % job.id, data=data)
+ self.hub._request("/builders/jobs/%s/buildroot" % job.id, method="POST", data=data)