From: Michael Tremer Date: Tue, 28 Jan 2025 15:49:26 +0000 (+0000) Subject: jobs: Upload packages after the build is done X-Git-Tag: 0.9.30~316 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=803aa08d83f5c5aa9544aa6fb8804f0687511a2b;p=pakfire.git jobs: Upload packages after the build is done Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/daemon.c b/src/pakfire/daemon.c index f857e270..fcb55408 100644 --- a/src/pakfire/daemon.c +++ b/src/pakfire/daemon.c @@ -59,6 +59,9 @@ struct pakfire_daemon { // HTTP Client struct pakfire_httpclient* client; + // Build Service + struct pakfire_buildservice* service; + // URL char url[PATH_MAX]; @@ -1094,6 +1097,8 @@ static void pakfire_daemon_free(struct pakfire_daemon* daemon) { sd_event_source_unref(daemon->connect_timer); if (daemon->stats_timer) sd_event_source_unref(daemon->stats_timer); + if (daemon->service) + pakfire_buildservice_unref(daemon->service); if (daemon->client) pakfire_httpclient_unref(daemon->client); if (daemon->cgroup) @@ -1147,6 +1152,11 @@ int pakfire_daemon_create(struct pakfire_daemon** daemon, struct pakfire_ctx* ct if (r) goto ERROR; + // Connect to the build service + r = pakfire_buildservice_create(&d->service, d->ctx, d->url); + if (r < 0) + goto ERROR; + // Create the cgroup r = pakfire_cgroup_create(&d->cgroup, d->ctx, NULL, "pakfire-daemon", 0); if (r < 0) @@ -1190,6 +1200,10 @@ sd_event* pakfire_daemon_loop(struct pakfire_daemon* daemon) { return sd_event_ref(daemon->loop); } +struct pakfire_buildservice* pakfire_daemon_buildservice(struct pakfire_daemon* daemon) { + return pakfire_buildservice_ref(daemon->service); +} + struct pakfire_httpclient* pakfire_daemon_httpclient(struct pakfire_daemon* daemon) { return pakfire_httpclient_ref(daemon->client); } diff --git a/src/pakfire/daemon.h b/src/pakfire/daemon.h index ccca574c..2bbffc57 100644 --- a/src/pakfire/daemon.h +++ b/src/pakfire/daemon.h @@ -25,6 +25,7 @@ struct pakfire_daemon; +#include #include #include @@ -34,6 +35,7 @@ struct pakfire_daemon* pakfire_daemon_ref(struct pakfire_daemon* daemon); struct pakfire_daemon* pakfire_daemon_unref(struct pakfire_daemon* daemon); sd_event* pakfire_daemon_loop(struct pakfire_daemon* daemon); +struct pakfire_buildservice* pakfire_daemon_buildservice(struct pakfire_daemon* daemon); struct pakfire_httpclient* pakfire_daemon_httpclient(struct pakfire_daemon* daemon); const char* pakfire_daemon_url(struct pakfire_daemon* daemon); diff --git a/src/pakfire/job.c b/src/pakfire/job.c index d764d94f..4f4303b6 100644 --- a/src/pakfire/job.c +++ b/src/pakfire/job.c @@ -49,8 +49,12 @@ struct pakfire_job { struct pakfire_ctx* ctx; int nrefs; + // Daemon struct pakfire_daemon* daemon; + // Build Service + struct pakfire_buildservice* service; + // Event Loop sd_event* loop; @@ -101,6 +105,9 @@ struct pakfire_job { PAKFIRE_JOB_STATE_EXITED, PAKFIRE_JOB_STATE_KILLED, } state; + + // Uploads + char** uploads; }; static int pakfire_parse_job(struct pakfire_job* job, json_object* data) { @@ -236,6 +243,10 @@ static void pakfire_job_free(struct pakfire_job* job) { if (job->log.stderr) pakfire_log_stream_unref(job->log.stderr); + if (job->service) + pakfire_buildservice_unref(job->service); + if (job->uploads) + pakfire_strings_free(job->uploads); if (job->config) pakfire_config_unref(job->config); if (job->loop) @@ -337,6 +348,60 @@ ERROR: return r; } +static int pakfire_job_result(struct pakfire_ctx* ctx, struct pakfire* pakfire, + struct pakfire_build* build, struct pakfire_archive* archive, void* data) { + struct pakfire_job* job = data; + struct pakfire_package* pkg = NULL; + char* uuid = NULL; + int r; + + // Fetch package metadata + r = pakfire_archive_make_package(archive, NULL, &pkg); + if (r < 0) + goto ERROR; + + // Fetch NEVRA + const char* nevra = pakfire_package_get_string(pkg, PAKFIRE_PKG_NEVRA); + if (!nevra) { + r = -EINVAL; + goto ERROR; + } + + // Fetch filename + const char* filename = pakfire_package_get_filename(pkg); + if (!filename) { + r = -EINVAL; + goto ERROR; + } + + // Fetch path + const char* path = pakfire_archive_get_path(archive); + if (!path) { + r = -EINVAL; + goto ERROR; + } + + // Upload the file + r = pakfire_buildservice_upload(job->service, path, filename, &uuid); + if (r < 0) { + ERROR(job->ctx, "Could not upload %s: %s\n", nevra, strerror(-r)); + goto ERROR; + } + + // Store the ID of the upload + r = pakfire_strings_append(&job->uploads, uuid); + if (r < 0) + goto ERROR; + +ERROR: + if (pkg) + pakfire_package_unref(pkg); + if (uuid) + free(uuid); + + return r; +} + /* This method is triggered by SIGCHLD whenever the job exits */ @@ -522,7 +587,7 @@ static int pakfire_job_child(struct pakfire_job* job) { // XXX need to set target // Run the build - r = pakfire_build_exec(build, job->pkg, NULL, NULL); + r = pakfire_build_exec(build, job->pkg, pakfire_job_result, job); ERROR: if (build) @@ -817,6 +882,9 @@ int pakfire_job_create(struct pakfire_job** job, struct pakfire_ctx* ctx, goto ERROR; } + // Fetch a reference to the build service + j->service = pakfire_daemon_buildservice(daemon); + // Initialize the PID file descriptor j->pidfd = -1;