wip: custom environment variable passing
You should be able to tell your sci deployment which env vars should be passed to the pipelines with -e ENV1 -e ENV2 and so on
This commit is contained in:
parent
e442800779
commit
05701d9d85
5
Makefile
5
Makefile
@ -29,14 +29,15 @@ all: out/bin/sci
|
||||
out/obj/%.o: src/%.c | $(OBJDIR)
|
||||
$(CC) -c $? $(CFLAGS) -o $@
|
||||
|
||||
OBJ += out/obj/main.o
|
||||
OBJ += out/obj/cli.o
|
||||
OBJ += out/obj/log.o
|
||||
OBJ += out/obj/main.o
|
||||
OBJ += out/obj/notify.o
|
||||
OBJ += out/obj/util.o
|
||||
OBJ += out/obj/pipeline.o
|
||||
OBJ += out/obj/strlist.o
|
||||
OBJ += out/obj/threadlist.o
|
||||
OBJ += out/obj/threadpool.o
|
||||
OBJ += out/obj/util.o
|
||||
out/bin/sci: $(OBJ) | $(BINDIR)
|
||||
$(CC) -o $@ $^ $(CFLAGS)
|
||||
|
||||
|
2
TODO.md
2
TODO.md
@ -18,6 +18,8 @@
|
||||
- [-] ~~docker~~ later.
|
||||
- [ ] Eight things eight, try it out! - maybe even write the python webhook extension.
|
||||
- [ ] Port this document to gitea issue tracking
|
||||
- [x] enable PATH-able programs and argv in the command section
|
||||
- [ ] custom environment variable passing. Something like `-e MY_TOKEN` ala docker-style
|
||||
- [ ] Ninth things ninth, fix bugs, see below
|
||||
- [ ] Tenth things tenth, write manpages, choose license
|
||||
- [ ] Eleventh things Eleventh, polish
|
||||
|
@ -29,6 +29,7 @@ typedef struct {
|
||||
bool use_colors;
|
||||
optional_str log_file;
|
||||
optional_str pipeline_log_dir;
|
||||
optional_strlist environment_vars;
|
||||
} cli_options;
|
||||
|
||||
// Construct a new cli_options struct instance.
|
||||
|
@ -17,6 +17,7 @@
|
||||
*/
|
||||
#ifndef SCI_OPTIONAL_H
|
||||
#define SCI_OPTIONAL_H
|
||||
#include "strlist.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#define optional_type(type) struct { bool has_value; type value; }
|
||||
@ -24,5 +25,6 @@ typedef optional_type(int) optional_int;
|
||||
typedef optional_type(float) optional_float;
|
||||
typedef optional_type(char*) optional_str;
|
||||
typedef optional_type(const char*) optional_cstr;
|
||||
typedef optional_type(strlist_node*) optional_strlist;
|
||||
|
||||
#endif
|
||||
|
51
include/strlist.h
Normal file
51
include/strlist.h
Normal file
@ -0,0 +1,51 @@
|
||||
/**
|
||||
* sci - a simple ci system
|
||||
Copyright (C) 2024 Asger Gitz-Johansen
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef SCI_STRLIST_H
|
||||
#define SCI_STRLIST_H
|
||||
|
||||
// doubly linked list
|
||||
typedef struct strlist_node {
|
||||
char* str;
|
||||
struct strlist_node* previous;
|
||||
struct strlist_node* next;
|
||||
} strlist_node;
|
||||
|
||||
// Create a new root node.
|
||||
// This function is not threadsafe.
|
||||
strlist_node* create_strlist_node(char* str);
|
||||
|
||||
// Add a new string to the string list.
|
||||
// This function is not threadsafe.
|
||||
strlist_node* add_str(char* str, strlist_node* root);
|
||||
|
||||
// add a new string list node to the list.
|
||||
// This function is not threadsafe.
|
||||
strlist_node* add_str_node(strlist_node* str_node, strlist_node* root);
|
||||
|
||||
// Remove a string list node from the list.
|
||||
// This will free the str and stitch the "previous" and "next" ptrs.
|
||||
// This function is not threadsafe.
|
||||
void remove_strlist_node(strlist_node* node);
|
||||
|
||||
// Completely clear the list.
|
||||
// The list is completely invalid after this call and should be discarded.
|
||||
// root itself will not be free'd by this function, but all content will be.
|
||||
// This function is not threadsafe.
|
||||
void clear_strlist(strlist_node* root);
|
||||
|
||||
#endif
|
@ -42,9 +42,8 @@ void remove_thread_node(pthread_list_node* node);
|
||||
// Completely clear the thread list.
|
||||
// This will call pthread_join on all nodes.
|
||||
// The list is completely invalid after this call and should be discarded.
|
||||
// Note:
|
||||
// - `root` has already been free'd.
|
||||
// - this function is not thread-safe.
|
||||
// Even root itself will be free'd by this function so it should be discarded as well.
|
||||
// This function is not thread-safe.
|
||||
void clear_thread_list(pthread_list_node* root);
|
||||
|
||||
#endif
|
||||
|
@ -2,6 +2,7 @@
|
||||
# NOTE: This script assumes that the url is a .tar.gz file.
|
||||
# TODO: check if $# is >= 1 and give a warning that the extract dir should be provided.
|
||||
set -ex # print all that we're doing (no need for echo's)
|
||||
env
|
||||
tmpdir=$(mktemp -d)
|
||||
wget "$SCI_PIPELINE_URL" -P "$tmpdir"
|
||||
cd "$tmpdir"
|
||||
|
38
src/cli.c
38
src/cli.c
@ -15,11 +15,12 @@
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "cli.h"
|
||||
#include "log.h"
|
||||
#include <getopt.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <getopt.h>
|
||||
#include "cli.h"
|
||||
|
||||
cli_options new_options() {
|
||||
cli_options result;
|
||||
@ -50,6 +51,19 @@ cli_options new_options() {
|
||||
char* pipeline_log_dir = getenv("SCI_PIPELINE_LOG_DIR");
|
||||
result.pipeline_log_dir.has_value = pipeline_log_dir != NULL;
|
||||
result.pipeline_log_dir.value = pipeline_log_dir;
|
||||
|
||||
char* environment_vars = getenv("SCI_PIPELINE_ENV_VARS");
|
||||
if(environment_vars == NULL) {
|
||||
result.environment_vars.has_value = false;
|
||||
result.environment_vars.value = NULL;
|
||||
} else {
|
||||
char* tok = strtok(environment_vars, ";");
|
||||
result.environment_vars.has_value = true;
|
||||
result.environment_vars.value = create_strlist_node(tok);
|
||||
tok = strtok(NULL, ";");
|
||||
while(tok != NULL)
|
||||
add_str(tok, result.environment_vars.value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -60,24 +74,27 @@ void destroy_options(cli_options v) {
|
||||
free(v.log_file.value);
|
||||
if(v.pipeline_log_dir.has_value)
|
||||
free(v.pipeline_log_dir.value);
|
||||
if(v.environment_vars.has_value)
|
||||
clear_strlist(v.environment_vars.value);
|
||||
}
|
||||
|
||||
// <max
|
||||
const char* optstring = "f:L:e:v:Cl:hV";
|
||||
const char* optstring = "f:L:w:v:Cl:e:hV";
|
||||
const char* help_msg =
|
||||
"%s %s\n"
|
||||
"Usage: [-f file] [-L dir] [-e count] [-v level] \n"
|
||||
" [-C] [-l file] [-h] [-V]\n"
|
||||
"Usage: [-f file] [-L dir] [-w count] [-v level] \n"
|
||||
" [-C] [-l file] [-e ENV] [-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"
|
||||
" -w 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 sci's log to output to a file\n"
|
||||
" -e ENV Pass an env variable to pipelines\n"
|
||||
" -h Show this message and exit\n"
|
||||
" -V Show version and exit\n"
|
||||
"\n"
|
||||
@ -106,7 +123,7 @@ cli_options parse(int argc, char** argv) {
|
||||
case 'v':
|
||||
options.verbosity = atoi(optarg);
|
||||
break;
|
||||
case 'e':
|
||||
case 'w':
|
||||
options.executors = atoi(optarg);
|
||||
break;
|
||||
case 'C':
|
||||
@ -122,6 +139,13 @@ cli_options parse(int argc, char** argv) {
|
||||
case 'h':
|
||||
options.help = true;
|
||||
break;
|
||||
case 'e':
|
||||
if(!options.environment_vars.has_value) {
|
||||
options.environment_vars.has_value = true;
|
||||
options.environment_vars.value = create_strlist_node(optarg);
|
||||
} else
|
||||
add_str(optarg, options.environment_vars.value);
|
||||
break;
|
||||
default:
|
||||
print_help(stderr, argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
|
@ -71,7 +71,6 @@ disable all warnings.
|
||||
.SH AUTHOR
|
||||
Asger Gitz\-Johansen <asger.gitz@hotmail.com>.
|
||||
|
||||
\" TODO: decide on license
|
||||
.SH COPYRIGHT
|
||||
Copyright (C) 2024 Asger Gitz-Johansen
|
||||
|
||||
|
62
src/strlist.c
Normal file
62
src/strlist.c
Normal file
@ -0,0 +1,62 @@
|
||||
#include "strlist.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_STRLEN 512
|
||||
|
||||
strlist_node* create_strlist_node(char* str) {
|
||||
strlist_node* new = malloc(sizeof(strlist_node));
|
||||
new->previous = NULL;
|
||||
new->next = NULL;
|
||||
if(str)
|
||||
new->str = strndup(str, MAX_STRLEN);
|
||||
else
|
||||
new->str = NULL;
|
||||
return new;
|
||||
}
|
||||
|
||||
strlist_node* add_str(char* str, strlist_node* root) {
|
||||
strlist_node* cursor = root;
|
||||
while(cursor->next != NULL)
|
||||
cursor = cursor->next;
|
||||
strlist_node* new = malloc(sizeof(strlist_node));
|
||||
new->previous = cursor;
|
||||
new->next = NULL;
|
||||
new->str = strndup(str, MAX_STRLEN);
|
||||
cursor->next = new;
|
||||
return new;
|
||||
}
|
||||
|
||||
strlist_node* add_str_node(strlist_node* root, strlist_node* node) {
|
||||
strlist_node* cursor = root;
|
||||
while(cursor->next != NULL)
|
||||
cursor = cursor->next;
|
||||
node->previous = cursor;
|
||||
node->next = NULL;
|
||||
cursor->next = node;
|
||||
return node;
|
||||
}
|
||||
|
||||
void remove_strlist_node(strlist_node* node) {
|
||||
strlist_node* prev = node->previous;
|
||||
strlist_node* next = node->next;
|
||||
if(node->str)
|
||||
free(node->str);
|
||||
node->str = NULL;
|
||||
free(node);
|
||||
if(prev != NULL)
|
||||
prev->next = next;
|
||||
if(next != NULL)
|
||||
next->previous = prev;
|
||||
}
|
||||
|
||||
void clear_strlist(strlist_node* root) {
|
||||
strlist_node* cursor = root;
|
||||
while(cursor != NULL) {
|
||||
cursor = cursor->next;
|
||||
if(cursor->str)
|
||||
free(cursor->str);
|
||||
cursor->str = NULL;
|
||||
free(cursor->previous);
|
||||
}
|
||||
}
|
120
src/util.c
120
src/util.c
@ -22,45 +22,45 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
char* trim(const char* const str) {
|
||||
char* begin = strdup(str);
|
||||
char* end;
|
||||
while(isspace((unsigned char)*begin))
|
||||
begin++;
|
||||
if(*begin == 0)
|
||||
return begin;
|
||||
end = begin + strlen(begin) - 1;
|
||||
while(end > begin && isspace((unsigned char)*end))
|
||||
end--;
|
||||
*(end + 1) = '\0';
|
||||
return begin;
|
||||
char* begin = strdup(str);
|
||||
char* end;
|
||||
while(isspace((unsigned char)*begin))
|
||||
begin++;
|
||||
if(*begin == 0)
|
||||
return begin;
|
||||
end = begin + strlen(begin) - 1;
|
||||
while(end > begin && isspace((unsigned char)*end))
|
||||
end--;
|
||||
*(end + 1) = '\0';
|
||||
return begin;
|
||||
}
|
||||
|
||||
void per_line(const char* file, line_handler handler) {
|
||||
FILE* stream;
|
||||
char* line = NULL;
|
||||
size_t len = 0;
|
||||
ssize_t nread;
|
||||
log_trace("reading file %s", file);
|
||||
stream = fopen(file, "r");
|
||||
if(stream == NULL) {
|
||||
perror("fopen");
|
||||
return;
|
||||
}
|
||||
while((nread = getline(&line, &len, stream)) != -1) {
|
||||
char* line_trimmed = trim(line);
|
||||
handler(line_trimmed);
|
||||
free(line_trimmed);
|
||||
}
|
||||
free(line);
|
||||
fclose(stream);
|
||||
FILE* stream;
|
||||
char* line = NULL;
|
||||
size_t len = 0;
|
||||
ssize_t nread;
|
||||
log_trace("reading file %s", file);
|
||||
stream = fopen(file, "r");
|
||||
if(stream == NULL) {
|
||||
perror("fopen");
|
||||
return;
|
||||
}
|
||||
while((nread = getline(&line, &len, stream)) != -1) {
|
||||
char* line_trimmed = trim(line);
|
||||
handler(line_trimmed);
|
||||
free(line_trimmed);
|
||||
}
|
||||
free(line);
|
||||
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;
|
||||
size_t alen = strlen(a);
|
||||
size_t blen = strlen(b);
|
||||
char* result = malloc(alen + blen + 1);
|
||||
sprintf(result, "%s%s", a, b);
|
||||
return result;
|
||||
}
|
||||
|
||||
const char* skip_arg(const char* cp) {
|
||||
@ -122,31 +122,31 @@ char** argv_split(const char* str, int* argc_out) {
|
||||
}
|
||||
|
||||
int which(const char* program_name, char* out_full_program, int max_path) {
|
||||
assert(out_full_program);
|
||||
assert(max_path > 0);
|
||||
// sanity check - maybe program_name is actually a full-path to begin with
|
||||
if(access(program_name, X_OK) == 0) {
|
||||
snprintf(out_full_program, max_path, "%s", program_name);
|
||||
return 0;
|
||||
}
|
||||
char* path = getenv("PATH");
|
||||
if (path == NULL) {
|
||||
log_error("PATH environment variable not found.");
|
||||
return -1;
|
||||
}
|
||||
char* path_cpy = strdup(path);
|
||||
char* dir = strtok(path_cpy, ":");
|
||||
char full_path[PATH_MAX];
|
||||
while(dir != NULL) {
|
||||
snprintf(full_path, sizeof(full_path), "%s/%s", dir, program_name);
|
||||
if(access(full_path, X_OK) == 0) {
|
||||
snprintf(out_full_program, max_path, "%s", full_path);
|
||||
free(path_cpy);
|
||||
return 0;
|
||||
}
|
||||
dir = strtok(NULL, ":");
|
||||
}
|
||||
log_error("'%s' not found in PATH", program_name);
|
||||
free(path_cpy);
|
||||
return -1;
|
||||
assert(out_full_program);
|
||||
assert(max_path > 0);
|
||||
// sanity check - maybe program_name is actually a full-path to begin with
|
||||
if(access(program_name, X_OK) == 0) {
|
||||
snprintf(out_full_program, max_path, "%s", program_name);
|
||||
return 0;
|
||||
}
|
||||
char* path = getenv("PATH");
|
||||
if (path == NULL) {
|
||||
log_error("PATH environment variable not found.");
|
||||
return -1;
|
||||
}
|
||||
char* path_cpy = strdup(path);
|
||||
char* dir = strtok(path_cpy, ":");
|
||||
char full_path[PATH_MAX];
|
||||
while(dir != NULL) {
|
||||
snprintf(full_path, sizeof(full_path), "%s/%s", dir, program_name);
|
||||
if(access(full_path, X_OK) == 0) {
|
||||
snprintf(out_full_program, max_path, "%s", full_path);
|
||||
free(path_cpy);
|
||||
return 0;
|
||||
}
|
||||
dir = strtok(NULL, ":");
|
||||
}
|
||||
log_error("'%s' not found in PATH", program_name);
|
||||
free(path_cpy);
|
||||
return -1;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user