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:
2024-08-25 15:52:43 +02:00
parent e442800779
commit 05701d9d85
11 changed files with 215 additions and 73 deletions

View File

@ -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);

View File

@ -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
View 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);
}
}

View File

@ -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;
}