feat: inject pipeline env and dont use system

This commit is contained in:
Asger Gitz-Johansen 2024-08-06 18:38:42 +02:00
parent 1bcdca9e21
commit d8b8b7b729
6 changed files with 47 additions and 8 deletions

View File

@ -13,7 +13,10 @@ typedef optional_type(pipeline_conf*) optional_pipeline_conf;
typedef struct { typedef struct {
const struct inotify_event* event; const struct inotify_event* event;
const char* command; char* name;
char* url;
char* trigger;
char* command;
} pipeline_event; } pipeline_event;
// create a new pipeline_conf struct instance based on a configuration line. // create a new pipeline_conf struct instance based on a configuration line.

View File

@ -22,4 +22,6 @@ char* trim(const char* const str);
typedef void(*line_handler)(const char*); typedef void(*line_handler)(const char*);
void per_line(const char* file, line_handler handler); void per_line(const char* file, line_handler handler);
char* join(const char* a, const char* b);
#endif #endif

View File

@ -4,7 +4,7 @@ AssertPathExists=/etc/sci/pipelines.conf
[Service] [Service]
Type=exec Type=exec
ExecStart=/usr/local/bin/sci ExecStart=/usr/local/bin/sci -f /etc/sci/pipelines.conf
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View File

@ -6,16 +6,39 @@
#include "util.h" #include "util.h"
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <spawn.h>
void executor(void* data) { #include <wait.h>
const char* command = data;
system(command);
}
threadpool* pool = NULL; threadpool* pool = NULL;
void executor(void* data) {
const pipeline_event* const e = data;
pid_t pid;
char* name = join("SCI_PIPELINE_NAME=", e->name);
char* url = join("SCI_PIPELINE_URL=", e->url);
char* trigger = join("SCI_PIPELINE_TRIGGER=", e->trigger);
char *envp[] = {
name,
url,
trigger,
NULL
};
char* argv[] = { "/bin/sh", "-c", e->command, NULL };
if(posix_spawn(&pid, "/bin/sh", NULL, NULL, argv, envp) != 0) {
perror("posix_spawn");
return;
}
int status;
waitpid(pid, &status, 0);
if(WIFEXITED(status))
log_trace("pipeline exited with status %d", WEXITSTATUS(status));
free(name);
free(url);
free(trigger);
}
void on_event(pipeline_event* const e) { void on_event(pipeline_event* const e) {
if(!threadpool_add_work(pool, executor, (void*)e->command)) if(!threadpool_add_work(pool, executor, (void*)e))
log_error("could not add work to the threadpool"); log_error("could not add work to the threadpool");
} }

View File

@ -24,6 +24,9 @@ void listen_for_changes(const pipeline_conf* config, notify_callback callback) {
struct inotify_event* e = (struct inotify_event*)&buffer[i]; struct inotify_event* e = (struct inotify_event*)&buffer[i];
pipeline_event ev; pipeline_event ev;
ev.event = e; ev.event = e;
ev.name = config->name;
ev.url = config->url;
ev.trigger = config->trigger;
ev.command = config->command; ev.command = config->command;
callback(&ev); callback(&ev);
i += EV_SIZE + e->len; i += EV_SIZE + e->len;

View File

@ -36,3 +36,11 @@ void per_line(const char* file, line_handler handler) {
free(line); free(line);
fclose(stream); fclose(stream);
} }
char* join(const char* a, const char* b) {
size_t alen = strlen(a);
size_t blen = strlen(b);
char* result = malloc(alen + blen + 1);
sprintf(result, "%s%s", a, b);
return result;
}