]> git.ipfire.org Git - pakfire.git/commitdiff
daemon: Parse the received job
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 13 Aug 2024 18:47:55 +0000 (18:47 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 13 Aug 2024 18:47:55 +0000 (18:47 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/daemon.c

index 3c37c87b88b10b0c3031d068cdaa613b451e303b..ce348e95d7b69f6787b38fc11f14b17ac16e626e 100644 (file)
 #include <systemd/sd-daemon.h>
 #include <systemd/sd-event.h>
 
+#include <uuid/uuid.h>
+
 #include <pakfire/buildservice.h>
+#include <pakfire/constants.h>
 #include <pakfire/ctx.h>
 #include <pakfire/daemon.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
+struct pakfire_worker {
+       uuid_t job_id;
+
+       char name[NAME_MAX];
+       char arch[ARCH_MAX];
+
+       // Flags
+       enum pakfire_worker_flags {
+               PAKFIRE_WORKER_TEST   = (1 << 0),
+               PAKFIRE_WORKER_CCACHE = (1 << 1),
+       } flags;
+
+       // Package URL
+       char pkg[PATH_MAX];
+
+       // ccache path
+       char ccache_path[PATH_MAX];
+
+       // Configuration
+       char conf[4096];
+};
+
 struct pakfire_daemon {
        struct pakfire_ctx* ctx;
        int nrefs;
@@ -53,10 +79,150 @@ static int pakfire_daemon_terminate(sd_event_source* source,
        return sd_event_exit(sd_event_source_get_event(source), 0);
 }
 
+static int pakfire_parse_job(struct pakfire_daemon* daemon,
+               struct pakfire_worker* worker, json_object* data) {
+       json_object* ccache = NULL;
+       json_object* o = NULL;
+       const char* s = NULL;
+       int r;
+
+       // Fetch the Job ID
+       if (!json_object_object_get_ex(data, "id", &o)) {
+               CTX_ERROR(daemon->ctx, "Job does not have an ID\n");
+
+               return -EINVAL;
+       }
+
+       s = json_object_get_string(o);
+
+       // Parse the Job ID
+       r = uuid_parse(s, worker->job_id);
+       if (r) {
+               CTX_ERROR(daemon->ctx, "Could not parse the Job ID (%s)\n", s);
+
+               return -EINVAL;
+       }
+
+       // Fetch the name
+       if (!json_object_object_get_ex(data, "name", &o)) {
+               CTX_ERROR(daemon->ctx, "Job does not have a name\n");
+
+               return -EINVAL;
+       }
+
+       // Store the name
+       r = pakfire_string_set(worker->name, json_object_get_string(o));
+       if (r) {
+               CTX_ERROR(daemon->ctx, "Could not store name: %m\n");
+
+               return r;
+       }
+
+       // Fetch the arch
+       if (!json_object_object_get_ex(data, "arch", &o)) {
+               CTX_ERROR(daemon->ctx, "Job does not have an architecture\n");
+
+               return -EINVAL;
+       }
+
+       // Store the arch
+       r = pakfire_string_set(worker->arch, json_object_get_string(o));
+       if (r) {
+               CTX_ERROR(daemon->ctx, "Could not store arch: %m\n");
+
+               return r;
+       }
+
+       // Fetch ccache settings
+       if (json_object_object_get_ex(data, "ccache", &ccache)) {
+               if (json_object_object_get_ex(ccache, "enabled", &o)) {
+                       if (json_object_get_boolean(o)) {
+                               worker->flags |= PAKFIRE_WORKER_CCACHE;
+                       }
+               }
+
+               // Path
+               if (json_object_object_get_ex(ccache, "path", &o)) {
+                       r = pakfire_string_set(worker->ccache_path, json_object_get_string(o));
+                       if (r) {
+                               CTX_ERROR(daemon->ctx, "Could not store the ccache path: %m\n");
+
+                               return r;
+                       }
+               }
+       }
+
+       // Check if this is a test job
+       if (json_object_object_get_ex(data, "test", &o)) {
+               if (json_object_get_boolean(o)) {
+                       worker->flags |= PAKFIRE_WORKER_TEST;
+               }
+       }
+
+       // Fetch the configuration
+       if (!json_object_object_get_ex(data, "conf", &o)) {
+               CTX_ERROR(daemon->ctx, "Job does not have a configuration\n");
+
+               return -EINVAL;
+       }
+
+       // Store the configuration
+       r = pakfire_string_set(worker->conf, json_object_get_string(o));
+       if (r) {
+               CTX_ERROR(daemon->ctx, "Could not store the configuration: %m\n");
+
+               return r;
+       }
+
+       // Fetch the package URL
+       if (!json_object_object_get_ex(data, "pkg", &o)) {
+               CTX_ERROR(daemon->ctx, "Job does not have a package URL\n");
+
+               return -EINVAL;
+       }
+
+       // Store the package URL
+       r = pakfire_string_set(worker->pkg, json_object_get_string(o));
+       if (r) {
+               CTX_ERROR(daemon->ctx, "Could not store the package URL: %m\n");
+
+               return r;
+       }
+
+       CTX_DEBUG(daemon->ctx, "Job parsing completed\n");
+
+       return 0;
+}
+
 /*
        Called when a new job has been received
 */
 static int pakfire_daemon_job(struct pakfire_daemon* daemon, json_object* m) {
+       struct pakfire_worker worker = {};
+       struct json_object* data = NULL;
+       char job_id[UUID_STR_LEN];
+       int r;
+
+       // Fetch the data from the message
+       if (!json_object_object_get_ex(m, "data", &data)) {
+               CTX_ERROR(daemon->ctx, "Job does not have any data\n");
+
+               return -EINVAL;
+       }
+
+       // Parse the job
+       r = pakfire_parse_job(daemon, &worker, data);
+       if (r)
+               return r;
+
+       // Format the Job ID as string
+       uuid_unparse_lower(worker.job_id, job_id);
+
+       CTX_INFO(daemon->ctx, "Received a new job:\n");
+       CTX_INFO(daemon->ctx, "  ID   : %s\n", job_id);
+       CTX_INFO(daemon->ctx, "  Name : %s\n", worker.name);
+       CTX_INFO(daemon->ctx, "  Arch : %s\n", worker.arch);
+
        return 0;
 }