feat: add environment variable passing

This commit is contained in:
Asger Gitz-Johansen 2024-08-26 19:51:12 +02:00
parent cfffa43428
commit 484efc200a
7 changed files with 63 additions and 12 deletions

View File

@ -19,7 +19,7 @@
- [ ] Eight things eight, try it out! - maybe even write the python webhook extension. - [ ] Eight things eight, try it out! - maybe even write the python webhook extension.
- [ ] Port this document to gitea issue tracking - [ ] Port this document to gitea issue tracking
- [x] enable PATH-able programs and argv in the command section - [x] enable PATH-able programs and argv in the command section
- [ ] custom environment variable passing. Something like `-e MY_TOKEN` ala docker-style - [x] custom environment variable passing. Something like `-e MY_TOKEN` ala docker-style
- [x] address sanitizers please. - [x] address sanitizers please.
- [ ] Ninth things ninth, fix bugs, see below - [ ] Ninth things ninth, fix bugs, see below
- [ ] Tenth things tenth, write manpages, choose license - [ ] Tenth things tenth, write manpages, choose license

View File

@ -1,7 +1,9 @@
#ifndef SCI_EXECUTOR_H #ifndef SCI_EXECUTOR_H
#define SCI_EXECUTOR_H #define SCI_EXECUTOR_H
#include "strlist.h"
void executor(void* pipeline_event); void executor(void* pipeline_event);
void set_logdir(const char* logdir); void set_logdir(const char* logdir);
void set_shared_environment(const strlist_node* root);
#endif #endif

View File

@ -40,6 +40,7 @@ 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); char* join(const char* a, const char* b);
char* join3(const char* a, const char* b, const char* c);
const char* skip_arg(const char* cp); const char* skip_arg(const char* cp);
char* skip_spaces(const char* str); char* skip_spaces(const char* str);

View File

@ -2,6 +2,7 @@
#include "log.h" #include "log.h"
#include "optional.h" #include "optional.h"
#include "pipeline.h" #include "pipeline.h"
#include "strlist.h"
#include "util.h" #include "util.h"
#include <fcntl.h> #include <fcntl.h>
#include <linux/limits.h> #include <linux/limits.h>
@ -13,6 +14,11 @@
#include <uuid/uuid.h> #include <uuid/uuid.h>
const char* log_dir = "./"; // NOTE: must end with a / const char* log_dir = "./"; // NOTE: must end with a /
const strlist_node* shared_environment = NULL;
void set_shared_environment(const strlist_node* root) {
shared_environment = root;
}
void set_logdir(const char* logdir) { void set_logdir(const char* logdir) {
log_dir = logdir; log_dir = logdir;
@ -49,6 +55,38 @@ optional_int open_logfile(const char* const pipeline_id) {
return result; return result;
} }
void add_joined_str(strlist_node* root, const char* a, const char* b) {
char* tmp = join(a, b);
add_str(tmp, root);
free(tmp);
}
void add_env(strlist_node* root, const char* env) {
char* tmp = join3(env, "=", getenv(env));
add_str(tmp, root);
free(tmp);
}
char** create_environment(const pipeline_event* const e, const char* pipeline_id) {
char* tmp = join("PATH=", getenv("PATH")); // TODO: consider removing PATH default, since it can be done as -e PATH
strlist_node* env = create_strlist_node(tmp);
free(tmp);
add_joined_str(env, "SCI_PIPELINE_NAME=", e->name);
add_joined_str(env, "SCI_PIPELINE_URL=", e->url);
add_joined_str(env, "SCI_PIPELINE_TRIGGER=", e->trigger);
add_joined_str(env, "SCI_PIPELINE_ID=", pipeline_id);
if(shared_environment != NULL) {
const strlist_node* cursor = shared_environment;
while(cursor != NULL) {
add_env(env, cursor->str);
cursor = cursor->next;
}
}
char** envp = strlist_to_array(env);
clear_strlist(env);
return envp;
}
void executor(void* data) { void executor(void* data) {
// Create pipeline id // Create pipeline id
char* pipeline_id = create_pipeline_id(); char* pipeline_id = create_pipeline_id();
@ -67,12 +105,7 @@ void executor(void* data) {
posix_spawn_file_actions_adddup2(&actions, fd.value, STDOUT_FILENO); posix_spawn_file_actions_adddup2(&actions, fd.value, STDOUT_FILENO);
posix_spawn_file_actions_adddup2(&actions, fd.value, STDERR_FILENO); posix_spawn_file_actions_adddup2(&actions, fd.value, STDERR_FILENO);
const pipeline_event* const e = data; const pipeline_event* const e = data;
char* path = join("PATH=", getenv("PATH")); char** envp = create_environment(e, pipeline_id);
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* id = join("SCI_PIPELINE_ID=", pipeline_id);
char* envp[] = { path, name, url, trigger, id, NULL };
int argc; int argc;
char** argv = argv_split(e->command, &argc); char** argv = argv_split(e->command, &argc);
log_trace("executing pipeline %s with argv:", e->name); log_trace("executing pipeline %s with argv:", e->name);
@ -98,9 +131,11 @@ end:
argv_free(argv); argv_free(argv);
close(fd.value); close(fd.value);
free(pipeline_id); free(pipeline_id);
free(name);
free(url);
free(trigger);
free(id);
free(data); free(data);
char** cursor = envp;
while(*cursor != NULL) {
free(*cursor);
cursor++;
}
free(envp);
} }

View File

@ -110,6 +110,9 @@ int main(int argc, char** argv) {
fprintf(stderr, "no such file or directory %s\n", args.config_file.value); fprintf(stderr, "no such file or directory %s\n", args.config_file.value);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if(args.environment_vars.has_value)
set_shared_environment(args.environment_vars.value);
pool = threadpool_create(args.executors); pool = threadpool_create(args.executors);
per_line(args.config_file.value, &config_interpret_line); per_line(args.config_file.value, &config_interpret_line);

View File

@ -76,7 +76,8 @@ char** strlist_to_array(strlist_node* root) {
if(len <= 0) if(len <= 0)
return NULL; return NULL;
char** result = malloc(sizeof(char*) * (len + 1)); char** result = malloc(sizeof(char*) * (len + 1));
memset(result, len+1, (size_t)NULL); for(int i = 0; i < len+1; i++)
result[i] = NULL;
strlist_node* cursor = root; strlist_node* cursor = root;
for(int i = 0; i < len; i++) { for(int i = 0; i < len; i++) {
if(cursor == NULL) if(cursor == NULL)

View File

@ -63,6 +63,15 @@ char* join(const char* a, const char* b) {
return result; return result;
} }
char* join3(const char* a, const char* b, const char* c) {
size_t alen = strlen(a);
size_t blen = strlen(b);
size_t clen = strlen(c);
char* result = malloc(alen + blen + clen + 1);
sprintf(result, "%s%s%s", a, b, c);
return result;
}
const char* skip_arg(const char* cp) { const char* skip_arg(const char* cp) {
while(*cp && !isspace(*cp)) while(*cp && !isspace(*cp))
cp++; cp++;