feat: add option to set where pipeline logfiles go

This commit is contained in:
Asger Gitz-Johansen 2024-08-06 20:05:20 +02:00
parent 2ebbc86799
commit 6c1102db43
5 changed files with 48 additions and 26 deletions

View File

@ -11,6 +11,7 @@ typedef struct {
bool version; bool version;
bool use_colors; bool use_colors;
optional_str log_file; optional_str log_file;
optional_str pipeline_log_dir;
} cli_options; } cli_options;
// Construct a new cli_options struct instance. // Construct a new cli_options struct instance.

View File

@ -21,6 +21,9 @@ cli_options new_options() {
result.log_file.has_value = false; result.log_file.has_value = false;
result.log_file.value = NULL; result.log_file.value = NULL;
result.pipeline_log_dir.has_value = false;
result.pipeline_log_dir.value = NULL;
return result; return result;
} }
@ -29,28 +32,33 @@ void destroy_options(cli_options v) {
free(v.config_file.value); free(v.config_file.value);
if(v.log_file.has_value) if(v.log_file.has_value)
free(v.log_file.value); free(v.log_file.value);
if(v.pipeline_log_dir.has_value)
free(v.pipeline_log_dir.value);
} }
// <max // <max
const char* optstring = "f:e:v:Cl:hV"; const char* optstring = "f:L:e:v:Cl:hV";
const char* help_msg = const char* help_msg =
"Usage: %s [-f file] [-e count] [-v level] [-C] [-l file] [-h] [-V]\n" "%s %s\n"
"Usage: [-f file] [-L dir] [-e count] [-v level] \n"
" [-C] [-l file] [-h] [-V]\n"
"\n" "\n"
SCI_DESCRIPTION "\n" SCI_DESCRIPTION "\n"
"\n" "\n"
"OPTIONS:\n" "OPTIONS:\n"
" -f file Set sci config file\n" " -f file Set sci config file\n"
" -L dir Set pipeline log output directory\n"
" -e count Set the amount of worker threads\n" " -e count Set the amount of worker threads\n"
" -v level Set verbosity level [0-4]\n" " -v level Set verbosity level [0-4]\n"
" -C Force color output, ignoring $NO_COLOR\n" " -C Force color output, ignoring $NO_COLOR\n"
" -l file Set log to output to a file\n" " -l file Set sci's log to output to a file\n"
" -h Show this message and exit\n" " -h Show this message and exit\n"
" -V Show version and exit\n" " -V Show version and exit\n"
; ;
// <max // <max
void print_help(FILE * out, char* prog_name) { void print_help(FILE * out, char* prog_name) {
fprintf(out, help_msg, prog_name); fprintf(out, help_msg, prog_name, SCI_VERSION);
} }
cli_options parse(int argc, char** argv) { cli_options parse(int argc, char** argv) {
@ -62,6 +70,10 @@ cli_options parse(int argc, char** argv) {
options.config_file.value = strdup(optarg); options.config_file.value = strdup(optarg);
options.config_file.has_value = true; options.config_file.has_value = true;
break; break;
case 'L':
options.pipeline_log_dir.value = strdup(optarg);
options.pipeline_log_dir.has_value = true;
break;
case 'v': case 'v':
options.verbosity = atoi(optarg); options.verbosity = atoi(optarg);
break; break;

View File

@ -12,34 +12,38 @@
#include <wait.h> #include <wait.h>
threadpool* pool = NULL; threadpool* pool = NULL;
char* log_dir = "./"; // NOTE: must end with a /
void executor(void* data) { void executor(void* data) {
const pipeline_event* const e = data; const pipeline_event* const e = data;
pid_t pid; 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 };
uuid_t uuid; uuid_t uuid;
uuid_generate(uuid); uuid_generate(uuid);
char* pipeline_id = malloc(32); char* pipeline_id = malloc(32);
uuid_unparse_lower(uuid, pipeline_id); uuid_unparse_lower(uuid, pipeline_id);
posix_spawn_file_actions_t actions; posix_spawn_file_actions_t actions;
posix_spawn_file_actions_init(&actions); posix_spawn_file_actions_init(&actions);
char* output_file = join(pipeline_id, ".log"); char* log_file = join(pipeline_id, ".log");
int fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); char* log_filepath = join(log_dir, log_file);
int fd = open(log_filepath, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1) { if (fd == -1) {
perror("open"); perror("open");
return; return;
} }
posix_spawn_file_actions_adddup2(&actions, fd, STDOUT_FILENO); posix_spawn_file_actions_adddup2(&actions, fd, STDOUT_FILENO);
posix_spawn_file_actions_adddup2(&actions, fd, STDERR_FILENO); posix_spawn_file_actions_adddup2(&actions, fd, STDERR_FILENO);
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[] = {
name,
url,
trigger,
id,
NULL
};
char* argv[] = { "/bin/sh", "-c", e->command, NULL };
if(posix_spawn(&pid, "/bin/sh", &actions, NULL, argv, envp) != 0) { if(posix_spawn(&pid, "/bin/sh", &actions, NULL, argv, envp) != 0) {
perror("posix_spawn"); perror("posix_spawn");
return; return;
@ -49,8 +53,10 @@ void executor(void* data) {
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
if(WIFEXITED(status)) if(WIFEXITED(status))
log_trace("{%s} exited with status %d", pipeline_id, WEXITSTATUS(status)); log_trace("{%s} exited with status %d", pipeline_id, WEXITSTATUS(status));
// TODO: clean this function up!
free(pipeline_id); free(pipeline_id);
free(output_file); free(log_file);
free(log_filepath);
free(name); free(name);
free(url); free(url);
free(trigger); free(trigger);
@ -127,7 +133,13 @@ int main(int argc, char** argv) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if(args.pipeline_log_dir.has_value)
log_dir = args.pipeline_log_dir.value;
struct stat st = {0}; struct stat st = {0};
if(stat(log_dir, &st) == -1)
mkdir(log_dir, 0700);
if(stat("/tmp/sci", &st) == -1) if(stat("/tmp/sci", &st) == -1)
mkdir("/tmp/sci", 0700); mkdir("/tmp/sci", 0700);

View File

@ -38,14 +38,11 @@ optional_pipeline_conf pipeline_create(const char* config_line) {
result.value->url = opts[1]; result.value->url = opts[1];
result.value->trigger = opts[2]; result.value->trigger = opts[2];
result.value->command = opts[3]; result.value->command = opts[3];
const char* msg = log_trace("read config:");
"config:\n" log_trace(" name=%s", result.value->name);
"name: %s\n" log_trace(" url=%s", result.value->url);
"url: %s\n" log_trace(" trigger=%s", result.value->trigger);
"trigger: %s\n" log_trace(" command=%s", result.value->command);
"command: %s"
;
log_trace(msg, result.value->name, result.value->url, result.value->trigger, result.value->command);
result.has_value = true; result.has_value = true;
return result; return result;
} }