feat: add option to set where pipeline logfiles go
This commit is contained in:
parent
2ebbc86799
commit
6c1102db43
@ -11,6 +11,7 @@ typedef struct {
|
||||
bool version;
|
||||
bool use_colors;
|
||||
optional_str log_file;
|
||||
optional_str pipeline_log_dir;
|
||||
} cli_options;
|
||||
|
||||
// Construct a new cli_options struct instance.
|
||||
|
20
src/cli.c
20
src/cli.c
@ -21,6 +21,9 @@ cli_options new_options() {
|
||||
|
||||
result.log_file.has_value = false;
|
||||
result.log_file.value = NULL;
|
||||
|
||||
result.pipeline_log_dir.has_value = false;
|
||||
result.pipeline_log_dir.value = NULL;
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -29,28 +32,33 @@ void destroy_options(cli_options v) {
|
||||
free(v.config_file.value);
|
||||
if(v.log_file.has_value)
|
||||
free(v.log_file.value);
|
||||
if(v.pipeline_log_dir.has_value)
|
||||
free(v.pipeline_log_dir.value);
|
||||
}
|
||||
|
||||
// <max
|
||||
const char* optstring = "f:e:v:Cl:hV";
|
||||
const char* optstring = "f:L:e:v:Cl:hV";
|
||||
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"
|
||||
SCI_DESCRIPTION "\n"
|
||||
"\n"
|
||||
"OPTIONS:\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"
|
||||
" -v level Set verbosity level [0-4]\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"
|
||||
" -V Show version and exit\n"
|
||||
;
|
||||
// <max
|
||||
|
||||
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) {
|
||||
@ -62,6 +70,10 @@ cli_options parse(int argc, char** argv) {
|
||||
options.config_file.value = strdup(optarg);
|
||||
options.config_file.has_value = true;
|
||||
break;
|
||||
case 'L':
|
||||
options.pipeline_log_dir.value = strdup(optarg);
|
||||
options.pipeline_log_dir.has_value = true;
|
||||
break;
|
||||
case 'v':
|
||||
options.verbosity = atoi(optarg);
|
||||
break;
|
||||
|
38
src/main.c
38
src/main.c
@ -12,34 +12,38 @@
|
||||
#include <wait.h>
|
||||
|
||||
threadpool* pool = NULL;
|
||||
char* log_dir = "./"; // NOTE: must end with a /
|
||||
|
||||
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 };
|
||||
uuid_t uuid;
|
||||
uuid_generate(uuid);
|
||||
char* pipeline_id = malloc(32);
|
||||
uuid_unparse_lower(uuid, pipeline_id);
|
||||
posix_spawn_file_actions_t actions;
|
||||
posix_spawn_file_actions_init(&actions);
|
||||
char* output_file = join(pipeline_id, ".log");
|
||||
int fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
char* log_file = join(pipeline_id, ".log");
|
||||
char* log_filepath = join(log_dir, log_file);
|
||||
int fd = open(log_filepath, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
if (fd == -1) {
|
||||
perror("open");
|
||||
return;
|
||||
}
|
||||
posix_spawn_file_actions_adddup2(&actions, fd, STDOUT_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) {
|
||||
perror("posix_spawn");
|
||||
return;
|
||||
@ -49,8 +53,10 @@ void executor(void* data) {
|
||||
waitpid(pid, &status, 0);
|
||||
if(WIFEXITED(status))
|
||||
log_trace("{%s} exited with status %d", pipeline_id, WEXITSTATUS(status));
|
||||
// TODO: clean this function up!
|
||||
free(pipeline_id);
|
||||
free(output_file);
|
||||
free(log_file);
|
||||
free(log_filepath);
|
||||
free(name);
|
||||
free(url);
|
||||
free(trigger);
|
||||
@ -127,7 +133,13 @@ int main(int argc, char** argv) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if(args.pipeline_log_dir.has_value)
|
||||
log_dir = args.pipeline_log_dir.value;
|
||||
|
||||
struct stat st = {0};
|
||||
if(stat(log_dir, &st) == -1)
|
||||
mkdir(log_dir, 0700);
|
||||
|
||||
if(stat("/tmp/sci", &st) == -1)
|
||||
mkdir("/tmp/sci", 0700);
|
||||
|
||||
|
@ -38,14 +38,11 @@ optional_pipeline_conf pipeline_create(const char* config_line) {
|
||||
result.value->url = opts[1];
|
||||
result.value->trigger = opts[2];
|
||||
result.value->command = opts[3];
|
||||
const char* msg =
|
||||
"config:\n"
|
||||
"name: %s\n"
|
||||
"url: %s\n"
|
||||
"trigger: %s\n"
|
||||
"command: %s"
|
||||
;
|
||||
log_trace(msg, result.value->name, result.value->url, result.value->trigger, result.value->command);
|
||||
log_trace("read config:");
|
||||
log_trace(" name=%s", result.value->name);
|
||||
log_trace(" url=%s", result.value->url);
|
||||
log_trace(" trigger=%s", result.value->trigger);
|
||||
log_trace(" command=%s", result.value->command);
|
||||
result.has_value = true;
|
||||
return result;
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ static threadpool_work* threadpool_work_pop(threadpool* pool) {
|
||||
return work;
|
||||
}
|
||||
|
||||
static void* threadpool_worker(void *arg) {
|
||||
static void* threadpool_worker(void* arg) {
|
||||
log_trace("threadpool worker spawned");
|
||||
threadpool* pool = arg;
|
||||
threadpool_work* work;
|
||||
|
Loading…
x
Reference in New Issue
Block a user