From 3e40eeff2d87d2b504c37046353c161b9aaa7318 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 19 Nov 2022 23:32:51 +0100 Subject: [PATCH 01/97] feat(package): started rewrite in C --- .editorconfig | 2 +- .gitignore | 2 +- src/package/c/dynarray.c | 43 +++++++++++++++++ src/package/c/dynarray.h | 14 ++++++ src/package/c/package.c | 94 ++++++++++++++++++++++++++++++++++++ src/package/c/package.h | 25 ++++++++++ src/package/c/package_info.c | 46 ++++++++++++++++++ src/package/c/package_info.h | 39 +++++++++++++++ 8 files changed, 263 insertions(+), 2 deletions(-) create mode 100644 src/package/c/dynarray.c create mode 100644 src/package/c/dynarray.h create mode 100644 src/package/c/package.c create mode 100644 src/package/c/package.h create mode 100644 src/package/c/package_info.c create mode 100644 src/package/c/package_info.h diff --git a/.editorconfig b/.editorconfig index e23a3c7..e9bac80 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,6 +5,6 @@ root = true end_of_line = lf insert_final_newline = true -[*.v] +[*.{v,c,h}] # vfmt wants it :( indent_style = tab diff --git a/.gitignore b/.gitignore index aaec9ef..daeb3d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -*.c +vieter.c /data/ # Build artifacts diff --git a/src/package/c/dynarray.c b/src/package/c/dynarray.c new file mode 100644 index 0000000..a70feaf --- /dev/null +++ b/src/package/c/dynarray.c @@ -0,0 +1,43 @@ +#include "dynarray.h" + +struct dyn_array { + char **array; + size_t size; + size_t capacity; +}; + +DynArray *dynarray_init(size_t initial_capacity) { + DynArray *da = malloc(sizeof(DynArray)); + da->size = 0; + da->capacity = initial_capacity; + + return da; +} + +void dynarray_add(DynArray *da, const char *s) { + // An empty dynarray does not have an allocated internal array yet + if (da->size == 0) { + da->array = malloc(sizeof(char*) * da->capacity); + } + // Double array size if it's full + else if (da->size == da->capacity) { + da->array = realloc(da->array, da->capacity * 2); + da->capacity *= 2; + } + + da->array[da->size] = strdup(s); + da->size++; +} + +void dynarray_free(DynArray **ptp) { + DynArray *da = *ptp; + + for (size_t i = 0; i < da->size; i++) { + free(da->array[i]); + } + + free(da->array); + free(da); + + *ptp = NULL; +} diff --git a/src/package/c/dynarray.h b/src/package/c/dynarray.h new file mode 100644 index 0000000..73f5a0a --- /dev/null +++ b/src/package/c/dynarray.h @@ -0,0 +1,14 @@ +#ifndef VIETER_DYNARRAY +#define VIETER_DYNARRAY + +#include +#include + +typedef struct dyn_array DynArray; + +DynArray *dynarray_init(size_t initial_capacity); +void dynarray_add(DynArray *da, const char * s); +char ** dynarray_get_array(DynArray *da); +void dynarray_free(DynArray **ptp); + +#endif diff --git a/src/package/c/package.c b/src/package/c/package.c new file mode 100644 index 0000000..a248c92 --- /dev/null +++ b/src/package/c/package.c @@ -0,0 +1,94 @@ +#include "package.h" + +static char *ignored_names[5] = { + ".BUILDINFO", + ".INSTALL", + ".MTREE", + ".PKGINFO", + ".CHANGELOG" +}; +static int ignored_words_len = sizeof(ignored_names) / sizeof(char *); + +inline Pkg *package_init() { + return calloc(sizeof(PkgInfo), 1); +} + +Pkg *package_read_archive(const char *pkg_path) { + struct archive *a = archive_read_new(); + struct archive_entry *entry = archive_entry_new(); + + // These three are the most commonly used compression methods + archive_read_support_filter_zstd(a); + archive_read_support_filter_gzip(a); + archive_read_support_filter_xz(a); + + // Contents should always be a tarball + archive_read_support_format_tar(a); + + // TODO where does this 10240 come from? + int r = archive_read_open_filename(a, pkg_path, 10240); + + // Exit early if we weren't able to successfully open the archive for reading + if (r != ARCHIVE_OK) { + return NULL; + } + + int compression_code = archive_filter_code(a, 0); + const char *path_name; + + PkgInfo *pkg_info; + DynArray *files = dynarray_init(16); + + while (archive_read_next_header(a, &entry) == ARCHIVE_OK) { + path_name = archive_entry_pathname(entry); + + bool ignore = false; + + for (size_t i = 0; i < ignored_words_len; i++) { + if (strcmp(path_name, ignored_names[i]) == 0) { + ignore = true; + break; + } + } + + if (!ignore) { + dynarray_add(files, path_name); + } + + if (strcmp(path_name, ".PKGINFO") == 0) { + // Read data of file into memory buffer + int size = archive_entry_size(entry); + char *buf = malloc(size); + archive_read_data(a, buf, size); + + // Parse package info string into a struct + pkg_info = package_info_init(); + package_info_parse(pkg_info, buf); + + free(buf); + } else { + archive_read_data_skip(a); + } + } + + // Get size of file + struct stat stats; + + if (stat(pkg_path, &stats) != 0) { + return NULL; + } + + pkg_info->csize = stats.st_size; + + archive_read_free(a); + archive_entry_free(entry); + + // Create final return value + Pkg *pkg = package_init(); + pkg->path = strdup(pkg_path); + pkg->info = pkg_info; + pkg->files = files; + pkg->compression = compression_code; + + return pkg; +} diff --git a/src/package/c/package.h b/src/package/c/package.h new file mode 100644 index 0000000..76ec5ac --- /dev/null +++ b/src/package/c/package.h @@ -0,0 +1,25 @@ +#ifndef VIETER_PACKAGE +#define VIETER_PACKAGE + +#include +#include +#include +#include + +#include "archive.h" +#include "archive_entry.h" + +#include "package_info.h" +#include "dynarray.h" + +typedef struct pkg { + char *path; + PkgInfo *info; + DynArray *files; + int compression; +} Pkg; + +Pkg *package_read_archive(const char *pkg_path); +void package_free(Pkg ** ptp); + +#endif diff --git a/src/package/c/package_info.c b/src/package/c/package_info.c new file mode 100644 index 0000000..a8959ff --- /dev/null +++ b/src/package/c/package_info.c @@ -0,0 +1,46 @@ +#include "package_info.h" + +PkgInfo *package_info_init() { + PkgInfo *pkg_info = calloc(1, sizeof(PkgInfo)); + + pkg_info->groups = dynarray_init(4); + pkg_info->licenses = dynarray_init(4); + pkg_info->replaces = dynarray_init(4); + pkg_info->depends = dynarray_init(4); + pkg_info->conflicts = dynarray_init(4); + pkg_info->provides = dynarray_init(4); + pkg_info->optdepends = dynarray_init(4); + pkg_info->makedepends = dynarray_init(4); + pkg_info->checkdepends = dynarray_init(4); + + return pkg_info; +} + +void package_info_free(PkgInfo **ptp) { + PkgInfo *pkg_info = *ptp; + + FREE_STRING(pkg_info->name); + FREE_STRING(pkg_info->base); + FREE_STRING(pkg_info->version); + FREE_STRING(pkg_info->description); + FREE_STRING(pkg_info->url); + FREE_STRING(pkg_info->arch); + FREE_STRING(pkg_info->packager); + FREE_STRING(pkg_info->pgpsig); + + dynarray_free(&pkg_info->groups); + dynarray_free(&pkg_info->licenses); + dynarray_free(&pkg_info->replaces); + dynarray_free(&pkg_info->depends); + dynarray_free(&pkg_info->conflicts); + dynarray_free(&pkg_info->provides); + dynarray_free(&pkg_info->optdepends); + dynarray_free(&pkg_info->makedepends); + dynarray_free(&pkg_info->checkdepends); + + *ptp = NULL; +} + +void package_info_parse(PkgInfo *pkg_info, const char *pkg_info_str) { + +} diff --git a/src/package/c/package_info.h b/src/package/c/package_info.h new file mode 100644 index 0000000..7e16f4b --- /dev/null +++ b/src/package/c/package_info.h @@ -0,0 +1,39 @@ +#ifndef VIETER_PACKAGE_INFO +#define VIETER_PACKAGE_INFO + +#define FREE_STRING(sp) if (sp != NULL) free(sp) + +#include + +#include "dynarray.h" + +typedef struct pkg_info { + char *name; + char *base; + char *version; + char *description; + int64_t size; + int64_t csize; + char *url; + char *arch; + int64_t build_date; + char *packager; + char *pgpsig; + int64_t pgpsigsize; + + DynArray *groups; + DynArray *licenses; + DynArray *replaces; + DynArray *depends; + DynArray *conflicts; + DynArray *provides; + DynArray *optdepends; + DynArray *makedepends; + DynArray *checkdepends; +} PkgInfo; + +PkgInfo *package_info_init(); +void package_info_parse(PkgInfo *pkg_info, const char *pkg_info_str); +void package_info_free(PkgInfo **ptp); + +#endif From 32ff1206000f4371fde32597be72d6ab5cf964e6 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 19 Nov 2022 23:53:42 +0100 Subject: [PATCH 02/97] chore: figure out how to compile everything with C --- Makefile | 3 ++- src/package/c/package.c | 2 +- src/package/package.c.v | 13 +++++++++++++ src/package/v.mod | 3 +++ src/{package => util}/c/dynarray.c | 0 src/{package => util}/c/dynarray.h | 0 src/util/util.c.v | 7 +++++++ src/util/v.mod | 3 +++ 8 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/package/package.c.v create mode 100644 src/package/v.mod rename src/{package => util}/c/dynarray.c (100%) rename src/{package => util}/c/dynarray.h (100%) create mode 100644 src/util/util.c.v create mode 100644 src/util/v.mod diff --git a/Makefile b/Makefile index e716807..5927d41 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,8 @@ SRC_DIR := src SOURCES != find '$(SRC_DIR)' -iname '*.v' V_PATH ?= v -V := $(V_PATH) -showcc -gc boehm -W -d use_openssl +# We need to use GCC because TCC doesn't like the way we use C bindings +V := $(V_PATH) -showcc -gc boehm -W -d use_openssl -cc gcc all: vieter diff --git a/src/package/c/package.c b/src/package/c/package.c index a248c92..d2e3c94 100644 --- a/src/package/c/package.c +++ b/src/package/c/package.c @@ -9,7 +9,7 @@ static char *ignored_names[5] = { }; static int ignored_words_len = sizeof(ignored_names) / sizeof(char *); -inline Pkg *package_init() { +Pkg *package_init() { return calloc(sizeof(PkgInfo), 1); } diff --git a/src/package/package.c.v b/src/package/package.c.v new file mode 100644 index 0000000..a9c4784 --- /dev/null +++ b/src/package/package.c.v @@ -0,0 +1,13 @@ +module package + +#flag -I @VMODROOT/c + +// We need to specify *every* C file here. Otherwise, Vieter doesn't compile. +#flag @VMODROOT/c/package.o +#flag @VMODROOT/c/package_info.o + +#include "package.h" + +struct C.Pkg{} + +fn C.package_read_archive(pkg_path &char) &C.pkg diff --git a/src/package/v.mod b/src/package/v.mod new file mode 100644 index 0000000..dc06e78 --- /dev/null +++ b/src/package/v.mod @@ -0,0 +1,3 @@ +Module{ + name: 'package' +} diff --git a/src/package/c/dynarray.c b/src/util/c/dynarray.c similarity index 100% rename from src/package/c/dynarray.c rename to src/util/c/dynarray.c diff --git a/src/package/c/dynarray.h b/src/util/c/dynarray.h similarity index 100% rename from src/package/c/dynarray.h rename to src/util/c/dynarray.h diff --git a/src/util/util.c.v b/src/util/util.c.v new file mode 100644 index 0000000..997a18d --- /dev/null +++ b/src/util/util.c.v @@ -0,0 +1,7 @@ +module util + +#flag -I @VMODROOT/c + +// This makes the V compiler include this object file when linking, allowing +// all other C parts of the codebase to use it as well. +#flag @VMODROOT/c/dynarray.o diff --git a/src/util/v.mod b/src/util/v.mod new file mode 100644 index 0000000..39a57af --- /dev/null +++ b/src/util/v.mod @@ -0,0 +1,3 @@ +Module{ + name: 'util' +} From 65a756da48942f4052ad2c8f515b82115f7870ff Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sun, 20 Nov 2022 16:32:46 +0100 Subject: [PATCH 03/97] refactor: moved dynarray back to package model --- src/{util => package}/c/dynarray.c | 0 src/{util => package}/c/dynarray.h | 0 src/package/package.c.v | 1 + src/util/util.c.v | 7 ------- src/util/v.mod | 3 --- 5 files changed, 1 insertion(+), 10 deletions(-) rename src/{util => package}/c/dynarray.c (100%) rename src/{util => package}/c/dynarray.h (100%) delete mode 100644 src/util/util.c.v delete mode 100644 src/util/v.mod diff --git a/src/util/c/dynarray.c b/src/package/c/dynarray.c similarity index 100% rename from src/util/c/dynarray.c rename to src/package/c/dynarray.c diff --git a/src/util/c/dynarray.h b/src/package/c/dynarray.h similarity index 100% rename from src/util/c/dynarray.h rename to src/package/c/dynarray.h diff --git a/src/package/package.c.v b/src/package/package.c.v index a9c4784..25272f8 100644 --- a/src/package/package.c.v +++ b/src/package/package.c.v @@ -5,6 +5,7 @@ module package // We need to specify *every* C file here. Otherwise, Vieter doesn't compile. #flag @VMODROOT/c/package.o #flag @VMODROOT/c/package_info.o +#flag @VMODROOT/c/dynarray.o #include "package.h" diff --git a/src/util/util.c.v b/src/util/util.c.v deleted file mode 100644 index 997a18d..0000000 --- a/src/util/util.c.v +++ /dev/null @@ -1,7 +0,0 @@ -module util - -#flag -I @VMODROOT/c - -// This makes the V compiler include this object file when linking, allowing -// all other C parts of the codebase to use it as well. -#flag @VMODROOT/c/dynarray.o diff --git a/src/util/v.mod b/src/util/v.mod deleted file mode 100644 index 39a57af..0000000 --- a/src/util/v.mod +++ /dev/null @@ -1,3 +0,0 @@ -Module{ - name: 'util' -} From 3c0422b998b73227f4eed3f5d6f4390bd2c827bc Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sun, 20 Nov 2022 18:00:13 +0100 Subject: [PATCH 04/97] feat(package): first version of parse function in C --- src/package/c/package_info.c | 106 ++++++++++++++++++++++++++++++++++- src/package/c/package_info.h | 2 +- 2 files changed, 106 insertions(+), 2 deletions(-) diff --git a/src/package/c/package_info.c b/src/package/c/package_info.c index a8959ff..79d8720 100644 --- a/src/package/c/package_info.c +++ b/src/package/c/package_info.c @@ -1,3 +1,5 @@ +#include + #include "package_info.h" PkgInfo *package_info_init() { @@ -41,6 +43,108 @@ void package_info_free(PkgInfo **ptp) { *ptp = NULL; } -void package_info_parse(PkgInfo *pkg_info, const char *pkg_info_str) { +/** + * Advance the pointer until all spaces are skipped. + */ +static inline char *trim_spaces_front(char *ptr) { + while (ptr[0] == ' ') { + ptr++; + } + return ptr; +} + +/** + * Given a string pointer in the middle of a string, move over all spaces in the + * given direction. The final space is replaced with a NULL-byte. + */ +static inline void trim_spaces_back(char *ptr) { + if (ptr[0] != ' ') { + return; + } + + while (ptr[-1] == ' ') { + ptr--; + } + + ptr[0] = '\0'; +} + +#define PKG_INFO_STRING(key, field) if (strcmp(key_ptr, key) == 0) { pkg_info->field = strdup(value_ptr); goto advance; } +#define PKG_INFO_INT(key, field) if (strcmp(key_ptr, key) == 0) { pkg_info->field = atoi(value_ptr); goto advance; } +#define PKG_INFO_ARRAY(key, field) if (strcmp(key_ptr, key) == 0) { dynarray_add(pkg_info->field, value_ptr); goto advance; } + +int package_info_parse(PkgInfo *pkg_info, char *pkg_info_str) { + char *offset_ptr, *equals_ptr, *key_ptr, *value_ptr; + + bool end = false; + + // Iterate over all lines in file + while (!end) { + // This pointer will always point to the final character of the + // current line, be it the position of a newline or the NULL byte at + // the end of the entire string + offset_ptr = strchr(pkg_info_str, '\n'); + + // We replace the newline with a NULL byte. Now we know the line runs + // until the next NULL byte. + if (offset_ptr != NULL) { + offset_ptr[0] = '\0'; + } else { + // Advance pointer to the NULL byte of the string + offset_ptr = pkg_info_str + 1; + + while (*offset_ptr != '\0') { + offset_ptr++; + } + + end = true; + } + + // Skip comment lines + if (pkg_info_str[0] == '#') { + goto advance; + } + + equals_ptr = strchr(pkg_info_str, '='); + + // If a line doesn't contain an equals sign, the file is invalid + if (equals_ptr == NULL) { + return 1; + } + + // Trim whitespace from key + key_ptr = trim_spaces_front(pkg_info_str); + trim_spaces_back(equals_ptr - 1); + + // Trim spaces from value + value_ptr = trim_spaces_front(equals_ptr + 1); + trim_spaces_back(offset_ptr - 1); + + // Match key + PKG_INFO_STRING("pkgname", name); + PKG_INFO_STRING("pkgbase", base); + PKG_INFO_STRING("pkgver", version); + PKG_INFO_STRING("pkgdesc", description); + PKG_INFO_INT("size", size); + PKG_INFO_STRING("url", url); + PKG_INFO_STRING("arch", arch); + PKG_INFO_INT("builddate", build_date); + PKG_INFO_STRING("packager", packager); + PKG_INFO_STRING("pgpsig", pgpsig); + PKG_INFO_INT("pgpsigsize", pgpsigsize); + PKG_INFO_ARRAY("group", groups); + PKG_INFO_ARRAY("license", licenses); + PKG_INFO_ARRAY("replaces", replaces); + PKG_INFO_ARRAY("depend", depends); + PKG_INFO_ARRAY("optdepend", optdepends); + PKG_INFO_ARRAY("makedepend", makedepends); + PKG_INFO_ARRAY("checkdepend", checkdepends); + +advance: + pkg_info_str = offset_ptr + 1; + continue; + } + + return 0; } diff --git a/src/package/c/package_info.h b/src/package/c/package_info.h index 7e16f4b..c3df790 100644 --- a/src/package/c/package_info.h +++ b/src/package/c/package_info.h @@ -33,7 +33,7 @@ typedef struct pkg_info { } PkgInfo; PkgInfo *package_info_init(); -void package_info_parse(PkgInfo *pkg_info, const char *pkg_info_str); +int package_info_parse(PkgInfo *pkg_info, char *pkg_info_str); void package_info_free(PkgInfo **ptp); #endif From 640e2914bf42b58faabd6b837f69c1378bd38dd4 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sun, 20 Nov 2022 19:58:25 +0100 Subject: [PATCH 05/97] feat(package.c): some cleanup --- Makefile | 2 +- src/package/c/dynarray.c | 28 ++++++++++++++++------- src/package/c/dynarray.h | 9 ++++++-- src/package/c/package.c | 4 ++++ src/package/c/package.h | 1 + src/package/c/package_info.c | 43 ++++++++++++++---------------------- src/package/c/package_info.h | 2 +- 7 files changed, 50 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index 5927d41..fbcabfe 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # =====CONFIG===== SRC_DIR := src -SOURCES != find '$(SRC_DIR)' -iname '*.v' +SOURCES != find '$(SRC_DIR)' -type f \( -iname '*.v' -or -iname '*.c' -or -iname '*.h' \) V_PATH ?= v # We need to use GCC because TCC doesn't like the way we use C bindings diff --git a/src/package/c/dynarray.c b/src/package/c/dynarray.c index a70feaf..480c08f 100644 --- a/src/package/c/dynarray.c +++ b/src/package/c/dynarray.c @@ -29,15 +29,27 @@ void dynarray_add(DynArray *da, const char *s) { da->size++; } -void dynarray_free(DynArray **ptp) { - DynArray *da = *ptp; - - for (size_t i = 0; i < da->size; i++) { - free(da->array[i]); +void dynarray_free(DynArray *da) { + if (da == NULL) { + return; } - free(da->array); - free(da); + if (da->array != NULL) { + for (size_t i = 0; i < da->size; i++) { + free(da->array[i]); + } - *ptp = NULL; + free(da->array); + } + + free(da); +} + +char **dynarray_convert(DynArray *da) { + char **array = da->array; + + da->array = NULL; + dynarray_free(da); + + return array; } diff --git a/src/package/c/dynarray.h b/src/package/c/dynarray.h index 73f5a0a..e552ec4 100644 --- a/src/package/c/dynarray.h +++ b/src/package/c/dynarray.h @@ -8,7 +8,12 @@ typedef struct dyn_array DynArray; DynArray *dynarray_init(size_t initial_capacity); void dynarray_add(DynArray *da, const char * s); -char ** dynarray_get_array(DynArray *da); -void dynarray_free(DynArray **ptp); +void dynarray_free(DynArray *da); + +/** + * Convert a DynArray into an array by freeing all its surrounding components + * and returning the underlying array pointer. + */ +char **dynarray_convert(DynArray *da); #endif diff --git a/src/package/c/package.c b/src/package/c/package.c index d2e3c94..a098abb 100644 --- a/src/package/c/package.c +++ b/src/package/c/package.c @@ -92,3 +92,7 @@ Pkg *package_read_archive(const char *pkg_path) { return pkg; } + +char *package_to_description(Pkg *pkg) { + +} diff --git a/src/package/c/package.h b/src/package/c/package.h index 76ec5ac..09e91bd 100644 --- a/src/package/c/package.h +++ b/src/package/c/package.h @@ -21,5 +21,6 @@ typedef struct pkg { Pkg *package_read_archive(const char *pkg_path); void package_free(Pkg ** ptp); +char *package_to_description(Pkg *pkg); #endif diff --git a/src/package/c/package_info.c b/src/package/c/package_info.c index 79d8720..1202485 100644 --- a/src/package/c/package_info.c +++ b/src/package/c/package_info.c @@ -3,24 +3,10 @@ #include "package_info.h" PkgInfo *package_info_init() { - PkgInfo *pkg_info = calloc(1, sizeof(PkgInfo)); - - pkg_info->groups = dynarray_init(4); - pkg_info->licenses = dynarray_init(4); - pkg_info->replaces = dynarray_init(4); - pkg_info->depends = dynarray_init(4); - pkg_info->conflicts = dynarray_init(4); - pkg_info->provides = dynarray_init(4); - pkg_info->optdepends = dynarray_init(4); - pkg_info->makedepends = dynarray_init(4); - pkg_info->checkdepends = dynarray_init(4); - - return pkg_info; + return calloc(1, sizeof(PkgInfo)); } -void package_info_free(PkgInfo **ptp) { - PkgInfo *pkg_info = *ptp; - +void package_info_free(PkgInfo *pkg_info) { FREE_STRING(pkg_info->name); FREE_STRING(pkg_info->base); FREE_STRING(pkg_info->version); @@ -30,17 +16,17 @@ void package_info_free(PkgInfo **ptp) { FREE_STRING(pkg_info->packager); FREE_STRING(pkg_info->pgpsig); - dynarray_free(&pkg_info->groups); - dynarray_free(&pkg_info->licenses); - dynarray_free(&pkg_info->replaces); - dynarray_free(&pkg_info->depends); - dynarray_free(&pkg_info->conflicts); - dynarray_free(&pkg_info->provides); - dynarray_free(&pkg_info->optdepends); - dynarray_free(&pkg_info->makedepends); - dynarray_free(&pkg_info->checkdepends); + dynarray_free(pkg_info->groups); + dynarray_free(pkg_info->licenses); + dynarray_free(pkg_info->replaces); + dynarray_free(pkg_info->depends); + dynarray_free(pkg_info->conflicts); + dynarray_free(pkg_info->provides); + dynarray_free(pkg_info->optdepends); + dynarray_free(pkg_info->makedepends); + dynarray_free(pkg_info->checkdepends); - *ptp = NULL; + free(pkg_info); } /** @@ -72,7 +58,10 @@ static inline void trim_spaces_back(char *ptr) { #define PKG_INFO_STRING(key, field) if (strcmp(key_ptr, key) == 0) { pkg_info->field = strdup(value_ptr); goto advance; } #define PKG_INFO_INT(key, field) if (strcmp(key_ptr, key) == 0) { pkg_info->field = atoi(value_ptr); goto advance; } -#define PKG_INFO_ARRAY(key, field) if (strcmp(key_ptr, key) == 0) { dynarray_add(pkg_info->field, value_ptr); goto advance; } +#define PKG_INFO_ARRAY(key, field) if (strcmp(key_ptr, key) == 0) { \ + if (pkg_info->field == NULL) { pkg_info->field = dynarray_init(4); } \ + dynarray_add(pkg_info->field, value_ptr); goto advance; \ +} int package_info_parse(PkgInfo *pkg_info, char *pkg_info_str) { char *offset_ptr, *equals_ptr, *key_ptr, *value_ptr; diff --git a/src/package/c/package_info.h b/src/package/c/package_info.h index c3df790..d71386e 100644 --- a/src/package/c/package_info.h +++ b/src/package/c/package_info.h @@ -34,6 +34,6 @@ typedef struct pkg_info { PkgInfo *package_info_init(); int package_info_parse(PkgInfo *pkg_info, char *pkg_info_str); -void package_info_free(PkgInfo **ptp); +void package_info_free(PkgInfo *pkg_info); #endif From 6281ef76070f1ccf7b2bed05e06f9ca7cdf5322c Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 6 Dec 2022 13:50:25 +0100 Subject: [PATCH 06/97] feat: start of agent code --- src/agent/agent.v | 25 ++++++++++++++++++ src/agent/cli.v | 31 ++++++++++++++++++++++ src/agent/daemon.v | 65 ++++++++++++++++++++++++++++++++++++++++++++++ src/build/build.v | 10 +++++++ src/main.v | 2 ++ 5 files changed, 133 insertions(+) create mode 100644 src/agent/agent.v create mode 100644 src/agent/cli.v create mode 100644 src/agent/daemon.v diff --git a/src/agent/agent.v b/src/agent/agent.v new file mode 100644 index 0000000..3affd21 --- /dev/null +++ b/src/agent/agent.v @@ -0,0 +1,25 @@ +module agent + +import log +import os + +const log_file_name = 'vieter.agent.log' + +// agent start an agent service +pub fn agent(conf Config) ! { + // Configure logger + log_level := log.level_from_tag(conf.log_level) or { + return error('Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.') + } + + mut logger := log.Log{ + level: log_level + } + + log_file := os.join_path_single(conf.data_dir, agent.log_file_name) + logger.set_full_logpath(log_file) + logger.log_to_console_too() + + mut d := agent.agent_init(logger, conf) + d.run() +} diff --git a/src/agent/cli.v b/src/agent/cli.v new file mode 100644 index 0000000..46942ec --- /dev/null +++ b/src/agent/cli.v @@ -0,0 +1,31 @@ +module agent + +import cli +import conf as vconf + +struct Config { +pub: + log_level string = 'WARN' + api_key string + address string + data_dir string + max_concurrent_builds int = 1 + polling_frequency int = 30 + // Architecture of agent + /* arch string */ + /* image_rebuild_frequency int = 1440 */ +} + +// cmd returns the cli module that handles the cron daemon. +pub fn cmd() cli.Command { + return cli.Command{ + name: 'agent' + description: 'Start an agent service & start polling for new builds.' + execute: fn (cmd cli.Command) ! { + config_file := cmd.flags.get_string('config-file')! + conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + + agent(conf)! + } + } +} diff --git a/src/agent/daemon.v b/src/agent/daemon.v new file mode 100644 index 0000000..389a148 --- /dev/null +++ b/src/agent/daemon.v @@ -0,0 +1,65 @@ +module agent + +import log +import sync.stdatomic +import build { BuildConfig } +import client + +const ( + build_empty = 0 + build_running = 1 + build_done = 2 +) + +struct AgentDaemon { + logger shared log.Log + conf Config + // Which builds are currently running; length is same as + // conf.max_concurrent_builds + builds []BuildConfig + // Atomic variables used to detect when a build has finished; length is the + // same as conf.max_concurrent_builds + client client.Client + atomics []u64 +} + +fn agent_init(logger log.Log, conf Config) AgentDaemon { + mut d := AgentDaemon{ + logger: logger + client: client.new(conf.address, conf.api_key) + conf: conf + builds: []BuildConfig{len: conf.max_concurrent_builds} + atomics: []u64{len: conf.max_concurrent_builds} + } + + return d +} + +pub fn (mut d AgentDaemon) run() { + for { + free_builds := d.update_atomics() + + if free_builds > 0 { + + } + + } +} + +// clean_finished_builds checks for each build whether it's completed, and sets +// it to free again if so. The return value is how many fields are now set to +// free. +fn (mut d AgentDaemon) update_atomics() int { + mut count := 0 + + for i in 0 .. d.atomics.len { + if stdatomic.load_u64(&d.atomics[i]) == agent.build_done { + stdatomic.store_u64(&d.atomics[i], agent.build_empty) + count++ + } else if stdatomic.load_u64(&d.atomics[i]) == agent.build_empty { + count++ + } + } + + return count +} diff --git a/src/build/build.v b/src/build/build.v index 247df6e..b7c5cb6 100644 --- a/src/build/build.v +++ b/src/build/build.v @@ -16,6 +16,16 @@ const ( '/usr/local/bin', '/usr/bin/site_perl', '/usr/bin/vendor_perl', '/usr/bin/core_perl'] ) +pub struct BuildConfig { +pub: + id int + kind string + url string + branch string + repo string + base_image string +} + // create_build_image creates a builder image given some base image which can // then be used to build & package Arch images. It mostly just updates the // system, install some necessary packages & creates a non-root user to run diff --git a/src/main.v b/src/main.v index fc09f7e..424e328 100644 --- a/src/main.v +++ b/src/main.v @@ -9,6 +9,7 @@ import console.schedule import console.man import console.aur import cron +import agent fn main() { mut app := cli.Command{ @@ -40,6 +41,7 @@ fn main() { schedule.cmd(), man.cmd(), aur.cmd(), + agent.cmd() ] } app.setup() From 9a49d96e202208453169524585eb28882628f10f Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 6 Dec 2022 14:11:17 +0100 Subject: [PATCH 07/97] feat(build): start of server-side job queue --- src/agent/agent.v | 2 +- src/agent/cli.v | 16 +++++----- src/agent/daemon.v | 10 +++--- src/build/build.v | 10 +++--- src/build/queue.v | 70 +++++++++++++++++++++++++++++++++++++++++ src/main.v | 2 +- src/server/api_builds.v | 39 +++++++++++++++++++++++ src/server/cli.v | 14 +++++---- src/server/server.v | 39 ++++++++++++++++++++++- 9 files changed, 174 insertions(+), 28 deletions(-) create mode 100644 src/build/queue.v create mode 100644 src/server/api_builds.v diff --git a/src/agent/agent.v b/src/agent/agent.v index 3affd21..1758c85 100644 --- a/src/agent/agent.v +++ b/src/agent/agent.v @@ -20,6 +20,6 @@ pub fn agent(conf Config) ! { logger.set_full_logpath(log_file) logger.log_to_console_too() - mut d := agent.agent_init(logger, conf) + mut d := agent_init(logger, conf) d.run() } diff --git a/src/agent/cli.v b/src/agent/cli.v index 46942ec..a0a249c 100644 --- a/src/agent/cli.v +++ b/src/agent/cli.v @@ -5,15 +5,15 @@ import conf as vconf struct Config { pub: - log_level string = 'WARN' - api_key string - address string - data_dir string - max_concurrent_builds int = 1 - polling_frequency int = 30 + log_level string = 'WARN' + api_key string + address string + data_dir string + max_concurrent_builds int = 1 + polling_frequency int = 30 // Architecture of agent - /* arch string */ - /* image_rebuild_frequency int = 1440 */ + // arch string + // image_rebuild_frequency int = 1440 } // cmd returns the cli module that handles the cron daemon. diff --git a/src/agent/daemon.v b/src/agent/daemon.v index 389a148..fd5fe04 100644 --- a/src/agent/daemon.v +++ b/src/agent/daemon.v @@ -13,13 +13,13 @@ const ( struct AgentDaemon { logger shared log.Log - conf Config + conf Config // Which builds are currently running; length is same as // conf.max_concurrent_builds builds []BuildConfig // Atomic variables used to detect when a build has finished; length is the // same as conf.max_concurrent_builds - client client.Client + client client.Client atomics []u64 } @@ -39,10 +39,8 @@ pub fn (mut d AgentDaemon) run() { for { free_builds := d.update_atomics() - if free_builds > 0 { - - } - + if free_builds > 0 { + } } } diff --git a/src/build/build.v b/src/build/build.v index b7c5cb6..13d3e45 100644 --- a/src/build/build.v +++ b/src/build/build.v @@ -18,11 +18,11 @@ const ( pub struct BuildConfig { pub: - id int - kind string - url string - branch string - repo string + target_id int + kind string + url string + branch string + repo string base_image string } diff --git a/src/build/queue.v b/src/build/queue.v new file mode 100644 index 0000000..81d3fa9 --- /dev/null +++ b/src/build/queue.v @@ -0,0 +1,70 @@ +module build + +import models { Target } +import cron.expression { CronExpression, parse_expression } +import time +import datatypes { MinHeap } + +struct BuildJob { +pub: + // Earliest point this + timestamp time.Time + config BuildConfig +} + +// Overloaded operator for comparing ScheduledBuild objects +fn (r1 BuildJob) < (r2 BuildJob) bool { + return r1.timestamp < r2.timestamp +} + +pub struct BuildJobQueue { + // Schedule to use for targets without explicitely defined cron expression + default_schedule CronExpression + // Base image to use for targets without defined base image + default_base_image string +mut: + // For each architecture, a priority queue is tracked + queues map[string]MinHeap + // Each queued build job is also stored in a map, with the keys being the + // target IDs. This is used when removing or editing targets. + // jobs map[int]BuildJob +} + +pub fn new_job_queue(default_schedule CronExpression, default_base_image string) BuildJobQueue { + return BuildJobQueue{ + default_schedule: default_schedule + default_base_image: default_base_image + } +} + +// insert a new job into the queue for a given target on an architecture. +pub fn (mut q BuildJobQueue) insert(target Target, arch string) ! { + if arch !in q.queues { + q.queues[arch] = MinHeap{} + } + + ce := if target.schedule != '' { + parse_expression(target.schedule) or { + return error("Error while parsing cron expression '$target.schedule' (id $target.id): $err.msg()") + } + } else { + q.default_schedule + } + + timestamp := ce.next_from_now()! + + job := BuildJob{ + timestamp: timestamp + config: BuildConfig{ + target_id: target.id + kind: target.kind + url: target.url + branch: target.branch + repo: target.repo + // TODO make this configurable + base_image: q.default_base_image + } + } + + q.queues[arch].insert(job) +} diff --git a/src/main.v b/src/main.v index 424e328..34387bf 100644 --- a/src/main.v +++ b/src/main.v @@ -41,7 +41,7 @@ fn main() { schedule.cmd(), man.cmd(), aur.cmd(), - agent.cmd() + agent.cmd(), ] } app.setup() diff --git a/src/server/api_builds.v b/src/server/api_builds.v new file mode 100644 index 0000000..888fe9d --- /dev/null +++ b/src/server/api_builds.v @@ -0,0 +1,39 @@ +module server + +/* import web */ +/* import web.response { new_data_response, new_response } */ +/* import time */ +/* import build { BuildConfig } */ +/* // import os */ +/* // import util */ +/* // import models { BuildLog, BuildLogFilter } */ + +/* ['/api/v1/builds/poll'; auth; get] */ +/* fn (mut app App) v1_poll_build_queue() web.Result { */ +/* arch := app.query['arch'] or { */ +/* return app.json(.bad_request, new_response('Missing arch query arg.')) */ +/* } */ + +/* max_str := app.query['max'] or { */ +/* return app.json(.bad_request, new_response('Missing max query arg.')) */ +/* } */ +/* max := max_str.int() */ + +/* mut out := []BuildConfig{} */ + +/* now := time.now() */ + +/* lock app.build_queues { */ +/* mut queue := app.build_queues[arch] or { return app.json(.ok, new_data_response(out)) } */ + +/* for queue.len() > 0 && out.len < max { */ +/* next := queue.peek() or { return app.status(.internal_server_error) } */ + +/* if next.timestamp < now { */ +/* out << queue.pop() or { return app.status(.internal_server_error) }.config */ +/* } */ +/* } */ +/* } */ + +/* return app.json(.ok, new_data_response(out)) */ +/* } */ diff --git a/src/server/cli.v b/src/server/cli.v index a9644f3..2fede6c 100644 --- a/src/server/cli.v +++ b/src/server/cli.v @@ -5,12 +5,14 @@ import conf as vconf struct Config { pub: - log_level string = 'WARN' - pkg_dir string - data_dir string - api_key string - default_arch string - port int = 8000 + log_level string = 'WARN' + pkg_dir string + data_dir string + api_key string + default_arch string + global_schedule string = '0 3' + port int = 8000 + base_image string = 'archlinux:base-devel' } // cmd returns the cli submodule that handles starting the server diff --git a/src/server/server.v b/src/server/server.v index d5f6135..fb45e6d 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -6,6 +6,8 @@ import log import repo import util import db +import build { BuildJobQueue } +import cron.expression const ( log_file_name = 'vieter.log' @@ -20,9 +22,37 @@ pub: conf Config [required; web_global] pub mut: repo repo.RepoGroupManager [required; web_global] - db db.VieterDb + // Keys are the various architectures for packages + job_queue BuildJobQueue [required; web_global] + db db.VieterDb } +// fn (mut app App) init_build_queues() { +// // Initialize build queues +// mut i := 0 +// mut targets := app.db.get_targets(limit: 25) + +// default_ce := expression.parse_expression(conf.global_schedule) or { return } + +// for targets.len > 0 { +// for t in targets { +// ce := parse_expression(t.schedule) or { default_ce } + +// for arch in t.arch { +// if arch !in app.build_queues { +// app.build_queues[arch] = Minheap{} +// } + +// build_config := BuildConfig{} +// app.build_queues[arch].push(ScheduledBuild{ +// timestamp: ce.next() +// config: build_config +// }) +// } +// } +// } +//} + // server starts the web server & starts listening for requests pub fn server(conf Config) ! { // Prevent using 'any' as the default arch @@ -30,6 +60,10 @@ pub fn server(conf Config) ! { util.exit_with_message(1, "'any' is not allowed as the value for default_arch.") } + global_ce := expression.parse_expression(conf.global_schedule) or { + util.exit_with_message(1, 'Invalid global cron expression: $err.msg()') + } + // Configure logger log_level := log.level_from_tag(conf.log_level) or { util.exit_with_message(1, 'Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.') @@ -71,11 +105,14 @@ pub fn server(conf Config) ! { util.exit_with_message(1, 'Failed to initialize database: $err.msg()') } + mut queue := build.new_job_queue(global_ce, conf.base_image) + web.run(&App{ logger: logger api_key: conf.api_key conf: conf repo: repo db: db + job_queue: queue }, conf.port) } From c57de4d8ee4994b6efcc3370690835e86bd893e4 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 12 Dec 2022 20:33:51 +0100 Subject: [PATCH 08/97] feat(server): initialize job queue on start; api endpoint for polling jobs --- src/build/queue.v | 113 +++++++++++++++++++++++++++++++--------- src/server/api_builds.v | 50 ++++++------------ src/server/server.v | 52 ++++++++---------- 3 files changed, 129 insertions(+), 86 deletions(-) diff --git a/src/build/queue.v b/src/build/queue.v index 81d3fa9..65b279e 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -4,6 +4,7 @@ import models { Target } import cron.expression { CronExpression, parse_expression } import time import datatypes { MinHeap } +import util struct BuildJob { pub: @@ -23,6 +24,7 @@ pub struct BuildJobQueue { // Base image to use for targets without defined base image default_base_image string mut: + mutex shared util.Dummy // For each architecture, a priority queue is tracked queues map[string]MinHeap // Each queued build job is also stored in a map, with the keys being the @@ -39,32 +41,95 @@ pub fn new_job_queue(default_schedule CronExpression, default_base_image string) // insert a new job into the queue for a given target on an architecture. pub fn (mut q BuildJobQueue) insert(target Target, arch string) ! { - if arch !in q.queues { - q.queues[arch] = MinHeap{} - } - - ce := if target.schedule != '' { - parse_expression(target.schedule) or { - return error("Error while parsing cron expression '$target.schedule' (id $target.id): $err.msg()") + lock q.mutex { + if arch !in q.queues { + q.queues[arch] = MinHeap{} } - } else { - q.default_schedule - } - timestamp := ce.next_from_now()! - - job := BuildJob{ - timestamp: timestamp - config: BuildConfig{ - target_id: target.id - kind: target.kind - url: target.url - branch: target.branch - repo: target.repo - // TODO make this configurable - base_image: q.default_base_image + ce := if target.schedule != '' { + parse_expression(target.schedule) or { + return error("Error while parsing cron expression '$target.schedule' (id $target.id): $err.msg()") + } + } else { + q.default_schedule } - } - q.queues[arch].insert(job) + timestamp := ce.next_from_now()! + + job := BuildJob{ + timestamp: timestamp + config: BuildConfig{ + target_id: target.id + kind: target.kind + url: target.url + branch: target.branch + repo: target.repo + // TODO make this configurable + base_image: q.default_base_image + } + } + + q.queues[arch].insert(job) + } +} + +// peek shows the first job for the given architecture that's ready to be +// executed, if present. +pub fn (q &BuildJobQueue) peek(arch string) ?BuildJob { + rlock q.mutex { + if arch !in q.queues { + return none + } + + job := q.queues[arch].peek() or { return none } + + if job.timestamp < time.now() { + return job + } + } + + return none +} + +// pop removes the first job for the given architecture that's ready to be +// executed from the queue and returns it, if present. +pub fn (mut q BuildJobQueue) pop(arch string) ?BuildJob { + lock q.mutex { + if arch !in q.queues { + return none + } + + job := q.queues[arch].peek() or { return none } + + if job.timestamp < time.now() { + return q.queues[arch].pop() + } + } + + return none +} + +// pop_n tries to pop at most n available jobs for the given architecture. +pub fn (mut q BuildJobQueue) pop_n(arch string, n int) []BuildJob { + lock q.mutex { + if arch !in q.queues { + return [] + } + + mut out := []BuildJob{} + + for out.len < n { + job := q.queues[arch].peek() or { break } + + if job.timestamp < time.now() { + out << q.queues[arch].pop() or { break } + } else { + break + } + } + + return out + } + + return [] } diff --git a/src/server/api_builds.v b/src/server/api_builds.v index 888fe9d..62948cd 100644 --- a/src/server/api_builds.v +++ b/src/server/api_builds.v @@ -1,39 +1,23 @@ module server -/* import web */ -/* import web.response { new_data_response, new_response } */ -/* import time */ -/* import build { BuildConfig } */ -/* // import os */ -/* // import util */ -/* // import models { BuildLog, BuildLogFilter } */ +import web +import web.response { new_data_response, new_response } +// import os +// import util +// import models { BuildLog, BuildLogFilter } -/* ['/api/v1/builds/poll'; auth; get] */ -/* fn (mut app App) v1_poll_build_queue() web.Result { */ -/* arch := app.query['arch'] or { */ -/* return app.json(.bad_request, new_response('Missing arch query arg.')) */ -/* } */ +['/api/v1/builds/poll'; auth; get] +fn (mut app App) v1_poll_build_queue() web.Result { + arch := app.query['arch'] or { + return app.json(.bad_request, new_response('Missing arch query arg.')) + } -/* max_str := app.query['max'] or { */ -/* return app.json(.bad_request, new_response('Missing max query arg.')) */ -/* } */ -/* max := max_str.int() */ + max_str := app.query['max'] or { + return app.json(.bad_request, new_response('Missing max query arg.')) + } + max := max_str.int() -/* mut out := []BuildConfig{} */ + mut out := app.job_queue.pop_n(arch, max) -/* now := time.now() */ - -/* lock app.build_queues { */ -/* mut queue := app.build_queues[arch] or { return app.json(.ok, new_data_response(out)) } */ - -/* for queue.len() > 0 && out.len < max { */ -/* next := queue.peek() or { return app.status(.internal_server_error) } */ - -/* if next.timestamp < now { */ -/* out << queue.pop() or { return app.status(.internal_server_error) }.config */ -/* } */ -/* } */ -/* } */ - -/* return app.json(.ok, new_data_response(out)) */ -/* } */ + return app.json(.ok, new_data_response(out)) +} diff --git a/src/server/server.v b/src/server/server.v index fb45e6d..e2c19c2 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -24,34 +24,25 @@ pub mut: repo repo.RepoGroupManager [required; web_global] // Keys are the various architectures for packages job_queue BuildJobQueue [required; web_global] - db db.VieterDb + db db.VieterDb } -// fn (mut app App) init_build_queues() { -// // Initialize build queues -// mut i := 0 -// mut targets := app.db.get_targets(limit: 25) +fn (mut app App) init_job_queue() ! { + // Initialize build queues + mut targets := app.db.get_targets(limit: 25) + mut i := u64(0) -// default_ce := expression.parse_expression(conf.global_schedule) or { return } + for targets.len > 0 { + for target in targets { + for arch in target.arch { + app.job_queue.insert(target, arch.value)! + } + } -// for targets.len > 0 { -// for t in targets { -// ce := parse_expression(t.schedule) or { default_ce } - -// for arch in t.arch { -// if arch !in app.build_queues { -// app.build_queues[arch] = Minheap{} -// } - -// build_config := BuildConfig{} -// app.build_queues[arch].push(ScheduledBuild{ -// timestamp: ce.next() -// config: build_config -// }) -// } -// } -// } -//} + i += 25 + targets = app.db.get_targets(limit: 25, offset: i) + } +} // server starts the web server & starts listening for requests pub fn server(conf Config) ! { @@ -105,14 +96,17 @@ pub fn server(conf Config) ! { util.exit_with_message(1, 'Failed to initialize database: $err.msg()') } - mut queue := build.new_job_queue(global_ce, conf.base_image) - - web.run(&App{ + mut app := &App{ logger: logger api_key: conf.api_key conf: conf repo: repo db: db - job_queue: queue - }, conf.port) + job_queue: build.new_job_queue(global_ce, conf.base_image) + } + app.init_job_queue() or { + util.exit_with_message(1, 'Failed to inialize job queue: $err.msg()') + } + + web.run(app, conf.port) } From 0a5c4295e008b3687d160957328a934c85489f9b Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 12 Dec 2022 20:59:43 +0100 Subject: [PATCH 09/97] feat(server): properly reschedule jobs after polling --- src/build/queue.v | 50 ++++++++++++++++++++++++++++++++++------- src/server/api_builds.v | 6 ++--- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/build/queue.v b/src/build/queue.v index 65b279e..b704926 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -8,12 +8,16 @@ import util struct BuildJob { pub: - // Earliest point this + // Next timestamp from which point this job is allowed to be executed timestamp time.Time - config BuildConfig + // Required for calculating next timestamp after having pop'ed a job + ce CronExpression + // Actual build config sent to the agent + config BuildConfig } -// Overloaded operator for comparing ScheduledBuild objects +// Allows BuildJob structs to be sorted according to their timestamp in +// MinHeaps fn (r1 BuildJob) < (r2 BuildJob) bool { return r1.timestamp < r2.timestamp } @@ -39,7 +43,9 @@ pub fn new_job_queue(default_schedule CronExpression, default_base_image string) } } -// insert a new job into the queue for a given target on an architecture. +// insert a new target's job into the queue for the given architecture. This +// job will then be endlessly rescheduled after being pop'ed, unless removed +// explicitely. pub fn (mut q BuildJobQueue) insert(target Target, arch string) ! { lock q.mutex { if arch !in q.queues { @@ -58,6 +64,7 @@ pub fn (mut q BuildJobQueue) insert(target Target, arch string) ! { job := BuildJob{ timestamp: timestamp + ce: ce config: BuildConfig{ target_id: target.id kind: target.kind @@ -69,10 +76,25 @@ pub fn (mut q BuildJobQueue) insert(target Target, arch string) ! { } } + dump(job) q.queues[arch].insert(job) } } +// reschedule the given job by calculating the next timestamp and re-adding it +// to its respective queue. This function is called by the pop functions +// *after* having pop'ed the job. +fn (mut q BuildJobQueue) reschedule(job BuildJob, arch string) ! { + new_timestamp := job.ce.next_from_now()! + + new_job := BuildJob{ + ...job + timestamp: new_timestamp + } + + q.queues[arch].insert(new_job) +} + // peek shows the first job for the given architecture that's ready to be // executed, if present. pub fn (q &BuildJobQueue) peek(arch string) ?BuildJob { @@ -99,10 +121,17 @@ pub fn (mut q BuildJobQueue) pop(arch string) ?BuildJob { return none } - job := q.queues[arch].peek() or { return none } + mut job := q.queues[arch].peek() or { return none } if job.timestamp < time.now() { - return q.queues[arch].pop() + job = q.queues[arch].pop()? + + // TODO how do we handle this properly? Is it even possible for a + // cron expression to not return a next time if it's already been + // used before? + q.reschedule(job, arch) or {} + + return job } } @@ -119,10 +148,15 @@ pub fn (mut q BuildJobQueue) pop_n(arch string, n int) []BuildJob { mut out := []BuildJob{} for out.len < n { - job := q.queues[arch].peek() or { break } + mut job := q.queues[arch].peek() or { break } if job.timestamp < time.now() { - out << q.queues[arch].pop() or { break } + job = q.queues[arch].pop() or { break } + + // TODO idem + q.reschedule(job, arch) or {} + + out << job } else { break } diff --git a/src/server/api_builds.v b/src/server/api_builds.v index 62948cd..ec3c8ec 100644 --- a/src/server/api_builds.v +++ b/src/server/api_builds.v @@ -6,8 +6,8 @@ import web.response { new_data_response, new_response } // import util // import models { BuildLog, BuildLogFilter } -['/api/v1/builds/poll'; auth; get] -fn (mut app App) v1_poll_build_queue() web.Result { +['/api/v1/jobs/poll'; auth; get] +fn (mut app App) v1_poll_job_queue() web.Result { arch := app.query['arch'] or { return app.json(.bad_request, new_response('Missing arch query arg.')) } @@ -17,7 +17,7 @@ fn (mut app App) v1_poll_build_queue() web.Result { } max := max_str.int() - mut out := app.job_queue.pop_n(arch, max) + mut out := app.job_queue.pop_n(arch, max).map(it.config) return app.json(.ok, new_data_response(out)) } From 5bab1f77f0686a3db9eedb2ecab36d7592299655 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 12 Dec 2022 21:21:58 +0100 Subject: [PATCH 10/97] feat(agent): begin reforming for new api --- src/agent/daemon.v | 7 ++++--- src/agent/images.v | 49 ++++++++++++++++++++++++++++++++++++++++++++++ src/agent/log.v | 35 +++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 src/agent/images.v create mode 100644 src/agent/log.v diff --git a/src/agent/daemon.v b/src/agent/daemon.v index fd5fe04..71f4780 100644 --- a/src/agent/daemon.v +++ b/src/agent/daemon.v @@ -14,6 +14,8 @@ const ( struct AgentDaemon { logger shared log.Log conf Config + // List of last built builder images + builder_images []string // Which builds are currently running; length is same as // conf.max_concurrent_builds builds []BuildConfig @@ -44,9 +46,8 @@ pub fn (mut d AgentDaemon) run() { } } -// clean_finished_builds checks for each build whether it's completed, and sets -// it to free again if so. The return value is how many fields are now set to -// free. +// update_atomics checks for each build whether it's completed, and sets it to +// free again if so. The return value is how many fields are now set to free. fn (mut d AgentDaemon) update_atomics() int { mut count := 0 diff --git a/src/agent/images.v b/src/agent/images.v new file mode 100644 index 0000000..454f85f --- /dev/null +++ b/src/agent/images.v @@ -0,0 +1,49 @@ +module agent + +import time +import docker + +struct ImageManager { + images map[string]string + timestamps map[string]time.Time +} + +// clean_old_base_images tries to remove any old but still present builder +// images. +fn (mut d AgentDaemon) clean_old_base_images() { + mut i := 0 + + mut dd := docker.new_conn() or { + d.lerror('Failed to connect to Docker socket.') + return + } + + defer { + dd.close() or {} + } + + for i < d.builder_images.len - 1 { + // For each builder image, we try to remove it by calling the Docker + // API. If the function returns an error or false, that means the image + // wasn't deleted. Therefore, we move the index over. If the function + // returns true, the array's length has decreased by one so we don't + // move the index. + dd.remove_image(d.builder_images[i]) or { i += 1 } + } +} + +// rebuild_base_image builds a builder image from the given base image. +/* fn (mut d AgentDaemon) build_base_image(base_image string) bool { */ +/* d.linfo('Rebuilding builder image....') */ + +/* d.builder_images << build.create_build_image(d.base_image) or { */ +/* d.lerror('Failed to rebuild base image. Retrying in ${daemon.rebuild_base_image_retry_timout}s...') */ +/* d.image_build_timestamp = time.now().add_seconds(daemon.rebuild_base_image_retry_timout) */ + +/* return false */ +/* } */ + +/* d.image_build_timestamp = time.now().add_seconds(60 * d.image_rebuild_frequency) */ + +/* return true */ +/* } */ diff --git a/src/agent/log.v b/src/agent/log.v new file mode 100644 index 0000000..d47df0f --- /dev/null +++ b/src/agent/log.v @@ -0,0 +1,35 @@ +module agent + +import log + +// log reate a log message with the given level +pub fn (mut d AgentDaemon) log(msg string, level log.Level) { + lock d.logger { + d.logger.send_output(msg, level) + } +} + +// lfatal create a log message with the fatal level +pub fn (mut d AgentDaemon) lfatal(msg string) { + d.log(msg, log.Level.fatal) +} + +// lerror create a log message with the error level +pub fn (mut d AgentDaemon) lerror(msg string) { + d.log(msg, log.Level.error) +} + +// lwarn create a log message with the warn level +pub fn (mut d AgentDaemon) lwarn(msg string) { + d.log(msg, log.Level.warn) +} + +// linfo create a log message with the info level +pub fn (mut d AgentDaemon) linfo(msg string) { + d.log(msg, log.Level.info) +} + +// ldebug create a log message with the debug level +pub fn (mut d AgentDaemon) ldebug(msg string) { + d.log(msg, log.Level.debug) +} From 7ef8d4b846a6245258a23503862e8f95a2985d81 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 12 Dec 2022 21:50:34 +0100 Subject: [PATCH 11/97] feat(agent): wrote ImageManager --- src/agent/cli.v | 2 +- src/agent/daemon.v | 4 +-- src/agent/images.v | 73 ++++++++++++++++++++++++++-------------------- 3 files changed, 45 insertions(+), 34 deletions(-) diff --git a/src/agent/cli.v b/src/agent/cli.v index a0a249c..063d960 100644 --- a/src/agent/cli.v +++ b/src/agent/cli.v @@ -13,7 +13,7 @@ pub: polling_frequency int = 30 // Architecture of agent // arch string - // image_rebuild_frequency int = 1440 + image_rebuild_frequency int = 1440 } // cmd returns the cli module that handles the cron daemon. diff --git a/src/agent/daemon.v b/src/agent/daemon.v index 71f4780..0508790 100644 --- a/src/agent/daemon.v +++ b/src/agent/daemon.v @@ -14,8 +14,7 @@ const ( struct AgentDaemon { logger shared log.Log conf Config - // List of last built builder images - builder_images []string + images ImageManager // Which builds are currently running; length is same as // conf.max_concurrent_builds builds []BuildConfig @@ -30,6 +29,7 @@ fn agent_init(logger log.Log, conf Config) AgentDaemon { logger: logger client: client.new(conf.address, conf.api_key) conf: conf + images: new_image_manager(conf.image_rebuild_frequency) builds: []BuildConfig{len: conf.max_concurrent_builds} atomics: []u64{len: conf.max_concurrent_builds} } diff --git a/src/agent/images.v b/src/agent/images.v index 454f85f..aee2be0 100644 --- a/src/agent/images.v +++ b/src/agent/images.v @@ -2,48 +2,59 @@ module agent import time import docker +import build struct ImageManager { - images map[string]string - timestamps map[string]time.Time +mut: + refresh_frequency int + images map[string][]string [required] + timestamps map[string]time.Time [required] } -// clean_old_base_images tries to remove any old but still present builder -// images. -fn (mut d AgentDaemon) clean_old_base_images() { - mut i := 0 +fn new_image_manager(refresh_frequency int) ImageManager { + return ImageManager{ + refresh_frequency: refresh_frequency + images: map[string][]string{} + timestamps: map[string]time.Time{} + } +} - mut dd := docker.new_conn() or { - d.lerror('Failed to connect to Docker socket.') +fn (mut m ImageManager) refresh_image(base_image string) ! { + // No need to refresh the image if the previous one is still new enough + if base_image in m.timestamps + && m.timestamps[base_image].add_seconds(m.refresh_frequency) > time.now() { return } + // TODO use better image tags for built images + new_image := build.create_build_image(base_image) or { + return error('Failed to build builder image from base image $base_image') + } + + m.images[base_image] << new_image + m.timestamps[base_image] = time.now() +} + +// clean_old_images tries to remove any old but still present builder images. +fn (mut m ImageManager) clean_old_images() { + mut dd := docker.new_conn() or { return } + defer { dd.close() or {} } - for i < d.builder_images.len - 1 { - // For each builder image, we try to remove it by calling the Docker - // API. If the function returns an error or false, that means the image - // wasn't deleted. Therefore, we move the index over. If the function - // returns true, the array's length has decreased by one so we don't - // move the index. - dd.remove_image(d.builder_images[i]) or { i += 1 } + mut i := 0 + + for image in m.images.keys() { + i = 0 + + for i < m.images[image].len - 1 { + // For each builder image, we try to remove it by calling the Docker + // API. If the function returns an error or false, that means the image + // wasn't deleted. Therefore, we move the index over. If the function + // returns true, the array's length has decreased by one so we don't + // move the index. + dd.remove_image(m.images[image][i]) or { i += 1 } + } } } - -// rebuild_base_image builds a builder image from the given base image. -/* fn (mut d AgentDaemon) build_base_image(base_image string) bool { */ -/* d.linfo('Rebuilding builder image....') */ - -/* d.builder_images << build.create_build_image(d.base_image) or { */ -/* d.lerror('Failed to rebuild base image. Retrying in ${daemon.rebuild_base_image_retry_timout}s...') */ -/* d.image_build_timestamp = time.now().add_seconds(daemon.rebuild_base_image_retry_timout) */ - -/* return false */ -/* } */ - -/* d.image_build_timestamp = time.now().add_seconds(60 * d.image_rebuild_frequency) */ - -/* return true */ -/* } */ From 6f23d690a7a0a78d7d9203850c96204832149df0 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 12 Dec 2022 22:09:57 +0100 Subject: [PATCH 12/97] feat(agent): partially wrote daemon code --- src/agent/cli.v | 2 ++ src/agent/daemon.v | 77 ++++++++++++++++++++++++++++++++++++++++++++-- src/agent/images.v | 4 +++ src/build/build.v | 19 ++++++++++-- src/build/shell.v | 16 +++++----- src/client/jobs.v | 11 +++++++ 6 files changed, 116 insertions(+), 13 deletions(-) create mode 100644 src/client/jobs.v diff --git a/src/agent/cli.v b/src/agent/cli.v index 063d960..1badbab 100644 --- a/src/agent/cli.v +++ b/src/agent/cli.v @@ -6,6 +6,8 @@ import conf as vconf struct Config { pub: log_level string = 'WARN' + // Architecture that the agent represents + arch string api_key string address string data_dir string diff --git a/src/agent/daemon.v b/src/agent/daemon.v index 0508790..aabcb44 100644 --- a/src/agent/daemon.v +++ b/src/agent/daemon.v @@ -4,6 +4,8 @@ import log import sync.stdatomic import build { BuildConfig } import client +import time +import os const ( build_empty = 0 @@ -14,6 +16,7 @@ const ( struct AgentDaemon { logger shared log.Log conf Config +mut: images ImageManager // Which builds are currently running; length is same as // conf.max_concurrent_builds @@ -41,13 +44,33 @@ pub fn (mut d AgentDaemon) run() { for { free_builds := d.update_atomics() - if free_builds > 0 { + // All build slots are taken, so there's nothing to be done + if free_builds == 0 { + time.sleep(1 * time.second) + continue + } + + // Builds have finished, so old builder images might have freed up. + d.images.clean_old_images() + + // Poll for new jobs + new_configs := d.client.poll_jobs(free_builds) or { + d.lerror('Failed to poll jobs: $err.msg()') + + time.sleep(1 * time.second) + continue + } + + // Schedule new jobs + for config in new_configs { + d.start_build(config) } } } // update_atomics checks for each build whether it's completed, and sets it to -// free again if so. The return value is how many fields are now set to free. +// free again if so. The return value is how many build slots are currently +// free. fn (mut d AgentDaemon) update_atomics() int { mut count := 0 @@ -62,3 +85,53 @@ fn (mut d AgentDaemon) update_atomics() int { return count } + +// start_build starts a build for the given BuildConfig object. +fn (mut d AgentDaemon) start_build(config BuildConfig) bool { + for i in 0 .. d.atomics.len { + if stdatomic.load_u64(&d.atomics[i]) == agent.build_empty { + stdatomic.store_u64(&d.atomics[i], agent.build_running) + d.builds[i] = config + + go d.run_build(i, config) + + return true + } + } + + return false +} + +// run_build actually starts the build process for a given target. +fn (mut d AgentDaemon) run_build(build_index int, config BuildConfig) { + d.linfo('started build: $config.url -> $config.repo') + + // 0 means success, 1 means failure + mut status := 0 + + new_config := BuildConfig{ + ...config + base_image: d.images.get(config.base_image) + } + + res := build.build_config(d.client.address, d.client.api_key, new_config) or { + d.ldebug('build_config error: $err.msg()') + status = 1 + + build.BuildResult{} + } + + if status == 0 { + d.linfo('finished build: $config.url -> $config.repo; uploading logs...') + + build_arch := os.uname().machine + d.client.add_build_log(config.target_id, res.start_time, res.end_time, build_arch, + res.exit_code, res.logs) or { + d.lerror('Failed to upload logs for build: $config.url -> $config.repo') + } + } else { + d.linfo('an error occured during build: $config.url -> $config.repo') + } + + stdatomic.store_u64(&d.atomics[build_index], agent.build_done) +} diff --git a/src/agent/images.v b/src/agent/images.v index aee2be0..78bf2d0 100644 --- a/src/agent/images.v +++ b/src/agent/images.v @@ -19,6 +19,10 @@ fn new_image_manager(refresh_frequency int) ImageManager { } } +pub fn (m &ImageManager) get(base_image string) string { + return m.images[base_image].last() +} + fn (mut m ImageManager) refresh_image(base_image string) ! { // No need to refresh the image if the previous one is still new enough if base_image in m.timestamps diff --git a/src/build/build.v b/src/build/build.v index 13d3e45..744ce9c 100644 --- a/src/build/build.v +++ b/src/build/build.v @@ -103,10 +103,23 @@ pub: logs string } +pub fn build_target(address string, api_key string, base_image_id string, target &Target) !BuildResult { +config := BuildConfig{ + target_id: target.id + kind: target.kind + url: target.url + branch: target.branch + repo: target.repo + base_image: base_image_id + } + + return build_config(address, api_key, config) +} + // build_target builds, packages & publishes a given Arch package based on the // provided target. The base image ID should be of an image previously created // by create_build_image. It returns the logs of the container. -pub fn build_target(address string, api_key string, base_image_id string, target &Target) !BuildResult { +pub fn build_config(address string, api_key string, config BuildConfig) !BuildResult { mut dd := docker.new_conn()! defer { @@ -114,14 +127,14 @@ pub fn build_target(address string, api_key string, base_image_id string, target } build_arch := os.uname().machine - build_script := create_build_script(address, target, build_arch) + build_script := create_build_script(address, config, build_arch) // We convert the build script into a base64 string, which then gets passed // to the container as an env var base64_script := base64.encode_str(build_script) c := docker.NewContainer{ - image: '$base_image_id' + image: '$config.base_image' env: [ 'BUILD_SCRIPT=$base64_script', 'API_KEY=$api_key', diff --git a/src/build/shell.v b/src/build/shell.v index e573d53..42ec3c0 100644 --- a/src/build/shell.v +++ b/src/build/shell.v @@ -23,13 +23,13 @@ pub fn echo_commands(cmds []string) []string { } // create_build_script generates a shell script that builds a given Target. -fn create_build_script(address string, target &Target, build_arch string) string { - repo_url := '$address/$target.repo' +fn create_build_script(address string, config BuildConfig, build_arch string) string { + repo_url := '$address/$config.repo' mut commands := [ // This will later be replaced by a proper setting for changing the // mirrorlist - "echo -e '[$target.repo]\\nServer = $address/\$repo/\$arch\\nSigLevel = Optional' >> /etc/pacman.conf" + "echo -e '[$config.repo]\\nServer = $address/\$repo/\$arch\\nSigLevel = Optional' >> /etc/pacman.conf" // We need to update the package list of the repo we just added above. // This should however not pull in a lot of packages as long as the // builder image is rebuilt frequently. @@ -38,22 +38,22 @@ fn create_build_script(address string, target &Target, build_arch string) string 'su builder', ] - commands << match target.kind { + commands << match config.kind { 'git' { - if target.branch == '' { + if config.branch == '' { [ - "git clone --single-branch --depth 1 '$target.url' repo", + "git clone --single-branch --depth 1 '$config.url' repo", ] } else { [ - "git clone --single-branch --depth 1 --branch $target.branch '$target.url' repo", + "git clone --single-branch --depth 1 --branch $config.branch '$config.url' repo", ] } } 'url' { [ 'mkdir repo', - "curl -o repo/PKGBUILD -L '$target.url'", + "curl -o repo/PKGBUILD -L '$config.url'", ] } else { diff --git a/src/client/jobs.v b/src/client/jobs.v new file mode 100644 index 0000000..281d6ce --- /dev/null +++ b/src/client/jobs.v @@ -0,0 +1,11 @@ +module client + +import build { BuildConfig } + +pub fn (c &Client) poll_jobs(max int) ![]BuildConfig { + data := c.send_request<[]BuildConfig>(.get, '/api/v1/jobs/poll', { + 'max': max.str() + })! + + return data.data +} From 3611123f4549523f2420ac8a1157d146c9064c8d Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 12 Dec 2022 22:58:43 +0100 Subject: [PATCH 13/97] feat(agent): initial working version --- src/agent/cli.v | 4 ++-- src/agent/daemon.v | 39 ++++++++++++++++++++++++++++++++------- src/build/build.v | 18 +++++++++--------- src/build/queue.v | 1 - src/build/shell.v | 2 -- src/client/jobs.v | 5 +++-- vieter.toml | 2 +- 7 files changed, 47 insertions(+), 24 deletions(-) diff --git a/src/agent/cli.v b/src/agent/cli.v index 1badbab..a375f08 100644 --- a/src/agent/cli.v +++ b/src/agent/cli.v @@ -5,9 +5,9 @@ import conf as vconf struct Config { pub: - log_level string = 'WARN' + log_level string = 'WARN' // Architecture that the agent represents - arch string + arch string api_key string address string data_dir string diff --git a/src/agent/daemon.v b/src/agent/daemon.v index aabcb44..f060863 100644 --- a/src/agent/daemon.v +++ b/src/agent/daemon.v @@ -41,6 +41,10 @@ fn agent_init(logger log.Log, conf Config) AgentDaemon { } pub fn (mut d AgentDaemon) run() { + // This is just so that the very first time the loop is ran, the jobs are + // always polled + mut last_poll_time := time.now().add_seconds(-d.conf.polling_frequency) + for { free_builds := d.update_atomics() @@ -54,16 +58,37 @@ pub fn (mut d AgentDaemon) run() { d.images.clean_old_images() // Poll for new jobs - new_configs := d.client.poll_jobs(free_builds) or { - d.lerror('Failed to poll jobs: $err.msg()') + if time.now() >= last_poll_time.add_seconds(d.conf.polling_frequency) { + new_configs := d.client.poll_jobs(d.conf.arch, free_builds) or { + d.lerror('Failed to poll jobs: $err.msg()') + + time.sleep(5 * time.second) + continue + } + last_poll_time = time.now() + + // Schedule new jobs + for config in new_configs { + // TODO handle this better than to just skip the config + // Make sure a recent build base image is available for building the config + d.images.refresh_image(config.base_image) or { + d.lerror(err.msg()) + continue + } + d.start_build(config) + } time.sleep(1 * time.second) - continue } - - // Schedule new jobs - for config in new_configs { - d.start_build(config) + // Builds are running, so check again after one second + else if free_builds < d.conf.max_concurrent_builds { + time.sleep(1 * time.second) + } + // The agent is not doing anything, so we just wait until the next poll + // time + else { + time_until_next_poll := time.now() - last_poll_time + time.sleep(time_until_next_poll) } } } diff --git a/src/build/build.v b/src/build/build.v index 744ce9c..2d51156 100644 --- a/src/build/build.v +++ b/src/build/build.v @@ -104,16 +104,16 @@ pub: } pub fn build_target(address string, api_key string, base_image_id string, target &Target) !BuildResult { -config := BuildConfig{ - target_id: target.id - kind: target.kind - url: target.url - branch: target.branch - repo: target.repo - base_image: base_image_id - } + config := BuildConfig{ + target_id: target.id + kind: target.kind + url: target.url + branch: target.branch + repo: target.repo + base_image: base_image_id + } - return build_config(address, api_key, config) + return build_config(address, api_key, config) } // build_target builds, packages & publishes a given Arch package based on the diff --git a/src/build/queue.v b/src/build/queue.v index b704926..29036e4 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -76,7 +76,6 @@ pub fn (mut q BuildJobQueue) insert(target Target, arch string) ! { } } - dump(job) q.queues[arch].insert(job) } } diff --git a/src/build/shell.v b/src/build/shell.v index 42ec3c0..c2d0c9b 100644 --- a/src/build/shell.v +++ b/src/build/shell.v @@ -1,7 +1,5 @@ module build -import models { Target } - // escape_shell_string escapes any characters that could be interpreted // incorrectly by a shell. The resulting value should be safe to use inside an // echo statement. diff --git a/src/client/jobs.v b/src/client/jobs.v index 281d6ce..30f2531 100644 --- a/src/client/jobs.v +++ b/src/client/jobs.v @@ -2,9 +2,10 @@ module client import build { BuildConfig } -pub fn (c &Client) poll_jobs(max int) ![]BuildConfig { +pub fn (c &Client) poll_jobs(arch string, max int) ![]BuildConfig { data := c.send_request<[]BuildConfig>(.get, '/api/v1/jobs/poll', { - 'max': max.str() + 'arch': arch + 'max': max.str() })! return data.data diff --git a/vieter.toml b/vieter.toml index d3922a4..9a68ae3 100644 --- a/vieter.toml +++ b/vieter.toml @@ -4,6 +4,7 @@ data_dir = "data" pkg_dir = "data/pkgs" log_level = "DEBUG" default_arch = "x86_64" +arch = "x86_64" address = "http://localhost:8000" @@ -11,4 +12,3 @@ global_schedule = '* *' api_update_frequency = 2 image_rebuild_frequency = 1 max_concurrent_builds = 3 - From 882a9a60a973427b7d0a181dc5f2c1117cd6188f Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 13 Dec 2022 08:58:27 +0100 Subject: [PATCH 14/97] feat(build): allowed invalidating entries in build queue --- src/build/queue.v | 91 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 67 insertions(+), 24 deletions(-) diff --git a/src/build/queue.v b/src/build/queue.v index 29036e4..b559552 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -8,6 +8,8 @@ import util struct BuildJob { pub: + // Time at which this build job was created/queued + created time.Time // Next timestamp from which point this job is allowed to be executed timestamp time.Time // Required for calculating next timestamp after having pop'ed a job @@ -22,6 +24,8 @@ fn (r1 BuildJob) < (r2 BuildJob) bool { return r1.timestamp < r2.timestamp } +// The build job queue is responsible for managing the list of scheduled builds +// for each architecture. Agents receive jobs from this queue. pub struct BuildJobQueue { // Schedule to use for targets without explicitely defined cron expression default_schedule CronExpression @@ -31,15 +35,17 @@ mut: mutex shared util.Dummy // For each architecture, a priority queue is tracked queues map[string]MinHeap - // Each queued build job is also stored in a map, with the keys being the - // target IDs. This is used when removing or editing targets. - // jobs map[int]BuildJob + // When a target is removed from the server or edited, its previous build + // configs will be invalid. This map allows for those to be simply skipped + // by ignoring any build configs created before this timestamp. + invalidated map[int]time.Time } pub fn new_job_queue(default_schedule CronExpression, default_base_image string) BuildJobQueue { return BuildJobQueue{ default_schedule: default_schedule default_base_image: default_base_image + invalidated: map[int]time.Time{} } } @@ -63,6 +69,7 @@ pub fn (mut q BuildJobQueue) insert(target Target, arch string) ! { timestamp := ce.next_from_now()! job := BuildJob{ + created: time.now() timestamp: timestamp ce: ce config: BuildConfig{ @@ -88,6 +95,7 @@ fn (mut q BuildJobQueue) reschedule(job BuildJob, arch string) ! { new_job := BuildJob{ ...job + created: time.now() timestamp: new_timestamp } @@ -96,16 +104,26 @@ fn (mut q BuildJobQueue) reschedule(job BuildJob, arch string) ! { // peek shows the first job for the given architecture that's ready to be // executed, if present. -pub fn (q &BuildJobQueue) peek(arch string) ?BuildJob { +pub fn (mut q BuildJobQueue) peek(arch string) ?BuildJob { rlock q.mutex { if arch !in q.queues { return none } - job := q.queues[arch].peek() or { return none } + for { + job := q.queues[arch].peek() or { return none } - if job.timestamp < time.now() { - return job + // Skip any invalidated jobs + if job.config.target_id in q.invalidated + && job.created < q.invalidated[job.config.target_id] { + // This pop *should* never fail according to the source code + q.queues[arch].pop() or { return none } + continue + } + + if job.timestamp < time.now() { + return job + } } } @@ -120,17 +138,27 @@ pub fn (mut q BuildJobQueue) pop(arch string) ?BuildJob { return none } - mut job := q.queues[arch].peek() or { return none } + for { + mut job := q.queues[arch].peek() or { return none } - if job.timestamp < time.now() { - job = q.queues[arch].pop()? + // Skip any invalidated jobs + if job.config.target_id in q.invalidated + && job.created < q.invalidated[job.config.target_id] { + // This pop *should* never fail according to the source code + q.queues[arch].pop() or { return none } + continue + } - // TODO how do we handle this properly? Is it even possible for a - // cron expression to not return a next time if it's already been - // used before? - q.reschedule(job, arch) or {} + if job.timestamp < time.now() { + job = q.queues[arch].pop()? - return job + // TODO how do we handle this properly? Is it even possible for a + // cron expression to not return a next time if it's already been + // used before? + q.reschedule(job, arch) or {} + + return job + } } } @@ -146,18 +174,28 @@ pub fn (mut q BuildJobQueue) pop_n(arch string, n int) []BuildJob { mut out := []BuildJob{} - for out.len < n { - mut job := q.queues[arch].peek() or { break } + outer: for out.len < n { + for { + mut job := q.queues[arch].peek() or { break outer } - if job.timestamp < time.now() { - job = q.queues[arch].pop() or { break } + // Skip any invalidated jobs + if job.config.target_id in q.invalidated + && job.created < q.invalidated[job.config.target_id] { + // This pop *should* never fail according to the source code + q.queues[arch].pop() or { break outer } + continue + } - // TODO idem - q.reschedule(job, arch) or {} + if job.timestamp < time.now() { + job = q.queues[arch].pop() or { break outer } - out << job - } else { - break + // TODO idem + q.reschedule(job, arch) or {} + + out << job + } else { + break outer + } } } @@ -166,3 +204,8 @@ pub fn (mut q BuildJobQueue) pop_n(arch string, n int) []BuildJob { return [] } + +// invalidate a target's old build jobs. +pub fn (mut q BuildJobQueue) invalidate(target_id int) { + q.invalidated[target_id] = time.now() +} From b6168a3060752474bb1ba4bd961ac119eddce16f Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 13 Dec 2022 12:38:39 +0100 Subject: [PATCH 15/97] fix(build): change tests to use BuildConfig instead --- src/build/shell_test.v | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/build/shell_test.v b/src/build/shell_test.v index 341df88..d228faf 100644 --- a/src/build/shell_test.v +++ b/src/build/shell_test.v @@ -1,42 +1,48 @@ module build -import models { Target } +import models fn test_create_build_script_git_branch() { - target := Target{ - id: 1 + config := BuildConfig{ + target_id: 1 kind: 'git' url: 'https://examplerepo.com' branch: 'main' repo: 'vieter' + base_image: 'not-used:latest' } - build_script := create_build_script('https://example.com', target, 'x86_64') + + build_script := create_build_script('https://example.com', config, 'x86_64') expected := $embed_file('build_script_git_branch.sh') assert build_script == expected.to_string().trim_space() } fn test_create_build_script_git() { - target := Target{ - id: 1 + config := BuildConfig{ + target_id: 1 kind: 'git' url: 'https://examplerepo.com' repo: 'vieter' + base_image: 'not-used:latest' } - build_script := create_build_script('https://example.com', target, 'x86_64') + + build_script := create_build_script('https://example.com', config, 'x86_64') expected := $embed_file('build_script_git.sh') assert build_script == expected.to_string().trim_space() } fn test_create_build_script_url() { - target := Target{ - id: 1 + config := BuildConfig{ + target_id: 1 kind: 'url' url: 'https://examplerepo.com' repo: 'vieter' + base_image: 'not-used:latest' } - build_script := create_build_script('https://example.com', target, 'x86_64') + + build_script := create_build_script('https://example.com', config, 'x86_64') expected := $embed_file('build_script_url.sh') assert build_script == expected.to_string().trim_space() From e742d3de6da36298ba4f34c2f12820a85e08fb47 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 13 Dec 2022 13:46:07 +0100 Subject: [PATCH 16/97] fix(db): return correct id when adding targets --- src/db/logs.v | 2 ++ src/db/targets.v | 11 +++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/db/logs.v b/src/db/logs.v index 923dde2..2745467 100644 --- a/src/db/logs.v +++ b/src/db/logs.v @@ -84,6 +84,8 @@ pub fn (db &VieterDb) add_build_log(log BuildLog) int { insert log into BuildLog } + // Here, this does work because a log doesn't contain any foreign keys, + // meaning the ORM only has to do a single add inserted_id := db.conn.last_id() as int return inserted_id diff --git a/src/db/targets.v b/src/db/targets.v index a705ebb..41e56df 100644 --- a/src/db/targets.v +++ b/src/db/targets.v @@ -38,14 +38,17 @@ pub fn (db &VieterDb) get_target(target_id int) ?Target { } // add_target inserts the given target into the database. -pub fn (db &VieterDb) add_target(repo Target) int { +pub fn (db &VieterDb) add_target(target Target) int { sql db.conn { - insert repo into Target + insert target into Target } - inserted_id := db.conn.last_id() as int + // ID of inserted target is the largest id + inserted_target := sql db.conn { + select from Target order by id desc limit 1 + } - return inserted_id + return inserted_target.id } // delete_target deletes the target with the given id from the database. From 63427899217aae4390e01b72238758ac1457856d Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 13 Dec 2022 13:58:51 +0100 Subject: [PATCH 17/97] feat(server): update job queue when adding, removing or updating targets --- src/build/queue.v | 7 +++++++ src/server/api_targets.v | 29 +++++++++++++++++++---------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/build/queue.v b/src/build/queue.v index b559552..a78e56a 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -49,6 +49,13 @@ pub fn new_job_queue(default_schedule CronExpression, default_base_image string) } } +// insert_all executes insert for each architecture of the given Target. +pub fn (mut q BuildJobQueue) insert_all(target Target) ! { + for arch in target.arch { + q.insert(target, arch.value)! + } +} + // insert a new target's job into the queue for the given architecture. This // job will then be endlessly rescheduled after being pop'ed, unless removed // explicitely. diff --git a/src/server/api_targets.v b/src/server/api_targets.v index 16db7e9..dc39d37 100644 --- a/src/server/api_targets.v +++ b/src/server/api_targets.v @@ -12,17 +12,17 @@ fn (mut app App) v1_get_targets() web.Result { filter := models.from_params(app.query) or { return app.json(http.Status.bad_request, new_response('Invalid query parameters.')) } - repos := app.db.get_targets(filter) + targets := app.db.get_targets(filter) - return app.json(.ok, new_data_response(repos)) + return app.json(.ok, new_data_response(targets)) } // v1_get_single_target returns the information for a single target. ['/api/v1/targets/:id'; auth; get] fn (mut app App) v1_get_single_target(id int) web.Result { - repo := app.db.get_target(id) or { return app.not_found() } + target := app.db.get_target(id) or { return app.not_found() } - return app.json(.ok, new_data_response(repo)) + return app.json(.ok, new_data_response(target)) } // v1_post_target creates a new target from the provided query string. @@ -30,22 +30,27 @@ fn (mut app App) v1_get_single_target(id int) web.Result { fn (mut app App) v1_post_target() web.Result { mut params := app.query.clone() - // If a repo is created without specifying the arch, we assume it's meant + // If a target is created without specifying the arch, we assume it's meant // for the default architecture. if 'arch' !in params || params['arch'] == '' { params['arch'] = app.conf.default_arch } - new_repo := models.from_params(params) or { + mut new_target := models.from_params(params) or { return app.json(http.Status.bad_request, new_response(err.msg())) } // Ensure someone doesn't submit an invalid kind - if new_repo.kind !in models.valid_kinds { + if new_target.kind !in models.valid_kinds { return app.json(http.Status.bad_request, new_response('Invalid kind.')) } - id := app.db.add_target(new_repo) + id := app.db.add_target(new_target) + new_target.id = id + + // Add the target to the job queue + // TODO return better error here if it's the cron schedule that's incorrect + app.job_queue.insert_all(new_target) or { return app.status(.internal_server_error) } return app.json(.ok, new_data_response(id)) } @@ -54,6 +59,7 @@ fn (mut app App) v1_post_target() web.Result { ['/api/v1/targets/:id'; auth; delete] fn (mut app App) v1_delete_target(id int) web.Result { app.db.delete_target(id) + app.job_queue.invalidate(id) return app.json(.ok, new_response('')) } @@ -69,7 +75,10 @@ fn (mut app App) v1_patch_target(id int) web.Result { app.db.update_target_archs(id, arch_objs) } - repo := app.db.get_target(id) or { return app.status(.internal_server_error) } + target := app.db.get_target(id) or { return app.status(.internal_server_error) } - return app.json(.ok, new_data_response(repo)) + app.job_queue.invalidate(id) + app.job_queue.insert_all(target) or { return app.status(.internal_server_error) } + + return app.json(.ok, new_data_response(target)) } From 5cbfc0ebcb45e08b5d445d6e3997f7a92628a797 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 13 Dec 2022 17:42:49 +0100 Subject: [PATCH 18/97] feat(agent): clean up code a bit; add frequent polling when active --- src/agent/agent.v | 6 ++-- src/agent/cli.v | 16 +++++----- src/agent/daemon.v | 78 ++++++++++++++++++++++++++++------------------ src/agent/images.v | 31 +++++++++++++----- src/agent/log.v | 2 +- 5 files changed, 83 insertions(+), 50 deletions(-) diff --git a/src/agent/agent.v b/src/agent/agent.v index 1758c85..69b9947 100644 --- a/src/agent/agent.v +++ b/src/agent/agent.v @@ -2,12 +2,12 @@ module agent import log import os +import util const log_file_name = 'vieter.agent.log' -// agent start an agent service +// agent starts an agent service pub fn agent(conf Config) ! { - // Configure logger log_level := log.level_from_tag(conf.log_level) or { return error('Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.') } @@ -16,6 +16,8 @@ pub fn agent(conf Config) ! { level: log_level } + os.mkdir_all(conf.data_dir) or { util.exit_with_message(1, 'Failed to create data directory.') } + log_file := os.join_path_single(conf.data_dir, agent.log_file_name) logger.set_full_logpath(log_file) logger.log_to_console_too() diff --git a/src/agent/cli.v b/src/agent/cli.v index a375f08..1535e17 100644 --- a/src/agent/cli.v +++ b/src/agent/cli.v @@ -7,14 +7,12 @@ struct Config { pub: log_level string = 'WARN' // Architecture that the agent represents - arch string - api_key string - address string - data_dir string - max_concurrent_builds int = 1 - polling_frequency int = 30 - // Architecture of agent - // arch string + arch string + api_key string + address string + data_dir string + max_concurrent_builds int = 1 + polling_frequency int = 30 image_rebuild_frequency int = 1440 } @@ -22,7 +20,7 @@ pub: pub fn cmd() cli.Command { return cli.Command{ name: 'agent' - description: 'Start an agent service & start polling for new builds.' + description: 'Start an agent daemon.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! diff --git a/src/agent/daemon.v b/src/agent/daemon.v index f060863..f753e25 100644 --- a/src/agent/daemon.v +++ b/src/agent/daemon.v @@ -16,17 +16,17 @@ const ( struct AgentDaemon { logger shared log.Log conf Config + client client.Client mut: images ImageManager - // Which builds are currently running; length is same as - // conf.max_concurrent_builds + // Which builds are currently running; length is conf.max_concurrent_builds builds []BuildConfig - // Atomic variables used to detect when a build has finished; length is the - // same as conf.max_concurrent_builds - client client.Client + // Atomic variables used to detect when a build has finished; length is + // conf.max_concurrent_builds atomics []u64 } +// agent_init initializes a new agent fn agent_init(logger log.Log, conf Config) AgentDaemon { mut d := AgentDaemon{ logger: logger @@ -40,37 +40,49 @@ fn agent_init(logger log.Log, conf Config) AgentDaemon { return d } +// run starts the actual agent daemon. This function will run forever. pub fn (mut d AgentDaemon) run() { // This is just so that the very first time the loop is ran, the jobs are // always polled mut last_poll_time := time.now().add_seconds(-d.conf.polling_frequency) + mut sleep_time := 1 * time.second + mut finished, mut empty := 0, 0 for { - free_builds := d.update_atomics() + finished, empty = d.update_atomics() - // All build slots are taken, so there's nothing to be done - if free_builds == 0 { + // No new finished builds and no free slots, so there's nothing to be + // done + if finished + empty == 0 { time.sleep(1 * time.second) continue } // Builds have finished, so old builder images might have freed up. - d.images.clean_old_images() + // TODO this might query the docker daemon too frequently. + if finished > 0 { + d.images.clean_old_images() + } - // Poll for new jobs - if time.now() >= last_poll_time.add_seconds(d.conf.polling_frequency) { - new_configs := d.client.poll_jobs(d.conf.arch, free_builds) or { + // The agent will always poll for new jobs after at most + // `polling_frequency` seconds. However, when jobs have finished, the + // agent will also poll for new jobs. This is because jobs are often + // clustered together (especially when mostly using the global cron + // schedule), so there's a much higher chance jobs are available. + if finished > 0 || time.now() >= last_poll_time.add_seconds(d.conf.polling_frequency) { + new_configs := d.client.poll_jobs(d.conf.arch, finished + empty) or { d.lerror('Failed to poll jobs: $err.msg()') + // TODO pick a better delay here time.sleep(5 * time.second) continue } last_poll_time = time.now() - // Schedule new jobs for config in new_configs { // TODO handle this better than to just skip the config - // Make sure a recent build base image is available for building the config + // Make sure a recent build base image is available for + // building the config d.images.refresh_image(config.base_image) or { d.lerror(err.msg()) continue @@ -78,40 +90,45 @@ pub fn (mut d AgentDaemon) run() { d.start_build(config) } - time.sleep(1 * time.second) - } - // Builds are running, so check again after one second - else if free_builds < d.conf.max_concurrent_builds { - time.sleep(1 * time.second) + // No new jobs were scheduled and the agent isn't doing anything, + // so we just wait until the next polling period. + if new_configs.len == 0 && finished + empty == d.conf.max_concurrent_builds { + sleep_time = time.now() - last_poll_time + } } // The agent is not doing anything, so we just wait until the next poll // time - else { - time_until_next_poll := time.now() - last_poll_time - time.sleep(time_until_next_poll) + else if finished + empty == d.conf.max_concurrent_builds { + sleep_time = time.now() - last_poll_time } + + time.sleep(sleep_time) } } // update_atomics checks for each build whether it's completed, and sets it to -// free again if so. The return value is how many build slots are currently -// free. -fn (mut d AgentDaemon) update_atomics() int { - mut count := 0 +// empty again if so. The return value is a tuple `(finished, empty)` where +// `finished` is how many builds were just finished and thus set to empty, and +// `empty` is how many build slots were already empty. The amount of running +// builds can then be calculated by substracting these two values from the +// total allowed concurrent builds. +fn (mut d AgentDaemon) update_atomics() (int, int) { + mut finished := 0 + mut empty := 0 for i in 0 .. d.atomics.len { if stdatomic.load_u64(&d.atomics[i]) == agent.build_done { stdatomic.store_u64(&d.atomics[i], agent.build_empty) - count++ + finished++ } else if stdatomic.load_u64(&d.atomics[i]) == agent.build_empty { - count++ + empty++ } } - return count + return finished, empty } -// start_build starts a build for the given BuildConfig object. +// start_build starts a build for the given BuildConfig. fn (mut d AgentDaemon) start_build(config BuildConfig) bool { for i in 0 .. d.atomics.len { if stdatomic.load_u64(&d.atomics[i]) == agent.build_empty { @@ -149,6 +166,7 @@ fn (mut d AgentDaemon) run_build(build_index int, config BuildConfig) { if status == 0 { d.linfo('finished build: $config.url -> $config.repo; uploading logs...') + // TODO use the arch value here build_arch := os.uname().machine d.client.add_build_log(config.target_id, res.start_time, res.end_time, build_arch, res.exit_code, res.logs) or { diff --git a/src/agent/images.v b/src/agent/images.v index 78bf2d0..64a8f74 100644 --- a/src/agent/images.v +++ b/src/agent/images.v @@ -4,29 +4,42 @@ import time import docker import build +// An ImageManager is a utility that creates builder images from given base +// images, updating these builder images if they've become too old. This +// structure can manage images from any number of base images, paving the way +// for configurable base images per target/repository. struct ImageManager { mut: - refresh_frequency int - images map[string][]string [required] - timestamps map[string]time.Time [required] + max_image_age int [required] + // For each base images, one or more builder images can exist at the same + // time + images map[string][]string [required] + // For each base image, we track when its newest image was built + timestamps map[string]time.Time [required] } -fn new_image_manager(refresh_frequency int) ImageManager { +// new_image_manager initializes a new image manager. +fn new_image_manager(max_image_age int) ImageManager { return ImageManager{ - refresh_frequency: refresh_frequency + max_image_age: max_image_age images: map[string][]string{} timestamps: map[string]time.Time{} } } +// get returns the name of the newest image for the given base image. Note that +// this function should only be called *after* a first call to `refresh_image`. pub fn (m &ImageManager) get(base_image string) string { return m.images[base_image].last() } +// refresh_image builds a new builder image from the given base image if the +// previous builder image is too old or non-existent. This function will do +// nothing if these conditions aren't met, so it's safe to call it every time +// you want to ensure an image is up to date. fn (mut m ImageManager) refresh_image(base_image string) ! { - // No need to refresh the image if the previous one is still new enough if base_image in m.timestamps - && m.timestamps[base_image].add_seconds(m.refresh_frequency) > time.now() { + && m.timestamps[base_image].add_seconds(m.max_image_age) > time.now() { return } @@ -39,7 +52,9 @@ fn (mut m ImageManager) refresh_image(base_image string) ! { m.timestamps[base_image] = time.now() } -// clean_old_images tries to remove any old but still present builder images. +// clean_old_images removes all older builder images that are no longer in use. +// The function will always leave at least one builder image, namely the newest +// one. fn (mut m ImageManager) clean_old_images() { mut dd := docker.new_conn() or { return } diff --git a/src/agent/log.v b/src/agent/log.v index d47df0f..cd59207 100644 --- a/src/agent/log.v +++ b/src/agent/log.v @@ -2,7 +2,7 @@ module agent import log -// log reate a log message with the given level +// log a message with the given level pub fn (mut d AgentDaemon) log(msg string, level log.Level) { lock d.logger { d.logger.send_output(msg, level) From 03f2240ff63e2f115e348626941d8f7919bd3e0f Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 13 Dec 2022 17:51:42 +0100 Subject: [PATCH 19/97] chore: please the linter --- src/build/build.v | 3 ++- src/build/queue.v | 1 + src/build/shell_test.v | 2 -- src/client/jobs.v | 1 + src/server/api_builds.v | 4 +--- src/server/server.v | 2 ++ 6 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/build/build.v b/src/build/build.v index 2d51156..84d288c 100644 --- a/src/build/build.v +++ b/src/build/build.v @@ -103,6 +103,7 @@ pub: logs string } +// build_target builds the given target. Internally it calls `build_config`. pub fn build_target(address string, api_key string, base_image_id string, target &Target) !BuildResult { config := BuildConfig{ target_id: target.id @@ -116,7 +117,7 @@ pub fn build_target(address string, api_key string, base_image_id string, target return build_config(address, api_key, config) } -// build_target builds, packages & publishes a given Arch package based on the +// build_config builds, packages & publishes a given Arch package based on the // provided target. The base image ID should be of an image previously created // by create_build_image. It returns the logs of the container. pub fn build_config(address string, api_key string, config BuildConfig) !BuildResult { diff --git a/src/build/queue.v b/src/build/queue.v index a78e56a..2a28e62 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -41,6 +41,7 @@ mut: invalidated map[int]time.Time } +// new_job_queue initializes a new job queue pub fn new_job_queue(default_schedule CronExpression, default_base_image string) BuildJobQueue { return BuildJobQueue{ default_schedule: default_schedule diff --git a/src/build/shell_test.v b/src/build/shell_test.v index d228faf..8bb22d9 100644 --- a/src/build/shell_test.v +++ b/src/build/shell_test.v @@ -1,7 +1,5 @@ module build -import models - fn test_create_build_script_git_branch() { config := BuildConfig{ target_id: 1 diff --git a/src/client/jobs.v b/src/client/jobs.v index 30f2531..7fee94f 100644 --- a/src/client/jobs.v +++ b/src/client/jobs.v @@ -2,6 +2,7 @@ module client import build { BuildConfig } +// poll_jobs requests a list of new build jobs from the server. pub fn (c &Client) poll_jobs(arch string, max int) ![]BuildConfig { data := c.send_request<[]BuildConfig>(.get, '/api/v1/jobs/poll', { 'arch': arch diff --git a/src/server/api_builds.v b/src/server/api_builds.v index ec3c8ec..922b252 100644 --- a/src/server/api_builds.v +++ b/src/server/api_builds.v @@ -2,10 +2,8 @@ module server import web import web.response { new_data_response, new_response } -// import os -// import util -// import models { BuildLog, BuildLogFilter } +// v1_poll_job_queue allows agents to poll for new build jobs. ['/api/v1/jobs/poll'; auth; get] fn (mut app App) v1_poll_job_queue() web.Result { arch := app.query['arch'] or { diff --git a/src/server/server.v b/src/server/server.v index e2c19c2..1e86906 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -27,6 +27,8 @@ pub mut: db db.VieterDb } +// init_job_queue populates a fresh job queue with all the targets currently +// stored in the database. fn (mut app App) init_job_queue() ! { // Initialize build queues mut targets := app.db.get_targets(limit: 25) From d3151863ee88c7fdf75d6a569be25e76511c38c1 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 13 Dec 2022 18:24:21 +0100 Subject: [PATCH 20/97] refactor(build): remove some code duplication from queue --- CHANGELOG.md | 2 ++ README.md | 5 +-- src/build/queue.v | 88 ++++++++++++++++++++--------------------------- 3 files changed, 43 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2dd760..aed7571 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Migrated codebase to V 0.3.2 * Cron expression parser now uses bitfields instead of bool arrays +* Added option to deploy using agent-server architecture instead of cron daemon ### Fixed @@ -19,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * CLI no longer exits with non-zero status code when removing/patching target * Allow NULL values for branch in database +* Endpoint for adding targets now returns the correct id ## [0.4.0](https://git.rustybever.be/vieter-v/vieter/src/tag/0.4.0) diff --git a/README.md b/README.md index b9fff69..637d4c1 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,8 @@ quicker. I chose [V](https://vlang.io/) as I've been very intrigued by this language for a while now. I wanted a fast language that I could code while relaxing, without having to exert too much mental effort & V seemed like the right choice for -that. +that. Sadly, this didn't quite turn out the way I expected, but I'm sticking +with it anyways ;p ## Features @@ -49,7 +50,7 @@ update`. I used to maintain a mirror that tracked the latest master, but nowadays, I maintain a Docker image containing the specific compiler version that Vieter -builds with. Currently, this is V 0.3. +builds with. Currently, this is V 0.3.2. ## Contributing diff --git a/src/build/queue.v b/src/build/queue.v index 2a28e62..dd2bb87 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -110,6 +110,21 @@ fn (mut q BuildJobQueue) reschedule(job BuildJob, arch string) ! { q.queues[arch].insert(new_job) } +// pop_invalid pops all invalid jobs. +fn (mut q BuildJobQueue) pop_invalid(arch string) { + for { + job := q.queues[arch].peek() or { return } + + if job.config.target_id in q.invalidated + && job.created < q.invalidated[job.config.target_id] { + // This pop *should* never fail according to the source code + q.queues[arch].pop() or {} + } else { + break + } + } +} + // peek shows the first job for the given architecture that's ready to be // executed, if present. pub fn (mut q BuildJobQueue) peek(arch string) ?BuildJob { @@ -118,20 +133,11 @@ pub fn (mut q BuildJobQueue) peek(arch string) ?BuildJob { return none } - for { - job := q.queues[arch].peek() or { return none } + q.pop_invalid(arch) + job := q.queues[arch].peek()? - // Skip any invalidated jobs - if job.config.target_id in q.invalidated - && job.created < q.invalidated[job.config.target_id] { - // This pop *should* never fail according to the source code - q.queues[arch].pop() or { return none } - continue - } - - if job.timestamp < time.now() { - return job - } + if job.timestamp < time.now() { + return job } } @@ -146,27 +152,18 @@ pub fn (mut q BuildJobQueue) pop(arch string) ?BuildJob { return none } - for { - mut job := q.queues[arch].peek() or { return none } + q.pop_invalid(arch) + mut job := q.queues[arch].peek()? - // Skip any invalidated jobs - if job.config.target_id in q.invalidated - && job.created < q.invalidated[job.config.target_id] { - // This pop *should* never fail according to the source code - q.queues[arch].pop() or { return none } - continue - } + if job.timestamp < time.now() { + job = q.queues[arch].pop()? - if job.timestamp < time.now() { - job = q.queues[arch].pop()? + // TODO how do we handle this properly? Is it even possible for a + // cron expression to not return a next time if it's already been + // used before? + q.reschedule(job, arch) or {} - // TODO how do we handle this properly? Is it even possible for a - // cron expression to not return a next time if it's already been - // used before? - q.reschedule(job, arch) or {} - - return job - } + return job } } @@ -182,28 +179,19 @@ pub fn (mut q BuildJobQueue) pop_n(arch string, n int) []BuildJob { mut out := []BuildJob{} - outer: for out.len < n { - for { - mut job := q.queues[arch].peek() or { break outer } + for out.len < n { + q.pop_invalid(arch) + mut job := q.queues[arch].peek() or { break } - // Skip any invalidated jobs - if job.config.target_id in q.invalidated - && job.created < q.invalidated[job.config.target_id] { - // This pop *should* never fail according to the source code - q.queues[arch].pop() or { break outer } - continue - } + if job.timestamp < time.now() { + job = q.queues[arch].pop() or { break } - if job.timestamp < time.now() { - job = q.queues[arch].pop() or { break outer } + // TODO idem + q.reschedule(job, arch) or {} - // TODO idem - q.reschedule(job, arch) or {} - - out << job - } else { - break outer - } + out << job + } else { + break } } From 8a2f720bdf1702c7ffaadd27230372fb94519ceb Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 13 Dec 2022 19:33:21 +0100 Subject: [PATCH 21/97] docs(agent): added agent configuration docs --- docs/content/configuration.md | 24 +++++++++++++++++++++++- docs/content/installation.md | 8 ++++---- docs/content/usage/builds/schedule.md | 2 +- src/agent/daemon.v | 2 +- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/docs/content/configuration.md b/docs/content/configuration.md index af941a2..95bf713 100644 --- a/docs/content/configuration.md +++ b/docs/content/configuration.md @@ -17,7 +17,7 @@ If a variable is both present in the config file & as an environment variable, the value in the environment variable is used. {{< hint info >}} -**Note** +**Note** All environment variables can also be provided from a file by appending them with `_FILE`. This for example allows you to provide the API key from a Docker secrets file. @@ -97,3 +97,25 @@ configuration variable required for each command. build`. * Default: `archlinux:base-devel` +### `vieter agent` + +* `log_level`: log verbosity level. Value should be one of `FATAL`, `ERROR`, + `WARN`, `INFO` or `DEBUG`. + * Default: `WARN` +* `address`: *public* URL of the Vieter repository server to build for. From + this server jobs are retrieved. All built packages are published to this + server. +* `api_key`: API key of the above server. +* `data_dir`: directory to store log file in. +* `max_concurrent_builds`: how many builds to run at the same time. + * Default: `1` +* `polling_frequency`: how often (in seconds) to poll the server for new + builds. Note that the agent might poll more frequently when it's actively + processing builds. +* `image_rebuild_frequency`: Vieter periodically builds images that are then + used as a basis for running build containers. This is to prevent each build + from downloading an entire repository worth of dependencies. This setting + defines how frequently (in minutes) to rebuild these images. + * Default: `1440` (every 24 hours) +* `arch`: architecture for which this agent should pull down builds (e.g. + `x86_64`) diff --git a/docs/content/installation.md b/docs/content/installation.md index 87b9cba..21eda64 100644 --- a/docs/content/installation.md +++ b/docs/content/installation.md @@ -21,7 +21,7 @@ branch. This branch will be the most up to date, but does not give any guarantees about stability, so beware! Thanks to the single-binary design of Vieter, this image can be used both for -the repository server & the cron daemon. +the repository server, the cron daemon and the agent. Below is an example compose file to set up both the repository server & the cron daemon: @@ -76,7 +76,7 @@ architectures will build on both. ## Binary On the -[releases](https://git.rustybever.be/vieter/vieter/releases) +[releases](https://git.rustybever.be/vieter-v/vieter/releases) page, you can find statically compiled binaries for all released versions. This is the same binary as used inside the Docker images. @@ -106,5 +106,5 @@ guarantee that a compiler update won't temporarily break them. ## Building from source -The project [README](https://git.rustybever.be/vieter/vieter#building) contains -instructions for building Vieter from source. +The project [README](https://git.rustybever.be/vieter-v/vieter#building) +contains instructions for building Vieter from source. diff --git a/docs/content/usage/builds/schedule.md b/docs/content/usage/builds/schedule.md index 38f76a4..de59e25 100644 --- a/docs/content/usage/builds/schedule.md +++ b/docs/content/usage/builds/schedule.md @@ -37,6 +37,6 @@ Each section can consist of as many of these parts as necessary. ## CLI tool The Vieter binary contains a command that shows you the next matching times for -a given expression. This can be useful to understand the syntax. For more +a given expression. This can be useful for understanding the syntax. For more information, see [vieter-schedule(1)](https://rustybever.be/man/vieter/vieter-schedule.1.html). diff --git a/src/agent/daemon.v b/src/agent/daemon.v index f753e25..ff29d5e 100644 --- a/src/agent/daemon.v +++ b/src/agent/daemon.v @@ -32,7 +32,7 @@ fn agent_init(logger log.Log, conf Config) AgentDaemon { logger: logger client: client.new(conf.address, conf.api_key) conf: conf - images: new_image_manager(conf.image_rebuild_frequency) + images: new_image_manager(conf.image_rebuild_frequency * 60) builds: []BuildConfig{len: conf.max_concurrent_builds} atomics: []u64{len: conf.max_concurrent_builds} } From f6c5e7c2469f474193270cd09264ee1fb022499c Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 13 Dec 2022 19:59:18 +0100 Subject: [PATCH 22/97] feat: add option to force-build package --- src/build/build.v | 4 +++- src/build/shell.v | 20 ++++++++++++++------ src/console/targets/build.v | 4 ++-- src/console/targets/targets.v | 9 ++++++++- src/cron/daemon/build.v | 2 +- vieter.toml | 2 +- 6 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/build/build.v b/src/build/build.v index 84d288c..6da851a 100644 --- a/src/build/build.v +++ b/src/build/build.v @@ -24,6 +24,7 @@ pub: branch string repo string base_image string + force bool } // create_build_image creates a builder image given some base image which can @@ -104,7 +105,7 @@ pub: } // build_target builds the given target. Internally it calls `build_config`. -pub fn build_target(address string, api_key string, base_image_id string, target &Target) !BuildResult { +pub fn build_target(address string, api_key string, base_image_id string, target &Target, force bool) !BuildResult { config := BuildConfig{ target_id: target.id kind: target.kind @@ -112,6 +113,7 @@ pub fn build_target(address string, api_key string, base_image_id string, target branch: target.branch repo: target.repo base_image: base_image_id + force: force } return build_config(address, api_key, config) diff --git a/src/build/shell.v b/src/build/shell.v index c2d0c9b..ac61e07 100644 --- a/src/build/shell.v +++ b/src/build/shell.v @@ -63,14 +63,22 @@ fn create_build_script(address string, config BuildConfig, build_arch string) st 'cd repo', 'makepkg --nobuild --syncdeps --needed --noconfirm', 'source PKGBUILD', + ] + + if !config.force { // The build container checks whether the package is already present on // the server. - 'curl -s --head --fail $repo_url/$build_arch/\$pkgname-\$pkgver-\$pkgrel && exit 0', - // If the above curl command succeeds, we don't need to rebuild the - // package. However, because we're in a su shell, the exit command will - // drop us back into the root shell. Therefore, we must check whether - // we're in root so we don't proceed. - '[ "\$(id -u)" == 0 ] && exit 0', + commands << [ + 'curl -s --head --fail $repo_url/$build_arch/\$pkgname-\$pkgver-\$pkgrel && exit 0', + // If the above curl command succeeds, we don't need to rebuild the + // package. However, because we're in a su shell, the exit command will + // drop us back into the root shell. Therefore, we must check whether + // we're in root so we don't proceed. + '[ "\$(id -u)" == 0 ] && exit 0', + ] + } + + commands << [ 'MAKEFLAGS="-j\$(nproc)" makepkg -s --noconfirm --needed && for pkg in \$(ls -1 *.pkg*); do curl -XPOST -T "\$pkg" -H "X-API-KEY: \$API_KEY" $repo_url/publish; done', ] diff --git a/src/console/targets/build.v b/src/console/targets/build.v index 9368558..e18077d 100644 --- a/src/console/targets/build.v +++ b/src/console/targets/build.v @@ -6,7 +6,7 @@ import os import build // build locally builds the target with the given id. -fn build(conf Config, target_id int) ! { +fn build(conf Config, target_id int, force bool) ! { c := client.new(conf.address, conf.api_key) target := c.get_target(target_id)! @@ -16,7 +16,7 @@ fn build(conf Config, target_id int) ! { image_id := build.create_build_image(conf.base_image)! println('Running build...') - res := build.build_target(conf.address, conf.api_key, image_id, target)! + res := build.build_target(conf.address, conf.api_key, image_id, target, force)! println('Removing build image...') diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index 4179363..ffcd36c 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -182,11 +182,18 @@ pub fn cmd() cli.Command { required_args: 1 usage: 'id' description: 'Build the target with the given id & publish it.' + flags: [ + cli.Flag{ + name: 'force' + description: 'Build the target without checking whether it needs to be renewed.' + flag: cli.FlagType.bool + }, + ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! - build(conf, cmd.args[0].int())! + build(conf, cmd.args[0].int(), cmd.flags.get_bool('force')!)! } }, ] diff --git a/src/cron/daemon/build.v b/src/cron/daemon/build.v index beed9fc..42edc92 100644 --- a/src/cron/daemon/build.v +++ b/src/cron/daemon/build.v @@ -79,7 +79,7 @@ fn (mut d Daemon) run_build(build_index int, sb ScheduledBuild) { mut status := 0 res := build.build_target(d.client.address, d.client.api_key, d.builder_images.last(), - &sb.target) or { + &sb.target, false) or { d.ldebug('build_target error: $err.msg()') status = 1 diff --git a/vieter.toml b/vieter.toml index 9a68ae3..74a7397 100644 --- a/vieter.toml +++ b/vieter.toml @@ -8,7 +8,7 @@ arch = "x86_64" address = "http://localhost:8000" -global_schedule = '* *' +# global_schedule = '* *' api_update_frequency = 2 image_rebuild_frequency = 1 max_concurrent_builds = 3 From 6a208dbe6ca73b25e1e0e30f3b3b266620061ebc Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 13 Dec 2022 21:22:22 +0100 Subject: [PATCH 23/97] feat: allow queueing one-time builds --- src/build/queue.v | 46 ++++++++++++++++++++++++++--------------- src/server/api_builds.v | 21 +++++++++++++++++++ src/server/server.v | 2 +- 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/build/queue.v b/src/build/queue.v index dd2bb87..1395a0b 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -16,6 +16,8 @@ pub: ce CronExpression // Actual build config sent to the agent config BuildConfig + // Whether this is a one-time job + single bool } // Allows BuildJob structs to be sorted according to their timestamp in @@ -53,22 +55,29 @@ pub fn new_job_queue(default_schedule CronExpression, default_base_image string) // insert_all executes insert for each architecture of the given Target. pub fn (mut q BuildJobQueue) insert_all(target Target) ! { for arch in target.arch { - q.insert(target, arch.value)! + q.insert(target: target, arch: arch.value)! } } +[params] +pub struct InsertConfig { + target Target [required] + arch string [required] + single bool +} + // insert a new target's job into the queue for the given architecture. This // job will then be endlessly rescheduled after being pop'ed, unless removed // explicitely. -pub fn (mut q BuildJobQueue) insert(target Target, arch string) ! { +pub fn (mut q BuildJobQueue) insert(input InsertConfig) ! { lock q.mutex { - if arch !in q.queues { - q.queues[arch] = MinHeap{} + if input.arch !in q.queues { + q.queues[input.arch] = MinHeap{} } - ce := if target.schedule != '' { - parse_expression(target.schedule) or { - return error("Error while parsing cron expression '$target.schedule' (id $target.id): $err.msg()") + ce := if input.target.schedule != '' { + parse_expression(input.target.schedule) or { + return error("Error while parsing cron expression '$input.target.schedule' (id $input.target.id): $err.msg()") } } else { q.default_schedule @@ -80,18 +89,19 @@ pub fn (mut q BuildJobQueue) insert(target Target, arch string) ! { created: time.now() timestamp: timestamp ce: ce + single: input.single config: BuildConfig{ - target_id: target.id - kind: target.kind - url: target.url - branch: target.branch - repo: target.repo + target_id: input.target.id + kind: input.target.kind + url: input.target.url + branch: input.target.branch + repo: input.target.repo // TODO make this configurable base_image: q.default_base_image } } - q.queues[arch].insert(job) + q.queues[input.arch].insert(job) } } @@ -158,10 +168,12 @@ pub fn (mut q BuildJobQueue) pop(arch string) ?BuildJob { if job.timestamp < time.now() { job = q.queues[arch].pop()? - // TODO how do we handle this properly? Is it even possible for a - // cron expression to not return a next time if it's already been - // used before? - q.reschedule(job, arch) or {} + if !job.single { + // TODO how do we handle this properly? Is it even possible for a + // cron expression to not return a next time if it's already been + // used before? + q.reschedule(job, arch) or {} + } return job } diff --git a/src/server/api_builds.v b/src/server/api_builds.v index 922b252..bc841ce 100644 --- a/src/server/api_builds.v +++ b/src/server/api_builds.v @@ -19,3 +19,24 @@ fn (mut app App) v1_poll_job_queue() web.Result { return app.json(.ok, new_data_response(out)) } + +['/api/v1/jobs/queue'; auth; post] +fn (mut app App) v1_queue_job() web.Result { + target_id := app.query['target'] or { + return app.json(.bad_request, new_response('Missing target query arg.')) + }.int() + + arch := app.query['arch'] or { + return app.json(.bad_request, new_response('Missing arch query arg.')) + } + + target := app.db.get_target(target_id) or { + return app.json(.bad_request, new_response('Unknown target id.')) + } + + app.job_queue.insert(target: target, arch: arch, single: true) or { + return app.status(.internal_server_error) + } + + return app.status(.ok) +} diff --git a/src/server/server.v b/src/server/server.v index 1e86906..6d18f09 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -37,7 +37,7 @@ fn (mut app App) init_job_queue() ! { for targets.len > 0 { for target in targets { for arch in target.arch { - app.job_queue.insert(target, arch.value)! + app.job_queue.insert(target: target, arch: arch.value)! } } From 2cc3e8404e98329bb8b0dfd5dda39393b0abc2da Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 13 Dec 2022 22:03:04 +0100 Subject: [PATCH 24/97] feat: queue one-time builds from CLI --- src/build/queue.v | 40 +++++++++++++++---------- src/client/jobs.v | 11 +++++++ src/console/targets/targets.v | 28 ++++++++++++++++- src/server/{api_builds.v => api_jobs.v} | 8 ++++- 4 files changed, 69 insertions(+), 18 deletions(-) rename src/server/{api_builds.v => api_jobs.v} (83%) diff --git a/src/build/queue.v b/src/build/queue.v index 1395a0b..5d50f34 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -7,7 +7,7 @@ import datatypes { MinHeap } import util struct BuildJob { -pub: +pub mut: // Time at which this build job was created/queued created time.Time // Next timestamp from which point this job is allowed to be executed @@ -64,6 +64,8 @@ pub struct InsertConfig { target Target [required] arch string [required] single bool + force bool + now bool } // insert a new target's job into the queue for the given architecture. This @@ -75,20 +77,8 @@ pub fn (mut q BuildJobQueue) insert(input InsertConfig) ! { q.queues[input.arch] = MinHeap{} } - ce := if input.target.schedule != '' { - parse_expression(input.target.schedule) or { - return error("Error while parsing cron expression '$input.target.schedule' (id $input.target.id): $err.msg()") - } - } else { - q.default_schedule - } - - timestamp := ce.next_from_now()! - - job := BuildJob{ + mut job := BuildJob{ created: time.now() - timestamp: timestamp - ce: ce single: input.single config: BuildConfig{ target_id: input.target.id @@ -98,9 +88,25 @@ pub fn (mut q BuildJobQueue) insert(input InsertConfig) ! { repo: input.target.repo // TODO make this configurable base_image: q.default_base_image + force: input.force } } + if !input.now { + ce := if input.target.schedule != '' { + parse_expression(input.target.schedule) or { + return error("Error while parsing cron expression '$input.target.schedule' (id $input.target.id): $err.msg()") + } + } else { + q.default_schedule + } + + job.timestamp = ce.next_from_now()! + job.ce = ce + } else { + job.timestamp = time.now() + } + q.queues[input.arch].insert(job) } } @@ -198,8 +204,10 @@ pub fn (mut q BuildJobQueue) pop_n(arch string, n int) []BuildJob { if job.timestamp < time.now() { job = q.queues[arch].pop() or { break } - // TODO idem - q.reschedule(job, arch) or {} + if !job.single { + // TODO idem + q.reschedule(job, arch) or {} + } out << job } else { diff --git a/src/client/jobs.v b/src/client/jobs.v index 7fee94f..2d8e99b 100644 --- a/src/client/jobs.v +++ b/src/client/jobs.v @@ -1,6 +1,7 @@ module client import build { BuildConfig } +import web.response { Response } // poll_jobs requests a list of new build jobs from the server. pub fn (c &Client) poll_jobs(arch string, max int) ![]BuildConfig { @@ -11,3 +12,13 @@ pub fn (c &Client) poll_jobs(arch string, max int) ![]BuildConfig { return data.data } + +pub fn (c &Client) queue_job(target_id int, arch string, force bool) !Response { + data := c.send_request(.post, '/api/v1/jobs/queue', { + 'target': target_id.str() + 'arch': arch + 'force': force.str() + })! + + return data +} diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index ffcd36c..b527896 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -188,12 +188,38 @@ pub fn cmd() cli.Command { description: 'Build the target without checking whether it needs to be renewed.' flag: cli.FlagType.bool }, + cli.Flag{ + name: 'remote' + description: 'Schedule the build on the server instead of running it locally.' + flag: cli.FlagType.bool + }, + cli.Flag{ + name: 'arch' + description: 'Architecture to schedule build for. Required when using -remote.' + flag: cli.FlagType.string + }, ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! - build(conf, cmd.args[0].int(), cmd.flags.get_bool('force')!)! + remote := cmd.flags.get_bool('remote')! + force := cmd.flags.get_bool('force')! + target_id := cmd.args[0].int() + + if remote { + arch := cmd.flags.get_string('arch')! + + if arch == '' { + return error('When scheduling the build remotely, you have to specify an architecture.') + } + + c := client.new(conf.address, conf.api_key) + res := c.queue_job(target_id, arch, force)! + println(res.message) + } else { + build(conf, target_id, force)! + } } }, ] diff --git a/src/server/api_builds.v b/src/server/api_jobs.v similarity index 83% rename from src/server/api_builds.v rename to src/server/api_jobs.v index bc841ce..b75e70e 100644 --- a/src/server/api_builds.v +++ b/src/server/api_jobs.v @@ -30,11 +30,17 @@ fn (mut app App) v1_queue_job() web.Result { return app.json(.bad_request, new_response('Missing arch query arg.')) } + if arch == '' { + app.json(.bad_request, new_response('Empty arch query arg.')) + } + + force := 'force' in app.query + target := app.db.get_target(target_id) or { return app.json(.bad_request, new_response('Unknown target id.')) } - app.job_queue.insert(target: target, arch: arch, single: true) or { + app.job_queue.insert(target: target, arch: arch, single: true, now: true, force: force) or { return app.status(.internal_server_error) } From d7a04c6ebff26a95d8680d3fb233bf370943a646 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 14 Dec 2022 16:03:57 +0100 Subject: [PATCH 25/97] chore: please the great lint --- CHANGELOG.md | 4 ++++ src/agent/images.v | 4 ++-- src/client/jobs.v | 2 ++ src/server/api_jobs.v | 1 + 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aed7571..c55e16b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Migrated codebase to V 0.3.2 * Cron expression parser now uses bitfields instead of bool arrays * Added option to deploy using agent-server architecture instead of cron daemon +* Allow force-building packages, meaning the build won't check if the + repository is already up to date +* Allow scheduling builds on the server from the CLI tool instead of building + them locally ### Fixed diff --git a/src/agent/images.v b/src/agent/images.v index 64a8f74..185192e 100644 --- a/src/agent/images.v +++ b/src/agent/images.v @@ -9,9 +9,9 @@ import build // structure can manage images from any number of base images, paving the way // for configurable base images per target/repository. struct ImageManager { -mut: max_image_age int [required] - // For each base images, one or more builder images can exist at the same +mut: + // For each base image, one or more builder images can exist at the same // time images map[string][]string [required] // For each base image, we track when its newest image was built diff --git a/src/client/jobs.v b/src/client/jobs.v index 2d8e99b..440affa 100644 --- a/src/client/jobs.v +++ b/src/client/jobs.v @@ -13,6 +13,8 @@ pub fn (c &Client) poll_jobs(arch string, max int) ![]BuildConfig { return data.data } +// queue_job adds a new one-time build job for the given target to the job +// queue. pub fn (c &Client) queue_job(target_id int, arch string, force bool) !Response { data := c.send_request(.post, '/api/v1/jobs/queue', { 'target': target_id.str() diff --git a/src/server/api_jobs.v b/src/server/api_jobs.v index b75e70e..7795351 100644 --- a/src/server/api_jobs.v +++ b/src/server/api_jobs.v @@ -20,6 +20,7 @@ fn (mut app App) v1_poll_job_queue() web.Result { return app.json(.ok, new_data_response(out)) } +// v1_queue_job allows queueing a new one-time build job for the given target. ['/api/v1/jobs/queue'; auth; post] fn (mut app App) v1_queue_job() web.Result { target_id := app.query['target'] or { From 51df1874f5b2d31b88ed68a02a910ac091b53af3 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 14 Dec 2022 16:33:50 +0100 Subject: [PATCH 26/97] agent: some better logging --- src/agent/daemon.v | 10 ++++------ src/build/build.v | 5 +++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/agent/daemon.v b/src/agent/daemon.v index ff29d5e..0647733 100644 --- a/src/agent/daemon.v +++ b/src/agent/daemon.v @@ -146,7 +146,7 @@ fn (mut d AgentDaemon) start_build(config BuildConfig) bool { // run_build actually starts the build process for a given target. fn (mut d AgentDaemon) run_build(build_index int, config BuildConfig) { - d.linfo('started build: $config.url -> $config.repo') + d.linfo('started build: $config') // 0 means success, 1 means failure mut status := 0 @@ -164,16 +164,14 @@ fn (mut d AgentDaemon) run_build(build_index int, config BuildConfig) { } if status == 0 { - d.linfo('finished build: $config.url -> $config.repo; uploading logs...') + d.linfo('Uploading build logs for $config') // TODO use the arch value here build_arch := os.uname().machine d.client.add_build_log(config.target_id, res.start_time, res.end_time, build_arch, - res.exit_code, res.logs) or { - d.lerror('Failed to upload logs for build: $config.url -> $config.repo') - } + res.exit_code, res.logs) or { d.lerror('Failed to upload logs for $config') } } else { - d.linfo('an error occured during build: $config.url -> $config.repo') + d.lwarn('an error occurred during build: $config') } stdatomic.store_u64(&d.atomics[build_index], agent.build_done) diff --git a/src/build/build.v b/src/build/build.v index 6da851a..3d916bf 100644 --- a/src/build/build.v +++ b/src/build/build.v @@ -27,6 +27,11 @@ pub: force bool } +// str return a single-line string representation of a build log +pub fn (c BuildConfig) str() string { + return '{ target: $c.target_id, kind: $c.kind, url: $c.url, branch: $c.branch, repo: $c.repo, base_image: $c.base_image, force: $c.force }' +} + // create_build_image creates a builder image given some base image which can // then be used to build & package Arch images. It mostly just updates the // system, install some necessary packages & creates a non-root user to run From 60cb91c18cf1cf7ea66835d1b8e73cb66ff05ab3 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 14 Dec 2022 17:23:51 +0100 Subject: [PATCH 27/97] chore: final read before merging --- src/build/queue.v | 4 +++- src/server/server.v | 4 +--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/build/queue.v b/src/build/queue.v index 5d50f34..7902173 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -144,7 +144,9 @@ fn (mut q BuildJobQueue) pop_invalid(arch string) { // peek shows the first job for the given architecture that's ready to be // executed, if present. pub fn (mut q BuildJobQueue) peek(arch string) ?BuildJob { - rlock q.mutex { + // Even peek requires a write lock, because pop_invalid can modify the data + // structure + lock q.mutex { if arch !in q.queues { return none } diff --git a/src/server/server.v b/src/server/server.v index 6d18f09..74b1f37 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -36,9 +36,7 @@ fn (mut app App) init_job_queue() ! { for targets.len > 0 { for target in targets { - for arch in target.arch { - app.job_queue.insert(target: target, arch: arch.value)! - } + app.job_queue.insert_all(target)! } i += 25 From 0bd51586088588c352e04f5e4727f856b66e5aeb Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 15 Dec 2022 09:46:48 +0100 Subject: [PATCH 28/97] feat(client): handle empty and non-successful responses --- src/client/client.v | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/client/client.v b/src/client/client.v index aa6094a..3541555 100644 --- a/src/client/client.v +++ b/src/client/client.v @@ -1,8 +1,8 @@ module client -import net.http { Method } +import net.http { Method, Status } import net.urllib -import web.response { Response } +import web.response { Response, new_data_response } import json pub struct Client { @@ -56,8 +56,29 @@ fn (c &Client) send_request(method Method, url string, params map[string]stri // send_request_with_body calls send_request_raw_response & parses its // output as a Response object. fn (c &Client) send_request_with_body(method Method, url string, params map[string]string, body string) !Response { - res_text := c.send_request_raw_response(method, url, params, body)! - data := json.decode(Response, res_text)! + res := c.send_request_raw(method, url, params, body)! + + // Just return an empty successful response + if res.status_code == Status.no_content.int() { + return new_data_response(T{}) + } + + // Non-successful requests are expected to return either an empty body or + // Response + if res.status_code < 200 || res.status_code > 299 { + status_string := http.status_from_int(res.status_code).str() + + // A non-successful status call will have an empty body + if res.body == '' { + return error('Error $res.status_code ($status_string): (empty response)') + } + + data := json.decode(Response, res.body)! + + return error('Status $res.status_code ($status_string): $data.message') + } + + data := json.decode(Response, res.body)! return data } From 0727d0fd2517cb5dd67aa6f25d94b651c0f351f1 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 15 Dec 2022 10:01:45 +0100 Subject: [PATCH 29/97] refactor(client): streamline code & improve error propagation --- src/client/client.v | 13 ++++++------- src/client/jobs.v | 7 ++----- src/client/logs.v | 19 ++++--------------- src/client/targets.v | 13 ++++++------- src/console/logs/logs.v | 12 ++---------- src/console/targets/targets.v | 32 +++++++++----------------------- 6 files changed, 29 insertions(+), 67 deletions(-) diff --git a/src/client/client.v b/src/client/client.v index 3541555..5f24197 100644 --- a/src/client/client.v +++ b/src/client/client.v @@ -1,6 +1,6 @@ module client -import net.http { Method, Status } +import net.http { Method } import net.urllib import web.response { Response, new_data_response } import json @@ -57,25 +57,24 @@ fn (c &Client) send_request(method Method, url string, params map[string]stri // output as a Response object. fn (c &Client) send_request_with_body(method Method, url string, params map[string]string, body string) !Response { res := c.send_request_raw(method, url, params, body)! + status := http.status_from_int(res.status_code) // Just return an empty successful response - if res.status_code == Status.no_content.int() { + if status.is_success() && res.body == '' { return new_data_response(T{}) } // Non-successful requests are expected to return either an empty body or // Response - if res.status_code < 200 || res.status_code > 299 { - status_string := http.status_from_int(res.status_code).str() - + if status.is_error() { // A non-successful status call will have an empty body if res.body == '' { - return error('Error $res.status_code ($status_string): (empty response)') + return error('Error $res.status_code ($status.str()): (empty response)') } data := json.decode(Response, res.body)! - return error('Status $res.status_code ($status_string): $data.message') + return error('Status $res.status_code ($status.str()): $data.message') } data := json.decode(Response, res.body)! diff --git a/src/client/jobs.v b/src/client/jobs.v index 440affa..a545499 100644 --- a/src/client/jobs.v +++ b/src/client/jobs.v @@ -1,7 +1,6 @@ module client import build { BuildConfig } -import web.response { Response } // poll_jobs requests a list of new build jobs from the server. pub fn (c &Client) poll_jobs(arch string, max int) ![]BuildConfig { @@ -15,12 +14,10 @@ pub fn (c &Client) poll_jobs(arch string, max int) ![]BuildConfig { // queue_job adds a new one-time build job for the given target to the job // queue. -pub fn (c &Client) queue_job(target_id int, arch string, force bool) !Response { - data := c.send_request(.post, '/api/v1/jobs/queue', { +pub fn (c &Client) queue_job(target_id int, arch string, force bool) ! { + c.send_request(.post, '/api/v1/jobs/queue', { 'target': target_id.str() 'arch': arch 'force': force.str() })! - - return data } diff --git a/src/client/logs.v b/src/client/logs.v index eaddc8c..85063bc 100644 --- a/src/client/logs.v +++ b/src/client/logs.v @@ -6,29 +6,18 @@ import web.response { Response } import time // get_build_logs returns all build logs. -pub fn (c &Client) get_build_logs(filter BuildLogFilter) !Response<[]BuildLog> { +pub fn (c &Client) get_build_logs(filter BuildLogFilter) ![]BuildLog { params := models.params_from(filter) data := c.send_request<[]BuildLog>(Method.get, '/api/v1/logs', params)! - return data -} - -// get_build_logs_for_target returns all build logs for a given target. -pub fn (c &Client) get_build_logs_for_target(target_id int) !Response<[]BuildLog> { - params := { - 'repo': target_id.str() - } - - data := c.send_request<[]BuildLog>(Method.get, '/api/v1/logs', params)! - - return data + return data.data } // get_build_log returns a specific build log. -pub fn (c &Client) get_build_log(id int) !Response { +pub fn (c &Client) get_build_log(id int) !BuildLog { data := c.send_request(Method.get, '/api/v1/logs/$id', {})! - return data + return data.data } // get_build_log_content returns the contents of the build log file. diff --git a/src/client/targets.v b/src/client/targets.v index fd4254c..40bfdae 100644 --- a/src/client/targets.v +++ b/src/client/targets.v @@ -2,7 +2,6 @@ module client import models { Target, TargetFilter } import net.http { Method } -import web.response { Response } // get_targets returns a list of targets, given a filter object. pub fn (c &Client) get_targets(filter TargetFilter) ![]Target { @@ -49,24 +48,24 @@ pub struct NewTarget { } // add_target adds a new target to the server. -pub fn (c &Client) add_target(t NewTarget) !Response { +pub fn (c &Client) add_target(t NewTarget) !int { params := models.params_from(t) data := c.send_request(Method.post, '/api/v1/targets', params)! - return data + return data.data } // remove_target removes the target with the given id from the server. -pub fn (c &Client) remove_target(id int) !Response { +pub fn (c &Client) remove_target(id int) !string { data := c.send_request(Method.delete, '/api/v1/targets/$id', {})! - return data + return data.data } // patch_target sends a PATCH request to the given target with the params as // payload. -pub fn (c &Client) patch_target(id int, params map[string]string) !Response { +pub fn (c &Client) patch_target(id int, params map[string]string) !string { data := c.send_request(Method.patch, '/api/v1/targets/$id', params)! - return data + return data.data } diff --git a/src/console/logs/logs.v b/src/console/logs/logs.v index 1330dd0..3064a58 100644 --- a/src/console/logs/logs.v +++ b/src/console/logs/logs.v @@ -183,15 +183,7 @@ fn print_log_list(logs []BuildLog, raw bool) ! { // list prints a list of all build logs. fn list(conf Config, filter BuildLogFilter, raw bool) ! { c := client.new(conf.address, conf.api_key) - logs := c.get_build_logs(filter)!.data - - print_log_list(logs, raw)! -} - -// list prints a list of all build logs for a given target. -fn list_for_target(conf Config, target_id int, raw bool) ! { - c := client.new(conf.address, conf.api_key) - logs := c.get_build_logs_for_target(target_id)!.data + logs := c.get_build_logs(filter)! print_log_list(logs, raw)! } @@ -199,7 +191,7 @@ fn list_for_target(conf Config, target_id int, raw bool) ! { // info print the detailed info for a given build log. fn info(conf Config, id int) ! { c := client.new(conf.address, conf.api_key) - log := c.get_build_log(id)!.data + log := c.get_build_log(id)! print(log) } diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index b527896..b277410 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -215,8 +215,7 @@ pub fn cmd() cli.Command { } c := client.new(conf.address, conf.api_key) - res := c.queue_job(target_id, arch, force)! - println(res.message) + c.queue_job(target_id, arch, force)! } else { build(conf, target_id, force)! } @@ -245,23 +244,19 @@ fn list(conf Config, filter TargetFilter, raw bool) ! { // add adds a new repository to the server's list. fn add(conf Config, t &NewTarget, raw bool) ! { c := client.new(conf.address, conf.api_key) - res := c.add_target(t)! + target_id := c.add_target(t)! if raw { - println(res.data) + println(target_id) } else { - println('Target added with id $res.data') + println('Target added with id $target_id') } } // remove removes a repository from the server's list. fn remove(conf Config, id string) ! { - id_int := id.int() - - if id_int != 0 { - c := client.new(conf.address, conf.api_key) - c.remove_target(id_int)! - } + c := client.new(conf.address, conf.api_key) + c.remove_target(id.int())! } // patch patches a given repository with the provided params. @@ -274,22 +269,13 @@ fn patch(conf Config, id string, params map[string]string) ! { } } - id_int := id.int() - if id_int != 0 { - c := client.new(conf.address, conf.api_key) - c.patch_target(id_int, params)! - } + c := client.new(conf.address, conf.api_key) + c.patch_target(id.int(), params)! } // info shows detailed information for a given repo. fn info(conf Config, id string) ! { - id_int := id.int() - - if id_int == 0 { - return - } - c := client.new(conf.address, conf.api_key) - repo := c.get_target(id_int)! + repo := c.get_target(id.int())! println(repo) } From b634775ca387d9827f933af1c7c1a521e1c4926b Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 15 Dec 2022 10:46:58 +0100 Subject: [PATCH 30/97] refactor(server): clean up server responses a bit --- src/server/api_logs.v | 23 +++++++++++------------ src/server/api_targets.v | 11 +++++------ src/web/web.v | 7 ------- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/server/api_logs.v b/src/server/api_logs.v index fcbf024..c7521dd 100644 --- a/src/server/api_logs.v +++ b/src/server/api_logs.v @@ -1,7 +1,6 @@ module server import web -import net.http import net.urllib import web.response { new_data_response, new_response } import db @@ -15,7 +14,7 @@ import models { BuildLog, BuildLogFilter } ['/api/v1/logs'; auth; get] fn (mut app App) v1_get_logs() web.Result { filter := models.from_params(app.query) or { - return app.json(http.Status.bad_request, new_response('Invalid query parameters.')) + return app.json(.bad_request, new_response('Invalid query parameters.')) } logs := app.db.get_build_logs(filter) @@ -25,7 +24,7 @@ fn (mut app App) v1_get_logs() web.Result { // v1_get_single_log returns the build log with the given id. ['/api/v1/logs/:id'; auth; get] fn (mut app App) v1_get_single_log(id int) web.Result { - log := app.db.get_build_log(id) or { return app.not_found() } + log := app.db.get_build_log(id) or { return app.status(.not_found) } return app.json(.ok, new_data_response(log)) } @@ -33,7 +32,7 @@ fn (mut app App) v1_get_single_log(id int) web.Result { // v1_get_log_content returns the actual build log file for the given id. ['/api/v1/logs/:id/content'; auth; get] fn (mut app App) v1_get_log_content(id int) web.Result { - log := app.db.get_build_log(id) or { return app.not_found() } + log := app.db.get_build_log(id) or { return app.status(.not_found) } file_name := log.start_time.custom_format('YYYY-MM-DD_HH-mm-ss') full_path := os.join_path(app.conf.data_dir, logs_dir_name, log.target_id.str(), log.arch, file_name) @@ -57,25 +56,25 @@ fn (mut app App) v1_post_log() web.Result { start_time_int := app.query['startTime'].int() if start_time_int == 0 { - return app.json(http.Status.bad_request, new_response('Invalid or missing start time.')) + return app.json(.bad_request, new_response('Invalid or missing start time.')) } start_time := time.unix(start_time_int) end_time_int := app.query['endTime'].int() if end_time_int == 0 { - return app.json(http.Status.bad_request, new_response('Invalid or missing end time.')) + return app.json(.bad_request, new_response('Invalid or missing end time.')) } end_time := time.unix(end_time_int) if 'exitCode' !in app.query { - return app.json(http.Status.bad_request, new_response('Missing exit code.')) + return app.json(.bad_request, new_response('Missing exit code.')) } exit_code := app.query['exitCode'].int() if 'arch' !in app.query { - return app.json(http.Status.bad_request, new_response("Missing parameter 'arch'.")) + return app.json(.bad_request, new_response("Missing parameter 'arch'.")) } arch := app.query['arch'] @@ -83,7 +82,7 @@ fn (mut app App) v1_post_log() web.Result { target_id := app.query['target'].int() if !app.db.target_exists(target_id) { - return app.json(http.Status.bad_request, new_response('Unknown target.')) + return app.json(.bad_request, new_response('Unknown target.')) } // Store log in db @@ -105,7 +104,7 @@ fn (mut app App) v1_post_log() web.Result { os.mkdir_all(repo_logs_dir) or { app.lerror("Couldn't create dir '$repo_logs_dir'.") - return app.json(http.Status.internal_server_error, new_response('An error occured while processing the request.')) + return app.status(.internal_server_error) } } @@ -117,10 +116,10 @@ fn (mut app App) v1_post_log() web.Result { util.reader_to_file(mut app.reader, length.int(), full_path) or { app.lerror('An error occured while receiving logs: $err.msg()') - return app.json(http.Status.internal_server_error, new_response('Failed to upload logs.')) + return app.status(.internal_server_error) } } else { - return app.status(http.Status.length_required) + return app.status(.length_required) } return app.json(.ok, new_data_response(log_id)) diff --git a/src/server/api_targets.v b/src/server/api_targets.v index dc39d37..cd5cb0a 100644 --- a/src/server/api_targets.v +++ b/src/server/api_targets.v @@ -1,7 +1,6 @@ module server import web -import net.http import web.response { new_data_response, new_response } import db import models { Target, TargetArch, TargetFilter } @@ -10,7 +9,7 @@ import models { Target, TargetArch, TargetFilter } ['/api/v1/targets'; auth; get] fn (mut app App) v1_get_targets() web.Result { filter := models.from_params(app.query) or { - return app.json(http.Status.bad_request, new_response('Invalid query parameters.')) + return app.json(.bad_request, new_response('Invalid query parameters.')) } targets := app.db.get_targets(filter) @@ -20,7 +19,7 @@ fn (mut app App) v1_get_targets() web.Result { // v1_get_single_target returns the information for a single target. ['/api/v1/targets/:id'; auth; get] fn (mut app App) v1_get_single_target(id int) web.Result { - target := app.db.get_target(id) or { return app.not_found() } + target := app.db.get_target(id) or { return app.status(.not_found) } return app.json(.ok, new_data_response(target)) } @@ -37,12 +36,12 @@ fn (mut app App) v1_post_target() web.Result { } mut new_target := models.from_params(params) or { - return app.json(http.Status.bad_request, new_response(err.msg())) + return app.json(.bad_request, new_response(err.msg())) } // Ensure someone doesn't submit an invalid kind if new_target.kind !in models.valid_kinds { - return app.json(http.Status.bad_request, new_response('Invalid kind.')) + return app.json(.bad_request, new_response('Invalid kind.')) } id := app.db.add_target(new_target) @@ -61,7 +60,7 @@ fn (mut app App) v1_delete_target(id int) web.Result { app.db.delete_target(id) app.job_queue.invalidate(id) - return app.json(.ok, new_response('')) + return app.status(.ok) } // v1_patch_target updates a target's data with the given query params. diff --git a/src/web/web.v b/src/web/web.v index 1b40e7a..565baff 100644 --- a/src/web/web.v +++ b/src/web/web.v @@ -260,13 +260,6 @@ pub fn (mut ctx Context) redirect(url string) Result { return Result{} } -// not_found Send an not_found response -pub fn (mut ctx Context) not_found() Result { - ctx.send_custom_response(http_404) or {} - - return Result{} -} - interface DbInterface { db voidptr } From dbbe5c1e51cbd54483d2a4aee89a194960106ff5 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 15 Dec 2022 12:09:43 +0100 Subject: [PATCH 31/97] fix(agent): remove infinite loop and account for externally removed images --- src/agent/images.v | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/agent/images.v b/src/agent/images.v index 185192e..dd32656 100644 --- a/src/agent/images.v +++ b/src/agent/images.v @@ -73,7 +73,21 @@ fn (mut m ImageManager) clean_old_images() { // wasn't deleted. Therefore, we move the index over. If the function // returns true, the array's length has decreased by one so we don't // move the index. - dd.remove_image(m.images[image][i]) or { i += 1 } + dd.remove_image(m.images[image][i]) or { + // The image was removed by an external event + if err.code() == 404 { + m.images[image].delete(i) + } + // The image couldn't be removed, so we need to keep track of + // it + else { + i += 1 + } + + continue + } + + m.images[image].delete(i) } } } From a48358fd75101b82aa78c5b196324f08719b918f Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 15 Dec 2022 23:47:41 +0100 Subject: [PATCH 32/97] fix: don't run prepare step twice in builds --- src/build/build_script_git.sh | 4 ++-- src/build/build_script_git_branch.sh | 4 ++-- src/build/build_script_url.sh | 4 ++-- src/build/shell.v | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/build/build_script_git.sh b/src/build/build_script_git.sh index 73e0965..2644243 100644 --- a/src/build/build_script_git.sh +++ b/src/build/build_script_git.sh @@ -16,5 +16,5 @@ echo -e '+ curl -s --head --fail https://example.com/vieter/x86_64/$pkgname-$pkg curl -s --head --fail https://example.com/vieter/x86_64/$pkgname-$pkgver-$pkgrel && exit 0 echo -e '+ [ "$(id -u)" == 0 ] && exit 0' [ "$(id -u)" == 0 ] && exit 0 -echo -e '+ MAKEFLAGS="-j$(nproc)" makepkg -s --noconfirm --needed && for pkg in $(ls -1 *.pkg*); do curl -XPOST -T "$pkg" -H "X-API-KEY: $API_KEY" https://example.com/vieter/publish; done' -MAKEFLAGS="-j$(nproc)" makepkg -s --noconfirm --needed && for pkg in $(ls -1 *.pkg*); do curl -XPOST -T "$pkg" -H "X-API-KEY: $API_KEY" https://example.com/vieter/publish; done +echo -e '+ MAKEFLAGS="-j$(nproc)" makepkg -s --noconfirm --needed --noextract && for pkg in $(ls -1 *.pkg*); do curl -XPOST -T "$pkg" -H "X-API-KEY: $API_KEY" https://example.com/vieter/publish; done' +MAKEFLAGS="-j$(nproc)" makepkg -s --noconfirm --needed --noextract && for pkg in $(ls -1 *.pkg*); do curl -XPOST -T "$pkg" -H "X-API-KEY: $API_KEY" https://example.com/vieter/publish; done diff --git a/src/build/build_script_git_branch.sh b/src/build/build_script_git_branch.sh index be1ff4f..9f36bdc 100644 --- a/src/build/build_script_git_branch.sh +++ b/src/build/build_script_git_branch.sh @@ -16,5 +16,5 @@ echo -e '+ curl -s --head --fail https://example.com/vieter/x86_64/$pkgname-$pkg curl -s --head --fail https://example.com/vieter/x86_64/$pkgname-$pkgver-$pkgrel && exit 0 echo -e '+ [ "$(id -u)" == 0 ] && exit 0' [ "$(id -u)" == 0 ] && exit 0 -echo -e '+ MAKEFLAGS="-j$(nproc)" makepkg -s --noconfirm --needed && for pkg in $(ls -1 *.pkg*); do curl -XPOST -T "$pkg" -H "X-API-KEY: $API_KEY" https://example.com/vieter/publish; done' -MAKEFLAGS="-j$(nproc)" makepkg -s --noconfirm --needed && for pkg in $(ls -1 *.pkg*); do curl -XPOST -T "$pkg" -H "X-API-KEY: $API_KEY" https://example.com/vieter/publish; done +echo -e '+ MAKEFLAGS="-j$(nproc)" makepkg -s --noconfirm --needed --noextract && for pkg in $(ls -1 *.pkg*); do curl -XPOST -T "$pkg" -H "X-API-KEY: $API_KEY" https://example.com/vieter/publish; done' +MAKEFLAGS="-j$(nproc)" makepkg -s --noconfirm --needed --noextract && for pkg in $(ls -1 *.pkg*); do curl -XPOST -T "$pkg" -H "X-API-KEY: $API_KEY" https://example.com/vieter/publish; done diff --git a/src/build/build_script_url.sh b/src/build/build_script_url.sh index 3bc97e1..2d27de7 100644 --- a/src/build/build_script_url.sh +++ b/src/build/build_script_url.sh @@ -18,5 +18,5 @@ echo -e '+ curl -s --head --fail https://example.com/vieter/x86_64/$pkgname-$pkg curl -s --head --fail https://example.com/vieter/x86_64/$pkgname-$pkgver-$pkgrel && exit 0 echo -e '+ [ "$(id -u)" == 0 ] && exit 0' [ "$(id -u)" == 0 ] && exit 0 -echo -e '+ MAKEFLAGS="-j$(nproc)" makepkg -s --noconfirm --needed && for pkg in $(ls -1 *.pkg*); do curl -XPOST -T "$pkg" -H "X-API-KEY: $API_KEY" https://example.com/vieter/publish; done' -MAKEFLAGS="-j$(nproc)" makepkg -s --noconfirm --needed && for pkg in $(ls -1 *.pkg*); do curl -XPOST -T "$pkg" -H "X-API-KEY: $API_KEY" https://example.com/vieter/publish; done +echo -e '+ MAKEFLAGS="-j$(nproc)" makepkg -s --noconfirm --needed --noextract && for pkg in $(ls -1 *.pkg*); do curl -XPOST -T "$pkg" -H "X-API-KEY: $API_KEY" https://example.com/vieter/publish; done' +MAKEFLAGS="-j$(nproc)" makepkg -s --noconfirm --needed --noextract && for pkg in $(ls -1 *.pkg*); do curl -XPOST -T "$pkg" -H "X-API-KEY: $API_KEY" https://example.com/vieter/publish; done diff --git a/src/build/shell.v b/src/build/shell.v index ac61e07..6aa2413 100644 --- a/src/build/shell.v +++ b/src/build/shell.v @@ -79,7 +79,7 @@ fn create_build_script(address string, config BuildConfig, build_arch string) st } commands << [ - 'MAKEFLAGS="-j\$(nproc)" makepkg -s --noconfirm --needed && for pkg in \$(ls -1 *.pkg*); do curl -XPOST -T "\$pkg" -H "X-API-KEY: \$API_KEY" $repo_url/publish; done', + 'MAKEFLAGS="-j\$(nproc)" makepkg -s --noconfirm --needed --noextract && for pkg in \$(ls -1 *.pkg*); do curl -XPOST -T "\$pkg" -H "X-API-KEY: \$API_KEY" $repo_url/publish; done', ] return echo_commands(commands).join('\n') From 1ce7b9d5715d8d93deda284bd8b0dbd3d113dc26 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Fri, 16 Dec 2022 11:21:28 +0100 Subject: [PATCH 33/97] feat: add option to specify subdirectory in repo to use --- src/build/build.v | 4 +- .../{build_script_git.sh => scripts/git.sh} | 0 .../git_branch.sh} | 0 src/build/scripts/git_path.sh | 20 +++++++ src/build/scripts/git_path_spaces.sh | 20 +++++++ .../{build_script_url.sh => scripts/url.sh} | 0 src/build/shell.v | 7 ++- src/build/shell_test.v | 60 +++++++++++++------ src/client/targets.v | 1 + src/console/targets/targets.v | 11 ++++ src/db/db.v | 2 + src/db/migrations/005-repo-path/down.sql | 1 + src/db/migrations/005-repo-path/up.sql | 1 + src/models/targets.v | 21 ++++--- 14 files changed, 120 insertions(+), 28 deletions(-) rename src/build/{build_script_git.sh => scripts/git.sh} (100%) rename src/build/{build_script_git_branch.sh => scripts/git_branch.sh} (100%) create mode 100644 src/build/scripts/git_path.sh create mode 100644 src/build/scripts/git_path_spaces.sh rename src/build/{build_script_url.sh => scripts/url.sh} (100%) create mode 100644 src/db/migrations/005-repo-path/down.sql create mode 100644 src/db/migrations/005-repo-path/up.sql diff --git a/src/build/build.v b/src/build/build.v index 3d916bf..c6aa7f1 100644 --- a/src/build/build.v +++ b/src/build/build.v @@ -22,6 +22,7 @@ pub: kind string url string branch string + path string repo string base_image string force bool @@ -29,7 +30,7 @@ pub: // str return a single-line string representation of a build log pub fn (c BuildConfig) str() string { - return '{ target: $c.target_id, kind: $c.kind, url: $c.url, branch: $c.branch, repo: $c.repo, base_image: $c.base_image, force: $c.force }' + return '{ target: $c.target_id, kind: $c.kind, url: $c.url, branch: $c.branch, path: $c.path, repo: $c.repo, base_image: $c.base_image, force: $c.force }' } // create_build_image creates a builder image given some base image which can @@ -116,6 +117,7 @@ pub fn build_target(address string, api_key string, base_image_id string, target kind: target.kind url: target.url branch: target.branch + path: target.path repo: target.repo base_image: base_image_id force: force diff --git a/src/build/build_script_git.sh b/src/build/scripts/git.sh similarity index 100% rename from src/build/build_script_git.sh rename to src/build/scripts/git.sh diff --git a/src/build/build_script_git_branch.sh b/src/build/scripts/git_branch.sh similarity index 100% rename from src/build/build_script_git_branch.sh rename to src/build/scripts/git_branch.sh diff --git a/src/build/scripts/git_path.sh b/src/build/scripts/git_path.sh new file mode 100644 index 0000000..65b7fb9 --- /dev/null +++ b/src/build/scripts/git_path.sh @@ -0,0 +1,20 @@ +echo -e '+ echo -e '\''[vieter]\\nServer = https://example.com/$repo/$arch\\nSigLevel = Optional'\'' >> /etc/pacman.conf' +echo -e '[vieter]\nServer = https://example.com/$repo/$arch\nSigLevel = Optional' >> /etc/pacman.conf +echo -e '+ pacman -Syu --needed --noconfirm' +pacman -Syu --needed --noconfirm +echo -e '+ su builder' +su builder +echo -e '+ git clone --single-branch --depth 1 '\''https://examplerepo.com'\'' repo' +git clone --single-branch --depth 1 'https://examplerepo.com' repo +echo -e '+ cd '\''repo/example/path'\''' +cd 'repo/example/path' +echo -e '+ makepkg --nobuild --syncdeps --needed --noconfirm' +makepkg --nobuild --syncdeps --needed --noconfirm +echo -e '+ source PKGBUILD' +source PKGBUILD +echo -e '+ curl -s --head --fail https://example.com/vieter/x86_64/$pkgname-$pkgver-$pkgrel && exit 0' +curl -s --head --fail https://example.com/vieter/x86_64/$pkgname-$pkgver-$pkgrel && exit 0 +echo -e '+ [ "$(id -u)" == 0 ] && exit 0' +[ "$(id -u)" == 0 ] && exit 0 +echo -e '+ MAKEFLAGS="-j$(nproc)" makepkg -s --noconfirm --needed --noextract && for pkg in $(ls -1 *.pkg*); do curl -XPOST -T "$pkg" -H "X-API-KEY: $API_KEY" https://example.com/vieter/publish; done' +MAKEFLAGS="-j$(nproc)" makepkg -s --noconfirm --needed --noextract && for pkg in $(ls -1 *.pkg*); do curl -XPOST -T "$pkg" -H "X-API-KEY: $API_KEY" https://example.com/vieter/publish; done diff --git a/src/build/scripts/git_path_spaces.sh b/src/build/scripts/git_path_spaces.sh new file mode 100644 index 0000000..b632b91 --- /dev/null +++ b/src/build/scripts/git_path_spaces.sh @@ -0,0 +1,20 @@ +echo -e '+ echo -e '\''[vieter]\\nServer = https://example.com/$repo/$arch\\nSigLevel = Optional'\'' >> /etc/pacman.conf' +echo -e '[vieter]\nServer = https://example.com/$repo/$arch\nSigLevel = Optional' >> /etc/pacman.conf +echo -e '+ pacman -Syu --needed --noconfirm' +pacman -Syu --needed --noconfirm +echo -e '+ su builder' +su builder +echo -e '+ git clone --single-branch --depth 1 '\''https://examplerepo.com'\'' repo' +git clone --single-branch --depth 1 'https://examplerepo.com' repo +echo -e '+ cd '\''repo/example/path with spaces'\''' +cd 'repo/example/path with spaces' +echo -e '+ makepkg --nobuild --syncdeps --needed --noconfirm' +makepkg --nobuild --syncdeps --needed --noconfirm +echo -e '+ source PKGBUILD' +source PKGBUILD +echo -e '+ curl -s --head --fail https://example.com/vieter/x86_64/$pkgname-$pkgver-$pkgrel && exit 0' +curl -s --head --fail https://example.com/vieter/x86_64/$pkgname-$pkgver-$pkgrel && exit 0 +echo -e '+ [ "$(id -u)" == 0 ] && exit 0' +[ "$(id -u)" == 0 ] && exit 0 +echo -e '+ MAKEFLAGS="-j$(nproc)" makepkg -s --noconfirm --needed --noextract && for pkg in $(ls -1 *.pkg*); do curl -XPOST -T "$pkg" -H "X-API-KEY: $API_KEY" https://example.com/vieter/publish; done' +MAKEFLAGS="-j$(nproc)" makepkg -s --noconfirm --needed --noextract && for pkg in $(ls -1 *.pkg*); do curl -XPOST -T "$pkg" -H "X-API-KEY: $API_KEY" https://example.com/vieter/publish; done diff --git a/src/build/build_script_url.sh b/src/build/scripts/url.sh similarity index 100% rename from src/build/build_script_url.sh rename to src/build/scripts/url.sh diff --git a/src/build/shell.v b/src/build/shell.v index 6aa2413..c459a99 100644 --- a/src/build/shell.v +++ b/src/build/shell.v @@ -59,8 +59,13 @@ fn create_build_script(address string, config BuildConfig, build_arch string) st } } + commands << if config.path != '' { + "cd 'repo/$config.path'" + } else { + 'cd repo' + } + commands << [ - 'cd repo', 'makepkg --nobuild --syncdeps --needed --noconfirm', 'source PKGBUILD', ] diff --git a/src/build/shell_test.v b/src/build/shell_test.v index 8bb22d9..e44c5ff 100644 --- a/src/build/shell_test.v +++ b/src/build/shell_test.v @@ -1,5 +1,46 @@ module build +fn test_create_build_script_git() { + config := BuildConfig{ + target_id: 1 + kind: 'git' + url: 'https://examplerepo.com' + repo: 'vieter' + base_image: 'not-used:latest' + } + + build_script := create_build_script('https://example.com', config, 'x86_64') + expected := $embed_file('scripts/git.sh') + + assert build_script == expected.to_string().trim_space() +} + +fn test_create_build_script_git_path() { + mut config := BuildConfig{ + target_id: 1 + kind: 'git' + url: 'https://examplerepo.com' + repo: 'vieter' + path: 'example/path' + base_image: 'not-used:latest' + } + + mut build_script := create_build_script('https://example.com', config, 'x86_64') + mut expected := $embed_file('scripts/git_path.sh') + + assert build_script == expected.to_string().trim_space() + + config = BuildConfig{ + ...config + path: 'example/path with spaces' + } + + build_script = create_build_script('https://example.com', config, 'x86_64') + expected = $embed_file('scripts/git_path_spaces.sh') + + assert build_script == expected.to_string().trim_space() +} + fn test_create_build_script_git_branch() { config := BuildConfig{ target_id: 1 @@ -11,22 +52,7 @@ fn test_create_build_script_git_branch() { } build_script := create_build_script('https://example.com', config, 'x86_64') - expected := $embed_file('build_script_git_branch.sh') - - assert build_script == expected.to_string().trim_space() -} - -fn test_create_build_script_git() { - config := BuildConfig{ - target_id: 1 - kind: 'git' - url: 'https://examplerepo.com' - repo: 'vieter' - base_image: 'not-used:latest' - } - - build_script := create_build_script('https://example.com', config, 'x86_64') - expected := $embed_file('build_script_git.sh') + expected := $embed_file('scripts/git_branch.sh') assert build_script == expected.to_string().trim_space() } @@ -41,7 +67,7 @@ fn test_create_build_script_url() { } build_script := create_build_script('https://example.com', config, 'x86_64') - expected := $embed_file('build_script_url.sh') + expected := $embed_file('scripts/url.sh') assert build_script == expected.to_string().trim_space() } diff --git a/src/client/targets.v b/src/client/targets.v index 40bfdae..da6a9e4 100644 --- a/src/client/targets.v +++ b/src/client/targets.v @@ -44,6 +44,7 @@ pub struct NewTarget { url string branch string repo string + path string arch []string } diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index b277410..a134926 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -82,6 +82,11 @@ pub fn cmd() cli.Command { description: "Which branch to clone; only applies to kind 'git'." flag: cli.FlagType.string }, + cli.Flag{ + name: 'path' + description: 'Subdirectory inside Git repository to use.' + flag: cli.FlagType.string + }, ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! @@ -92,6 +97,7 @@ pub fn cmd() cli.Command { url: cmd.args[0] repo: cmd.args[1] branch: cmd.flags.get_string('branch') or { '' } + path: cmd.flags.get_string('path') or { '' } } raw := cmd.flags.get_bool('raw')! @@ -159,6 +165,11 @@ pub fn cmd() cli.Command { description: 'Kind of target.' flag: cli.FlagType.string }, + cli.Flag{ + name: 'path' + description: 'Subdirectory inside Git repository to use.' + flag: cli.FlagType.string + }, ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! diff --git a/src/db/db.v b/src/db/db.v index 1a0160e..98ee000 100644 --- a/src/db/db.v +++ b/src/db/db.v @@ -18,12 +18,14 @@ const ( $embed_file('migrations/002-rename-to-targets/up.sql'), $embed_file('migrations/003-target-url-type/up.sql'), $embed_file('migrations/004-nullable-branch/up.sql'), + $embed_file('migrations/005-repo-path/up.sql'), ] migrations_down = [ $embed_file('migrations/001-initial/down.sql'), $embed_file('migrations/002-rename-to-targets/down.sql'), $embed_file('migrations/003-target-url-type/down.sql'), $embed_file('migrations/004-nullable-branch/down.sql'), + $embed_file('migrations/005-repo-path/down.sql'), ] ) diff --git a/src/db/migrations/005-repo-path/down.sql b/src/db/migrations/005-repo-path/down.sql new file mode 100644 index 0000000..8a6f021 --- /dev/null +++ b/src/db/migrations/005-repo-path/down.sql @@ -0,0 +1 @@ +ALTER TABLE Target DROP COLUMN path; diff --git a/src/db/migrations/005-repo-path/up.sql b/src/db/migrations/005-repo-path/up.sql new file mode 100644 index 0000000..f7e5c29 --- /dev/null +++ b/src/db/migrations/005-repo-path/up.sql @@ -0,0 +1 @@ +ALTER TABLE Target ADD COLUMN path TEXT; diff --git a/src/models/targets.v b/src/models/targets.v index c8aa535..cb60650 100644 --- a/src/models/targets.v +++ b/src/models/targets.v @@ -28,21 +28,24 @@ pub mut: repo string [nonull] // Cron schedule describing how frequently to build the repo. schedule string + // Subdirectory in the Git repository to cd into + path string // On which architectures the package is allowed to be built. In reality, - // this controls which builders will periodically build the image. + // this controls which agents will build this package when scheduled. arch []TargetArch [fkey: 'target_id'] } // str returns a string representation. -pub fn (gr &Target) str() string { +pub fn (t &Target) str() string { mut parts := [ - 'id: $gr.id', - 'kind: $gr.kind', - 'url: $gr.url', - 'branch: $gr.branch', - 'repo: $gr.repo', - 'schedule: $gr.schedule', - 'arch: ${gr.arch.map(it.value).join(', ')}', + 'id: $t.id', + 'kind: $t.kind', + 'url: $t.url', + 'branch: $t.branch', + 'path: $t.path', + 'repo: $t.repo', + 'schedule: $t.schedule', + 'arch: ${t.arch.map(it.value).join(', ')}', ] str := parts.join('\n') From 489931eaa809bf4b0997c86f2da2dd38f86d3f0f Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Fri, 16 Dec 2022 11:37:51 +0100 Subject: [PATCH 34/97] fix: don't buffer stdout even if not a terminal --- Dockerfile | 1 + src/main.v | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/Dockerfile b/Dockerfile index 210ae66..a27ad44 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,6 +23,7 @@ RUN if [ -n "${CI_COMMIT_SHA}" ]; then \ "https://s3.rustybever.be/vieter/commits/${CI_COMMIT_SHA}/vieter-$(echo "${TARGETPLATFORM}" | sed 's:/:-:g')" && \ chmod +x vieter ; \ else \ + cd src && v install && cd .. && \ LDFLAGS='-lz -lbz2 -llzma -lexpat -lzstd -llz4 -lsqlite3 -static' make prod && \ mv pvieter vieter ; \ fi diff --git a/src/main.v b/src/main.v index 34387bf..fe0364f 100644 --- a/src/main.v +++ b/src/main.v @@ -12,6 +12,11 @@ import cron import agent fn main() { + // Stop buffering output so logs always show up immediately + unsafe { + C.setbuf(C.stdout, 0) + } + mut app := cli.Command{ name: 'vieter' description: 'Vieter is a lightweight implementation of an Arch repository server.' From 0604de26c48c4fd8e4c3285ce4d6008a6e1d64ca Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Fri, 16 Dec 2022 14:33:16 +0100 Subject: [PATCH 35/97] feat(agent): ensure images exist when starting build --- src/agent/daemon.v | 19 +++++++++++++++---- src/agent/images.v | 42 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/agent/daemon.v b/src/agent/daemon.v index 0647733..8fa3816 100644 --- a/src/agent/daemon.v +++ b/src/agent/daemon.v @@ -80,13 +80,24 @@ pub fn (mut d AgentDaemon) run() { last_poll_time = time.now() for config in new_configs { - // TODO handle this better than to just skip the config // Make sure a recent build base image is available for // building the config - d.images.refresh_image(config.base_image) or { - d.lerror(err.msg()) - continue + if !d.images.up_to_date(config.base_image) { + d.linfo('Building builder image from base image $config.base_image') + + // TODO handle this better than to just skip the config + d.images.refresh_image(config.base_image) or { + d.lerror(err.msg()) + continue + } } + + // It's technically still possible that the build image is + // removed in the very short period between building the + // builder image and starting a build container with it. If + // this happens, faith really just didn't want you to do this + // build. + d.start_build(config) } diff --git a/src/agent/images.v b/src/agent/images.v index dd32656..23b741d 100644 --- a/src/agent/images.v +++ b/src/agent/images.v @@ -33,16 +33,42 @@ pub fn (m &ImageManager) get(base_image string) string { return m.images[base_image].last() } -// refresh_image builds a new builder image from the given base image if the -// previous builder image is too old or non-existent. This function will do -// nothing if these conditions aren't met, so it's safe to call it every time -// you want to ensure an image is up to date. -fn (mut m ImageManager) refresh_image(base_image string) ! { - if base_image in m.timestamps - && m.timestamps[base_image].add_seconds(m.max_image_age) > time.now() { - return +// up_to_date returns whether the last known builder image is exists and is up +// to date. If this function returns true, the last builder image may be used +// to perform a build. +pub fn (mut m ImageManager) up_to_date(base_image string) bool { + if base_image !in m.timestamps + || m.timestamps[base_image].add_seconds(m.max_image_age) <= time.now() { + return false } + // It's possible the image has been removed by some external event, so we + // check whether it actually exists as well. + mut dd := docker.new_conn() or { return false } + + defer { + dd.close() or {} + } + + dd.image_inspect(m.images[base_image].last()) or { + // Image doesn't exist, so we stop tracking it + if err.code() == 404 { + m.images[base_image].delete_last() + m.timestamps.delete(base_image) + } + + // If the inspect fails, it's either because the image doesn't exist or + // because of some other error. Either we can't know *for certain* that + // the image exists, so we return false. + return false + } + + return true +} + +// refresh_image builds a new builder image from the given base image. This +// function should only be called if `up_to_date` return false. +fn (mut m ImageManager) refresh_image(base_image string) ! { // TODO use better image tags for built images new_image := build.create_build_image(base_image) or { return error('Failed to build builder image from base image $base_image') From af4c9e1d004ac2d6277f1333d95963eda1cf3f02 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Fri, 16 Dec 2022 16:35:40 +0100 Subject: [PATCH 36/97] chore: updated changelog --- CHANGELOG.md | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c55e16b..54d833a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,24 +7,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://git.rustybever.be/vieter-v/vieter/src/branch/dev) +### Added + +* Allow specifying subdirectory inside Git repository +* Added option to deploy using agent-server architecture instead of cron daemon +* Allow scheduling builds on the server from the CLI tool instead of building + them locally +* Allow force-building packages, meaning the build won't check if the + repository is already up to date + ### Changed * Migrated codebase to V 0.3.2 * Cron expression parser now uses bitfields instead of bool arrays -* Added option to deploy using agent-server architecture instead of cron daemon -* Allow force-building packages, meaning the build won't check if the - repository is already up to date -* Allow scheduling builds on the server from the CLI tool instead of building - them locally ### Fixed * Arch value for target is now properly set if not provided -* All API endpoints now return proper JSON on success - * CLI no longer exits with non-zero status code when removing/patching - target * Allow NULL values for branch in database * Endpoint for adding targets now returns the correct id +* CLI now correctly errors and doesn't error when sending requests +* Fixed possible infinite loop when removing old build images +* Check whether build image still exists before starting build +* Don't run makepkg `prepare()` function twice +* Don't buffer stdout in Docker containers ## [0.4.0](https://git.rustybever.be/vieter-v/vieter/src/tag/0.4.0) From fe3e6e2babce6b41973b10599d9c38c5ba09dcc1 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Fri, 16 Dec 2022 18:18:25 +0100 Subject: [PATCH 37/97] chore: some final revisions before pr merge --- src/agent/images.v | 12 ++++++------ src/client/client.v | 12 ++++++------ src/console/targets/targets.v | 21 +++++++++------------ 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/agent/images.v b/src/agent/images.v index 23b741d..1fec567 100644 --- a/src/agent/images.v +++ b/src/agent/images.v @@ -33,9 +33,9 @@ pub fn (m &ImageManager) get(base_image string) string { return m.images[base_image].last() } -// up_to_date returns whether the last known builder image is exists and is up -// to date. If this function returns true, the last builder image may be used -// to perform a build. +// up_to_date returns true if the last known builder image exists and is up to +// date. If this function returns true, the last builder image may be used to +// perform a build. pub fn (mut m ImageManager) up_to_date(base_image string) bool { if base_image !in m.timestamps || m.timestamps[base_image].add_seconds(m.max_image_age) <= time.now() { @@ -58,8 +58,8 @@ pub fn (mut m ImageManager) up_to_date(base_image string) bool { } // If the inspect fails, it's either because the image doesn't exist or - // because of some other error. Either we can't know *for certain* that - // the image exists, so we return false. + // because of some other error. Either way, we can't know *for certain* + // that the image exists, so we return false. return false } @@ -67,7 +67,7 @@ pub fn (mut m ImageManager) up_to_date(base_image string) bool { } // refresh_image builds a new builder image from the given base image. This -// function should only be called if `up_to_date` return false. +// function should only be called if `up_to_date` returned false. fn (mut m ImageManager) refresh_image(base_image string) ! { // TODO use better image tags for built images new_image := build.create_build_image(base_image) or { diff --git a/src/client/client.v b/src/client/client.v index 5f24197..cce4e70 100644 --- a/src/client/client.v +++ b/src/client/client.v @@ -57,12 +57,7 @@ fn (c &Client) send_request(method Method, url string, params map[string]stri // output as a Response object. fn (c &Client) send_request_with_body(method Method, url string, params map[string]string, body string) !Response { res := c.send_request_raw(method, url, params, body)! - status := http.status_from_int(res.status_code) - - // Just return an empty successful response - if status.is_success() && res.body == '' { - return new_data_response(T{}) - } + status := res.status() // Non-successful requests are expected to return either an empty body or // Response @@ -77,6 +72,11 @@ fn (c &Client) send_request_with_body(method Method, url string, params map[s return error('Status $res.status_code ($status.str()): $data.message') } + // Just return an empty successful response + if res.body == '' { + return new_data_response(T{}) + } + data := json.decode(Response, res.body)! return data diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index a134926..94deebd 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -13,7 +13,7 @@ struct Config { base_image string = 'archlinux:base-devel' } -// cmd returns the cli submodule that handles the repos API interaction +// cmd returns the cli submodule that handles the targets API interaction pub fn cmd() cli.Command { return cli.Command{ name: 'targets' @@ -236,14 +236,11 @@ pub fn cmd() cli.Command { } } -// get_repo_by_prefix tries to find the repo with the given prefix in its -// ID. If multiple or none are found, an error is raised. - // list prints out a list of all repositories. fn list(conf Config, filter TargetFilter, raw bool) ! { c := client.new(conf.address, conf.api_key) - repos := c.get_targets(filter)! - data := repos.map([it.id.str(), it.kind, it.url, it.repo]) + targets := c.get_targets(filter)! + data := targets.map([it.id.str(), it.kind, it.url, it.repo]) if raw { println(console.tabbed_table(data)) @@ -252,7 +249,7 @@ fn list(conf Config, filter TargetFilter, raw bool) ! { } } -// add adds a new repository to the server's list. +// add adds a new target to the server's list. fn add(conf Config, t &NewTarget, raw bool) ! { c := client.new(conf.address, conf.api_key) target_id := c.add_target(t)! @@ -264,13 +261,13 @@ fn add(conf Config, t &NewTarget, raw bool) ! { } } -// remove removes a repository from the server's list. +// remove removes a target from the server's list. fn remove(conf Config, id string) ! { c := client.new(conf.address, conf.api_key) c.remove_target(id.int())! } -// patch patches a given repository with the provided params. +// patch patches a given target with the provided params. fn patch(conf Config, id string, params map[string]string) ! { // We check the cron expression first because it's useless to send an // invalid one to the server. @@ -284,9 +281,9 @@ fn patch(conf Config, id string, params map[string]string) ! { c.patch_target(id.int(), params)! } -// info shows detailed information for a given repo. +// info shows detailed information for a given target. fn info(conf Config, id string) ! { c := client.new(conf.address, conf.api_key) - repo := c.get_target(id.int())! - println(repo) + target := c.get_target(id.int())! + println(target) } From 402fef475a8e036c9db2bf84cdfd7556cb6f2093 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Fri, 16 Dec 2022 20:38:26 +0100 Subject: [PATCH 38/97] fix: actually use path setting when building --- src/agent/daemon.v | 3 ++- src/build/build.v | 30 ++---------------------------- src/build/queue.v | 13 ++----------- src/build/shell.v | 2 ++ src/build/shell_test.v | 2 ++ src/client/jobs.v | 2 +- src/models/builds.v | 18 ++++++++++++++++++ src/models/targets.v | 15 +++++++++++++++ 8 files changed, 44 insertions(+), 41 deletions(-) create mode 100644 src/models/builds.v diff --git a/src/agent/daemon.v b/src/agent/daemon.v index 8fa3816..c55d0db 100644 --- a/src/agent/daemon.v +++ b/src/agent/daemon.v @@ -2,7 +2,8 @@ module agent import log import sync.stdatomic -import build { BuildConfig } +import build +import models { BuildConfig } import client import time import os diff --git a/src/build/build.v b/src/build/build.v index c6aa7f1..712c93b 100644 --- a/src/build/build.v +++ b/src/build/build.v @@ -6,7 +6,7 @@ import time import os import strings import util -import models { Target } +import models { BuildConfig, Target } const ( container_build_dir = '/build' @@ -16,23 +16,6 @@ const ( '/usr/local/bin', '/usr/bin/site_perl', '/usr/bin/vendor_perl', '/usr/bin/core_perl'] ) -pub struct BuildConfig { -pub: - target_id int - kind string - url string - branch string - path string - repo string - base_image string - force bool -} - -// str return a single-line string representation of a build log -pub fn (c BuildConfig) str() string { - return '{ target: $c.target_id, kind: $c.kind, url: $c.url, branch: $c.branch, path: $c.path, repo: $c.repo, base_image: $c.base_image, force: $c.force }' -} - // create_build_image creates a builder image given some base image which can // then be used to build & package Arch images. It mostly just updates the // system, install some necessary packages & creates a non-root user to run @@ -112,16 +95,7 @@ pub: // build_target builds the given target. Internally it calls `build_config`. pub fn build_target(address string, api_key string, base_image_id string, target &Target, force bool) !BuildResult { - config := BuildConfig{ - target_id: target.id - kind: target.kind - url: target.url - branch: target.branch - path: target.path - repo: target.repo - base_image: base_image_id - force: force - } + config := target.as_build_config(base_image_id, force) return build_config(address, api_key, config) } diff --git a/src/build/queue.v b/src/build/queue.v index 7902173..e74529c 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -1,6 +1,6 @@ module build -import models { Target } +import models { BuildConfig, Target } import cron.expression { CronExpression, parse_expression } import time import datatypes { MinHeap } @@ -80,16 +80,7 @@ pub fn (mut q BuildJobQueue) insert(input InsertConfig) ! { mut job := BuildJob{ created: time.now() single: input.single - config: BuildConfig{ - target_id: input.target.id - kind: input.target.kind - url: input.target.url - branch: input.target.branch - repo: input.target.repo - // TODO make this configurable - base_image: q.default_base_image - force: input.force - } + config: input.target.as_build_config(q.default_base_image, input.force) } if !input.now { diff --git a/src/build/shell.v b/src/build/shell.v index c459a99..16f93b5 100644 --- a/src/build/shell.v +++ b/src/build/shell.v @@ -1,5 +1,7 @@ module build +import models { BuildConfig } + // escape_shell_string escapes any characters that could be interpreted // incorrectly by a shell. The resulting value should be safe to use inside an // echo statement. diff --git a/src/build/shell_test.v b/src/build/shell_test.v index e44c5ff..e23d964 100644 --- a/src/build/shell_test.v +++ b/src/build/shell_test.v @@ -1,5 +1,7 @@ module build +import models { BuildConfig } + fn test_create_build_script_git() { config := BuildConfig{ target_id: 1 diff --git a/src/client/jobs.v b/src/client/jobs.v index a545499..784639e 100644 --- a/src/client/jobs.v +++ b/src/client/jobs.v @@ -1,6 +1,6 @@ module client -import build { BuildConfig } +import models { BuildConfig } // poll_jobs requests a list of new build jobs from the server. pub fn (c &Client) poll_jobs(arch string, max int) ![]BuildConfig { diff --git a/src/models/builds.v b/src/models/builds.v new file mode 100644 index 0000000..926a53c --- /dev/null +++ b/src/models/builds.v @@ -0,0 +1,18 @@ +module models + +pub struct BuildConfig { +pub: + target_id int + kind string + url string + branch string + path string + repo string + base_image string + force bool +} + +// str return a single-line string representation of a build log +pub fn (c BuildConfig) str() string { + return '{ target: $c.target_id, kind: $c.kind, url: $c.url, branch: $c.branch, path: $c.path, repo: $c.repo, base_image: $c.base_image, force: $c.force }' +} diff --git a/src/models/targets.v b/src/models/targets.v index cb60650..af3cb0d 100644 --- a/src/models/targets.v +++ b/src/models/targets.v @@ -52,6 +52,21 @@ pub fn (t &Target) str() string { return str } +// as_build_config converts a Target into a BuildConfig, given some extra +// needed information. +pub fn (t &Target) as_build_config(base_image string, force bool) BuildConfig { + return BuildConfig{ + target_id: t.id + kind: t.kind + url: t.url + branch: t.branch + path: t.path + repo: t.repo + base_image: base_image + force: force + } +} + [params] pub struct TargetFilter { pub mut: From 1797c0f5606e630d6dc14aeadf5de0b49d10f4a4 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Fri, 16 Dec 2022 21:47:02 +0100 Subject: [PATCH 39/97] fix(agent): correctly calculate sleep time --- src/agent/daemon.v | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/agent/daemon.v b/src/agent/daemon.v index c55d0db..b5a6968 100644 --- a/src/agent/daemon.v +++ b/src/agent/daemon.v @@ -71,6 +71,8 @@ pub fn (mut d AgentDaemon) run() { // clustered together (especially when mostly using the global cron // schedule), so there's a much higher chance jobs are available. if finished > 0 || time.now() >= last_poll_time.add_seconds(d.conf.polling_frequency) { + d.ldebug('Polling for new jobs') + new_configs := d.client.poll_jobs(d.conf.arch, finished + empty) or { d.lerror('Failed to poll jobs: $err.msg()') @@ -78,6 +80,9 @@ pub fn (mut d AgentDaemon) run() { time.sleep(5 * time.second) continue } + + d.ldebug('Received $new_configs.len jobs') + last_poll_time = time.now() for config in new_configs { @@ -105,16 +110,19 @@ pub fn (mut d AgentDaemon) run() { // No new jobs were scheduled and the agent isn't doing anything, // so we just wait until the next polling period. if new_configs.len == 0 && finished + empty == d.conf.max_concurrent_builds { - sleep_time = time.now() - last_poll_time + sleep_time = last_poll_time.add_seconds(d.conf.polling_frequency) - time.now() } } // The agent is not doing anything, so we just wait until the next poll // time else if finished + empty == d.conf.max_concurrent_builds { - sleep_time = time.now() - last_poll_time + sleep_time = last_poll_time.add_seconds(d.conf.polling_frequency) - time.now() } - time.sleep(sleep_time) + if sleep_time > 0 { + d.ldebug('Sleeping for $sleep_time') + time.sleep(sleep_time) + } } } From b067f9c589abcd1885b9bed5c8551c56374dfe11 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Fri, 16 Dec 2022 22:06:26 +0100 Subject: [PATCH 40/97] refactor: streamline agent loop code --- src/agent/daemon.v | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/agent/daemon.v b/src/agent/daemon.v index b5a6968..62f36c2 100644 --- a/src/agent/daemon.v +++ b/src/agent/daemon.v @@ -46,16 +46,22 @@ pub fn (mut d AgentDaemon) run() { // This is just so that the very first time the loop is ran, the jobs are // always polled mut last_poll_time := time.now().add_seconds(-d.conf.polling_frequency) - mut sleep_time := 1 * time.second - mut finished, mut empty := 0, 0 + mut sleep_time := 0 * time.second + mut finished, mut empty, mut running := 0, 0, 0 for { + if sleep_time > 0 { + d.ldebug('Sleeping for $sleep_time') + time.sleep(sleep_time) + } + finished, empty = d.update_atomics() + running = d.conf.max_concurrent_builds - finished - empty // No new finished builds and no free slots, so there's nothing to be // done if finished + empty == 0 { - time.sleep(1 * time.second) + sleep_time = 1 * time.second continue } @@ -77,7 +83,7 @@ pub fn (mut d AgentDaemon) run() { d.lerror('Failed to poll jobs: $err.msg()') // TODO pick a better delay here - time.sleep(5 * time.second) + sleep_time = 5 * time.second continue } @@ -105,23 +111,16 @@ pub fn (mut d AgentDaemon) run() { // build. d.start_build(config) - } - - // No new jobs were scheduled and the agent isn't doing anything, - // so we just wait until the next polling period. - if new_configs.len == 0 && finished + empty == d.conf.max_concurrent_builds { - sleep_time = last_poll_time.add_seconds(d.conf.polling_frequency) - time.now() + running++ } } + // The agent is not doing anything, so we just wait until the next poll // time - else if finished + empty == d.conf.max_concurrent_builds { + if running == 0 { sleep_time = last_poll_time.add_seconds(d.conf.polling_frequency) - time.now() - } - - if sleep_time > 0 { - d.ldebug('Sleeping for $sleep_time') - time.sleep(sleep_time) + } else { + sleep_time = 1 * time.second } } } From f9bb4b81deef489ac1254de85c20f6a703c5f33c Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 17 Dec 2022 14:00:51 +0100 Subject: [PATCH 41/97] chore: bump versions --- CHANGELOG.md | 2 ++ PKGBUILD | 2 +- src/main.v | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54d833a..27d9096 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://git.rustybever.be/vieter-v/vieter/src/branch/dev) +## [0.5.0-rc.1](https://git.rustybever.be/vieter-v/vieter/src/tag/0.5.0-rc.1) + ### Added * Allow specifying subdirectory inside Git repository diff --git a/PKGBUILD b/PKGBUILD index b600ba0..94db654 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -3,7 +3,7 @@ pkgbase='vieter' pkgname='vieter' -pkgver='0.4.0' +pkgver='0.5.0-rc.1' pkgrel=1 pkgdesc="Lightweight Arch repository server & package build system" depends=('glibc' 'openssl' 'libarchive' 'sqlite') diff --git a/src/main.v b/src/main.v index fe0364f..1053c2f 100644 --- a/src/main.v +++ b/src/main.v @@ -20,7 +20,7 @@ fn main() { mut app := cli.Command{ name: 'vieter' description: 'Vieter is a lightweight implementation of an Arch repository server.' - version: '0.4.0' + version: '0.5.0-rc.1' flags: [ cli.Flag{ flag: cli.FlagType.string From af409011e697d4c6b8ebf7e7ca262e184eb88041 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 17 Dec 2022 16:24:01 +0100 Subject: [PATCH 42/97] feat: add api & cli command to remove log --- src/client/logs.v | 7 +++++++ src/console/logs/logs.v | 18 ++++++++++++++++++ src/server/api_logs.v | 19 +++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/src/client/logs.v b/src/client/logs.v index 85063bc..e5969dd 100644 --- a/src/client/logs.v +++ b/src/client/logs.v @@ -41,3 +41,10 @@ pub fn (c &Client) add_build_log(target_id int, start_time time.Time, end_time t return data } + +// remove_build_log removes the build log with the given id from the server. +pub fn (c &Client) remove_build_log(id int) !string { + data := c.send_request(.delete, '/api/v1/logs/$id', {})! + + return data.data +} diff --git a/src/console/logs/logs.v b/src/console/logs/logs.v index 3064a58..19c46f6 100644 --- a/src/console/logs/logs.v +++ b/src/console/logs/logs.v @@ -138,6 +138,18 @@ pub fn cmd() cli.Command { list(conf, filter, raw)! } }, + cli.Command{ + name: 'remove' + required_args: 1 + usage: 'id' + description: 'Remove a build log that matches the given id.' + execute: fn (cmd cli.Command) ! { + config_file := cmd.flags.get_string('config-file')! + conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + + remove(conf, cmd.args[0])! + } + }, cli.Command{ name: 'info' required_args: 1 @@ -204,3 +216,9 @@ fn content(conf Config, id int) ! { println(content) } + +// remove removes a build log from the server's list. +fn remove(conf Config, id string) ! { + c := client.new(conf.address, conf.api_key) + c.remove_build_log(id.int())! +} diff --git a/src/server/api_logs.v b/src/server/api_logs.v index c7521dd..352266c 100644 --- a/src/server/api_logs.v +++ b/src/server/api_logs.v @@ -124,3 +124,22 @@ fn (mut app App) v1_post_log() web.Result { return app.json(.ok, new_data_response(log_id)) } + +// v1_delete_log allows removing a build log from the system. +['/api/v1/logs/:id'; auth; delete] +fn (mut app App) v1_delete_log(id int) web.Result { + log := app.db.get_build_log(id) or { return app.status(.not_found) } + file_name := log.start_time.custom_format('YYYY-MM-DD_HH-mm-ss') + full_path := os.join_path(app.conf.data_dir, logs_dir_name, log.target_id.str(), log.arch, + file_name) + + os.rm(full_path) or { + app.lerror('Failed to remove log file $full_path: $err.msg()') + + return app.status(.internal_server_error) + } + + app.db.delete_build_log(id) + + return app.status(.ok) +} From a9ad3088bbfd0366ffae6e8174c23c0808a0a385 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 17 Dec 2022 17:11:19 +0100 Subject: [PATCH 43/97] feat(server): add log removal daemon --- docs/api/source/includes/_logs.md | 14 ++++++++ docs/content/configuration.md | 5 +++ src/server/cli.v | 1 + src/server/log_removal.v | 56 +++++++++++++++++++++++++++++++ src/server/server.v | 4 +++ vieter.toml | 1 + 6 files changed, 81 insertions(+) create mode 100644 src/server/log_removal.v diff --git a/docs/api/source/includes/_logs.md b/docs/api/source/includes/_logs.md index 1c14e71..ba6dada 100644 --- a/docs/api/source/includes/_logs.md +++ b/docs/api/source/includes/_logs.md @@ -149,3 +149,17 @@ target | id of target this build is for ### Request body Plaintext contents of the build log. + +## Remove a build log + +Remove a build log from the server. + +### HTTP Request + +`DELETE /api/v1/logs/:id` + +### URL Parameters + +Parameter | Description +--------- | ----------- +id | id of log to remove diff --git a/docs/content/configuration.md b/docs/content/configuration.md index 95bf713..e59fe06 100644 --- a/docs/content/configuration.md +++ b/docs/content/configuration.md @@ -47,6 +47,11 @@ configuration variable required for each command. * Git repositories added without an `arch` value use this value instead. * `port`: HTTP port to run on * Default: `8000` +* `max_log_age`: maximum age of logs (in days). Logs older than this will get + cleaned by the log removal daemon every 24 hours. If set to a negative value, + no logs are ever removed. The age of logs is determined by the time the build + was started. + * Default: `-1` ### `vieter cron` diff --git a/src/server/cli.v b/src/server/cli.v index 2fede6c..52bce1e 100644 --- a/src/server/cli.v +++ b/src/server/cli.v @@ -13,6 +13,7 @@ pub: global_schedule string = '0 3' port int = 8000 base_image string = 'archlinux:base-devel' + max_log_age int = -1 } // cmd returns the cli submodule that handles starting the server diff --git a/src/server/log_removal.v b/src/server/log_removal.v new file mode 100644 index 0000000..f68c575 --- /dev/null +++ b/src/server/log_removal.v @@ -0,0 +1,56 @@ +module server + +import time +import models { BuildLog } +import os + +const log_removal_frequency = 24 * time.hour + +// log_removal_daemon removes old build logs every `log_removal_frequency`. +fn (mut app App) log_removal_daemon() { + mut start_time := time.Time{} + + for { + start_time = time.now() + + mut too_old_timestamp := time.now().add_days(-app.conf.max_log_age) + + app.linfo('Cleaning logs before $too_old_timestamp') + + mut offset := u64(0) + mut logs := []BuildLog{} + mut counter := 0 + mut failed := 0 + + // Remove old logs + for { + logs = app.db.get_build_logs(before: too_old_timestamp, offset: offset, limit: 50) + + for log in logs { + file_name := log.start_time.custom_format('YYYY-MM-DD_HH-mm-ss') + full_path := os.join_path(app.conf.data_dir, logs_dir_name, log.target_id.str(), + log.arch, file_name) + os.rm(full_path) or { + app.lerror('Failed to remove log file $full_path: $err.msg()') + failed += 1 + + continue + } + app.db.delete_build_log(log.id) + + counter += 1 + } + + if logs.len < 50 { + break + } + + offset += 50 + } + + app.linfo('Cleaned $counter logs ($failed failed)') + + // Sleep until the next cycle + time.sleep(start_time.add_days(1) - time.now()) + } +} diff --git a/src/server/server.v b/src/server/server.v index 74b1f37..bb59b84 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -108,5 +108,9 @@ pub fn server(conf Config) ! { util.exit_with_message(1, 'Failed to inialize job queue: $err.msg()') } + if conf.max_log_age > 0 { + go app.log_removal_daemon() + } + web.run(app, conf.port) } diff --git a/vieter.toml b/vieter.toml index 74a7397..1f839f0 100644 --- a/vieter.toml +++ b/vieter.toml @@ -12,3 +12,4 @@ address = "http://localhost:8000" api_update_frequency = 2 image_rebuild_frequency = 1 max_concurrent_builds = 3 +max_log_age = 64 From 09c61143b0ea3f0981f50ff1ebabf450f497cefb Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sat, 17 Dec 2022 20:23:25 +0100 Subject: [PATCH 44/97] docs: updated the docs --- docs/api/source/includes/_jobs.md | 51 +++++++++++++++++++++++++++++++ docs/api/source/index.html.md | 1 + docs/content/installation.md | 36 ++++++++++++---------- 3 files changed, 72 insertions(+), 16 deletions(-) create mode 100644 docs/api/source/includes/_jobs.md diff --git a/docs/api/source/includes/_jobs.md b/docs/api/source/includes/_jobs.md new file mode 100644 index 0000000..c124781 --- /dev/null +++ b/docs/api/source/includes/_jobs.md @@ -0,0 +1,51 @@ +# Jobs + + + +## Manually schedule a job + +```shell +curl \ + -H 'X-Api-Key: secret' \ + https://example.com/api/v1/jobs/queue?target=10&force&arch=x86_64 +``` + +Manually schedule a job on the server. + +### HTTP Request + +`POST /api/v1/jobs/queue` + +### Query Parameters + +Parameter | Description +--------- | ----------- +target | Id of target to schedule build for +arch | Architecture to build on +force | Whether it's a forced build (true if present) + +## Poll for new jobs + + + +Poll the server for new builds. + +### HTTP Request + +`GET /api/v1/jobs/poll` + +### Query Parameters + +Parameter | Description +--------- | ----------- +arch | For which architecture to receive jobs +max | How many jobs to receive at most diff --git a/docs/api/source/index.html.md b/docs/api/source/index.html.md index 4bfddb8..f61e44a 100644 --- a/docs/api/source/index.html.md +++ b/docs/api/source/index.html.md @@ -11,6 +11,7 @@ includes: - repository - targets - logs + - jobs search: true diff --git a/docs/content/installation.md b/docs/content/installation.md index 21eda64..5b8e2d8 100644 --- a/docs/content/installation.md +++ b/docs/content/installation.md @@ -23,15 +23,15 @@ guarantees about stability, so beware! Thanks to the single-binary design of Vieter, this image can be used both for the repository server, the cron daemon and the agent. -Below is an example compose file to set up both the repository server & the -cron daemon: +Below is a minimal compose file to set up both the repository server & a build +agent: ```yaml version: '3' services: server: - image: 'chewingbever/vieter:dev' + image: 'chewingbever/vieter:0.5.0-rc.1' restart: 'always' environment: @@ -41,18 +41,19 @@ services: - 'data:/data' cron: - image: 'chewingbever/vieter:dev' + image: 'chewingbever/vieter:0.5.0-rc.1' restart: 'always' + # Required to connect to the Docker daemon user: root - command: 'vieter cron' + command: 'vieter agent' environment: - 'VIETER_API_KEY=secret' # MUST be public URL of Vieter repository - 'VIETER_ADDRESS=https://example.com' - - 'VIETER_DEFAULT_ARCH=x86_64' + # Architecture for which the agent builds + - 'VIETER_ARCH=x86_64' - 'VIETER_MAX_CONCURRENT_BUILDS=2' - - 'VIETER_GLOBAL_SCHEDULE=0 3' volumes: - '/var/run/docker.sock:/var/run/docker.sock' @@ -63,14 +64,17 @@ volumes: If you do not require the build system, the repository server can be used independently as well. +Of course, Vieter allows a lot more configuration than this. This compose file +is meant as a starting point for setting up your installation. + {{< hint info >}} **Note** -Builds are executed on the cron daemon's system using the host's Docker daemon. -A cron daemon on a specific architecture will only build packages for that -specific architecture. Therefore, if you wish to build packages for both -`x86_64` & `aarch64`, you'll have to deploy two cron daemons, one on each -architecture. Afterwards, any Git repositories enabled for those two -architectures will build on both. +Builds are executed on the agent's system using the host's Docker daemon. An +agent for a specific `arch` will only build packages for that specific +architecture. Therefore, if you wish to build packages for both `x86_64` & +`aarch64`, you'll have to deploy two agents, one on each architecture. +Afterwards, any Git repositories enabled for those two architectures will build +on both. {{< /hint >}} ## Binary @@ -99,9 +103,9 @@ latest official release or `vieter-git` for the latest development release. ### AUR If you prefer building the packages locally (or on your own Vieter instance), -there's the `[vieter](https://aur.archlinux.org/packages/vieter)` & -`[vieter-git](https://aur.archlinux.org/packages/vieter-git)` packages on the -AUR. These packages build using the `vlang-git` compiler package, so I can't +there's the [`vieter`](https://aur.archlinux.org/packages/vieter) & +[`vieter-git`](https://aur.archlinux.org/packages/vieter-git) packages on the +AUR. These packages build using the `vlang` compiler package, so I can't guarantee that a compiler update won't temporarily break them. ## Building from source From 26796f2228fe92fb32330ea8f8d0095532ec40b7 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Mon, 19 Dec 2022 09:47:53 +0100 Subject: [PATCH 45/97] feat(server): use cron schedule for log removal instead --- docs/content/configuration.md | 29 +++++++++++++++++++-------- docs/content/usage/builds/cleanup.md | 24 ++++++++++++++++++++++ docs/content/usage/builds/schedule.md | 4 ++++ src/server/cli.v | 19 +++++++++--------- src/server/log_removal.v | 13 +++++++++--- src/server/server.v | 6 +++++- vieter.toml | 1 + 7 files changed, 75 insertions(+), 21 deletions(-) create mode 100644 docs/content/usage/builds/cleanup.md diff --git a/docs/content/configuration.md b/docs/content/configuration.md index e59fe06..e974a58 100644 --- a/docs/content/configuration.md +++ b/docs/content/configuration.md @@ -32,11 +32,11 @@ configuration variable required for each command. ### `vieter server` +* `port`: HTTP port to run on + * Default: `8000` * `log_level`: log verbosity level. Value should be one of `FATAL`, `ERROR`, `WARN`, `INFO` or `DEBUG`. * Default: `WARN` -* `log_file`: log file to write logs to. - * Default: `vieter.log` (in the current directory) * `pkg_dir`: where Vieter should store the actual package archives. * `data_dir`: where Vieter stores the repositories, log file & database. * `api_key`: the API key to use when authenticating requests. @@ -44,14 +44,27 @@ configuration variable required for each command. * Packages with architecture `any` are always added to this architecture. This prevents the server from being confused when an `any` package is published as the very first package for a repository. - * Git repositories added without an `arch` value use this value instead. -* `port`: HTTP port to run on - * Default: `8000` + * Targets added without an `arch` value use this value instead. +* `global_schedule`: build schedule for any target that does not have a + schedule defined. For information about this syntax, see + [here](/usage/builds/schedule). + * Default: `0 3` (3AM every night) +* `base_image`: Docker image to use when building a package. Any Pacman-based + distro image should work, as long as `/etc/pacman.conf` is used & + `base-devel` exists in the repositories. Make sure that the image supports + the architecture of your cron daemon. + * Default: `archlinux:base-devel` (only works on `x86_64`). If you require + `aarch64` support, consider using + [`menci/archlinuxarm:base-devel`](https://hub.docker.com/r/menci/archlinuxarm) + ([GitHub](https://github.com/Menci/docker-archlinuxarm)). This is the + image used for the Vieter CI builds. * `max_log_age`: maximum age of logs (in days). Logs older than this will get - cleaned by the log removal daemon every 24 hours. If set to a negative value, - no logs are ever removed. The age of logs is determined by the time the build - was started. + cleaned by the log removal daemon . If set to a negative value, no logs are + ever removed. The age of logs is determined by the time the build was + started. * Default: `-1` +* `log_removal_schedule`: cron schedule defining when to clean old logs. + * Default: `0 0` (every day at midnight) ### `vieter cron` diff --git a/docs/content/usage/builds/cleanup.md b/docs/content/usage/builds/cleanup.md new file mode 100644 index 0000000..ddeeb85 --- /dev/null +++ b/docs/content/usage/builds/cleanup.md @@ -0,0 +1,24 @@ +--- +weight: 20 +--- + +# Cleanup + +Vieter stores the logs of every single package build. While this is great for +debugging why builds fails, it also causes an active or long-running Vieter +instance to accumulate thousands of logs. + +To combat this, a log removal daemon can be enabled that periodically removes +old build logs. By starting your server with the `max_log_age` variable (see +[Configuration](/configuration#vieter-server) for more info), a daemon will +get enabled that periodically removes logs older than this setting. By default, +this will happen every day at midnight, but this behavior can be changed using +the `log_removal_schedule` variable. + +{{< hint info >}} +**Note** +The daemon will always run a removal of logs on startup. Therefore, it's +possible the daemon will be *very* active when first enabling this setting. +After the initial surge of logs to remove, it'll calm down again. +{{< /hint >}} + diff --git a/docs/content/usage/builds/schedule.md b/docs/content/usage/builds/schedule.md index de59e25..d3802fd 100644 --- a/docs/content/usage/builds/schedule.md +++ b/docs/content/usage/builds/schedule.md @@ -1,3 +1,7 @@ +--- +weight: 10 +--- + # Cron schedule syntax The Vieter cron daemon uses a subset of the cron expression syntax to schedule diff --git a/src/server/cli.v b/src/server/cli.v index 52bce1e..795f764 100644 --- a/src/server/cli.v +++ b/src/server/cli.v @@ -5,15 +5,16 @@ import conf as vconf struct Config { pub: - log_level string = 'WARN' - pkg_dir string - data_dir string - api_key string - default_arch string - global_schedule string = '0 3' - port int = 8000 - base_image string = 'archlinux:base-devel' - max_log_age int = -1 + port int = 8000 + log_level string = 'WARN' + pkg_dir string + data_dir string + api_key string + default_arch string + global_schedule string = '0 3' + base_image string = 'archlinux:base-devel' + max_log_age int = -1 + log_removal_schedule string = '0 0' } // cmd returns the cli submodule that handles starting the server diff --git a/src/server/log_removal.v b/src/server/log_removal.v index f68c575..a901fea 100644 --- a/src/server/log_removal.v +++ b/src/server/log_removal.v @@ -3,11 +3,12 @@ module server import time import models { BuildLog } import os +import cron.expression { CronExpression } -const log_removal_frequency = 24 * time.hour +const fallback_log_removal_frequency = 24 * time.hour // log_removal_daemon removes old build logs every `log_removal_frequency`. -fn (mut app App) log_removal_daemon() { +fn (mut app App) log_removal_daemon(schedule CronExpression) { mut start_time := time.Time{} for { @@ -51,6 +52,12 @@ fn (mut app App) log_removal_daemon() { app.linfo('Cleaned $counter logs ($failed failed)') // Sleep until the next cycle - time.sleep(start_time.add_days(1) - time.now()) + next_time := schedule.next_from_now() or { + app.lerror("Log removal daemon couldn't calculate next time: $err.msg(); fallback to $server.fallback_log_removal_frequency") + + start_time.add(server.fallback_log_removal_frequency) + } + + time.sleep(next_time - time.now()) } } diff --git a/src/server/server.v b/src/server/server.v index bb59b84..178f657 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -55,6 +55,10 @@ pub fn server(conf Config) ! { util.exit_with_message(1, 'Invalid global cron expression: $err.msg()') } + log_removal_ce := expression.parse_expression(conf.log_removal_schedule) or { + util.exit_with_message(1, 'Invalid log removal cron expression: $err.msg()') + } + // Configure logger log_level := log.level_from_tag(conf.log_level) or { util.exit_with_message(1, 'Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.') @@ -109,7 +113,7 @@ pub fn server(conf Config) ! { } if conf.max_log_age > 0 { - go app.log_removal_daemon() + go app.log_removal_daemon(log_removal_ce) } web.run(app, conf.port) diff --git a/vieter.toml b/vieter.toml index 1f839f0..3f63d47 100644 --- a/vieter.toml +++ b/vieter.toml @@ -13,3 +13,4 @@ api_update_frequency = 2 image_rebuild_frequency = 1 max_concurrent_builds = 3 max_log_age = 64 +log_removal_schedule = '*/2 *' From b66d1161edaf9291e973de5666fec15bf0a1b806 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Mon, 19 Dec 2022 11:24:22 +0100 Subject: [PATCH 46/97] docs: update docs some more --- docs/api/source/includes/_jobs.md | 29 +++++++++++++++++++++++++++- docs/api/source/includes/_logs.md | 11 +++++++++-- docs/api/source/includes/_targets.md | 13 ++++++++++++- vieter.toml | 1 - 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/docs/api/source/includes/_jobs.md b/docs/api/source/includes/_jobs.md index c124781..6c08d49 100644 --- a/docs/api/source/includes/_jobs.md +++ b/docs/api/source/includes/_jobs.md @@ -33,10 +33,37 @@ force | Whether it's a forced build (true if present) +```shell +curl \ + -H 'x-api-key: secret' \ + 'https://example.com/api/v1/jobs/poll?arch=x86_64&max=2' +``` + +> JSON output format + +```json +{ + "message": "", + "data": [ + { + "target_id": 1, + "kind": "git", + "url": "https://aur.archlinux.org/discord-ptb.git", + "branch": "master", + "path": "", + "repo": "bur", + "base_image": "archlinux:base-devel", + "force": true + } + ] +} +``` + Poll the server for new builds. ### HTTP Request diff --git a/docs/api/source/includes/_logs.md b/docs/api/source/includes/_logs.md index ba6dada..d6134b7 100644 --- a/docs/api/source/includes/_logs.md +++ b/docs/api/source/includes/_logs.md @@ -125,8 +125,8 @@ id | ID of requested log @@ -152,6 +152,13 @@ Plaintext contents of the build log. ## Remove a build log +```shell +curl \ + -XDELETE \ + -H 'X-Api-Key: secret' \ + https://example.com/api/v1/logs/1 +``` + Remove a build log from the server. ### HTTP Request diff --git a/docs/api/source/includes/_targets.md b/docs/api/source/includes/_targets.md index 93a4e86..b71da84 100644 --- a/docs/api/source/includes/_targets.md +++ b/docs/api/source/includes/_targets.md @@ -27,6 +27,7 @@ curl \ "kind": "git", "url": "https://aur.archlinux.org/discord-ptb.git", "branch": "master", + "path" : "", "repo": "bur", "schedule": "", "arch": [ @@ -73,8 +74,9 @@ curl \ "kind": "git", "url": "https://aur.archlinux.org/discord-ptb.git", "branch": "master", + "path": "", "repo": "bur", - "schedule": "0 3", + "schedule": "0 2", "arch": [ { "id": 1, @@ -124,6 +126,7 @@ Parameter | Description kind | Kind of target to add; one of 'git', 'url'. url | URL of the Git repository. branch | Branch of the Git repository. +path | Subdirectory inside Git repository to use. repo | Vieter repository to publish built packages to. schedule | Cron build schedule (syntax explained [here](https://rustybever.be/docs/vieter/usage/builds/schedule/)) arch | Comma-separated list of architectures to build package on. @@ -149,12 +152,20 @@ Parameter | Description kind | Kind of target; one of 'git', 'url'. url | URL of the Git repository. branch | Branch of the Git repository. +path | Subdirectory inside Git repository to use. repo | Vieter repository to publish built packages to. schedule | Cron build schedule arch | Comma-separated list of architectures to build package on. ## Remove a target +```shell +curl \ + -XDELETE \ + -H 'X-Api-Key: secret' \ + https://example.com/api/v1/targets/1 +``` + Remove a target from the server. ### HTTP Request diff --git a/vieter.toml b/vieter.toml index 3f63d47..1f839f0 100644 --- a/vieter.toml +++ b/vieter.toml @@ -13,4 +13,3 @@ api_update_frequency = 2 image_rebuild_frequency = 1 max_concurrent_builds = 3 max_log_age = 64 -log_removal_schedule = '*/2 *' From ab81eebd8791248cdd279b4588465b506e6f79c8 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Mon, 19 Dec 2022 11:58:35 +0100 Subject: [PATCH 47/97] refactor: some small changes before PR --- docs/api/source/includes/_jobs.md | 4 ++-- docs/content/configuration.md | 2 +- docs/content/usage/builds/cleanup.md | 11 +++++------ src/client/logs.v | 6 ++---- src/models/logs.v | 8 ++++++++ src/server/api_logs.v | 25 +++++++++---------------- src/server/log_removal.v | 9 ++++----- 7 files changed, 31 insertions(+), 34 deletions(-) diff --git a/docs/api/source/includes/_jobs.md b/docs/api/source/includes/_jobs.md index 6c08d49..a25309d 100644 --- a/docs/api/source/includes/_jobs.md +++ b/docs/api/source/includes/_jobs.md @@ -40,8 +40,8 @@ meaning manual requests can cause builds to be skipped. ```shell curl \ - -H 'x-api-key: secret' \ - 'https://example.com/api/v1/jobs/poll?arch=x86_64&max=2' + -H 'X-Api-Key: secret' \ + https://example.com/api/v1/jobs/poll?arch=x86_64&max=2 ``` > JSON output format diff --git a/docs/content/configuration.md b/docs/content/configuration.md index e974a58..45c5de6 100644 --- a/docs/content/configuration.md +++ b/docs/content/configuration.md @@ -59,7 +59,7 @@ configuration variable required for each command. ([GitHub](https://github.com/Menci/docker-archlinuxarm)). This is the image used for the Vieter CI builds. * `max_log_age`: maximum age of logs (in days). Logs older than this will get - cleaned by the log removal daemon . If set to a negative value, no logs are + cleaned by the log removal daemon. If set to a negative value, no logs are ever removed. The age of logs is determined by the time the build was started. * Default: `-1` diff --git a/docs/content/usage/builds/cleanup.md b/docs/content/usage/builds/cleanup.md index ddeeb85..724a75f 100644 --- a/docs/content/usage/builds/cleanup.md +++ b/docs/content/usage/builds/cleanup.md @@ -5,15 +5,15 @@ weight: 20 # Cleanup Vieter stores the logs of every single package build. While this is great for -debugging why builds fails, it also causes an active or long-running Vieter +debugging why builds fail, it also causes an active or long-running Vieter instance to accumulate thousands of logs. To combat this, a log removal daemon can be enabled that periodically removes old build logs. By starting your server with the `max_log_age` variable (see -[Configuration](/configuration#vieter-server) for more info), a daemon will -get enabled that periodically removes logs older than this setting. By default, -this will happen every day at midnight, but this behavior can be changed using -the `log_removal_schedule` variable. +[Configuration](/configuration#vieter-server)), a daemon will get enabled that +periodically removes logs older than this setting. By default, this will happen +every day at midnight, but this behavior can be changed using the +`log_removal_schedule` variable. {{< hint info >}} **Note** @@ -21,4 +21,3 @@ The daemon will always run a removal of logs on startup. Therefore, it's possible the daemon will be *very* active when first enabling this setting. After the initial surge of logs to remove, it'll calm down again. {{< /hint >}} - diff --git a/src/client/logs.v b/src/client/logs.v index e5969dd..2ddb2e2 100644 --- a/src/client/logs.v +++ b/src/client/logs.v @@ -43,8 +43,6 @@ pub fn (c &Client) add_build_log(target_id int, start_time time.Time, end_time t } // remove_build_log removes the build log with the given id from the server. -pub fn (c &Client) remove_build_log(id int) !string { - data := c.send_request(.delete, '/api/v1/logs/$id', {})! - - return data.data +pub fn (c &Client) remove_build_log(id int) ! { + c.send_request(.delete, '/api/v1/logs/$id', {})! } diff --git a/src/models/logs.v b/src/models/logs.v index 12907d8..66a3a0a 100644 --- a/src/models/logs.v +++ b/src/models/logs.v @@ -1,6 +1,7 @@ module models import time +import os pub struct BuildLog { pub mut: @@ -28,6 +29,13 @@ pub fn (bl &BuildLog) str() string { return str } +// path returns the path to the log file, relative to the logs directory +pub fn (bl &BuildLog) path() string { + filename := bl.start_time.custom_format('YYYY-MM-DD_HH-mm-ss') + + return os.join_path(bl.target_id.str(), bl.arch, filename) +} + [params] pub struct BuildLogFilter { pub mut: diff --git a/src/server/api_logs.v b/src/server/api_logs.v index 352266c..13b50b9 100644 --- a/src/server/api_logs.v +++ b/src/server/api_logs.v @@ -86,7 +86,7 @@ fn (mut app App) v1_post_log() web.Result { } // Store log in db - log := BuildLog{ + mut log := BuildLog{ target_id: target_id start_time: start_time end_time: end_time @@ -95,25 +95,20 @@ fn (mut app App) v1_post_log() web.Result { } // id of newly created log - log_id := app.db.add_build_log(log) - - repo_logs_dir := os.join_path(app.conf.data_dir, logs_dir_name, target_id.str(), arch) + log.id = app.db.add_build_log(log) + log_file_path := os.join_path(app.conf.data_dir, logs_dir_name, log.path()) // Create the logs directory of it doesn't exist - if !os.exists(repo_logs_dir) { - os.mkdir_all(repo_logs_dir) or { - app.lerror("Couldn't create dir '$repo_logs_dir'.") + if !os.exists(os.dir(log_file_path)) { + os.mkdir_all(os.dir(log_file_path)) or { + app.lerror('Error while creating log file: $err.msg()') return app.status(.internal_server_error) } } - // Stream log contents to correct file - file_name := start_time.custom_format('YYYY-MM-DD_HH-mm-ss') - full_path := os.join_path_single(repo_logs_dir, file_name) - if length := app.req.header.get(.content_length) { - util.reader_to_file(mut app.reader, length.int(), full_path) or { + util.reader_to_file(mut app.reader, length.int(), log_file_path) or { app.lerror('An error occured while receiving logs: $err.msg()') return app.status(.internal_server_error) @@ -122,16 +117,14 @@ fn (mut app App) v1_post_log() web.Result { return app.status(.length_required) } - return app.json(.ok, new_data_response(log_id)) + return app.json(.ok, new_data_response(log.id)) } // v1_delete_log allows removing a build log from the system. ['/api/v1/logs/:id'; auth; delete] fn (mut app App) v1_delete_log(id int) web.Result { log := app.db.get_build_log(id) or { return app.status(.not_found) } - file_name := log.start_time.custom_format('YYYY-MM-DD_HH-mm-ss') - full_path := os.join_path(app.conf.data_dir, logs_dir_name, log.target_id.str(), log.arch, - file_name) + full_path := os.join_path(app.conf.data_dir, logs_dir_name, log.path()) os.rm(full_path) or { app.lerror('Failed to remove log file $full_path: $err.msg()') diff --git a/src/server/log_removal.v b/src/server/log_removal.v index a901fea..a0a5f78 100644 --- a/src/server/log_removal.v +++ b/src/server/log_removal.v @@ -28,11 +28,10 @@ fn (mut app App) log_removal_daemon(schedule CronExpression) { logs = app.db.get_build_logs(before: too_old_timestamp, offset: offset, limit: 50) for log in logs { - file_name := log.start_time.custom_format('YYYY-MM-DD_HH-mm-ss') - full_path := os.join_path(app.conf.data_dir, logs_dir_name, log.target_id.str(), - log.arch, file_name) - os.rm(full_path) or { - app.lerror('Failed to remove log file $full_path: $err.msg()') + log_file_path := os.join_path(app.conf.data_dir, logs_dir_name, log.path()) + + os.rm(log_file_path) or { + app.lerror('Failed to remove log file $log_file_path: $err.msg()') failed += 1 continue From 2c9331668853994d6f1c24a04e50da573c320008 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Mon, 19 Dec 2022 12:43:46 +0100 Subject: [PATCH 48/97] fix: log removal daemon now properly cleans all old logs --- src/server/log_removal.v | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/server/log_removal.v b/src/server/log_removal.v index a0a5f78..8e1a8c2 100644 --- a/src/server/log_removal.v +++ b/src/server/log_removal.v @@ -18,14 +18,16 @@ fn (mut app App) log_removal_daemon(schedule CronExpression) { app.linfo('Cleaning logs before $too_old_timestamp') - mut offset := u64(0) mut logs := []BuildLog{} mut counter := 0 - mut failed := 0 + mut failed := u64(0) // Remove old logs for { - logs = app.db.get_build_logs(before: too_old_timestamp, offset: offset, limit: 50) + // The offset is used to skip logs that failed to remove. Besides + // this, we don't need to move the offset, because all previously + // oldest logs will have been removed. + logs = app.db.get_build_logs(before: too_old_timestamp, offset: failed, limit: 50) for log in logs { log_file_path := os.join_path(app.conf.data_dir, logs_dir_name, log.path()) @@ -44,8 +46,6 @@ fn (mut app App) log_removal_daemon(schedule CronExpression) { if logs.len < 50 { break } - - offset += 50 } app.linfo('Cleaned $counter logs ($failed failed)') From ab6da78738637ab2624c1290ee73761412baed56 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 21 Dec 2022 23:42:15 +0100 Subject: [PATCH 49/97] feat(cli): use posx-style long options --- CHANGELOG.md | 9 +++++++++ src/main.v | 1 + 2 files changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27d9096..97f7021 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://git.rustybever.be/vieter-v/vieter/src/branch/dev) +### Added + +* API route for removing logs & accompanying CLI command +* Daemon for periodically removing old logs + +### Changed + +* Use `--long-option` instead of `-long-option` for CLI + ## [0.5.0-rc.1](https://git.rustybever.be/vieter-v/vieter/src/tag/0.5.0-rc.1) ### Added diff --git a/src/main.v b/src/main.v index 1053c2f..8b5c362 100644 --- a/src/main.v +++ b/src/main.v @@ -21,6 +21,7 @@ fn main() { name: 'vieter' description: 'Vieter is a lightweight implementation of an Arch repository server.' version: '0.5.0-rc.1' + posix_mode: true flags: [ cli.Flag{ flag: cli.FlagType.string From be3762835d29e8c9cf79f1110057eb6ed8436814 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 21 Dec 2022 23:45:42 +0100 Subject: [PATCH 50/97] chore: bump versions to 0.5.0-rc.2 --- CHANGELOG.md | 2 ++ PKGBUILD | 2 +- src/main.v | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97f7021..e615698 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://git.rustybever.be/vieter-v/vieter/src/branch/dev) +## [0.5.0-rc.2](https://git.rustybever.be/vieter-v/vieter/src/tag/0.5.0-rc.2) + ### Added * API route for removing logs & accompanying CLI command diff --git a/PKGBUILD b/PKGBUILD index 94db654..5e9530a 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -3,7 +3,7 @@ pkgbase='vieter' pkgname='vieter' -pkgver='0.5.0-rc.1' +pkgver='0.5.0_rc.2' pkgrel=1 pkgdesc="Lightweight Arch repository server & package build system" depends=('glibc' 'openssl' 'libarchive' 'sqlite') diff --git a/src/main.v b/src/main.v index 8b5c362..eda38e7 100644 --- a/src/main.v +++ b/src/main.v @@ -20,7 +20,7 @@ fn main() { mut app := cli.Command{ name: 'vieter' description: 'Vieter is a lightweight implementation of an Arch repository server.' - version: '0.5.0-rc.1' + version: '0.5.0-rc.2' posix_mode: true flags: [ cli.Flag{ From 3342eedfa45c94d94224f0367ef265bd14ed451b Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 22 Dec 2022 23:10:10 +0100 Subject: [PATCH 51/97] chore: compile with -skip-unused --- Makefile | 2 +- src/server/api_jobs.v | 4 ++-- src/server/api_logs.v | 10 +++++----- src/server/api_targets.v | 10 +++++----- src/server/repo.v | 6 +++--- src/server/repo_remove.v | 6 +++--- src/web/parse.v | 2 +- 7 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index e716807..4bd1edc 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ SRC_DIR := src SOURCES != find '$(SRC_DIR)' -iname '*.v' V_PATH ?= v -V := $(V_PATH) -showcc -gc boehm -W -d use_openssl +V := $(V_PATH) -showcc -gc boehm -W -d use_openssl -skip-unused all: vieter diff --git a/src/server/api_jobs.v b/src/server/api_jobs.v index 7795351..62bcb27 100644 --- a/src/server/api_jobs.v +++ b/src/server/api_jobs.v @@ -4,7 +4,7 @@ import web import web.response { new_data_response, new_response } // v1_poll_job_queue allows agents to poll for new build jobs. -['/api/v1/jobs/poll'; auth; get] +['/api/v1/jobs/poll'; auth; get; markused] fn (mut app App) v1_poll_job_queue() web.Result { arch := app.query['arch'] or { return app.json(.bad_request, new_response('Missing arch query arg.')) @@ -21,7 +21,7 @@ fn (mut app App) v1_poll_job_queue() web.Result { } // v1_queue_job allows queueing a new one-time build job for the given target. -['/api/v1/jobs/queue'; auth; post] +['/api/v1/jobs/queue'; auth; markused; post] fn (mut app App) v1_queue_job() web.Result { target_id := app.query['target'] or { return app.json(.bad_request, new_response('Missing target query arg.')) diff --git a/src/server/api_logs.v b/src/server/api_logs.v index 13b50b9..3db4204 100644 --- a/src/server/api_logs.v +++ b/src/server/api_logs.v @@ -11,7 +11,7 @@ import models { BuildLog, BuildLogFilter } // v1_get_logs returns all build logs in the database. A 'target' query param can // optionally be added to limit the list of build logs to that repository. -['/api/v1/logs'; auth; get] +['/api/v1/logs'; auth; get; markused] fn (mut app App) v1_get_logs() web.Result { filter := models.from_params(app.query) or { return app.json(.bad_request, new_response('Invalid query parameters.')) @@ -22,7 +22,7 @@ fn (mut app App) v1_get_logs() web.Result { } // v1_get_single_log returns the build log with the given id. -['/api/v1/logs/:id'; auth; get] +['/api/v1/logs/:id'; auth; get; markused] fn (mut app App) v1_get_single_log(id int) web.Result { log := app.db.get_build_log(id) or { return app.status(.not_found) } @@ -30,7 +30,7 @@ fn (mut app App) v1_get_single_log(id int) web.Result { } // v1_get_log_content returns the actual build log file for the given id. -['/api/v1/logs/:id/content'; auth; get] +['/api/v1/logs/:id/content'; auth; get; markused] fn (mut app App) v1_get_log_content(id int) web.Result { log := app.db.get_build_log(id) or { return app.status(.not_found) } file_name := log.start_time.custom_format('YYYY-MM-DD_HH-mm-ss') @@ -50,7 +50,7 @@ fn parse_query_time(query string) !time.Time { } // v1_post_log adds a new log to the database. -['/api/v1/logs'; auth; post] +['/api/v1/logs'; auth; markused; post] fn (mut app App) v1_post_log() web.Result { // Parse query params start_time_int := app.query['startTime'].int() @@ -121,7 +121,7 @@ fn (mut app App) v1_post_log() web.Result { } // v1_delete_log allows removing a build log from the system. -['/api/v1/logs/:id'; auth; delete] +['/api/v1/logs/:id'; auth; delete; markused] fn (mut app App) v1_delete_log(id int) web.Result { log := app.db.get_build_log(id) or { return app.status(.not_found) } full_path := os.join_path(app.conf.data_dir, logs_dir_name, log.path()) diff --git a/src/server/api_targets.v b/src/server/api_targets.v index cd5cb0a..4bb7d12 100644 --- a/src/server/api_targets.v +++ b/src/server/api_targets.v @@ -6,7 +6,7 @@ import db import models { Target, TargetArch, TargetFilter } // v1_get_targets returns the current list of targets. -['/api/v1/targets'; auth; get] +['/api/v1/targets'; auth; get; markused] fn (mut app App) v1_get_targets() web.Result { filter := models.from_params(app.query) or { return app.json(.bad_request, new_response('Invalid query parameters.')) @@ -17,7 +17,7 @@ fn (mut app App) v1_get_targets() web.Result { } // v1_get_single_target returns the information for a single target. -['/api/v1/targets/:id'; auth; get] +['/api/v1/targets/:id'; auth; get; markused] fn (mut app App) v1_get_single_target(id int) web.Result { target := app.db.get_target(id) or { return app.status(.not_found) } @@ -25,7 +25,7 @@ fn (mut app App) v1_get_single_target(id int) web.Result { } // v1_post_target creates a new target from the provided query string. -['/api/v1/targets'; auth; post] +['/api/v1/targets'; auth; markused; post] fn (mut app App) v1_post_target() web.Result { mut params := app.query.clone() @@ -55,7 +55,7 @@ fn (mut app App) v1_post_target() web.Result { } // v1_delete_target removes a given target from the server's list. -['/api/v1/targets/:id'; auth; delete] +['/api/v1/targets/:id'; auth; delete; markused] fn (mut app App) v1_delete_target(id int) web.Result { app.db.delete_target(id) app.job_queue.invalidate(id) @@ -64,7 +64,7 @@ fn (mut app App) v1_delete_target(id int) web.Result { } // v1_patch_target updates a target's data with the given query params. -['/api/v1/targets/:id'; auth; patch] +['/api/v1/targets/:id'; auth; markused; patch] fn (mut app App) v1_patch_target(id int) web.Result { app.db.update_target(id, app.query) diff --git a/src/server/repo.v b/src/server/repo.v index 06ab72e..38d07fe 100644 --- a/src/server/repo.v +++ b/src/server/repo.v @@ -10,7 +10,7 @@ import web.response { new_data_response, new_response } // healthcheck just returns a string, but can be used to quickly check if the // server is still responsive. -['/health'; get] +['/health'; get; markused] pub fn (mut app App) healthcheck() web.Result { return app.json(.ok, new_response('Healthy.')) } @@ -18,7 +18,7 @@ pub fn (mut app App) healthcheck() web.Result { // get_repo_file handles all Pacman-related routes. It returns both the // repository's archives, but also package archives or the contents of a // package's desc file. -['/:repo/:arch/:filename'; get; head] +['/:repo/:arch/:filename'; get; head; markused] fn (mut app App) get_repo_file(repo string, arch string, filename string) web.Result { mut full_path := '' @@ -48,7 +48,7 @@ fn (mut app App) get_repo_file(repo string, arch string, filename string) web.Re } // put_package handles publishing a package to a repository. -['/:repo/publish'; auth; post] +['/:repo/publish'; auth; markused; post] fn (mut app App) put_package(repo string) web.Result { // api is a reserved keyword for api routes & should never be allowed to be // a repository. diff --git a/src/server/repo_remove.v b/src/server/repo_remove.v index 694f085..9e6d747 100644 --- a/src/server/repo_remove.v +++ b/src/server/repo_remove.v @@ -3,7 +3,7 @@ module server import web // delete_package tries to remove the given package. -['/:repo/:arch/:pkg'; auth; delete] +['/:repo/:arch/:pkg'; auth; delete; markused] fn (mut app App) delete_package(repo string, arch string, pkg string) web.Result { res := app.repo.remove_pkg_from_arch_repo(repo, arch, pkg, true) or { app.lerror('Error while deleting package: $err.msg()') @@ -23,7 +23,7 @@ fn (mut app App) delete_package(repo string, arch string, pkg string) web.Result } // delete_arch_repo tries to remove the given arch-repo. -['/:repo/:arch'; auth; delete] +['/:repo/:arch'; auth; delete; markused] fn (mut app App) delete_arch_repo(repo string, arch string) web.Result { res := app.repo.remove_arch_repo(repo, arch) or { app.lerror('Error while deleting arch-repo: $err.msg()') @@ -43,7 +43,7 @@ fn (mut app App) delete_arch_repo(repo string, arch string) web.Result { } // delete_repo tries to remove the given repo. -['/:repo'; auth; delete] +['/:repo'; auth; delete; markused] fn (mut app App) delete_repo(repo string) web.Result { res := app.repo.remove_repo(repo) or { app.lerror('Error while deleting repo: $err.msg()') diff --git a/src/web/parse.v b/src/web/parse.v index 7af635f..889944b 100644 --- a/src/web/parse.v +++ b/src/web/parse.v @@ -5,7 +5,7 @@ import net.http // Method attributes that should be ignored when parsing, as they're used // elsewhere. -const attrs_to_ignore = ['auth'] +const attrs_to_ignore = ['auth', 'markused'] // Parsing function attributes for methods and path. fn parse_attrs(name string, attrs []string) !([]http.Method, string) { From 641cf22669b8e75618b72ae8002f68c760748314 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Fri, 23 Dec 2022 08:18:49 +0100 Subject: [PATCH 52/97] feat(cli): add flag to filter logs by exit codes --- CHANGELOG.md | 1 + src/console/logs/logs.v | 15 +++++++++++++-- src/console/targets/targets.v | 2 ++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e615698..3e67899 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * API route for removing logs & accompanying CLI command * Daemon for periodically removing old logs +* CLI flag to filter logs by specific exit codes ### Changed diff --git a/src/console/logs/logs.v b/src/console/logs/logs.v index 19c46f6..35ce4d7 100644 --- a/src/console/logs/logs.v +++ b/src/console/logs/logs.v @@ -24,11 +24,13 @@ pub fn cmd() cli.Command { flags: [ cli.Flag{ name: 'limit' + abbrev: 'l' description: 'How many results to return.' flag: cli.FlagType.int }, cli.Flag{ name: 'offset' + abbrev: 'o' description: 'Minimum index to return.' flag: cli.FlagType.int }, @@ -39,16 +41,18 @@ pub fn cmd() cli.Command { }, cli.Flag{ name: 'today' - description: 'Only list logs started today.' + abbrev: 't' + description: 'Only list logs started today. This flag overwrites any other date-related flag.' flag: cli.FlagType.bool }, cli.Flag{ name: 'failed' - description: 'Only list logs with non-zero exit codes.' + description: 'Only list logs with non-zero exit codes. This flag overwrites the --code flag.' flag: cli.FlagType.bool }, cli.Flag{ name: 'day' + abbrev: 'd' description: 'Only list logs started on this day. (format: YYYY-MM-DD)' flag: cli.FlagType.string }, @@ -62,6 +66,11 @@ pub fn cmd() cli.Command { description: 'Only list logs started after this timestamp. (format: YYYY-MM-DD HH:mm:ss)' flag: cli.FlagType.string }, + cli.Flag{ + name: 'code' + description: 'Only return logs with the given exit code. Prepend with `!` to exclude instead of include. Can be specified multiple times.' + flag: cli.FlagType.string_array + }, ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! @@ -131,6 +140,8 @@ pub fn cmd() cli.Command { filter.exit_codes = [ '!0', ] + } else { + filter.exit_codes = cmd.flags.get_strings('code')! } raw := cmd.flags.get_bool('raw')! diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index 94deebd..3c0d755 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -25,11 +25,13 @@ pub fn cmd() cli.Command { flags: [ cli.Flag{ name: 'limit' + abbrev: 'l' description: 'How many results to return.' flag: cli.FlagType.int }, cli.Flag{ name: 'offset' + abbrev: 'o' description: 'Minimum index to return.' flag: cli.FlagType.int }, From b7af0511038db4297450f46da952a4cfeab72da9 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 28 Dec 2022 21:24:30 +0100 Subject: [PATCH 53/97] feat(client): support removing repos, arch-repos & packages --- src/client/logs.v | 9 ++++----- src/client/repos.v | 16 ++++++++++++++++ src/client/targets.v | 11 +++++------ 3 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 src/client/repos.v diff --git a/src/client/logs.v b/src/client/logs.v index 2ddb2e2..6553837 100644 --- a/src/client/logs.v +++ b/src/client/logs.v @@ -1,28 +1,27 @@ module client import models { BuildLog, BuildLogFilter } -import net.http { Method } import web.response { Response } import time // get_build_logs returns all build logs. pub fn (c &Client) get_build_logs(filter BuildLogFilter) ![]BuildLog { params := models.params_from(filter) - data := c.send_request<[]BuildLog>(Method.get, '/api/v1/logs', params)! + data := c.send_request<[]BuildLog>(.get, '/api/v1/logs', params)! return data.data } // get_build_log returns a specific build log. pub fn (c &Client) get_build_log(id int) !BuildLog { - data := c.send_request(Method.get, '/api/v1/logs/$id', {})! + data := c.send_request(.get, '/api/v1/logs/$id', {})! return data.data } // get_build_log_content returns the contents of the build log file. pub fn (c &Client) get_build_log_content(id int) !string { - data := c.send_request_raw_response(Method.get, '/api/v1/logs/$id/content', {}, '')! + data := c.send_request_raw_response(.get, '/api/v1/logs/$id/content', {}, '')! return data } @@ -37,7 +36,7 @@ pub fn (c &Client) add_build_log(target_id int, start_time time.Time, end_time t 'exitCode': exit_code.str() } - data := c.send_request_with_body(Method.post, '/api/v1/logs', params, content)! + data := c.send_request_with_body(.post, '/api/v1/logs', params, content)! return data } diff --git a/src/client/repos.v b/src/client/repos.v new file mode 100644 index 0000000..9644e9b --- /dev/null +++ b/src/client/repos.v @@ -0,0 +1,16 @@ +module client + +// remove_repo removes an entire repository. +pub fn (c &Client) remove_repo(repo string) ! { + c.send_request(.delete, '/$repo', {})! +} + +// remove_arch_repo removes an entire arch-repo. +pub fn (c &Client) remove_arch_repo(repo string, arch string) ! { + c.send_request(.delete, '/$repo/$arch', {})! +} + +// remove_package removes a single package from the given arch-repo. +pub fn (c &Client) remove_package(repo string, arch string, pkgname string) ! { + c.send_request(.delete, '/$repo/$arch/$pkgname', {})! +} diff --git a/src/client/targets.v b/src/client/targets.v index da6a9e4..565832e 100644 --- a/src/client/targets.v +++ b/src/client/targets.v @@ -1,12 +1,11 @@ module client import models { Target, TargetFilter } -import net.http { Method } // get_targets returns a list of targets, given a filter object. pub fn (c &Client) get_targets(filter TargetFilter) ![]Target { params := models.params_from(filter) - data := c.send_request<[]Target>(Method.get, '/api/v1/targets', params)! + data := c.send_request<[]Target>(.get, '/api/v1/targets', params)! return data.data } @@ -34,7 +33,7 @@ pub fn (c &Client) get_all_targets() ![]Target { // get_target returns the target for a specific id. pub fn (c &Client) get_target(id int) !Target { - data := c.send_request(Method.get, '/api/v1/targets/$id', {})! + data := c.send_request(.get, '/api/v1/targets/$id', {})! return data.data } @@ -51,14 +50,14 @@ pub struct NewTarget { // add_target adds a new target to the server. pub fn (c &Client) add_target(t NewTarget) !int { params := models.params_from(t) - data := c.send_request(Method.post, '/api/v1/targets', params)! + data := c.send_request(.post, '/api/v1/targets', params)! return data.data } // remove_target removes the target with the given id from the server. pub fn (c &Client) remove_target(id int) !string { - data := c.send_request(Method.delete, '/api/v1/targets/$id', {})! + data := c.send_request(.delete, '/api/v1/targets/$id', {})! return data.data } @@ -66,7 +65,7 @@ pub fn (c &Client) remove_target(id int) !string { // patch_target sends a PATCH request to the given target with the params as // payload. pub fn (c &Client) patch_target(id int, params map[string]string) !string { - data := c.send_request(Method.patch, '/api/v1/targets/$id', params)! + data := c.send_request(.patch, '/api/v1/targets/$id', params)! return data.data } From cac74db086e0163f6097f62a518746904151328c Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 28 Dec 2022 21:52:16 +0100 Subject: [PATCH 54/97] feat(console): add commands for removing repos, arch-repos, packages --- CHANGELOG.md | 4 +++ src/console/repos/repos.v | 52 +++++++++++++++++++++++++++++++++++++++ src/main.v | 2 ++ 3 files changed, 58 insertions(+) create mode 100644 src/console/repos/repos.v diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e67899..55e6d65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://git.rustybever.be/vieter-v/vieter/src/branch/dev) +### Added + +* CLI commands for removing packages, arch-repos & repositories + ## [0.5.0-rc.2](https://git.rustybever.be/vieter-v/vieter/src/tag/0.5.0-rc.2) ### Added diff --git a/src/console/repos/repos.v b/src/console/repos/repos.v new file mode 100644 index 0000000..729208e --- /dev/null +++ b/src/console/repos/repos.v @@ -0,0 +1,52 @@ +module repos + +import cli +import conf as vconf +import client + +struct Config { + address string [required] + api_key string [required] +} + +// cmd returns the cli module that handles modifying the repository contents. +pub fn cmd() cli.Command { + return cli.Command{ + name: 'repos' + description: 'Interact with the repositories & packages stored on the server.' + commands: [ + cli.Command{ + name: 'remove' + required_args: 1 + usage: 'repo [arch [pkgname]]' + description: 'Remove a repo, arch-repo, or package from the server.' + flags: [ + cli.Flag{ + name: 'force' + flag: cli.FlagType.bool + }, + ] + execute: fn (cmd cli.Command) ! { + config_file := cmd.flags.get_string('config-file')! + conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + + if cmd.args.len < 3 { + if !cmd.flags.get_bool('force')! { + return error('Removing an arch-repo or repository is a very destructive command. If you really do wish to perform this operation, explicitely add the --force flag.') + } + } + + client := client.new(conf.address, conf.api_key) + + if cmd.args.len == 1 { + client.remove_repo(cmd.args[0])! + } else if cmd.args.len == 2 { + client.remove_arch_repo(cmd.args[0], cmd.args[1])! + } else { + client.remove_package(cmd.args[0], cmd.args[1], cmd.args[2])! + } + } + }, + ] + } +} diff --git a/src/main.v b/src/main.v index eda38e7..8d4ca04 100644 --- a/src/main.v +++ b/src/main.v @@ -8,6 +8,7 @@ import console.logs import console.schedule import console.man import console.aur +import console.repos import cron import agent @@ -48,6 +49,7 @@ fn main() { man.cmd(), aur.cmd(), agent.cmd(), + repos.cmd(), ] } app.setup() From bb4406404db0db6a983f960bfc16b52f8691ab8f Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 28 Dec 2022 22:02:02 +0100 Subject: [PATCH 55/97] chore: use new conf features --- docs/content/configuration.md | 7 +++---- src/server/cli.v | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/docs/content/configuration.md b/docs/content/configuration.md index 45c5de6..612c505 100644 --- a/docs/content/configuration.md +++ b/docs/content/configuration.md @@ -59,10 +59,9 @@ configuration variable required for each command. ([GitHub](https://github.com/Menci/docker-archlinuxarm)). This is the image used for the Vieter CI builds. * `max_log_age`: maximum age of logs (in days). Logs older than this will get - cleaned by the log removal daemon. If set to a negative value, no logs are - ever removed. The age of logs is determined by the time the build was - started. - * Default: `-1` + cleaned by the log removal daemon. If set to zero, no logs are ever removed. + The age of logs is determined by the time the build was started. + * Default: `0` * `log_removal_schedule`: cron schedule defining when to clean old logs. * Default: `0 0` (every day at midnight) diff --git a/src/server/cli.v b/src/server/cli.v index 795f764..aec93ca 100644 --- a/src/server/cli.v +++ b/src/server/cli.v @@ -13,7 +13,7 @@ pub: default_arch string global_schedule string = '0 3' base_image string = 'archlinux:base-devel' - max_log_age int = -1 + max_log_age int [empty_default] log_removal_schedule string = '0 0' } From 4635127ba2fec63f587dae353ff8114cce1a0c2f Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 28 Dec 2022 22:15:48 +0100 Subject: [PATCH 56/97] docs: removed an outdated page --- docs/content/other/_index.md | 3 - docs/content/other/builds-in-depth.md | 81 --------------------------- src/server/cli.v | 2 +- 3 files changed, 1 insertion(+), 85 deletions(-) delete mode 100644 docs/content/other/_index.md delete mode 100644 docs/content/other/builds-in-depth.md diff --git a/docs/content/other/_index.md b/docs/content/other/_index.md deleted file mode 100644 index 394456b..0000000 --- a/docs/content/other/_index.md +++ /dev/null @@ -1,3 +0,0 @@ ---- -weight: 100 ---- diff --git a/docs/content/other/builds-in-depth.md b/docs/content/other/builds-in-depth.md deleted file mode 100644 index d8df6ec..0000000 --- a/docs/content/other/builds-in-depth.md +++ /dev/null @@ -1,81 +0,0 @@ -# Builds In-depth - -For those interested, this page describes how the build system works -internally. - -## Builder image - -Every cron daemon perodically creates a builder image that is then used as a -base for all builds. This is done to prevent build containers having to pull -down a bunch of updates when they update their system. - -The build container is created by running the following commands inside a -container started from the image defined in `base_image`: - -```sh -# Update repos & install required packages -pacman -Syu --needed --noconfirm base-devel git -# Add a non-root user to run makepkg -groupadd -g 1000 builder -useradd -mg builder builder -# Make sure they can use sudo without a password -echo 'builder ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers -# Create the directory for the builds & make it writeable for the -# build user -mkdir /build -chown -R builder:builder /build -``` - -This script updates the packages to their latest versions & creates a non-root -user to use when running `makepkg`. - -This script is base64-encoded & passed to the container as an environment -variable. The container's entrypoint is set to `/bin/sh -c` & its command -argument to `echo $BUILD_SCRIPT | base64 -d | /bin/sh -e`, with the -`BUILD_SCRIPT` environment variable containing the base64-encoded script. - -Once the container exits, a new Docker image is created from it. This image is -then used as the base for any builds. - -## Running builds - -Each build has its own Docker container, using the builder image as its base. -The same base64-based technique as above is used, just with a different script. -To make the build logs more clear, each command is appended by an echo command -printing the next command to stdout. - -Given the Git repository URL is `https://examplerepo.com` with branch `main`, -the URL of the Vieter server is `https://example.com` and `vieter` is the -repository we wish to publish to, we get the following script: - -```sh -echo -e '+ echo -e '\''[vieter]\\nServer = https://example.com/$repo/$arch\\nSigLevel = Optional'\'' >> /etc/pacman.conf' -echo -e '[vieter]\nServer = https://example.com/$repo/$arch\nSigLevel = Optional' >> /etc/pacman.conf -echo -e '+ pacman -Syu --needed --noconfirm' -pacman -Syu --needed --noconfirm -echo -e '+ su builder' -su builder -echo -e '+ git clone --single-branch --depth 1 --branch main https://examplerepo.com repo' -git clone --single-branch --depth 1 --branch main https://examplerepo.com repo -echo -e '+ cd repo' -cd repo -echo -e '+ makepkg --nobuild --syncdeps --needed --noconfirm' -makepkg --nobuild --syncdeps --needed --noconfirm -echo -e '+ source PKGBUILD' -source PKGBUILD -echo -e '+ curl -s --head --fail https://example.com/vieter/x86_64/$pkgname-$pkgver-$pkgrel && exit 0' -curl -s --head --fail https://example.com/vieter/x86_64/$pkgname-$pkgver-$pkgrel && exit 0 -echo -e '+ [ "$(id -u)" == 0 ] && exit 0' -[ "$(id -u)" == 0 ] && exit 0 -echo -e '+ MAKEFLAGS="-j$(nproc)" makepkg -s --noconfirm --needed && for pkg in $(ls -1 *.pkg*); do curl -XPOST -T "$pkg" -H "X-API-KEY: $API_KEY" https://example.com/vieter/publish; done' -MAKEFLAGS="-j$(nproc)" makepkg -s --noconfirm --needed && for pkg in $(ls -1 *.pkg*); do curl -XPOST -T "$pkg" -H "X-API-KEY: $API_KEY" https://example.com/vieter/publish; done -``` - -This script: - -1. Adds the target repository as a repository in the build container -2. Updates mirrors & packages -3. Clones the Git repository -4. Runs `makepkg` without building to calculate `pkgver` -5. Checks whether the package version is already present on the server -6. If not, run `makepkg` & publish any generated package archives to the server diff --git a/src/server/cli.v b/src/server/cli.v index aec93ca..21fb15e 100644 --- a/src/server/cli.v +++ b/src/server/cli.v @@ -13,7 +13,7 @@ pub: default_arch string global_schedule string = '0 3' base_image string = 'archlinux:base-devel' - max_log_age int [empty_default] + max_log_age int [empty_default] log_removal_schedule string = '0 0' } From 1c70bce9e4385e609619ce0a248f11a45bb67a3d Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 29 Dec 2022 15:49:59 +0100 Subject: [PATCH 57/97] chore: bump versions to 0.5.0 --- CHANGELOG.md | 2 ++ PKGBUILD | 2 +- src/main.v | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55e6d65..0e4e228 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://git.rustybever.be/vieter-v/vieter/src/branch/dev) +## [0.5.0](https://git.rustybever.be/vieter-v/vieter/src/tag/0.5.0) + ### Added * CLI commands for removing packages, arch-repos & repositories diff --git a/PKGBUILD b/PKGBUILD index 5e9530a..bf9c621 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -3,7 +3,7 @@ pkgbase='vieter' pkgname='vieter' -pkgver='0.5.0_rc.2' +pkgver='0.5.0' pkgrel=1 pkgdesc="Lightweight Arch repository server & package build system" depends=('glibc' 'openssl' 'libarchive' 'sqlite') diff --git a/src/main.v b/src/main.v index 8d4ca04..1c8b816 100644 --- a/src/main.v +++ b/src/main.v @@ -21,7 +21,7 @@ fn main() { mut app := cli.Command{ name: 'vieter' description: 'Vieter is a lightweight implementation of an Arch repository server.' - version: '0.5.0-rc.2' + version: '0.5.0' posix_mode: true flags: [ cli.Flag{ From c0f58ddc77e2db11dcf7d54d07210934015aaffd Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 28 Dec 2022 16:09:00 +0100 Subject: [PATCH 58/97] feat(server): add metric collection --- src/server/api_metrics.v | 16 ++++++++++++++++ src/server/server.v | 2 ++ src/v.mod | 3 ++- src/web/web.v | 22 ++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/server/api_metrics.v diff --git a/src/server/api_metrics.v b/src/server/api_metrics.v new file mode 100644 index 0000000..af1b134 --- /dev/null +++ b/src/server/api_metrics.v @@ -0,0 +1,16 @@ +module server + +import metrics +import web + +['/api/v1/metrics'; get] +fn (mut app App) v1_metrics() web.Result { + mut exporter := metrics.new_prometheus_exporter([0.01, 0.05, 0.1, 0.5, 1, 100]) + exporter.load(app.collector) + + // TODO stream to connection instead + body := exporter.export_to_string() or { + return app.status(.internal_server_error) + } + return app.body(.ok, 'text/plain', body) +} diff --git a/src/server/server.v b/src/server/server.v index 178f657..9571b7b 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -8,6 +8,7 @@ import util import db import build { BuildJobQueue } import cron.expression +import metrics const ( log_file_name = 'vieter.log' @@ -107,6 +108,7 @@ pub fn server(conf Config) ! { repo: repo db: db job_queue: build.new_job_queue(global_ce, conf.base_image) + collector: metrics.new_default_collector() } app.init_job_queue() or { util.exit_with_message(1, 'Failed to inialize job queue: $err.msg()') diff --git a/src/v.mod b/src/v.mod index 710c976..461af6a 100644 --- a/src/v.mod +++ b/src/v.mod @@ -2,6 +2,7 @@ Module { dependencies: [ 'https://git.rustybever.be/vieter-v/conf', 'https://git.rustybever.be/vieter-v/docker', - 'https://git.rustybever.be/vieter-v/aur' + 'https://git.rustybever.be/vieter-v/aur', + 'https://git.rustybever.be/vieter-v/metrics' ] } diff --git a/src/web/web.v b/src/web/web.v index 565baff..95c91ed 100644 --- a/src/web/web.v +++ b/src/web/web.v @@ -11,6 +11,7 @@ import net.urllib import time import json import log +import metrics // The Context struct represents the Context which hold the HTTP request and response. // It has fields for the query, form, files. @@ -27,6 +28,8 @@ pub mut: conn &net.TcpConn = unsafe { nil } // Gives access to a shared logger object logger shared log.Log + // Used to collect metrics on the web server + collector &metrics.MetricsCollector // time.ticks() from start of web connection handle. // You can use it to determine how much time is spent on your request. page_gen_start i64 @@ -145,6 +148,14 @@ pub fn (ctx &Context) is_authenticated() bool { return false } +pub fn (mut ctx Context) body(status http.Status, content_type string, body string) Result { + ctx.status = status + ctx.content_type = content_type + ctx.send_response(body) + + return Result{} +} + // json HTTP_OK with json_s as payload with content-type `application/json` pub fn (mut ctx Context) json(status http.Status, j T) Result { ctx.status = status @@ -319,6 +330,16 @@ fn handle_conn(mut conn net.TcpConn, mut app T, routes map[string]Route) { app.logger.flush() } + // Record how long request took to process + labels := [ + ['method', app.req.method.str()]!, + ['path', app.req.url]!, + ['status', app.status.int().str()]! + ] + app.collector.counter_increment(name: 'http_requests_total', labels: labels) + app.collector.histogram_record(time.ticks() - app.page_gen_start, name: 'http_requests_time_ms', labels: labels) + /* app.collector.histogram_ */ + unsafe { free(app) } @@ -384,6 +405,7 @@ fn handle_conn(mut conn net.TcpConn, mut app T, routes map[string]Route) { static_mime_types: app.static_mime_types reader: reader logger: app.logger + collector: app.collector api_key: app.api_key } From 4ca2521937bc57bda4b6e45080dcd497687d8ec0 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 28 Dec 2022 17:39:45 +0100 Subject: [PATCH 59/97] feat(server): ability to disable metrics --- CHANGELOG.md | 2 ++ src/server/api_metrics.v | 13 ++++++++----- src/server/cli.v | 1 + src/server/server.v | 8 +++++++- src/web/web.v | 9 ++++++--- vieter.toml | 1 + 6 files changed, 25 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e4e228..72c5440 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://git.rustybever.be/vieter-v/vieter/src/branch/dev) +* Metrics endpoint for Prometheus integration + ## [0.5.0](https://git.rustybever.be/vieter-v/vieter/src/tag/0.5.0) ### Added diff --git a/src/server/api_metrics.v b/src/server/api_metrics.v index af1b134..8d6f654 100644 --- a/src/server/api_metrics.v +++ b/src/server/api_metrics.v @@ -3,14 +3,17 @@ module server import metrics import web -['/api/v1/metrics'; get] +// v1_metrics serves a Prometheus-compatible metrics endpoint. +['/api/v1/metrics'; get; markused] fn (mut app App) v1_metrics() web.Result { + if !app.conf.collect_metrics { + return app.status(.not_found) + } + mut exporter := metrics.new_prometheus_exporter([0.01, 0.05, 0.1, 0.5, 1, 100]) exporter.load(app.collector) - + // TODO stream to connection instead - body := exporter.export_to_string() or { - return app.status(.internal_server_error) - } + body := exporter.export_to_string() or { return app.status(.internal_server_error) } return app.body(.ok, 'text/plain', body) } diff --git a/src/server/cli.v b/src/server/cli.v index 21fb15e..9a8b144 100644 --- a/src/server/cli.v +++ b/src/server/cli.v @@ -15,6 +15,7 @@ pub: base_image string = 'archlinux:base-devel' max_log_age int [empty_default] log_removal_schedule string = '0 0' + collect_metrics bool [empty_default] } // cmd returns the cli submodule that handles starting the server diff --git a/src/server/server.v b/src/server/server.v index 9571b7b..76e7ad6 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -101,14 +101,20 @@ pub fn server(conf Config) ! { util.exit_with_message(1, 'Failed to initialize database: $err.msg()') } + collector := if conf.collect_metrics { + &metrics.MetricsCollector(metrics.new_default_collector()) + } else { + &metrics.MetricsCollector(metrics.new_null_collector()) + } + mut app := &App{ logger: logger api_key: conf.api_key conf: conf repo: repo db: db + collector: collector job_queue: build.new_job_queue(global_ce, conf.base_image) - collector: metrics.new_default_collector() } app.init_job_queue() or { util.exit_with_message(1, 'Failed to inialize job queue: $err.msg()') diff --git a/src/web/web.v b/src/web/web.v index 95c91ed..c44057e 100644 --- a/src/web/web.v +++ b/src/web/web.v @@ -148,6 +148,7 @@ pub fn (ctx &Context) is_authenticated() bool { return false } +// body sends the given body as an HTTP response. pub fn (mut ctx Context) body(status http.Status, content_type string, body string) Result { ctx.status = status ctx.content_type = content_type @@ -334,11 +335,13 @@ fn handle_conn(mut conn net.TcpConn, mut app T, routes map[string]Route) { labels := [ ['method', app.req.method.str()]!, ['path', app.req.url]!, - ['status', app.status.int().str()]! + ['status', app.status.int().str()]!, ] app.collector.counter_increment(name: 'http_requests_total', labels: labels) - app.collector.histogram_record(time.ticks() - app.page_gen_start, name: 'http_requests_time_ms', labels: labels) - /* app.collector.histogram_ */ + app.collector.histogram_record(time.ticks() - app.page_gen_start, + name: 'http_requests_time_ms' + labels: labels + ) unsafe { free(app) diff --git a/vieter.toml b/vieter.toml index 1f839f0..31eadc0 100644 --- a/vieter.toml +++ b/vieter.toml @@ -13,3 +13,4 @@ api_update_frequency = 2 image_rebuild_frequency = 1 max_concurrent_builds = 3 max_log_age = 64 +collect_metrics = true From 4ed4ef4a27b904180aa103b20c94fc32d2c87920 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 3 Jan 2023 09:29:55 +0100 Subject: [PATCH 60/97] chore: generate man pages using debug build --- .woodpecker/man.yml | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/.woodpecker/man.yml b/.woodpecker/man.yml index 8c6ca06..8102443 100644 --- a/.woodpecker/man.yml +++ b/.woodpecker/man.yml @@ -8,15 +8,21 @@ branches: depends_on: - build -skip_clone: true - pipeline: - generate: + install-modules: image: *vlang_image pull: true commands: - - curl -o vieter -L "https://s3.rustybever.be/vieter/commits/$CI_COMMIT_SHA/vieter-linux-amd64" - - chmod +x vieter + - export VMODULES=$PWD/.vmodules + - 'cd src && v install' + + generate: + image: *vlang_image + commands: + # - curl -o vieter -L "https://s3.rustybever.be/vieter/commits/$CI_COMMIT_SHA/vieter-linux-amd64" + # - chmod +x vieter + - export VMODULES=$PWD/.vmodules + - make - ./vieter man man - cd man From 60d5fb77e04a697f07cfc8c5e42b17b4b3e641d4 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 4 Jan 2023 09:19:02 +0100 Subject: [PATCH 61/97] feat(metrics): add prefix; use base unit for time --- src/server/api_metrics.v | 5 +++-- src/web/web.v | 8 ++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/server/api_metrics.v b/src/server/api_metrics.v index 8d6f654..cde4437 100644 --- a/src/server/api_metrics.v +++ b/src/server/api_metrics.v @@ -10,8 +10,9 @@ fn (mut app App) v1_metrics() web.Result { return app.status(.not_found) } - mut exporter := metrics.new_prometheus_exporter([0.01, 0.05, 0.1, 0.5, 1, 100]) - exporter.load(app.collector) + mut exporter := metrics.new_prometheus_exporter([0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, + 10]) + exporter.load('vieter_', app.collector) // TODO stream to connection instead body := exporter.export_to_string() or { return app.status(.internal_server_error) } diff --git a/src/web/web.v b/src/web/web.v index c44057e..f0f3523 100644 --- a/src/web/web.v +++ b/src/web/web.v @@ -335,11 +335,15 @@ fn handle_conn(mut conn net.TcpConn, mut app T, routes map[string]Route) { labels := [ ['method', app.req.method.str()]!, ['path', app.req.url]!, + // Not all methods properly set this value yet I think ['status', app.status.int().str()]!, ] app.collector.counter_increment(name: 'http_requests_total', labels: labels) - app.collector.histogram_record(time.ticks() - app.page_gen_start, - name: 'http_requests_time_ms' + // Prometheus prefers metrics containing base units, as defined here + // https://prometheus.io/docs/practices/naming/ + app.collector.histogram_record(f64(time.ticks() - app.page_gen_start) / 1000, + + name: 'http_requests_duration_seconds' labels: labels ) From f8f611f5c55ff2cbcabe5d6fdaf2c396e11c6dc1 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sat, 31 Dec 2022 10:19:59 +0100 Subject: [PATCH 62/97] feat(api): add search query to targets --- src/console/targets/targets.v | 11 ++++++++ src/db/targets.v | 53 +++++++++++++++++++++++++++-------- src/models/targets.v | 1 + src/server/api_targets.v | 3 ++ vieter.toml | 2 +- 5 files changed, 58 insertions(+), 12 deletions(-) diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index 3c0d755..dfc3792 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -40,6 +40,12 @@ pub fn cmd() cli.Command { description: 'Only return targets that publish to this repo.' flag: cli.FlagType.string }, + cli.Flag{ + name: 'query' + abbrev: 'q' + description: 'Search string to filter targets by.' + flag: cli.FlagType.string + }, ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! @@ -62,6 +68,11 @@ pub fn cmd() cli.Command { filter.repo = repo } + query := cmd.flags.get_string('query')! + if query != '' { + filter.query = query + } + raw := cmd.flags.get_bool('raw')! list(conf, filter, raw)! diff --git a/src/db/targets.v b/src/db/targets.v index 41e56df..fba227e 100644 --- a/src/db/targets.v +++ b/src/db/targets.v @@ -1,24 +1,55 @@ module db import models { Target, TargetArch, TargetFilter } +import math // get_targets returns all targets in the database. pub fn (db &VieterDb) get_targets(filter TargetFilter) []Target { - // This seems to currently be blocked by a bug in the ORM, I'll have to ask - // around. - if filter.repo != '' { - res := sql db.conn { - select from Target where repo == filter.repo order by id limit filter.limit offset filter.offset + window_size := 32 + + mut out := []Target{} + mut targets := []Target{cap: window_size} + + mut offset := 0 + mut filtered_offset := u64(0) + + for out.len < filter.limit { + targets = sql db.conn { + select from Target order by id limit window_size offset offset + } + offset += targets.len + + if targets.len == 0 { + break } - return res + if filter.repo != '' { + targets = targets.filter(it.repo == filter.repo) + } + + if filter.query != '' { + targets = targets.filter(it.url.contains(filter.query) || it.path.contains(filter.query) + || it.branch.contains(filter.query)) + } + + if filtered_offset > filter.offset { + end_index := math.min(filter.limit - u64(out.len), u64(targets.len)) + + out << targets[0..end_index] + } + // We start counting targets in the middle of the current window + else if filtered_offset + u64(targets.len) > filter.offset { + start_index := filter.offset - filtered_offset + end_index := start_index + + math.min(filter.limit - u64(out.len), u64(targets.len) - start_index) + + out << targets[start_index..end_index] + } + + filtered_offset += u64(targets.len) } - res := sql db.conn { - select from Target order by id limit filter.limit offset filter.offset - } - - return res + return out } // get_target tries to return a specific target. diff --git a/src/models/targets.v b/src/models/targets.v index af3cb0d..612f7fa 100644 --- a/src/models/targets.v +++ b/src/models/targets.v @@ -73,4 +73,5 @@ pub mut: limit u64 = 25 offset u64 repo string + query string } diff --git a/src/server/api_targets.v b/src/server/api_targets.v index 4bb7d12..f04fdae 100644 --- a/src/server/api_targets.v +++ b/src/server/api_targets.v @@ -81,3 +81,6 @@ fn (mut app App) v1_patch_target(id int) web.Result { return app.json(.ok, new_data_response(target)) } + +['/api/v1/targets/search'; auth; get; markused] +fn (mut app App) v1_search_targets() diff --git a/vieter.toml b/vieter.toml index 31eadc0..7744a56 100644 --- a/vieter.toml +++ b/vieter.toml @@ -12,5 +12,5 @@ address = "http://localhost:8000" api_update_frequency = 2 image_rebuild_frequency = 1 max_concurrent_builds = 3 -max_log_age = 64 +# max_log_age = 64 collect_metrics = true From c9edb55abcf2bbee5848a5c27dc2cfd662325671 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 31 Dec 2022 16:10:47 +0100 Subject: [PATCH 63/97] feat(db): implemented iterator over targets --- CHANGELOG.md | 3 + src/db/targets.v | 50 ---------------- src/db/targets_iter.v | 123 +++++++++++++++++++++++++++++++++++++++ src/server/api_targets.v | 7 +-- src/server/server.v | 13 +---- 5 files changed, 130 insertions(+), 66 deletions(-) create mode 100644 src/db/targets_iter.v diff --git a/CHANGELOG.md b/CHANGELOG.md index 72c5440..2ab14e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://git.rustybever.be/vieter-v/vieter/src/branch/dev) +### Added + * Metrics endpoint for Prometheus integration +* Search in list of targets using API & CLI ## [0.5.0](https://git.rustybever.be/vieter-v/vieter/src/tag/0.5.0) diff --git a/src/db/targets.v b/src/db/targets.v index fba227e..e022a70 100644 --- a/src/db/targets.v +++ b/src/db/targets.v @@ -1,56 +1,6 @@ module db import models { Target, TargetArch, TargetFilter } -import math - -// get_targets returns all targets in the database. -pub fn (db &VieterDb) get_targets(filter TargetFilter) []Target { - window_size := 32 - - mut out := []Target{} - mut targets := []Target{cap: window_size} - - mut offset := 0 - mut filtered_offset := u64(0) - - for out.len < filter.limit { - targets = sql db.conn { - select from Target order by id limit window_size offset offset - } - offset += targets.len - - if targets.len == 0 { - break - } - - if filter.repo != '' { - targets = targets.filter(it.repo == filter.repo) - } - - if filter.query != '' { - targets = targets.filter(it.url.contains(filter.query) || it.path.contains(filter.query) - || it.branch.contains(filter.query)) - } - - if filtered_offset > filter.offset { - end_index := math.min(filter.limit - u64(out.len), u64(targets.len)) - - out << targets[0..end_index] - } - // We start counting targets in the middle of the current window - else if filtered_offset + u64(targets.len) > filter.offset { - start_index := filter.offset - filtered_offset - end_index := start_index + - math.min(filter.limit - u64(out.len), u64(targets.len) - start_index) - - out << targets[start_index..end_index] - } - - filtered_offset += u64(targets.len) - } - - return out -} // get_target tries to return a specific target. pub fn (db &VieterDb) get_target(target_id int) ?Target { diff --git a/src/db/targets_iter.v b/src/db/targets_iter.v new file mode 100644 index 0000000..16b1080 --- /dev/null +++ b/src/db/targets_iter.v @@ -0,0 +1,123 @@ +module db + +import models { Target, TargetFilter } +import sqlite + +// Iterator providing a filtered view into the list of targets currently stored +// in the database. It replaces functionality usually performed in the database +// using SQL queries that can't currently be used due to missing stuff in V's +// ORM. +pub struct TargetsIterator { + conn sqlite.DB + filter TargetFilter + window_size int = 32 +mut: + window []Target + window_index u64 + // Offset in entire list of unfiltered targets + offset int + // Offset in filtered list of targets + filtered_offset u64 + started bool + done bool +} + +// targets returns an iterator allowing filtered access to the list of targets. +pub fn (db &VieterDb) targets(filter TargetFilter) TargetsIterator { + window_size := 32 + + return TargetsIterator{ + conn: db.conn + filter: filter + window: []Target{cap: window_size} + window_size: window_size + } +} + +// advance_window moves the sliding window over the filtered list of targets +// until it either reaches the end of the list of targets, or has encountered a +// non-empty window. +fn (mut ti TargetsIterator) advance_window() { + for { + ti.window = sql ti.conn { + select from Target order by id limit ti.window_size offset ti.offset + } + ti.offset += ti.window.len + + if ti.window.len == 0 { + ti.done = true + + return + } + + if ti.filter.repo != '' { + ti.window = ti.window.filter(it.repo == ti.filter.repo) + } + + if ti.filter.query != '' { + ti.window = ti.window.filter(it.url.contains(ti.filter.query) + || it.path.contains(ti.filter.query) || it.branch.contains(ti.filter.query)) + } + + if ti.window.len > 0 { + break + } + } +} + +// next returns the next target, if possible. +pub fn (mut ti TargetsIterator) next() ?Target { + if ti.done { + return none + } + + // The first call to `next` will cause the sliding window to move to where the requested offset starts + if !ti.started { + ti.advance_window() + + // Skip all matched targets until the requested offset + for !ti.done && ti.filtered_offset + u64(ti.window.len) <= ti.filter.offset { + ti.filtered_offset += u64(ti.window.len) + ti.advance_window() + } + + if ti.done { + return none + } + + left_inside_window := ti.filter.offset - ti.filtered_offset + ti.window_index = left_inside_window + ti.filtered_offset += left_inside_window + + ti.started = true + } + + return_value := ti.window[ti.window_index] + + ti.window_index++ + ti.filtered_offset++ + + // Next call will be past the requested offset + if ti.filter.limit > 0 && ti.filtered_offset == ti.filter.offset + ti.filter.limit { + ti.done = true + } + + // Ensure the next call has a new valid window + if ti.window_index == u64(ti.window.len) { + ti.advance_window() + ti.window_index = 0 + } + + return return_value +} + +// collect consumes the entire iterator & returns the result as an array. +pub fn (mut ti TargetsIterator) collect() []Target { + mut out := []Target{} + + for t in ti { + out << t + } + + return out +} diff --git a/src/server/api_targets.v b/src/server/api_targets.v index f04fdae..f47467a 100644 --- a/src/server/api_targets.v +++ b/src/server/api_targets.v @@ -11,9 +11,9 @@ fn (mut app App) v1_get_targets() web.Result { filter := models.from_params(app.query) or { return app.json(.bad_request, new_response('Invalid query parameters.')) } - targets := app.db.get_targets(filter) + mut iter := app.db.targets(filter) - return app.json(.ok, new_data_response(targets)) + return app.json(.ok, new_data_response(iter.collect())) } // v1_get_single_target returns the information for a single target. @@ -81,6 +81,3 @@ fn (mut app App) v1_patch_target(id int) web.Result { return app.json(.ok, new_data_response(target)) } - -['/api/v1/targets/search'; auth; get; markused] -fn (mut app App) v1_search_targets() diff --git a/src/server/server.v b/src/server/server.v index 76e7ad6..5dd1a20 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -31,17 +31,8 @@ pub mut: // init_job_queue populates a fresh job queue with all the targets currently // stored in the database. fn (mut app App) init_job_queue() ! { - // Initialize build queues - mut targets := app.db.get_targets(limit: 25) - mut i := u64(0) - - for targets.len > 0 { - for target in targets { - app.job_queue.insert_all(target)! - } - - i += 25 - targets = app.db.get_targets(limit: 25, offset: i) + for target in app.db.targets(limit: 0) { + app.job_queue.insert_all(target)! } } From b0fe6b73846a8df7564c3a0c1f0b99d1fad860f4 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Mon, 2 Jan 2023 16:10:57 +0100 Subject: [PATCH 64/97] chore: ran formatter --- src/db/targets.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/db/targets.v b/src/db/targets.v index e022a70..2644f49 100644 --- a/src/db/targets.v +++ b/src/db/targets.v @@ -1,6 +1,6 @@ module db -import models { Target, TargetArch, TargetFilter } +import models { Target, TargetArch } // get_target tries to return a specific target. pub fn (db &VieterDb) get_target(target_id int) ?Target { From 39a026fdb3ff01aafb40cb19d7f825cd4c26e102 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Mon, 2 Jan 2023 16:38:43 +0100 Subject: [PATCH 65/97] feat: add filtering of targets by arch --- CHANGELOG.md | 1 + src/console/targets/targets.v | 10 ++++++++++ src/db/targets_iter.v | 4 ++++ src/models/targets.v | 1 + 4 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ab14e8..be5f445 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Metrics endpoint for Prometheus integration * Search in list of targets using API & CLI +* Allow filtering targets by arch value ## [0.5.0](https://git.rustybever.be/vieter-v/vieter/src/tag/0.5.0) diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index dfc3792..6152a53 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -46,6 +46,11 @@ pub fn cmd() cli.Command { description: 'Search string to filter targets by.' flag: cli.FlagType.string }, + cli.Flag{ + name: 'arch' + description: 'Only list targets that build for this arch.' + flag: cli.FlagType.string + }, ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! @@ -73,6 +78,11 @@ pub fn cmd() cli.Command { filter.query = query } + arch := cmd.flags.get_string('arch')! + if arch != '' { + filter.arch = arch + } + raw := cmd.flags.get_bool('raw')! list(conf, filter, raw)! diff --git a/src/db/targets_iter.v b/src/db/targets_iter.v index 16b1080..190d906 100644 --- a/src/db/targets_iter.v +++ b/src/db/targets_iter.v @@ -54,6 +54,10 @@ fn (mut ti TargetsIterator) advance_window() { ti.window = ti.window.filter(it.repo == ti.filter.repo) } + if ti.filter.arch != '' { + ti.window = ti.window.filter(it.arch.any(it.value == ti.filter.arch)) + } + if ti.filter.query != '' { ti.window = ti.window.filter(it.url.contains(ti.filter.query) || it.path.contains(ti.filter.query) || it.branch.contains(ti.filter.query)) diff --git a/src/models/targets.v b/src/models/targets.v index 612f7fa..a0c88d0 100644 --- a/src/models/targets.v +++ b/src/models/targets.v @@ -74,4 +74,5 @@ pub mut: offset u64 repo string query string + arch string } From 398e2bd9ebc89b3ae1e2ac20f7757fb8d376ddbd Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 4 Jan 2023 14:37:41 +0100 Subject: [PATCH 66/97] chore: update docs; final read --- docs/api/source/includes/_targets.md | 2 ++ src/db/targets_iter.v | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/api/source/includes/_targets.md b/docs/api/source/includes/_targets.md index b71da84..1a5f3e0 100644 --- a/docs/api/source/includes/_targets.md +++ b/docs/api/source/includes/_targets.md @@ -55,6 +55,8 @@ Parameter | Description limit | Maximum amount of results to return. offset | Offset of results. repo | Limit results to targets that publish to the given repo. +query | Only return targets that have this substring in their URL, path or branch. +arch | Only return targets that publish to this arch. ## Get specific target diff --git a/src/db/targets_iter.v b/src/db/targets_iter.v index 190d906..081de1f 100644 --- a/src/db/targets_iter.v +++ b/src/db/targets_iter.v @@ -63,6 +63,7 @@ fn (mut ti TargetsIterator) advance_window() { || it.path.contains(ti.filter.query) || it.branch.contains(ti.filter.query)) } + // We break out of the loop once we found a non-empty window if ti.window.len > 0 { break } @@ -75,7 +76,8 @@ pub fn (mut ti TargetsIterator) next() ?Target { return none } - // The first call to `next` will cause the sliding window to move to where the requested offset starts + // The first call to `next` will cause the sliding window to move to where + // the requested offset starts if !ti.started { ti.advance_window() From 5176266ca1085b37396b4153f4a4971fdd09296b Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 5 Jan 2023 17:05:25 +0100 Subject: [PATCH 67/97] refactor: work with new docker lib --- src/agent/images.v | 2 +- src/build/build.v | 4 ++-- src/console/targets/build.v | 2 +- src/cron/daemon/daemon.v | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/agent/images.v b/src/agent/images.v index 1fec567..5fba0f7 100644 --- a/src/agent/images.v +++ b/src/agent/images.v @@ -99,7 +99,7 @@ fn (mut m ImageManager) clean_old_images() { // wasn't deleted. Therefore, we move the index over. If the function // returns true, the array's length has decreased by one so we don't // move the index. - dd.remove_image(m.images[image][i]) or { + dd.image_remove(m.images[image][i]) or { // The image was removed by an external event if err.code() == 404 { m.images[image].delete(i) diff --git a/src/build/build.v b/src/build/build.v index 712c93b..dfea8c0 100644 --- a/src/build/build.v +++ b/src/build/build.v @@ -57,7 +57,7 @@ pub fn create_build_image(base_image string) !string { image_tag := if image_parts.len > 1 { image_parts[1] } else { 'latest' } // We pull the provided image - dd.pull_image(image_name, image_tag)! + dd.image_pull(image_name, image_tag)! id := dd.container_create(c)!.id // id := docker.create_container(c)! @@ -79,7 +79,7 @@ pub fn create_build_image(base_image string) !string { // TODO also add the base image's name into the image name to prevent // conflicts. tag := time.sys_mono_now().str() - image := dd.create_image_from_container(id, 'vieter-build', tag)! + image := dd.image_from_container(id, 'vieter-build', tag)! dd.container_remove(id)! return image.id diff --git a/src/console/targets/build.v b/src/console/targets/build.v index e18077d..b8cbe7f 100644 --- a/src/console/targets/build.v +++ b/src/console/targets/build.v @@ -26,7 +26,7 @@ fn build(conf Config, target_id int, force bool) ! { dd.close() or {} } - dd.remove_image(image_id)! + dd.image_remove(image_id)! println('Uploading logs to Vieter...') c.add_build_log(target.id, res.start_time, res.end_time, build_arch, res.exit_code, diff --git a/src/cron/daemon/daemon.v b/src/cron/daemon/daemon.v index 0d30a23..b94dab8 100644 --- a/src/cron/daemon/daemon.v +++ b/src/cron/daemon/daemon.v @@ -269,6 +269,6 @@ fn (mut d Daemon) clean_old_base_images() { // wasn't deleted. Therefore, we move the index over. If the function // returns true, the array's length has decreased by one so we don't // move the index. - dd.remove_image(d.builder_images[i]) or { i += 1 } + dd.image_remove(d.builder_images[i]) or { i += 1 } } } From 8432f5915d51ae485313cdd03ea0d9983e1425a9 Mon Sep 17 00:00:00 2001 From: GreekStapler Date: Sat, 7 Jan 2023 21:09:55 +0000 Subject: [PATCH 68/97] Fix for configured log level being ignored. --- src/agent/log.v | 29 +++++++++++++++-------------- src/cron/daemon/log.v | 29 +++++++++++++++-------------- src/web/logging.v | 29 +++++++++++++++-------------- 3 files changed, 45 insertions(+), 42 deletions(-) diff --git a/src/agent/log.v b/src/agent/log.v index cd59207..fcd8373 100644 --- a/src/agent/log.v +++ b/src/agent/log.v @@ -1,35 +1,36 @@ module agent -import log - -// log a message with the given level -pub fn (mut d AgentDaemon) log(msg string, level log.Level) { - lock d.logger { - d.logger.send_output(msg, level) - } -} - // lfatal create a log message with the fatal level pub fn (mut d AgentDaemon) lfatal(msg string) { - d.log(msg, log.Level.fatal) + lock d.logger { + d.logger.fatal(msg) + } } // lerror create a log message with the error level pub fn (mut d AgentDaemon) lerror(msg string) { - d.log(msg, log.Level.error) + lock d.logger { + d.logger.error(msg) + } } // lwarn create a log message with the warn level pub fn (mut d AgentDaemon) lwarn(msg string) { - d.log(msg, log.Level.warn) + lock d.logger { + d.logger.warn(msg) + } } // linfo create a log message with the info level pub fn (mut d AgentDaemon) linfo(msg string) { - d.log(msg, log.Level.info) + lock d.logger { + d.logger.info(msg) + } } // ldebug create a log message with the debug level pub fn (mut d AgentDaemon) ldebug(msg string) { - d.log(msg, log.Level.debug) + lock d.logger { + d.logger.debug(msg) + } } diff --git a/src/cron/daemon/log.v b/src/cron/daemon/log.v index 95a50e7..4f978fc 100644 --- a/src/cron/daemon/log.v +++ b/src/cron/daemon/log.v @@ -1,35 +1,36 @@ module daemon -import log - -// log reate a log message with the given level -pub fn (mut d Daemon) log(msg string, level log.Level) { - lock d.logger { - d.logger.send_output(msg, level) - } -} - // lfatal create a log message with the fatal level pub fn (mut d Daemon) lfatal(msg string) { - d.log(msg, log.Level.fatal) + lock d.logger { + d.logger.fatal(msg) + } } // lerror create a log message with the error level pub fn (mut d Daemon) lerror(msg string) { - d.log(msg, log.Level.error) + lock d.logger { + d.logger.error(msg) + } } // lwarn create a log message with the warn level pub fn (mut d Daemon) lwarn(msg string) { - d.log(msg, log.Level.warn) + lock d.logger { + d.logger.warn(msg) + } } // linfo create a log message with the info level pub fn (mut d Daemon) linfo(msg string) { - d.log(msg, log.Level.info) + lock d.logger { + d.logger.info(msg) + } } // ldebug create a log message with the debug level pub fn (mut d Daemon) ldebug(msg string) { - d.log(msg, log.Level.debug) + lock d.logger { + d.logger.debug(msg) + } } diff --git a/src/web/logging.v b/src/web/logging.v index 12b07d7..7ba649c 100644 --- a/src/web/logging.v +++ b/src/web/logging.v @@ -1,35 +1,36 @@ module web -import log - -// log reate a log message with the given level -pub fn (mut ctx Context) log(msg string, level log.Level) { - lock ctx.logger { - ctx.logger.send_output(msg, level) - } -} - // lfatal create a log message with the fatal level pub fn (mut ctx Context) lfatal(msg string) { - ctx.log(msg, log.Level.fatal) + lock ctx.logger { + ctx.logger.fatal(msg) + } } // lerror create a log message with the error level pub fn (mut ctx Context) lerror(msg string) { - ctx.log(msg, log.Level.error) + lock ctx.logger { + ctx.logger.error(msg) + } } // lwarn create a log message with the warn level pub fn (mut ctx Context) lwarn(msg string) { - ctx.log(msg, log.Level.warn) + lock ctx.logger { + ctx.logger.warn(msg) + } } // linfo create a log message with the info level pub fn (mut ctx Context) linfo(msg string) { - ctx.log(msg, log.Level.info) + lock ctx.logger { + ctx.logger.info(msg) + } } // ldebug create a log message with the debug level pub fn (mut ctx Context) ldebug(msg string) { - ctx.log(msg, log.Level.debug) + lock ctx.logger { + ctx.logger.debug(msg) + } } From beb90d57561953837b42828293f97b57e0aed3f7 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 12 Jan 2023 12:26:12 +0100 Subject: [PATCH 69/97] refactor: link libvieter; remove cron code & daemon This giant commit removes the old cron daemon & parser, replacing the latter with a C implementation that will now be maintained in a separate C library that gets developed independently. This commit lays the groundwork for implementing features of Vieter in C where possible. --- .clang-format | 4 + .editorconfig | 3 +- .gitignore | 2 +- .gitmodules | 3 + CHANGELOG.md | 4 + Makefile | 25 +- src/build/queue.v | 26 +- src/console/schedule/schedule.v | 6 +- src/console/targets/targets.v | 4 +- src/cron/cli.v | 32 --- src/cron/cron.v | 33 --- src/cron/daemon/build.v | 115 -------- src/cron/daemon/daemon.v | 274 -------------------- src/cron/daemon/log.v | 36 --- src/cron/expression.c.v | 99 +++++++ src/cron/expression.v | 73 ++++++ src/cron/expression/expression.v | 136 ---------- src/cron/expression/expression_parse.v | 146 ----------- src/cron/expression/expression_parse_test.v | 89 ------- src/cron/{expression => }/expression_test.v | 17 +- src/cron/parse_test.v | 42 +++ src/libvieter | 1 + src/main.v | 2 - src/server/log_removal.v | 15 +- src/server/server.v | 6 +- vieter.toml | 1 + 26 files changed, 278 insertions(+), 916 deletions(-) create mode 100644 .clang-format delete mode 100644 src/cron/cli.v delete mode 100644 src/cron/cron.v delete mode 100644 src/cron/daemon/build.v delete mode 100644 src/cron/daemon/daemon.v delete mode 100644 src/cron/daemon/log.v create mode 100644 src/cron/expression.c.v create mode 100644 src/cron/expression.v delete mode 100644 src/cron/expression/expression.v delete mode 100644 src/cron/expression/expression_parse.v delete mode 100644 src/cron/expression/expression_parse_test.v rename src/cron/{expression => }/expression_test.v (56%) create mode 100644 src/cron/parse_test.v create mode 160000 src/libvieter diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..2e6afb4 --- /dev/null +++ b/.clang-format @@ -0,0 +1,4 @@ +# To stay consistent with the V formatting style, we use tabs +UseTab: Always +IndentWidth: 4 +TabWidth: 4 diff --git a/.editorconfig b/.editorconfig index e23a3c7..e9c1e63 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,6 +5,5 @@ root = true end_of_line = lf insert_final_newline = true -[*.v] -# vfmt wants it :( +[*.{v,c,h}] indent_style = tab diff --git a/.gitignore b/.gitignore index aaec9ef..daeb3d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -*.c +vieter.c /data/ # Build artifacts diff --git a/.gitmodules b/.gitmodules index 47029a0..24af818 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "docs/themes/hugo-book"] path = docs/themes/hugo-book url = https://github.com/alex-shpak/hugo-book +[submodule "src/libvieter"] + path = src/libvieter + url = https://git.rustybever.be/vieter-v/libvieter diff --git a/CHANGELOG.md b/CHANGELOG.md index be5f445..6b1e583 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Search in list of targets using API & CLI * Allow filtering targets by arch value +### Changed + +* Rewrote cron expression logic in C + ## [0.5.0](https://git.rustybever.be/vieter-v/vieter/src/tag/0.5.0) ### Added diff --git a/Makefile b/Makefile index 4bd1edc..2f6029e 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # =====CONFIG===== SRC_DIR := src -SOURCES != find '$(SRC_DIR)' -iname '*.v' +SRCS != find '$(SRC_DIR)' -iname '*.v' V_PATH ?= v V := $(V_PATH) -showcc -gc boehm -W -d use_openssl -skip-unused @@ -9,8 +9,12 @@ all: vieter # =====COMPILATION===== +.PHONY: libvieter +libvieter: + CFLAGS='-O3' make -C '$(SRC_DIR)/libvieter' + # Regular binary -vieter: $(SOURCES) +vieter: $(SOURCES) libvieter $(V) -g -o vieter $(SRC_DIR) # Debug build using gcc @@ -18,7 +22,7 @@ vieter: $(SOURCES) # multi-threaded and causes issues when running vieter inside gdb. .PHONY: debug debug: dvieter -dvieter: $(SOURCES) +dvieter: $(SOURCES) libvieter $(V_PATH) -showcc -keepc -cg -o dvieter $(SRC_DIR) # Run the debug build inside gdb @@ -29,12 +33,12 @@ gdb: dvieter # Optimised production build .PHONY: prod prod: pvieter -pvieter: $(SOURCES) +pvieter: $(SOURCES) libvieter $(V) -o pvieter -prod $(SRC_DIR) # Only generate C code .PHONY: c -c: $(SOURCES) +c: $(SOURCES) libvieter $(V) -o vieter.c $(SRC_DIR) @@ -67,6 +71,7 @@ man: vieter # =====OTHER===== +# Linting .PHONY: lint lint: $(V) fmt -verify $(SRC_DIR) @@ -74,18 +79,24 @@ lint: $(V_PATH) missdoc -p $(SRC_DIR) @ [ $$($(V_PATH) missdoc -p $(SRC_DIR) | wc -l) = 0 ] -# Format the V codebase + +# Formatting .PHONY: fmt fmt: $(V) fmt -w $(SRC_DIR) + +# Testing .PHONY: test test: - $(V) test $(SRC_DIR) + $(V) -g test $(SRC_DIR) + +# Cleaning .PHONY: clean clean: rm -rf 'data' 'vieter' 'dvieter' 'pvieter' 'vieter.c' 'pkg' 'src/vieter' *.pkg.tar.zst 'suvieter' 'afvieter' '$(SRC_DIR)/_docs' 'docs/public' + make -C '$(SRC_DIR)/libvieter' clean # =====EXPERIMENTAL===== diff --git a/src/build/queue.v b/src/build/queue.v index e74529c..abd4ec6 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -1,7 +1,7 @@ module build import models { BuildConfig, Target } -import cron.expression { CronExpression, parse_expression } +import cron import time import datatypes { MinHeap } import util @@ -13,7 +13,7 @@ pub mut: // Next timestamp from which point this job is allowed to be executed timestamp time.Time // Required for calculating next timestamp after having pop'ed a job - ce CronExpression + ce &cron.Expression = unsafe { nil } // Actual build config sent to the agent config BuildConfig // Whether this is a one-time job @@ -30,7 +30,7 @@ fn (r1 BuildJob) < (r2 BuildJob) bool { // for each architecture. Agents receive jobs from this queue. pub struct BuildJobQueue { // Schedule to use for targets without explicitely defined cron expression - default_schedule CronExpression + default_schedule &cron.Expression // Base image to use for targets without defined base image default_base_image string mut: @@ -44,9 +44,9 @@ mut: } // new_job_queue initializes a new job queue -pub fn new_job_queue(default_schedule CronExpression, default_base_image string) BuildJobQueue { +pub fn new_job_queue(default_schedule &cron.Expression, default_base_image string) BuildJobQueue { return BuildJobQueue{ - default_schedule: default_schedule + default_schedule: unsafe { default_schedule } default_base_image: default_base_image invalidated: map[int]time.Time{} } @@ -85,14 +85,14 @@ pub fn (mut q BuildJobQueue) insert(input InsertConfig) ! { if !input.now { ce := if input.target.schedule != '' { - parse_expression(input.target.schedule) or { + cron.parse_expression(input.target.schedule) or { return error("Error while parsing cron expression '$input.target.schedule' (id $input.target.id): $err.msg()") } } else { q.default_schedule } - job.timestamp = ce.next_from_now()! + job.timestamp = ce.next_from_now() job.ce = ce } else { job.timestamp = time.now() @@ -105,8 +105,8 @@ pub fn (mut q BuildJobQueue) insert(input InsertConfig) ! { // reschedule the given job by calculating the next timestamp and re-adding it // to its respective queue. This function is called by the pop functions // *after* having pop'ed the job. -fn (mut q BuildJobQueue) reschedule(job BuildJob, arch string) ! { - new_timestamp := job.ce.next_from_now()! +fn (mut q BuildJobQueue) reschedule(job BuildJob, arch string) { + new_timestamp := job.ce.next_from_now() new_job := BuildJob{ ...job @@ -168,10 +168,7 @@ pub fn (mut q BuildJobQueue) pop(arch string) ?BuildJob { job = q.queues[arch].pop()? if !job.single { - // TODO how do we handle this properly? Is it even possible for a - // cron expression to not return a next time if it's already been - // used before? - q.reschedule(job, arch) or {} + q.reschedule(job, arch) } return job @@ -198,8 +195,7 @@ pub fn (mut q BuildJobQueue) pop_n(arch string, n int) []BuildJob { job = q.queues[arch].pop() or { break } if !job.single { - // TODO idem - q.reschedule(job, arch) or {} + q.reschedule(job, arch) } out << job diff --git a/src/console/schedule/schedule.v b/src/console/schedule/schedule.v index 7ce0516..ceabf24 100644 --- a/src/console/schedule/schedule.v +++ b/src/console/schedule/schedule.v @@ -1,7 +1,7 @@ module schedule import cli -import cron.expression { parse_expression } +import cron import time // cmd returns the cli submodule for previewing a cron schedule. @@ -19,10 +19,10 @@ pub fn cmd() cli.Command { }, ] execute: fn (cmd cli.Command) ! { - ce := parse_expression(cmd.args.join(' '))! + ce := cron.parse_expression(cmd.args.join(' '))! count := cmd.flags.get_int('count')! - for t in ce.next_n(time.now(), count)! { + for t in ce.next_n(time.now(), count) { println(t) } } diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index 6152a53..709c196 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -2,7 +2,7 @@ module targets import cli import conf as vconf -import cron.expression { parse_expression } +import cron import client { NewTarget } import console import models { TargetFilter } @@ -295,7 +295,7 @@ fn patch(conf Config, id string, params map[string]string) ! { // We check the cron expression first because it's useless to send an // invalid one to the server. if 'schedule' in params && params['schedule'] != '' { - parse_expression(params['schedule']) or { + cron.parse_expression(params['schedule']) or { return error('Invalid cron expression: $err.msg()') } } diff --git a/src/cron/cli.v b/src/cron/cli.v deleted file mode 100644 index 16a3537..0000000 --- a/src/cron/cli.v +++ /dev/null @@ -1,32 +0,0 @@ -module cron - -import cli -import conf as vconf - -struct Config { -pub: - log_level string = 'WARN' - api_key string - address string - data_dir string - base_image string = 'archlinux:base-devel' - max_concurrent_builds int = 1 - api_update_frequency int = 15 - image_rebuild_frequency int = 1440 - // Replicates the behavior of the original cron system - global_schedule string = '0 3' -} - -// cmd returns the cli module that handles the cron daemon. -pub fn cmd() cli.Command { - return cli.Command{ - name: 'cron' - description: 'Start the cron service that periodically runs builds.' - execute: fn (cmd cli.Command) ! { - config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! - - cron(conf)! - } - } -} diff --git a/src/cron/cron.v b/src/cron/cron.v deleted file mode 100644 index f1d6b7b..0000000 --- a/src/cron/cron.v +++ /dev/null @@ -1,33 +0,0 @@ -module cron - -import log -import cron.daemon -import cron.expression -import os - -const log_file_name = 'vieter.cron.log' - -// cron starts a cron daemon & starts periodically scheduling builds. -pub fn cron(conf Config) ! { - // Configure logger - log_level := log.level_from_tag(conf.log_level) or { - return error('Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.') - } - - mut logger := log.Log{ - level: log_level - } - - log_file := os.join_path_single(conf.data_dir, cron.log_file_name) - logger.set_full_logpath(log_file) - logger.log_to_console_too() - - ce := expression.parse_expression(conf.global_schedule) or { - return error('Error while parsing global cron expression: $err.msg()') - } - - mut d := daemon.init_daemon(logger, conf.address, conf.api_key, conf.base_image, ce, - conf.max_concurrent_builds, conf.api_update_frequency, conf.image_rebuild_frequency)! - - d.run() -} diff --git a/src/cron/daemon/build.v b/src/cron/daemon/build.v deleted file mode 100644 index 42edc92..0000000 --- a/src/cron/daemon/build.v +++ /dev/null @@ -1,115 +0,0 @@ -module daemon - -import time -import sync.stdatomic -import build -import os - -const ( - build_empty = 0 - build_running = 1 - build_done = 2 -) - -// clean_finished_builds removes finished builds from the build slots & returns -// them. -fn (mut d Daemon) clean_finished_builds() []ScheduledBuild { - mut out := []ScheduledBuild{} - - for i in 0 .. d.atomics.len { - if stdatomic.load_u64(&d.atomics[i]) == daemon.build_done { - stdatomic.store_u64(&d.atomics[i], daemon.build_empty) - out << d.builds[i] - } - } - - return out -} - -// update_builds starts as many builds as possible. -fn (mut d Daemon) start_new_builds() { - now := time.now() - - for d.queue.len() > 0 { - elem := d.queue.peek() or { - d.lerror("queue.peek() unexpectedly returned an error. This shouldn't happen.") - - break - } - - if elem.timestamp < now { - sb := d.queue.pop() or { - d.lerror("queue.pop() unexpectedly returned an error. This shouldn't happen.") - - break - } - - // If this build couldn't be scheduled, no more will be possible. - if !d.start_build(sb) { - d.queue.insert(sb) - break - } - } else { - break - } - } -} - -// start_build starts a build for the given ScheduledBuild object. -fn (mut d Daemon) start_build(sb ScheduledBuild) bool { - for i in 0 .. d.atomics.len { - if stdatomic.load_u64(&d.atomics[i]) == daemon.build_empty { - stdatomic.store_u64(&d.atomics[i], daemon.build_running) - d.builds[i] = sb - - go d.run_build(i, sb) - - return true - } - } - - return false -} - -// run_build actually starts the build process for a given target. -fn (mut d Daemon) run_build(build_index int, sb ScheduledBuild) { - d.linfo('started build: $sb.target.url -> $sb.target.repo') - - // 0 means success, 1 means failure - mut status := 0 - - res := build.build_target(d.client.address, d.client.api_key, d.builder_images.last(), - &sb.target, false) or { - d.ldebug('build_target error: $err.msg()') - status = 1 - - build.BuildResult{} - } - - if status == 0 { - d.linfo('finished build: $sb.target.url -> $sb.target.repo; uploading logs...') - - build_arch := os.uname().machine - d.client.add_build_log(sb.target.id, res.start_time, res.end_time, build_arch, - res.exit_code, res.logs) or { - d.lerror('Failed to upload logs for build: $sb.target.url -> $sb.target.repo') - } - } else { - d.linfo('an error occured during build: $sb.target.url -> $sb.target.repo') - } - - stdatomic.store_u64(&d.atomics[build_index], daemon.build_done) -} - -// current_build_count returns how many builds are currently running. -fn (mut d Daemon) current_build_count() int { - mut res := 0 - - for i in 0 .. d.atomics.len { - if stdatomic.load_u64(&d.atomics[i]) == daemon.build_running { - res += 1 - } - } - - return res -} diff --git a/src/cron/daemon/daemon.v b/src/cron/daemon/daemon.v deleted file mode 100644 index b94dab8..0000000 --- a/src/cron/daemon/daemon.v +++ /dev/null @@ -1,274 +0,0 @@ -module daemon - -import time -import log -import datatypes { MinHeap } -import cron.expression { CronExpression, parse_expression } -import math -import build -import docker -import os -import client -import models { Target } - -const ( - // How many seconds to wait before retrying to update API if failed - api_update_retry_timeout = 5 - // How many seconds to wait before retrying to rebuild image if failed - rebuild_base_image_retry_timout = 30 -) - -struct ScheduledBuild { -pub: - target Target - timestamp time.Time -} - -// Overloaded operator for comparing ScheduledBuild objects -fn (r1 ScheduledBuild) < (r2 ScheduledBuild) bool { - return r1.timestamp < r2.timestamp -} - -pub struct Daemon { -mut: - client client.Client - base_image string - builder_images []string - global_schedule CronExpression - api_update_frequency int - image_rebuild_frequency int - // Targets currently loaded from API. - targets []Target - // At what point to update the list of targets. - api_update_timestamp time.Time - image_build_timestamp time.Time - queue MinHeap - // Which builds are currently running - builds []ScheduledBuild - // Atomic variables used to detect when a build has finished; length is the - // same as builds - atomics []u64 - logger shared log.Log -} - -// init_daemon initializes a new Daemon object. It renews the targets & -// populates the build queue for the first time. -pub fn init_daemon(logger log.Log, address string, api_key string, base_image string, global_schedule CronExpression, max_concurrent_builds int, api_update_frequency int, image_rebuild_frequency int) !Daemon { - mut d := Daemon{ - client: client.new(address, api_key) - base_image: base_image - global_schedule: global_schedule - api_update_frequency: api_update_frequency - image_rebuild_frequency: image_rebuild_frequency - atomics: []u64{len: max_concurrent_builds} - builds: []ScheduledBuild{len: max_concurrent_builds} - logger: logger - } - - // Initialize the targets & queue - d.renew_targets() - d.renew_queue() - if !d.rebuild_base_image() { - return error('The base image failed to build. The Vieter cron daemon cannot run without an initial builder image.') - } - - return d -} - -// run starts the actual daemon process. It runs builds when possible & -// periodically refreshes the list of targets to ensure we stay in sync. -pub fn (mut d Daemon) run() { - for { - finished_builds := d.clean_finished_builds() - - // Update the API's contents if needed & renew the queue - if time.now() >= d.api_update_timestamp { - d.renew_targets() - d.renew_queue() - } - // The finished builds should only be rescheduled if the API contents - // haven't been renewed. - else { - for sb in finished_builds { - d.schedule_build(sb.target) - } - } - - // TODO remove old builder images. - // This issue is less trivial than it sounds, because a build could - // still be running when the image has to be rebuilt. That would - // prevent the image from being removed. Therefore, we will need to - // keep track of a list or something & remove an image once we have - // made sure it isn't being used anymore. - if time.now() >= d.image_build_timestamp { - d.rebuild_base_image() - // In theory, executing this function here allows an old builder - // image to exist for at most image_rebuild_frequency minutes. - d.clean_old_base_images() - } - - // Schedules new builds when possible - d.start_new_builds() - - // If there are builds currently running, the daemon should refresh - // every second to clean up any finished builds & start new ones. - mut delay := time.Duration(1 * time.second) - - // Sleep either until we have to refresh the targets or when the next - // build has to start, with a minimum of 1 second. - if d.current_build_count() == 0 { - now := time.now() - delay = d.api_update_timestamp - now - - if d.queue.len() > 0 { - elem := d.queue.peek() or { - d.lerror("queue.peek() unexpectedly returned an error. This shouldn't happen.") - - // This is just a fallback option. In theory, queue.peek() - // should *never* return an error or none, because we check - // its len beforehand. - time.sleep(1) - continue - } - - time_until_next_job := elem.timestamp - now - - delay = math.min(delay, time_until_next_job) - } - } - - // We sleep for at least one second. This is to prevent the program - // from looping agressively when a cronjob can be scheduled, but - // there's no spots free for it to be started. - delay = math.max(delay, 1 * time.second) - - d.ldebug('Sleeping for ${delay}...') - - time.sleep(delay) - } -} - -// schedule_build adds the next occurence of the given targets build to the -// queue. -fn (mut d Daemon) schedule_build(target Target) { - ce := if target.schedule != '' { - parse_expression(target.schedule) or { - // TODO This shouldn't return an error if the expression is empty. - d.lerror("Error while parsing cron expression '$target.schedule' (id $target.id): $err.msg()") - - d.global_schedule - } - } else { - d.global_schedule - } - - // A target that can't be scheduled will just be skipped for now - timestamp := ce.next_from_now() or { - d.lerror("Couldn't calculate next timestamp from '$target.schedule'; skipping") - return - } - - d.queue.insert(ScheduledBuild{ - target: target - timestamp: timestamp - }) -} - -// renew_targets requests the newest list of targets from the server & replaces -// the old one. -fn (mut d Daemon) renew_targets() { - d.linfo('Renewing targets...') - - mut new_targets := d.client.get_all_targets() or { - d.lerror('Failed to renew targets. Retrying in ${daemon.api_update_retry_timeout}s...') - d.api_update_timestamp = time.now().add_seconds(daemon.api_update_retry_timeout) - - return - } - - // Filter out any targets that shouldn't run on this architecture - cur_arch := os.uname().machine - new_targets = new_targets.filter(it.arch.any(it.value == cur_arch)) - - d.targets = new_targets - - d.api_update_timestamp = time.now().add_seconds(60 * d.api_update_frequency) -} - -// renew_queue replaces the old queue with a new one that reflects the newest -// values in targets. -fn (mut d Daemon) renew_queue() { - d.linfo('Renewing queue...') - mut new_queue := MinHeap{} - - // Move any jobs that should have already started from the old queue onto - // the new one - now := time.now() - - // For some reason, using - // ```v - // for d.queue.len() > 0 && d.queue.peek() !.timestamp < now { - //``` - // here causes the function to prematurely just exit, without any errors or anything, very weird - // https://github.com/vlang/v/issues/14042 - for d.queue.len() > 0 { - elem := d.queue.pop() or { - d.lerror("queue.pop() returned an error. This shouldn't happen.") - continue - } - - if elem.timestamp < now { - new_queue.insert(elem) - } else { - break - } - } - - d.queue = new_queue - - // For each target in targets, parse their cron expression (or use the - // default one if not present) & add them to the queue - for target in d.targets { - d.schedule_build(target) - } -} - -// rebuild_base_image recreates the builder image. -fn (mut d Daemon) rebuild_base_image() bool { - d.linfo('Rebuilding builder image....') - - d.builder_images << build.create_build_image(d.base_image) or { - d.lerror('Failed to rebuild base image. Retrying in ${daemon.rebuild_base_image_retry_timout}s...') - d.image_build_timestamp = time.now().add_seconds(daemon.rebuild_base_image_retry_timout) - - return false - } - - d.image_build_timestamp = time.now().add_seconds(60 * d.image_rebuild_frequency) - - return true -} - -// clean_old_base_images tries to remove any old but still present builder -// images. -fn (mut d Daemon) clean_old_base_images() { - mut i := 0 - - mut dd := docker.new_conn() or { - d.lerror('Failed to connect to Docker socket.') - return - } - - defer { - dd.close() or {} - } - - for i < d.builder_images.len - 1 { - // For each builder image, we try to remove it by calling the Docker - // API. If the function returns an error or false, that means the image - // wasn't deleted. Therefore, we move the index over. If the function - // returns true, the array's length has decreased by one so we don't - // move the index. - dd.image_remove(d.builder_images[i]) or { i += 1 } - } -} diff --git a/src/cron/daemon/log.v b/src/cron/daemon/log.v deleted file mode 100644 index 4f978fc..0000000 --- a/src/cron/daemon/log.v +++ /dev/null @@ -1,36 +0,0 @@ -module daemon - -// lfatal create a log message with the fatal level -pub fn (mut d Daemon) lfatal(msg string) { - lock d.logger { - d.logger.fatal(msg) - } -} - -// lerror create a log message with the error level -pub fn (mut d Daemon) lerror(msg string) { - lock d.logger { - d.logger.error(msg) - } -} - -// lwarn create a log message with the warn level -pub fn (mut d Daemon) lwarn(msg string) { - lock d.logger { - d.logger.warn(msg) - } -} - -// linfo create a log message with the info level -pub fn (mut d Daemon) linfo(msg string) { - lock d.logger { - d.logger.info(msg) - } -} - -// ldebug create a log message with the debug level -pub fn (mut d Daemon) ldebug(msg string) { - lock d.logger { - d.logger.debug(msg) - } -} diff --git a/src/cron/expression.c.v b/src/cron/expression.c.v new file mode 100644 index 0000000..8c574c7 --- /dev/null +++ b/src/cron/expression.c.v @@ -0,0 +1,99 @@ +module cron + +#flag -I @VMODROOT/libvieter/include +#flag -L @VMODROOT/libvieter/build +#flag -lvieter +#include "vieter_cron.h" + +pub struct C.vieter_cron_expression { + minutes &u8 + hours &u8 + days &u8 + months &u8 + minute_count u8 + hour_count u8 + day_count u8 + month_count u8 +} + +pub type Expression = C.vieter_cron_expression + +// == returns whether the two expressions are equal by value. +fn (ce1 Expression) == (ce2 Expression) bool { + if ce1.month_count != ce2.month_count || ce1.day_count != ce2.day_count + || ce1.hour_count != ce2.hour_count || ce1.minute_count != ce2.minute_count { + return false + } + + for i in 0 .. ce1.month_count { + unsafe { + if ce1.months[i] != ce2.months[i] { + return false + } + } + } + for i in 0 .. ce1.day_count { + unsafe { + if ce1.days[i] != ce2.days[i] { + return false + } + } + } + for i in 0 .. ce1.hour_count { + unsafe { + if ce1.hours[i] != ce2.hours[i] { + return false + } + } + } + for i in 0 .. ce1.minute_count { + unsafe { + if ce1.minutes[i] != ce2.minutes[i] { + return false + } + } + } + + return true +} + +struct C.vieter_cron_simple_time { + year int + month int + day int + hour int + minute int +} + +type SimpleTime = C.vieter_cron_simple_time + +enum ParseError as u8 { + ok = 0 + invalid_expression = 1 + invalid_number = 2 + out_of_range = 3 + too_many_parts = 4 + not_enough_parts = 5 +} + +// str returns the string representation of a ParseError. +fn (e ParseError) str() string { + return match e { + .ok { '' } + .invalid_expression { 'Invalid expression' } + .invalid_number { 'Invalid number' } + .out_of_range { 'Out of range' } + .too_many_parts { 'Too many parts' } + .not_enough_parts { 'Not enough parts' } + } +} + +fn C.vieter_cron_expr_init() &C.vieter_cron_expression + +fn C.vieter_cron_expr_free(ce &C.vieter_cron_expression) + +fn C.vieter_cron_expr_next(out &C.vieter_cron_simple_time, ce &C.vieter_cron_expression, ref &C.vieter_cron_simple_time) + +fn C.vieter_cron_expr_next_from_now(out &C.vieter_cron_simple_time, ce &C.vieter_cron_expression) + +fn C.vieter_cron_expr_parse(out &C.vieter_cron_expression, s &char) ParseError diff --git a/src/cron/expression.v b/src/cron/expression.v new file mode 100644 index 0000000..62692fa --- /dev/null +++ b/src/cron/expression.v @@ -0,0 +1,73 @@ +module cron + +import time + +// free the memory associated with the Expression. +[unsafe] +pub fn (ce &Expression) free() { + C.vieter_cron_expr_free(ce) +} + +// parse_expression parses a string into an Expression. +pub fn parse_expression(exp string) !&Expression { + out := C.vieter_cron_expr_init() + res := C.vieter_cron_expr_parse(out, exp.str) + + if res != .ok { + return error(res.str()) + } + + return out +} + +// next calculates the next occurence of the cron schedule, given a reference +// point. +pub fn (ce &Expression) next(ref time.Time) time.Time { + st := SimpleTime{ + year: ref.year + month: ref.month + day: ref.day + hour: ref.hour + minute: ref.minute + } + + out := SimpleTime{} + C.vieter_cron_expr_next(&out, ce, &st) + + return time.new_time(time.Time{ + year: out.year + month: out.month + day: out.day + hour: out.hour + minute: out.minute + }) +} + +// next_from_now calculates the next occurence of the cron schedule with the +// current time as reference. +pub fn (ce &Expression) next_from_now() time.Time { + out := SimpleTime{} + C.vieter_cron_expr_next_from_now(&out, ce) + + return time.new_time(time.Time{ + year: out.year + month: out.month + day: out.day + hour: out.hour + minute: out.minute + }) +} + +// next_n returns the n next occurences of the expression, given a starting +// time. +pub fn (ce &Expression) next_n(ref time.Time, n int) []time.Time { + mut times := []time.Time{cap: n} + + times << ce.next(ref) + + for i in 1 .. n { + times << ce.next(times[i - 1]) + } + + return times +} diff --git a/src/cron/expression/expression.v b/src/cron/expression/expression.v deleted file mode 100644 index c3ff8c5..0000000 --- a/src/cron/expression/expression.v +++ /dev/null @@ -1,136 +0,0 @@ -module expression - -import time - -pub struct CronExpression { - minutes []int - hours []int - days []int - months []int -} - -// next calculates the earliest time this cron expression is valid. It will -// always pick a moment in the future, even if ref matches completely up to the -// minute. This function conciously does not take gap years into account. -pub fn (ce &CronExpression) next(ref time.Time) !time.Time { - // If the given ref matches the next cron occurence up to the minute, it - // will return that value. Because we always want to return a value in the - // future, we artifically shift the ref 60 seconds to make sure we always - // match in the future. A shift of 60 seconds is enough because the cron - // expression does not allow for accuracy smaller than one minute. - sref := ref - - // For all of these values, the rule is the following: if their value is - // the length of their respective array in the CronExpression object, that - // means we've looped back around. This means that the "bigger" value has - // to be incremented by one. For example, if the minutes have looped - // around, that means that the hour has to be incremented as well. - mut minute_index := 0 - mut hour_index := 0 - mut day_index := 0 - mut month_index := 0 - - // This chain is the same logic multiple times, namely that if a "bigger" - // value loops around, then the smaller value will always reset as well. - // For example, if we're going to a new day, the hour & minute will always - // be their smallest value again. - for month_index < ce.months.len && sref.month > ce.months[month_index] { - month_index++ - } - - if month_index < ce.months.len && sref.month == ce.months[month_index] { - for day_index < ce.days.len && sref.day > ce.days[day_index] { - day_index++ - } - - if day_index < ce.days.len && ce.days[day_index] == sref.day { - for hour_index < ce.hours.len && sref.hour > ce.hours[hour_index] { - hour_index++ - } - - if hour_index < ce.hours.len && ce.hours[hour_index] == sref.hour { - // Minute is the only value where we explicitely make sure we - // can't match sref's value exactly. This is to ensure we only - // return values in the future. - for minute_index < ce.minutes.len && sref.minute >= ce.minutes[minute_index] { - minute_index++ - } - } - } - } - - // Here, we increment the "bigger" values by one if the smaller ones loop - // around. The order is important, as it allows a sort-of waterfall effect - // to occur which updates all values if required. - if minute_index == ce.minutes.len && hour_index < ce.hours.len { - hour_index += 1 - } - - if hour_index == ce.hours.len && day_index < ce.days.len { - day_index += 1 - } - - if day_index == ce.days.len && month_index < ce.months.len { - month_index += 1 - } - - mut minute := ce.minutes[minute_index % ce.minutes.len] - mut hour := ce.hours[hour_index % ce.hours.len] - mut day := ce.days[day_index % ce.days.len] - - // Sometimes, we end up with a day that does not exist within the selected - // month, e.g. day 30 in February. When this occurs, we reset day back to - // the smallest value & loop over to the next month that does have this - // day. - if day > time.month_days[ce.months[month_index % ce.months.len] - 1] { - day = ce.days[0] - month_index += 1 - - for day > time.month_days[ce.months[month_index & ce.months.len] - 1] { - month_index += 1 - - // If for whatever reason the day value ends up being something - // that can't be scheduled in any month, we have to make sure we - // don't create an infinite loop. - if month_index == 2 * ce.months.len { - return error('No schedulable moment.') - } - } - } - - month := ce.months[month_index % ce.months.len] - mut year := sref.year - - // If the month loops over, we need to increment the year. - if month_index >= ce.months.len { - year++ - } - - return time.new_time(time.Time{ - year: year - month: month - day: day - minute: minute - hour: hour - }) -} - -// next_from_now returns the result of ce.next(ref) where ref is the result of -// time.now(). -pub fn (ce &CronExpression) next_from_now() !time.Time { - return ce.next(time.now()) -} - -// next_n returns the n next occurences of the expression, given a starting -// time. -pub fn (ce &CronExpression) next_n(ref time.Time, n int) ![]time.Time { - mut times := []time.Time{cap: n} - - times << ce.next(ref)! - - for i in 1 .. n { - times << ce.next(times[i - 1])! - } - - return times -} diff --git a/src/cron/expression/expression_parse.v b/src/cron/expression/expression_parse.v deleted file mode 100644 index 4aaec5b..0000000 --- a/src/cron/expression/expression_parse.v +++ /dev/null @@ -1,146 +0,0 @@ -module expression - -import bitfield - -// parse_range parses a given string into a range of sorted integers. Its -// result is a BitField with set bits for all numbers in the result. -fn parse_range(s string, min int, max int) !bitfield.BitField { - mut start := min - mut end := max - mut interval := 1 - mut bf := bitfield.new(max - min + 1) - - exps := s.split('/') - - if exps.len > 2 { - return error('Invalid expression.') - } - - if exps[0] != '*' { - dash_parts := exps[0].split('-') - - if dash_parts.len > 2 { - return error('Invalid expression.') - } - - start = dash_parts[0].int() - - // The builtin parsing functions return zero if the string can't be - // parsed into a number, so we have to explicitely check whether they - // actually entered zero or if it's an invalid number. - if start == 0 && dash_parts[0] != '0' { - return error('Invalid number.') - } - - // Check whether the start value is out of range - if start < min || start > max { - return error('Out of range.') - } - - if dash_parts.len == 2 { - end = dash_parts[1].int() - - if end == 0 && dash_parts[1] != '0' { - return error('Invalid number.') - } - - if end < start || end > max { - return error('Out of range.') - } - } - } - - if exps.len > 1 { - interval = exps[1].int() - - // interval being zero is always invalid, but we want to check why - // it's invalid for better error messages. - if interval == 0 { - if exps[1] != '0' { - return error('Invalid number.') - } else { - return error('Step size zero not allowed.') - } - } - - if interval > max - min { - return error('Step size too large.') - } - } - // Here, s solely consists of a number, so that's the only value we - // should return. - else if exps[0] != '*' && !exps[0].contains('-') { - bf.set_bit(start - min) - return bf - } - - for start <= end { - bf.set_bit(start - min) - start += interval - } - - return bf -} - -// bf_to_ints takes a BitField and converts it into the expected list of actual -// integers. -fn bf_to_ints(bf bitfield.BitField, min int) []int { - mut out := []int{} - - for i in 0 .. bf.get_size() { - if bf.get_bit(i) == 1 { - out << min + i - } - } - - return out -} - -// parse_part parses a given part of a cron expression & returns the -// corresponding array of ints. -fn parse_part(s string, min int, max int) ![]int { - mut bf := bitfield.new(max - min + 1) - - for range in s.split(',') { - bf2 := parse_range(range, min, max)! - bf = bitfield.bf_or(bf, bf2) - } - - return bf_to_ints(bf, min) -} - -// parse_expression parses an entire cron expression string into a -// CronExpression object, if possible. -pub fn parse_expression(exp string) !CronExpression { - // The filter allows for multiple spaces between parts - mut parts := exp.split(' ').filter(it != '') - - if parts.len < 2 || parts.len > 4 { - return error('Expression must contain between 2 and 4 space-separated parts.') - } - - // For ease of use, we allow the user to only specify as many parts as they - // need. - for parts.len < 4 { - parts << '*' - } - - mut part_results := [][]int{} - - mins := [0, 0, 1, 1] - maxs := [59, 23, 31, 12] - - // This for loop allows us to more clearly propagate the error to the user. - for i, min in mins { - part_results << parse_part(parts[i], min, maxs[i]) or { - return error('An error occurred with part $i: $err.msg()') - } - } - - return CronExpression{ - minutes: part_results[0] - hours: part_results[1] - days: part_results[2] - months: part_results[3] - } -} diff --git a/src/cron/expression/expression_parse_test.v b/src/cron/expression/expression_parse_test.v deleted file mode 100644 index 92e8291..0000000 --- a/src/cron/expression/expression_parse_test.v +++ /dev/null @@ -1,89 +0,0 @@ -module expression - -// parse_range_error returns the returned error message. If the result is '', -// that means the function didn't error. -fn parse_range_error(s string, min int, max int) string { - parse_range(s, min, max) or { return err.msg } - - return '' -} - -// =====parse_range===== -fn test_range_star_range() ! { - bf := parse_range('*', 0, 5)! - - assert bf_to_ints(bf, 0) == [0, 1, 2, 3, 4, 5] -} - -fn test_range_number() ! { - bf := parse_range('4', 0, 5)! - - assert bf_to_ints(bf, 0) == [4] -} - -fn test_range_number_too_large() ! { - assert parse_range_error('10', 0, 6) == 'Out of range.' -} - -fn test_range_number_too_small() ! { - assert parse_range_error('0', 2, 6) == 'Out of range.' -} - -fn test_range_number_invalid() ! { - assert parse_range_error('x', 0, 6) == 'Invalid number.' -} - -fn test_range_step_star_1() ! { - bf := parse_range('*/4', 0, 20)! - - assert bf_to_ints(bf, 0) == [0, 4, 8, 12, 16, 20] -} - -fn test_range_step_star_2() ! { - bf := parse_range('*/3', 1, 8)! - - assert bf_to_ints(bf, 1) == [1, 4, 7] -} - -fn test_range_step_star_too_large() ! { - assert parse_range_error('*/21', 0, 20) == 'Step size too large.' -} - -fn test_range_step_zero() ! { - assert parse_range_error('*/0', 0, 20) == 'Step size zero not allowed.' -} - -fn test_range_step_number() ! { - bf := parse_range('5/4', 2, 22)! - - assert bf_to_ints(bf, 2) == [5, 9, 13, 17, 21] -} - -fn test_range_step_number_too_large() ! { - assert parse_range_error('10/4', 0, 5) == 'Out of range.' -} - -fn test_range_step_number_too_small() ! { - assert parse_range_error('2/4', 5, 10) == 'Out of range.' -} - -fn test_range_dash() ! { - bf := parse_range('4-8', 0, 9)! - - assert bf_to_ints(bf, 0) == [4, 5, 6, 7, 8] -} - -fn test_range_dash_step() ! { - bf := parse_range('4-8/2', 0, 9)! - - assert bf_to_ints(bf, 0) == [4, 6, 8] -} - -// =====parse_part===== -fn test_part_single() ! { - assert parse_part('*', 0, 5)! == [0, 1, 2, 3, 4, 5] -} - -fn test_part_multiple() ! { - assert parse_part('*/2,2/3', 1, 8)! == [1, 2, 3, 5, 7, 8] -} diff --git a/src/cron/expression/expression_test.v b/src/cron/expression_test.v similarity index 56% rename from src/cron/expression/expression_test.v rename to src/cron/expression_test.v index 82bf959..c7065f8 100644 --- a/src/cron/expression/expression_test.v +++ b/src/cron/expression_test.v @@ -1,4 +1,4 @@ -module expression +module cron import time { parse } @@ -7,7 +7,7 @@ fn util_test_time(exp string, t1_str string, t2_str string) ! { t1 := parse(t1_str)! t2 := parse(t2_str)! - t3 := ce.next(t1)! + t3 := ce.next(t1) assert t2.year == t3.year assert t2.month == t3.month @@ -18,17 +18,18 @@ fn util_test_time(exp string, t1_str string, t2_str string) ! { fn test_next_simple() ! { // Very simple - util_test_time('0 3', '2002-01-01 00:00:00', '2002-01-01 03:00:00')! + // util_test_time('0 3', '2002-01-01 00:00:00', '2002-01-01 03:00:00')! // Overlap to next day - util_test_time('0 3', '2002-01-01 03:00:00', '2002-01-02 03:00:00')! - util_test_time('0 3', '2002-01-01 04:00:00', '2002-01-02 03:00:00')! + mut exp := '0 3 ' + util_test_time(exp, '2002-01-01 03:00:00', '2002-01-02 03:00:00')! + util_test_time(exp, '2002-01-01 04:00:00', '2002-01-02 03:00:00')! - util_test_time('0 3/4', '2002-01-01 04:00:00', '2002-01-01 07:00:00')! + util_test_time('0 3-7/4,7-19', '2002-01-01 04:00:00', '2002-01-01 07:00:00')! - // Overlap to next month + //// Overlap to next month util_test_time('0 3', '2002-11-31 04:00:00', '2002-12-01 03:00:00')! - // Overlap to next year + //// Overlap to next year util_test_time('0 3', '2002-12-31 04:00:00', '2003-01-01 03:00:00')! } diff --git a/src/cron/parse_test.v b/src/cron/parse_test.v new file mode 100644 index 0000000..0dce7c2 --- /dev/null +++ b/src/cron/parse_test.v @@ -0,0 +1,42 @@ +module cron + +fn test_not_allowed() { + illegal_expressions := [ + '4 *-7', + '4 *-7/4', + '4 7/*', + '0 0 30 2', + '0 /5', + '0 ', + '0', + ' 0', + ' 0 ', + '1 2 3 4~9', + '1 1-3-5', + '0 5/2-5', + '', + '1 1/2/3', + '*5 8', + 'x 8', + ] + + mut res := false + + for exp in illegal_expressions { + res = false + parse_expression(exp) or { res = true } + assert res, "'$exp' should produce an error" + } +} + +fn test_auto_extend() ! { + ce1 := parse_expression('5 5')! + ce2 := parse_expression('5 5 *')! + ce3 := parse_expression('5 5 * *')! + + assert ce1 == ce2 && ce2 == ce3 +} + +fn test_four() { + parse_expression('0 1 2 3 ') or { assert false } +} diff --git a/src/libvieter b/src/libvieter new file mode 160000 index 0000000..11709cc --- /dev/null +++ b/src/libvieter @@ -0,0 +1 @@ +Subproject commit 11709cc611c02a4e9140409a0e81d639522c06f1 diff --git a/src/main.v b/src/main.v index 1c8b816..ce9ec81 100644 --- a/src/main.v +++ b/src/main.v @@ -9,7 +9,6 @@ import console.schedule import console.man import console.aur import console.repos -import cron import agent fn main() { @@ -43,7 +42,6 @@ fn main() { commands: [ server.cmd(), targets.cmd(), - cron.cmd(), logs.cmd(), schedule.cmd(), man.cmd(), diff --git a/src/server/log_removal.v b/src/server/log_removal.v index 8e1a8c2..27dc0db 100644 --- a/src/server/log_removal.v +++ b/src/server/log_removal.v @@ -3,17 +3,13 @@ module server import time import models { BuildLog } import os -import cron.expression { CronExpression } +import cron const fallback_log_removal_frequency = 24 * time.hour // log_removal_daemon removes old build logs every `log_removal_frequency`. -fn (mut app App) log_removal_daemon(schedule CronExpression) { - mut start_time := time.Time{} - +fn (mut app App) log_removal_daemon(schedule &cron.Expression) { for { - start_time = time.now() - mut too_old_timestamp := time.now().add_days(-app.conf.max_log_age) app.linfo('Cleaning logs before $too_old_timestamp') @@ -51,12 +47,7 @@ fn (mut app App) log_removal_daemon(schedule CronExpression) { app.linfo('Cleaned $counter logs ($failed failed)') // Sleep until the next cycle - next_time := schedule.next_from_now() or { - app.lerror("Log removal daemon couldn't calculate next time: $err.msg(); fallback to $server.fallback_log_removal_frequency") - - start_time.add(server.fallback_log_removal_frequency) - } - + next_time := schedule.next_from_now() time.sleep(next_time - time.now()) } } diff --git a/src/server/server.v b/src/server/server.v index 5dd1a20..ae086f5 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -7,7 +7,7 @@ import repo import util import db import build { BuildJobQueue } -import cron.expression +import cron import metrics const ( @@ -43,11 +43,11 @@ pub fn server(conf Config) ! { util.exit_with_message(1, "'any' is not allowed as the value for default_arch.") } - global_ce := expression.parse_expression(conf.global_schedule) or { + global_ce := cron.parse_expression(conf.global_schedule) or { util.exit_with_message(1, 'Invalid global cron expression: $err.msg()') } - log_removal_ce := expression.parse_expression(conf.log_removal_schedule) or { + log_removal_ce := cron.parse_expression(conf.log_removal_schedule) or { util.exit_with_message(1, 'Invalid log removal cron expression: $err.msg()') } diff --git a/vieter.toml b/vieter.toml index 7744a56..34b4f4e 100644 --- a/vieter.toml +++ b/vieter.toml @@ -13,4 +13,5 @@ api_update_frequency = 2 image_rebuild_frequency = 1 max_concurrent_builds = 3 # max_log_age = 64 +log_removal_schedule = '* * *' collect_metrics = true From ad19bc660a17d6b7d89ad085059569dc3218c436 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Mon, 16 Jan 2023 22:48:40 +0100 Subject: [PATCH 70/97] chore: switch to alpine 3.17 ci image --- .woodpecker/build.yml | 2 +- .woodpecker/docs.yml | 2 +- .woodpecker/gitea.yml | 2 +- .woodpecker/lint.yml | 2 +- .woodpecker/man.yml | 2 +- .woodpecker/test.yml | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.woodpecker/build.yml b/.woodpecker/build.yml index f10e2a5..b77a405 100644 --- a/.woodpecker/build.yml +++ b/.woodpecker/build.yml @@ -1,5 +1,5 @@ variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2' + - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2-alpine3.17' matrix: PLATFORM: diff --git a/.woodpecker/docs.yml b/.woodpecker/docs.yml index cf4874e..e51f3d7 100644 --- a/.woodpecker/docs.yml +++ b/.woodpecker/docs.yml @@ -1,5 +1,5 @@ variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2' + - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2-alpine3.17' platform: 'linux/amd64' branches: diff --git a/.woodpecker/gitea.yml b/.woodpecker/gitea.yml index 9034f33..cff0eb9 100644 --- a/.woodpecker/gitea.yml +++ b/.woodpecker/gitea.yml @@ -1,5 +1,5 @@ variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2' + - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2-alpine3.17' platform: 'linux/amd64' branches: [ 'main' ] diff --git a/.woodpecker/lint.yml b/.woodpecker/lint.yml index ec64d13..1babcbc 100644 --- a/.woodpecker/lint.yml +++ b/.woodpecker/lint.yml @@ -1,5 +1,5 @@ variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2' + - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2-alpine3.17' # These checks already get performed on the feature branches branches: diff --git a/.woodpecker/man.yml b/.woodpecker/man.yml index 8102443..9ad8dcf 100644 --- a/.woodpecker/man.yml +++ b/.woodpecker/man.yml @@ -1,5 +1,5 @@ variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2' + - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2-alpine3.17' platform: 'linux/amd64' branches: diff --git a/.woodpecker/test.yml b/.woodpecker/test.yml index 39cb9f9..91ef7c6 100644 --- a/.woodpecker/test.yml +++ b/.woodpecker/test.yml @@ -1,5 +1,5 @@ variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2' + - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2-alpine3.17' matrix: PLATFORM: From ba89110eab01f4f27c3f402b8b39af353405e2f9 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 18 Jan 2023 18:10:47 +0100 Subject: [PATCH 71/97] chore: some fixes --- .woodpecker/build.yml | 2 +- Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.woodpecker/build.yml b/.woodpecker/build.yml index b77a405..0785392 100644 --- a/.woodpecker/build.yml +++ b/.woodpecker/build.yml @@ -57,7 +57,7 @@ pipeline: - export OBJ_PATH="/vieter/commits/$CI_COMMIT_SHA/vieter-$(echo '${PLATFORM}' | sed 's:/:-:g')" - export SIG_STRING="PUT\n\n$CONTENT_TYPE\n$DATE\n$OBJ_PATH" - - export SIGNATURE="$(echo -en $SIG_STRING | openssl sha1 -hmac $S3_PASSWORD -binary | base64)" + - export SIGNATURE="$(echo -en $SIG_STRING | openssl dgst -sha1 -hmac $S3_PASSWORD -binary | base64)" - > curl --silent diff --git a/Makefile b/Makefile index 2f6029e..4b75910 100644 --- a/Makefile +++ b/Makefile @@ -88,7 +88,7 @@ fmt: # Testing .PHONY: test -test: +test: libvieter $(V) -g test $(SRC_DIR) From 6ca53ce534bcf205195ed21ed1192fb5885f8065 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sat, 28 Jan 2023 13:00:37 +0100 Subject: [PATCH 72/97] chore: use libvieter dev branch instead --- Makefile | 2 +- src/libvieter | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 4b75910..1521676 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ all: vieter # =====COMPILATION===== .PHONY: libvieter libvieter: - CFLAGS='-O3' make -C '$(SRC_DIR)/libvieter' + make -C '$(SRC_DIR)/libvieter' CFLAGS='-O3' # Regular binary vieter: $(SOURCES) libvieter diff --git a/src/libvieter b/src/libvieter index 11709cc..379a05a 160000 --- a/src/libvieter +++ b/src/libvieter @@ -1 +1 @@ -Subproject commit 11709cc611c02a4e9140409a0e81d639522c06f1 +Subproject commit 379a05a7b6b604c107360e0a679fb3ea5400e02c From 8d14d5c3fd6931462bba53f7d8232e0beb35bf05 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 28 Jan 2023 14:36:46 +0100 Subject: [PATCH 73/97] chore: update PKGBUILD to use git submodule --- PKGBUILD.dev | 30 ++++++++++++++++++++---------- src/cron/expression.c.v | 2 ++ src/libvieter | 2 +- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/PKGBUILD.dev b/PKGBUILD.dev index 79c7f37..6c5123b 100644 --- a/PKGBUILD.dev +++ b/PKGBUILD.dev @@ -11,27 +11,37 @@ makedepends=('git' 'vlang') arch=('x86_64' 'aarch64') url='https://git.rustybever.be/vieter-v/vieter' license=('AGPL3') -source=("$pkgname::git+https://git.rustybever.be/vieter-v/vieter#branch=dev") +source=( + "${pkgname}::git+https://git.rustybever.be/vieter-v/vieter#branch=dev" + "libvieter::git+https://git.rustybever.be/vieter-v/libvieter" +) md5sums=('SKIP') provides=('vieter') conflicts=('vieter') pkgver() { - cd "$pkgname" + cd "${pkgname}" git describe --long --tags | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./g' } prepare() { - export VMODULES="$srcdir/.vmodules" + cd "${pkgname}" - cd "$pkgname/src" && v install + # Add the libvieter submodule + git submodule init + git config submodules.src/libvieter.url "${srcdir}/libvieter" + git -c protocol.file.allow=always submodule update + + export VMODULES="${srcdir}/.vmodules" + + cd "${pkgname}/src" && v install } build() { - export VMODULES="$srcdir/.vmodules" + export VMODULES="${srcdir}/.vmodules" - cd "$pkgname" + cd "${pkgname}" make prod @@ -42,9 +52,9 @@ build() { } package() { - install -dm755 "$pkgdir/usr/bin" - install -Dm755 "$pkgname/pvieter" "$pkgdir/usr/bin/vieter" + install -dm755 "${pkgdir}/usr/bin" + install -Dm755 "${pkgname}/pvieter" "${pkgdir}/usr/bin/vieter" - install -dm755 "$pkgdir/usr/share/man/man1" - install -Dm644 "$pkgname/man"/*.1 "$pkgdir/usr/share/man/man1" + install -dm755 "${pkgdir}/usr/share/man/man1" + install -Dm644 "${pkgname}/man"/*.1 "${pkgdir}/usr/share/man/man1" } diff --git a/src/cron/expression.c.v b/src/cron/expression.c.v index 8c574c7..a41ad27 100644 --- a/src/cron/expression.c.v +++ b/src/cron/expression.c.v @@ -5,6 +5,7 @@ module cron #flag -lvieter #include "vieter_cron.h" +[typedef] pub struct C.vieter_cron_expression { minutes &u8 hours &u8 @@ -57,6 +58,7 @@ fn (ce1 Expression) == (ce2 Expression) bool { return true } +[typeef] struct C.vieter_cron_simple_time { year int month int diff --git a/src/libvieter b/src/libvieter index 379a05a..11709cc 160000 --- a/src/libvieter +++ b/src/libvieter @@ -1 +1 @@ -Subproject commit 379a05a7b6b604c107360e0a679fb3ea5400e02c +Subproject commit 11709cc611c02a4e9140409a0e81d639522c06f1 From 3b320ac7c347ff36f127ed9e107dd58ce12fd4d7 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 28 Jan 2023 14:39:12 +0100 Subject: [PATCH 74/97] fix: accidentally changed submodule commit --- src/libvieter | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libvieter b/src/libvieter index 11709cc..379a05a 160000 --- a/src/libvieter +++ b/src/libvieter @@ -1 +1 @@ -Subproject commit 11709cc611c02a4e9140409a0e81d639522c06f1 +Subproject commit 379a05a7b6b604c107360e0a679fb3ea5400e02c From 434c4eb5582378f7e74050f8e4bcaaf33dc7e8f2 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 28 Jan 2023 14:53:31 +0100 Subject: [PATCH 75/97] chore: updated changelog --- CHANGELOG.md | 4 ++++ src/cron/expression.c.v | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b1e583..4c572bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Rewrote cron expression logic in C +### Removed + +* Deprecated cron daemon + ## [0.5.0](https://git.rustybever.be/vieter-v/vieter/src/tag/0.5.0) ### Added diff --git a/src/cron/expression.c.v b/src/cron/expression.c.v index a41ad27..e9686d6 100644 --- a/src/cron/expression.c.v +++ b/src/cron/expression.c.v @@ -58,7 +58,7 @@ fn (ce1 Expression) == (ce2 Expression) bool { return true } -[typeef] +[typedef] struct C.vieter_cron_simple_time { year int month int From da370f42fdca8ef188dbc0218986dfe78a98cf9d Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 28 Jan 2023 15:22:04 +0100 Subject: [PATCH 76/97] fix: update md5sums in pkgbuild --- PKGBUILD.dev | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PKGBUILD.dev b/PKGBUILD.dev index 6c5123b..67b674f 100644 --- a/PKGBUILD.dev +++ b/PKGBUILD.dev @@ -15,7 +15,7 @@ source=( "${pkgname}::git+https://git.rustybever.be/vieter-v/vieter#branch=dev" "libvieter::git+https://git.rustybever.be/vieter-v/libvieter" ) -md5sums=('SKIP') +md5sums=('SKIP' 'SKIP') provides=('vieter') conflicts=('vieter') From 8f32888dff5a800879e619722e7331f3d735aca1 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 28 Jan 2023 15:27:47 +0100 Subject: [PATCH 77/97] fix: i'm too lazy to test these --- PKGBUILD.dev | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PKGBUILD.dev b/PKGBUILD.dev index 67b674f..b07585a 100644 --- a/PKGBUILD.dev +++ b/PKGBUILD.dev @@ -35,7 +35,7 @@ prepare() { export VMODULES="${srcdir}/.vmodules" - cd "${pkgname}/src" && v install + cd src && v install } build() { From e10b450abd2fd859baf6893636a6191a3a6b6872 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 28 Jan 2023 17:35:01 +0100 Subject: [PATCH 78/97] fix: metrics no longer bloat memory --- src/server/api_metrics.v | 3 +-- src/server/server.v | 5 ++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/server/api_metrics.v b/src/server/api_metrics.v index cde4437..5ba0452 100644 --- a/src/server/api_metrics.v +++ b/src/server/api_metrics.v @@ -10,8 +10,7 @@ fn (mut app App) v1_metrics() web.Result { return app.status(.not_found) } - mut exporter := metrics.new_prometheus_exporter([0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, - 10]) + mut exporter := metrics.new_prometheus_exporter() exporter.load('vieter_', app.collector) // TODO stream to connection instead diff --git a/src/server/server.v b/src/server/server.v index ae086f5..c6bdd1a 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -92,11 +92,14 @@ pub fn server(conf Config) ! { util.exit_with_message(1, 'Failed to initialize database: $err.msg()') } - collector := if conf.collect_metrics { + mut collector := if conf.collect_metrics { &metrics.MetricsCollector(metrics.new_default_collector()) } else { &metrics.MetricsCollector(metrics.new_null_collector()) } + + collector.histogram_buckets_set('http_requests_duration_seconds', [0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, + 10] ) mut app := &App{ logger: logger From b3a119f221d0c1c22588ef6c7ec5502188bce509 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 8 Feb 2023 11:00:17 +0100 Subject: [PATCH 79/97] chore: ran v fmt for v 0.3.3 changes --- src/agent/cli.v | 2 +- src/agent/daemon.v | 20 ++++++++++---------- src/agent/images.v | 2 +- src/build/build.v | 8 ++++---- src/build/queue.v | 6 +++--- src/build/shell.v | 16 ++++++++-------- src/client/client.v | 20 ++++++++++---------- src/client/jobs.v | 4 ++-- src/client/logs.v | 12 ++++++------ src/client/repos.v | 6 +++--- src/client/targets.v | 12 ++++++------ src/console/aur/aur.v | 8 ++++---- src/console/logs/logs.v | 8 ++++---- src/console/repos/repos.v | 2 +- src/console/targets/targets.v | 16 ++++++++-------- src/cron/parse_test.v | 2 +- src/db/db.v | 6 +++--- src/db/logs.v | 20 ++++++++++---------- src/db/targets.v | 4 ++-- src/models/builds.v | 2 +- src/models/logs.v | 12 ++++++------ src/models/models.v | 8 ++++---- src/models/targets.v | 14 +++++++------- src/package/format.v | 14 +++++++------- src/package/package.v | 2 +- src/repo/add.v | 12 ++++++++---- src/server/api_logs.v | 8 ++++---- src/server/api_targets.v | 4 ++-- src/server/cli.v | 2 +- src/server/log_removal.v | 6 +++--- src/server/repo.v | 14 +++++++------- src/server/repo_remove.v | 18 +++++++++--------- src/server/server.v | 16 ++++++++-------- src/util/stream.v | 2 +- src/web/parse.v | 4 ++-- src/web/response/response.v | 14 +++++++------- src/web/web.v | 28 ++++++++++++++-------------- 37 files changed, 179 insertions(+), 175 deletions(-) diff --git a/src/agent/cli.v b/src/agent/cli.v index 1535e17..41e3421 100644 --- a/src/agent/cli.v +++ b/src/agent/cli.v @@ -23,7 +23,7 @@ pub fn cmd() cli.Command { description: 'Start an agent daemon.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! agent(conf)! } diff --git a/src/agent/daemon.v b/src/agent/daemon.v index 62f36c2..4364a92 100644 --- a/src/agent/daemon.v +++ b/src/agent/daemon.v @@ -51,7 +51,7 @@ pub fn (mut d AgentDaemon) run() { for { if sleep_time > 0 { - d.ldebug('Sleeping for $sleep_time') + d.ldebug('Sleeping for ${sleep_time}') time.sleep(sleep_time) } @@ -80,14 +80,14 @@ pub fn (mut d AgentDaemon) run() { d.ldebug('Polling for new jobs') new_configs := d.client.poll_jobs(d.conf.arch, finished + empty) or { - d.lerror('Failed to poll jobs: $err.msg()') + d.lerror('Failed to poll jobs: ${err.msg()}') // TODO pick a better delay here sleep_time = 5 * time.second continue } - d.ldebug('Received $new_configs.len jobs') + d.ldebug('Received ${new_configs.len} jobs') last_poll_time = time.now() @@ -95,7 +95,7 @@ pub fn (mut d AgentDaemon) run() { // Make sure a recent build base image is available for // building the config if !d.images.up_to_date(config.base_image) { - d.linfo('Building builder image from base image $config.base_image') + d.linfo('Building builder image from base image ${config.base_image}') // TODO handle this better than to just skip the config d.images.refresh_image(config.base_image) or { @@ -154,7 +154,7 @@ fn (mut d AgentDaemon) start_build(config BuildConfig) bool { stdatomic.store_u64(&d.atomics[i], agent.build_running) d.builds[i] = config - go d.run_build(i, config) + spawn d.run_build(i, config) return true } @@ -165,7 +165,7 @@ fn (mut d AgentDaemon) start_build(config BuildConfig) bool { // run_build actually starts the build process for a given target. fn (mut d AgentDaemon) run_build(build_index int, config BuildConfig) { - d.linfo('started build: $config') + d.linfo('started build: ${config}') // 0 means success, 1 means failure mut status := 0 @@ -176,21 +176,21 @@ fn (mut d AgentDaemon) run_build(build_index int, config BuildConfig) { } res := build.build_config(d.client.address, d.client.api_key, new_config) or { - d.ldebug('build_config error: $err.msg()') + d.ldebug('build_config error: ${err.msg()}') status = 1 build.BuildResult{} } if status == 0 { - d.linfo('Uploading build logs for $config') + d.linfo('Uploading build logs for ${config}') // TODO use the arch value here build_arch := os.uname().machine d.client.add_build_log(config.target_id, res.start_time, res.end_time, build_arch, - res.exit_code, res.logs) or { d.lerror('Failed to upload logs for $config') } + res.exit_code, res.logs) or { d.lerror('Failed to upload logs for ${config}') } } else { - d.lwarn('an error occurred during build: $config') + d.lwarn('an error occurred during build: ${config}') } stdatomic.store_u64(&d.atomics[build_index], agent.build_done) diff --git a/src/agent/images.v b/src/agent/images.v index 5fba0f7..9befc0c 100644 --- a/src/agent/images.v +++ b/src/agent/images.v @@ -71,7 +71,7 @@ pub fn (mut m ImageManager) up_to_date(base_image string) bool { fn (mut m ImageManager) refresh_image(base_image string) ! { // TODO use better image tags for built images new_image := build.create_build_image(base_image) or { - return error('Failed to build builder image from base image $base_image') + return error('Failed to build builder image from base image ${base_image}') } m.images[base_image] << new_image diff --git a/src/build/build.v b/src/build/build.v index dfea8c0..c69a613 100644 --- a/src/build/build.v +++ b/src/build/build.v @@ -45,7 +45,7 @@ pub fn create_build_image(base_image string) !string { c := docker.NewContainer{ image: base_image - env: ['BUILD_SCRIPT=$cmds_str'] + env: ['BUILD_SCRIPT=${cmds_str}'] entrypoint: ['/bin/sh', '-c'] cmd: ['echo \$BUILD_SCRIPT | base64 -d | /bin/sh -e'] } @@ -118,10 +118,10 @@ pub fn build_config(address string, api_key string, config BuildConfig) !BuildRe base64_script := base64.encode_str(build_script) c := docker.NewContainer{ - image: '$config.base_image' + image: '${config.base_image}' env: [ - 'BUILD_SCRIPT=$base64_script', - 'API_KEY=$api_key', + 'BUILD_SCRIPT=${base64_script}', + 'API_KEY=${api_key}', // `archlinux:base-devel` does not correctly set the path variable, // causing certain builds to fail. This fixes it. 'PATH=${build.path_dirs.join(':')}', diff --git a/src/build/queue.v b/src/build/queue.v index abd4ec6..2aa6e7a 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -36,7 +36,7 @@ pub struct BuildJobQueue { mut: mutex shared util.Dummy // For each architecture, a priority queue is tracked - queues map[string]MinHeap + queues map[string]MinHeap[BuildJob] // When a target is removed from the server or edited, its previous build // configs will be invalid. This map allows for those to be simply skipped // by ignoring any build configs created before this timestamp. @@ -74,7 +74,7 @@ pub struct InsertConfig { pub fn (mut q BuildJobQueue) insert(input InsertConfig) ! { lock q.mutex { if input.arch !in q.queues { - q.queues[input.arch] = MinHeap{} + q.queues[input.arch] = MinHeap[BuildJob]{} } mut job := BuildJob{ @@ -86,7 +86,7 @@ pub fn (mut q BuildJobQueue) insert(input InsertConfig) ! { if !input.now { ce := if input.target.schedule != '' { cron.parse_expression(input.target.schedule) or { - return error("Error while parsing cron expression '$input.target.schedule' (id $input.target.id): $err.msg()") + return error("Error while parsing cron expression '${input.target.schedule}' (id ${input.target.id}): ${err.msg()}") } } else { q.default_schedule diff --git a/src/build/shell.v b/src/build/shell.v index 16f93b5..f32cd08 100644 --- a/src/build/shell.v +++ b/src/build/shell.v @@ -24,12 +24,12 @@ pub fn echo_commands(cmds []string) []string { // create_build_script generates a shell script that builds a given Target. fn create_build_script(address string, config BuildConfig, build_arch string) string { - repo_url := '$address/$config.repo' + repo_url := '${address}/${config.repo}' mut commands := [ // This will later be replaced by a proper setting for changing the // mirrorlist - "echo -e '[$config.repo]\\nServer = $address/\$repo/\$arch\\nSigLevel = Optional' >> /etc/pacman.conf" + "echo -e '[${config.repo}]\\nServer = ${address}/\$repo/\$arch\\nSigLevel = Optional' >> /etc/pacman.conf" // We need to update the package list of the repo we just added above. // This should however not pull in a lot of packages as long as the // builder image is rebuilt frequently. @@ -42,18 +42,18 @@ fn create_build_script(address string, config BuildConfig, build_arch string) st 'git' { if config.branch == '' { [ - "git clone --single-branch --depth 1 '$config.url' repo", + "git clone --single-branch --depth 1 '${config.url}' repo", ] } else { [ - "git clone --single-branch --depth 1 --branch $config.branch '$config.url' repo", + "git clone --single-branch --depth 1 --branch ${config.branch} '${config.url}' repo", ] } } 'url' { [ 'mkdir repo', - "curl -o repo/PKGBUILD -L '$config.url'", + "curl -o repo/PKGBUILD -L '${config.url}'", ] } else { @@ -62,7 +62,7 @@ fn create_build_script(address string, config BuildConfig, build_arch string) st } commands << if config.path != '' { - "cd 'repo/$config.path'" + "cd 'repo/${config.path}'" } else { 'cd repo' } @@ -76,7 +76,7 @@ fn create_build_script(address string, config BuildConfig, build_arch string) st // The build container checks whether the package is already present on // the server. commands << [ - 'curl -s --head --fail $repo_url/$build_arch/\$pkgname-\$pkgver-\$pkgrel && exit 0', + 'curl -s --head --fail ${repo_url}/${build_arch}/\$pkgname-\$pkgver-\$pkgrel && exit 0', // If the above curl command succeeds, we don't need to rebuild the // package. However, because we're in a su shell, the exit command will // drop us back into the root shell. Therefore, we must check whether @@ -86,7 +86,7 @@ fn create_build_script(address string, config BuildConfig, build_arch string) st } commands << [ - 'MAKEFLAGS="-j\$(nproc)" makepkg -s --noconfirm --needed --noextract && for pkg in \$(ls -1 *.pkg*); do curl -XPOST -T "\$pkg" -H "X-API-KEY: \$API_KEY" $repo_url/publish; done', + 'MAKEFLAGS="-j\$(nproc)" makepkg -s --noconfirm --needed --noextract && for pkg in \$(ls -1 *.pkg*); do curl -XPOST -T "\$pkg" -H "X-API-KEY: \$API_KEY" ${repo_url}/publish; done', ] return echo_commands(commands).join('\n') diff --git a/src/client/client.v b/src/client/client.v index cce4e70..7d57e92 100644 --- a/src/client/client.v +++ b/src/client/client.v @@ -22,7 +22,7 @@ pub fn new(address string, api_key string) Client { // send_request_raw sends an HTTP request, returning the http.Response object. // It encodes the params so that they're safe to pass as HTTP query parameters. fn (c &Client) send_request_raw(method Method, url string, params map[string]string, body string) !http.Response { - mut full_url := '$c.address$url' + mut full_url := '${c.address}${url}' if params.len > 0 { mut params_escaped := map[string]string{} @@ -33,9 +33,9 @@ fn (c &Client) send_request_raw(method Method, url string, params map[string]str params_escaped[k] = urllib.query_escape(v) } - params_str := params_escaped.keys().map('$it=${params_escaped[it]}').join('&') + params_str := params_escaped.keys().map('${it}=${params_escaped[it]}').join('&') - full_url = '$full_url?$params_str' + full_url = '${full_url}?${params_str}' } // Looking at the source code, this function doesn't actually fail, so I'm @@ -49,13 +49,13 @@ fn (c &Client) send_request_raw(method Method, url string, params map[string]str } // send_request just calls send_request_with_body with an empty body. -fn (c &Client) send_request(method Method, url string, params map[string]string) !Response { - return c.send_request_with_body(method, url, params, '') +fn (c &Client) send_request[T](method Method, url string, params map[string]string) !Response[T] { + return c.send_request_with_body[T](method, url, params, '') } // send_request_with_body calls send_request_raw_response & parses its // output as a Response object. -fn (c &Client) send_request_with_body(method Method, url string, params map[string]string, body string) !Response { +fn (c &Client) send_request_with_body[T](method Method, url string, params map[string]string, body string) !Response[T] { res := c.send_request_raw(method, url, params, body)! status := res.status() @@ -64,12 +64,12 @@ fn (c &Client) send_request_with_body(method Method, url string, params map[s if status.is_error() { // A non-successful status call will have an empty body if res.body == '' { - return error('Error $res.status_code ($status.str()): (empty response)') + return error('Error ${res.status_code} (${status.str()}): (empty response)') } - data := json.decode(Response, res.body)! + data := json.decode(Response[string], res.body)! - return error('Status $res.status_code ($status.str()): $data.message') + return error('Status ${res.status_code} (${status.str()}): ${data.message}') } // Just return an empty successful response @@ -77,7 +77,7 @@ fn (c &Client) send_request_with_body(method Method, url string, params map[s return new_data_response(T{}) } - data := json.decode(Response, res.body)! + data := json.decode(Response[T], res.body)! return data } diff --git a/src/client/jobs.v b/src/client/jobs.v index 784639e..ddb9e2d 100644 --- a/src/client/jobs.v +++ b/src/client/jobs.v @@ -4,7 +4,7 @@ import models { BuildConfig } // poll_jobs requests a list of new build jobs from the server. pub fn (c &Client) poll_jobs(arch string, max int) ![]BuildConfig { - data := c.send_request<[]BuildConfig>(.get, '/api/v1/jobs/poll', { + data := c.send_request[[]BuildConfig](.get, '/api/v1/jobs/poll', { 'arch': arch 'max': max.str() })! @@ -15,7 +15,7 @@ pub fn (c &Client) poll_jobs(arch string, max int) ![]BuildConfig { // queue_job adds a new one-time build job for the given target to the job // queue. pub fn (c &Client) queue_job(target_id int, arch string, force bool) ! { - c.send_request(.post, '/api/v1/jobs/queue', { + c.send_request[string](.post, '/api/v1/jobs/queue', { 'target': target_id.str() 'arch': arch 'force': force.str() diff --git a/src/client/logs.v b/src/client/logs.v index 6553837..ff6b7c5 100644 --- a/src/client/logs.v +++ b/src/client/logs.v @@ -7,27 +7,27 @@ import time // get_build_logs returns all build logs. pub fn (c &Client) get_build_logs(filter BuildLogFilter) ![]BuildLog { params := models.params_from(filter) - data := c.send_request<[]BuildLog>(.get, '/api/v1/logs', params)! + data := c.send_request[[]BuildLog](.get, '/api/v1/logs', params)! return data.data } // get_build_log returns a specific build log. pub fn (c &Client) get_build_log(id int) !BuildLog { - data := c.send_request(.get, '/api/v1/logs/$id', {})! + data := c.send_request[BuildLog](.get, '/api/v1/logs/${id}', {})! return data.data } // get_build_log_content returns the contents of the build log file. pub fn (c &Client) get_build_log_content(id int) !string { - data := c.send_request_raw_response(.get, '/api/v1/logs/$id/content', {}, '')! + data := c.send_request_raw_response(.get, '/api/v1/logs/${id}/content', {}, '')! return data } // add_build_log adds a new build log to the server. -pub fn (c &Client) add_build_log(target_id int, start_time time.Time, end_time time.Time, arch string, exit_code int, content string) !Response { +pub fn (c &Client) add_build_log(target_id int, start_time time.Time, end_time time.Time, arch string, exit_code int, content string) !Response[int] { params := { 'target': target_id.str() 'startTime': start_time.unix_time().str() @@ -36,12 +36,12 @@ pub fn (c &Client) add_build_log(target_id int, start_time time.Time, end_time t 'exitCode': exit_code.str() } - data := c.send_request_with_body(.post, '/api/v1/logs', params, content)! + data := c.send_request_with_body[int](.post, '/api/v1/logs', params, content)! return data } // remove_build_log removes the build log with the given id from the server. pub fn (c &Client) remove_build_log(id int) ! { - c.send_request(.delete, '/api/v1/logs/$id', {})! + c.send_request[string](.delete, '/api/v1/logs/${id}', {})! } diff --git a/src/client/repos.v b/src/client/repos.v index 9644e9b..dff5d90 100644 --- a/src/client/repos.v +++ b/src/client/repos.v @@ -2,15 +2,15 @@ module client // remove_repo removes an entire repository. pub fn (c &Client) remove_repo(repo string) ! { - c.send_request(.delete, '/$repo', {})! + c.send_request[string](.delete, '/${repo}', {})! } // remove_arch_repo removes an entire arch-repo. pub fn (c &Client) remove_arch_repo(repo string, arch string) ! { - c.send_request(.delete, '/$repo/$arch', {})! + c.send_request[string](.delete, '/${repo}/${arch}', {})! } // remove_package removes a single package from the given arch-repo. pub fn (c &Client) remove_package(repo string, arch string, pkgname string) ! { - c.send_request(.delete, '/$repo/$arch/$pkgname', {})! + c.send_request[string](.delete, '/${repo}/${arch}/${pkgname}', {})! } diff --git a/src/client/targets.v b/src/client/targets.v index 565832e..3d43d43 100644 --- a/src/client/targets.v +++ b/src/client/targets.v @@ -5,7 +5,7 @@ import models { Target, TargetFilter } // get_targets returns a list of targets, given a filter object. pub fn (c &Client) get_targets(filter TargetFilter) ![]Target { params := models.params_from(filter) - data := c.send_request<[]Target>(.get, '/api/v1/targets', params)! + data := c.send_request[[]Target](.get, '/api/v1/targets', params)! return data.data } @@ -33,7 +33,7 @@ pub fn (c &Client) get_all_targets() ![]Target { // get_target returns the target for a specific id. pub fn (c &Client) get_target(id int) !Target { - data := c.send_request(.get, '/api/v1/targets/$id', {})! + data := c.send_request[Target](.get, '/api/v1/targets/${id}', {})! return data.data } @@ -49,15 +49,15 @@ pub struct NewTarget { // add_target adds a new target to the server. pub fn (c &Client) add_target(t NewTarget) !int { - params := models.params_from(t) - data := c.send_request(.post, '/api/v1/targets', params)! + params := models.params_from[NewTarget](t) + data := c.send_request[int](.post, '/api/v1/targets', params)! return data.data } // remove_target removes the target with the given id from the server. pub fn (c &Client) remove_target(id int) !string { - data := c.send_request(.delete, '/api/v1/targets/$id', {})! + data := c.send_request[string](.delete, '/api/v1/targets/${id}', {})! return data.data } @@ -65,7 +65,7 @@ pub fn (c &Client) remove_target(id int) !string { // patch_target sends a PATCH request to the given target with the params as // payload. pub fn (c &Client) patch_target(id int, params map[string]string) !string { - data := c.send_request(.patch, '/api/v1/targets/$id', params)! + data := c.send_request[string](.patch, '/api/v1/targets/${id}', params)! return data.data } diff --git a/src/console/aur/aur.v b/src/console/aur/aur.v index a6a3324..6fc8513 100644 --- a/src/console/aur/aur.v +++ b/src/console/aur/aur.v @@ -36,7 +36,7 @@ pub fn cmd() cli.Command { required_args: 2 execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! c := aur.new() pkgs := c.info(cmd.args[1..])! @@ -46,14 +46,14 @@ pub fn cmd() cli.Command { for pkg in pkgs { vc.add_target( kind: 'git' - url: 'https://aur.archlinux.org/$pkg.package_base' + '.git' + url: 'https://aur.archlinux.org/${pkg.package_base}' + '.git' repo: cmd.args[0] ) or { - println('Failed to add $pkg.name: $err.msg()') + println('Failed to add ${pkg.name}: ${err.msg()}') continue } - println('Added $pkg.name' + '.') + println('Added ${pkg.name}' + '.') } } }, diff --git a/src/console/logs/logs.v b/src/console/logs/logs.v index 35ce4d7..518b507 100644 --- a/src/console/logs/logs.v +++ b/src/console/logs/logs.v @@ -74,7 +74,7 @@ pub fn cmd() cli.Command { ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! mut filter := BuildLogFilter{} @@ -156,7 +156,7 @@ pub fn cmd() cli.Command { description: 'Remove a build log that matches the given id.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! remove(conf, cmd.args[0])! } @@ -168,7 +168,7 @@ pub fn cmd() cli.Command { description: 'Show all info for a specific build log.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! id := cmd.args[0].int() info(conf, id)! @@ -181,7 +181,7 @@ pub fn cmd() cli.Command { description: 'Output the content of a build log to stdout.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! id := cmd.args[0].int() content(conf, id)! diff --git a/src/console/repos/repos.v b/src/console/repos/repos.v index 729208e..0021d52 100644 --- a/src/console/repos/repos.v +++ b/src/console/repos/repos.v @@ -28,7 +28,7 @@ pub fn cmd() cli.Command { ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! if cmd.args.len < 3 { if !cmd.flags.get_bool('force')! { diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index 709c196..1b8d4be 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -54,7 +54,7 @@ pub fn cmd() cli.Command { ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! mut filter := TargetFilter{} @@ -113,7 +113,7 @@ pub fn cmd() cli.Command { ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! t := NewTarget{ kind: cmd.flags.get_string('kind')! @@ -135,7 +135,7 @@ pub fn cmd() cli.Command { description: 'Remove a target that matches the given id.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! remove(conf, cmd.args[0])! } @@ -147,7 +147,7 @@ pub fn cmd() cli.Command { description: 'Show detailed information for the target matching the id.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! info(conf, cmd.args[0])! } @@ -196,7 +196,7 @@ pub fn cmd() cli.Command { ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! found := cmd.flags.get_all_found() @@ -235,7 +235,7 @@ pub fn cmd() cli.Command { ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! remote := cmd.flags.get_bool('remote')! force := cmd.flags.get_bool('force')! @@ -280,7 +280,7 @@ fn add(conf Config, t &NewTarget, raw bool) ! { if raw { println(target_id) } else { - println('Target added with id $target_id') + println('Target added with id ${target_id}') } } @@ -296,7 +296,7 @@ fn patch(conf Config, id string, params map[string]string) ! { // invalid one to the server. if 'schedule' in params && params['schedule'] != '' { cron.parse_expression(params['schedule']) or { - return error('Invalid cron expression: $err.msg()') + return error('Invalid cron expression: ${err.msg()}') } } diff --git a/src/cron/parse_test.v b/src/cron/parse_test.v index 0dce7c2..19575d7 100644 --- a/src/cron/parse_test.v +++ b/src/cron/parse_test.v @@ -25,7 +25,7 @@ fn test_not_allowed() { for exp in illegal_expressions { res = false parse_expression(exp) or { res = true } - assert res, "'$exp' should produce an error" + assert res, "'${exp}' should produce an error" } } diff --git a/src/db/db.v b/src/db/db.v index 98ee000..73a5e83 100644 --- a/src/db/db.v +++ b/src/db/db.v @@ -55,7 +55,7 @@ pub fn init(db_path string) !VieterDb { version_num := i + 1 // vfmt does not like these dots - println('Applying migration $version_num' + '...') + println('Applying migration ${version_num}' + '...') // The sqlite library seems to not like it when multiple statements are // passed in a single exec. Therefore, we split them & run them all @@ -64,7 +64,7 @@ pub fn init(db_path string) !VieterDb { res := conn.exec_none(part) if res != sqlite.sqlite_done { - return error('An error occurred while applying migration $version_num: SQLite error code $res') + return error('An error occurred while applying migration ${version_num}: SQLite error code ${res}') } } @@ -82,7 +82,7 @@ pub fn init(db_path string) !VieterDb { // row_into converts an sqlite.Row into a given type T by parsing each field // from a string according to its type. -pub fn row_into(row sqlite.Row) T { +pub fn row_into[T](row sqlite.Row) T { mut i := 0 mut out := T{} diff --git a/src/db/logs.v b/src/db/logs.v index 2745467..0321183 100644 --- a/src/db/logs.v +++ b/src/db/logs.v @@ -8,20 +8,20 @@ pub fn (db &VieterDb) get_build_logs(filter BuildLogFilter) []BuildLog { mut where_parts := []string{} if filter.target != 0 { - where_parts << 'target_id == $filter.target' + where_parts << 'target_id == ${filter.target}' } if filter.before != time.Time{} { - where_parts << 'start_time < $filter.before.unix_time()' + where_parts << 'start_time < ${filter.before.unix_time()}' } if filter.after != time.Time{} { - where_parts << 'start_time > $filter.after.unix_time()' + where_parts << 'start_time > ${filter.after.unix_time()}' } // NOTE: possible SQL injection if filter.arch != '' { - where_parts << "arch == '$filter.arch'" + where_parts << "arch == '${filter.arch}'" } mut parts := []string{} @@ -30,27 +30,27 @@ pub fn (db &VieterDb) get_build_logs(filter BuildLogFilter) []BuildLog { if exp[0] == `!` { code := exp[1..].int() - parts << 'exit_code != $code' + parts << 'exit_code != ${code}' } else { code := exp.int() - parts << 'exit_code == $code' + parts << 'exit_code == ${code}' } } if parts.len > 0 { - where_parts << parts.map('($it)').join(' or ') + where_parts << parts.map('(${it})').join(' or ') } mut where_str := '' if where_parts.len > 0 { - where_str = 'where ' + where_parts.map('($it)').join(' and ') + where_str = 'where ' + where_parts.map('(${it})').join(' and ') } - query := 'select * from BuildLog $where_str limit $filter.limit offset $filter.offset' + query := 'select * from BuildLog ${where_str} limit ${filter.limit} offset ${filter.offset}' rows, _ := db.conn.exec(query) - res := rows.map(row_into(it)) + res := rows.map(row_into[BuildLog](it)) return res } diff --git a/src/db/targets.v b/src/db/targets.v index 2644f49..35ee270 100644 --- a/src/db/targets.v +++ b/src/db/targets.v @@ -49,13 +49,13 @@ pub fn (db &VieterDb) update_target(target_id int, params map[string]string) { if field.name in params { // Any fields that are array types require their own update method $if field.typ is string { - values << "$field.name = '${params[field.name]}'" + values << "${field.name} = '${params[field.name]}'" } } } values_str := values.join(', ') // I think this is actual SQL & not the ORM language - query := 'update Target set $values_str where id == $target_id' + query := 'update Target set ${values_str} where id == ${target_id}' db.conn.exec_none(query) } diff --git a/src/models/builds.v b/src/models/builds.v index 926a53c..be2910c 100644 --- a/src/models/builds.v +++ b/src/models/builds.v @@ -14,5 +14,5 @@ pub: // str return a single-line string representation of a build log pub fn (c BuildConfig) str() string { - return '{ target: $c.target_id, kind: $c.kind, url: $c.url, branch: $c.branch, path: $c.path, repo: $c.repo, base_image: $c.base_image, force: $c.force }' + return '{ target: ${c.target_id}, kind: ${c.kind}, url: ${c.url}, branch: ${c.branch}, path: ${c.path}, repo: ${c.repo}, base_image: ${c.base_image}, force: ${c.force} }' } diff --git a/src/models/logs.v b/src/models/logs.v index 66a3a0a..cb01d08 100644 --- a/src/models/logs.v +++ b/src/models/logs.v @@ -16,13 +16,13 @@ pub mut: // str returns a string representation. pub fn (bl &BuildLog) str() string { mut parts := [ - 'id: $bl.id', - 'target id: $bl.target_id', - 'start time: $bl.start_time.local()', - 'end time: $bl.end_time.local()', + 'id: ${bl.id}', + 'target id: ${bl.target_id}', + 'start time: ${bl.start_time.local()}', + 'end time: ${bl.end_time.local()}', 'duration: ${bl.end_time - bl.start_time}', - 'arch: $bl.arch', - 'exit code: $bl.exit_code', + 'arch: ${bl.arch}', + 'exit code: ${bl.exit_code}', ] str := parts.join('\n') diff --git a/src/models/models.v b/src/models/models.v index b6103d3..9111286 100644 --- a/src/models/models.v +++ b/src/models/models.v @@ -4,17 +4,17 @@ import time // from_params creates a new instance of T from the given map by parsing all // of its fields from the map. -pub fn from_params(params map[string]string) ?T { +pub fn from_params[T](params map[string]string) ?T { mut o := T{} - patch_from_params(mut o, params)? + patch_from_params[T](mut o, params)? return o } // patch_from_params updates the given T object with the params defined in // the map. -pub fn patch_from_params(mut o T, params map[string]string) ? { +pub fn patch_from_params[T](mut o T, params map[string]string) ? { $for field in T.fields { if field.name in params && params[field.name] != '' { $if field.typ is string { @@ -37,7 +37,7 @@ pub fn patch_from_params(mut o T, params map[string]string) ? { } // params_from converts a given T struct into a map of strings. -pub fn params_from(o &T) map[string]string { +pub fn params_from[T](o &T) map[string]string { mut out := map[string]string{} $for field in T.fields { diff --git a/src/models/targets.v b/src/models/targets.v index a0c88d0..3c0c9cf 100644 --- a/src/models/targets.v +++ b/src/models/targets.v @@ -38,13 +38,13 @@ pub mut: // str returns a string representation. pub fn (t &Target) str() string { mut parts := [ - 'id: $t.id', - 'kind: $t.kind', - 'url: $t.url', - 'branch: $t.branch', - 'path: $t.path', - 'repo: $t.repo', - 'schedule: $t.schedule', + 'id: ${t.id}', + 'kind: ${t.kind}', + 'url: ${t.url}', + 'branch: ${t.branch}', + 'path: ${t.path}', + 'repo: ${t.repo}', + 'schedule: ${t.schedule}', 'arch: ${t.arch.map(it.value).join(', ')}', ] str := parts.join('\n') diff --git a/src/package/format.v b/src/package/format.v index a81d327..b126f3a 100644 --- a/src/package/format.v +++ b/src/package/format.v @@ -3,14 +3,14 @@ module package // format_entry returns a string properly formatted to be added to a desc file. [inline] fn format_entry(key string, value string) string { - return '\n%$key%\n$value\n' + return '\n%${key}%\n${value}\n' } // full_name returns the properly formatted name for the package, including // version & architecture pub fn (pkg &Pkg) full_name() string { p := pkg.info - return '$p.name-$p.version-$p.arch' + return '${p.name}-${p.version}-${p.arch}' } // filename returns the correct filename of the package file @@ -20,10 +20,10 @@ pub fn (pkg &Pkg) filename() string { 1 { '.tar.gz' } 6 { '.tar.xz' } 14 { '.tar.zst' } - else { panic("Another compression code shouldn't be possible. Faulty code: $pkg.compression") } + else { panic("Another compression code shouldn't be possible. Faulty code: ${pkg.compression}") } } - return '${pkg.full_name()}.pkg$ext' + return '${pkg.full_name()}.pkg${ext}' } // to_desc returns a desc file valid string representation @@ -31,7 +31,7 @@ pub fn (pkg &Pkg) to_desc() !string { p := pkg.info // filename - mut desc := '%FILENAME%\n$pkg.filename()\n' + mut desc := '%FILENAME%\n${pkg.filename()}\n' desc += format_entry('NAME', p.name) desc += format_entry('BASE', p.base) @@ -94,10 +94,10 @@ pub fn (pkg &Pkg) to_desc() !string { desc += format_entry('CHECKDEPENDS', p.checkdepends.join_lines()) } - return '$desc\n' + return '${desc}\n' } // to_files returns a files file valid string representation pub fn (pkg &Pkg) to_files() string { - return '%FILES%\n$pkg.files.join_lines()\n' + return '%FILES%\n${pkg.files.join_lines()}\n' } diff --git a/src/package/package.v b/src/package/package.v index 4518ffd..6cf8e3d 100644 --- a/src/package/package.v +++ b/src/package/package.v @@ -103,7 +103,7 @@ fn parse_pkg_info_string(pkg_info_str &string) !PkgInfo { // NOTE: this command only supports zstd-, xz- & gzip-compressed tarballs. pub fn read_pkg_archive(pkg_path string) !Pkg { if !os.is_file(pkg_path) { - return error("'$pkg_path' doesn't exist or isn't a file.") + return error("'${pkg_path}' doesn't exist or isn't a file.") } a := C.archive_read_new() diff --git a/src/repo/add.v b/src/repo/add.v index 8ab3ae1..47b0d7e 100644 --- a/src/repo/add.v +++ b/src/repo/add.v @@ -31,11 +31,15 @@ pub: // new creates a new RepoGroupManager & creates the directories as needed pub fn new(repos_dir string, pkg_dir string, default_arch string) !RepoGroupManager { if !os.is_dir(repos_dir) { - os.mkdir_all(repos_dir) or { return error('Failed to create repos directory: $err.msg()') } + os.mkdir_all(repos_dir) or { + return error('Failed to create repos directory: ${err.msg()}') + } } if !os.is_dir(pkg_dir) { - os.mkdir_all(pkg_dir) or { return error('Failed to create package directory: $err.msg()') } + os.mkdir_all(pkg_dir) or { + return error('Failed to create package directory: ${err.msg()}') + } } return RepoGroupManager{ @@ -51,7 +55,7 @@ pub fn new(repos_dir string, pkg_dir string, default_arch string) !RepoGroupMana // the right subdirectories in r.pkg_dir if it was successfully added. pub fn (r &RepoGroupManager) add_pkg_from_path(repo string, pkg_path string) !RepoAddResult { pkg := package.read_pkg_archive(pkg_path) or { - return error('Failed to read package file: $err.msg()') + return error('Failed to read package file: ${err.msg()}') } archs := r.add_pkg_in_repo(repo, pkg)! @@ -129,7 +133,7 @@ fn (r &RepoGroupManager) add_pkg_in_repo(repo string, pkg &package.Pkg) ![]strin // files, and afterwards updates the db & files archives to reflect these // changes. fn (r &RepoGroupManager) add_pkg_in_arch_repo(repo string, arch string, pkg &package.Pkg) ! { - pkg_dir := os.join_path(r.repos_dir, repo, arch, '$pkg.info.name-$pkg.info.version') + pkg_dir := os.join_path(r.repos_dir, repo, arch, '${pkg.info.name}-${pkg.info.version}') // Remove the previous version of the package, if present r.remove_pkg_from_arch_repo(repo, arch, pkg.info.name, false)! diff --git a/src/server/api_logs.v b/src/server/api_logs.v index 3db4204..3e210b0 100644 --- a/src/server/api_logs.v +++ b/src/server/api_logs.v @@ -13,7 +13,7 @@ import models { BuildLog, BuildLogFilter } // optionally be added to limit the list of build logs to that repository. ['/api/v1/logs'; auth; get; markused] fn (mut app App) v1_get_logs() web.Result { - filter := models.from_params(app.query) or { + filter := models.from_params[BuildLogFilter](app.query) or { return app.json(.bad_request, new_response('Invalid query parameters.')) } logs := app.db.get_build_logs(filter) @@ -101,7 +101,7 @@ fn (mut app App) v1_post_log() web.Result { // Create the logs directory of it doesn't exist if !os.exists(os.dir(log_file_path)) { os.mkdir_all(os.dir(log_file_path)) or { - app.lerror('Error while creating log file: $err.msg()') + app.lerror('Error while creating log file: ${err.msg()}') return app.status(.internal_server_error) } @@ -109,7 +109,7 @@ fn (mut app App) v1_post_log() web.Result { if length := app.req.header.get(.content_length) { util.reader_to_file(mut app.reader, length.int(), log_file_path) or { - app.lerror('An error occured while receiving logs: $err.msg()') + app.lerror('An error occured while receiving logs: ${err.msg()}') return app.status(.internal_server_error) } @@ -127,7 +127,7 @@ fn (mut app App) v1_delete_log(id int) web.Result { full_path := os.join_path(app.conf.data_dir, logs_dir_name, log.path()) os.rm(full_path) or { - app.lerror('Failed to remove log file $full_path: $err.msg()') + app.lerror('Failed to remove log file ${full_path}: ${err.msg()}') return app.status(.internal_server_error) } diff --git a/src/server/api_targets.v b/src/server/api_targets.v index f47467a..a8fdf37 100644 --- a/src/server/api_targets.v +++ b/src/server/api_targets.v @@ -8,7 +8,7 @@ import models { Target, TargetArch, TargetFilter } // v1_get_targets returns the current list of targets. ['/api/v1/targets'; auth; get; markused] fn (mut app App) v1_get_targets() web.Result { - filter := models.from_params(app.query) or { + filter := models.from_params[TargetFilter](app.query) or { return app.json(.bad_request, new_response('Invalid query parameters.')) } mut iter := app.db.targets(filter) @@ -35,7 +35,7 @@ fn (mut app App) v1_post_target() web.Result { params['arch'] = app.conf.default_arch } - mut new_target := models.from_params(params) or { + mut new_target := models.from_params[Target](params) or { return app.json(.bad_request, new_response(err.msg())) } diff --git a/src/server/cli.v b/src/server/cli.v index 9a8b144..c272d52 100644 --- a/src/server/cli.v +++ b/src/server/cli.v @@ -25,7 +25,7 @@ pub fn cmd() cli.Command { description: 'Start the Vieter server.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load(prefix: 'VIETER_', default_path: config_file)! + conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! server(conf)! } diff --git a/src/server/log_removal.v b/src/server/log_removal.v index 27dc0db..bc51bcf 100644 --- a/src/server/log_removal.v +++ b/src/server/log_removal.v @@ -12,7 +12,7 @@ fn (mut app App) log_removal_daemon(schedule &cron.Expression) { for { mut too_old_timestamp := time.now().add_days(-app.conf.max_log_age) - app.linfo('Cleaning logs before $too_old_timestamp') + app.linfo('Cleaning logs before ${too_old_timestamp}') mut logs := []BuildLog{} mut counter := 0 @@ -29,7 +29,7 @@ fn (mut app App) log_removal_daemon(schedule &cron.Expression) { log_file_path := os.join_path(app.conf.data_dir, logs_dir_name, log.path()) os.rm(log_file_path) or { - app.lerror('Failed to remove log file $log_file_path: $err.msg()') + app.lerror('Failed to remove log file ${log_file_path}: ${err.msg()}') failed += 1 continue @@ -44,7 +44,7 @@ fn (mut app App) log_removal_daemon(schedule &cron.Expression) { } } - app.linfo('Cleaned $counter logs ($failed failed)') + app.linfo('Cleaned ${counter} logs (${failed} failed)') // Sleep until the next cycle next_time := schedule.next_from_now() diff --git a/src/server/repo.v b/src/server/repo.v index 38d07fe..724d9d0 100644 --- a/src/server/repo.v +++ b/src/server/repo.v @@ -26,7 +26,7 @@ fn (mut app App) get_repo_file(repo string, arch string, filename string) web.Re // There's no point in having the ability to serve db archives with wrong // filenames - if db_exts.any(filename == '$repo$it') { + if db_exts.any(filename == '${repo}${it}') { full_path = os.join_path(app.repo.repos_dir, repo, arch, filename) // repo-add does this using symlinks, but we just change the requested @@ -62,19 +62,19 @@ fn (mut app App) put_package(repo string) web.Result { // Generate a random filename for the temp file pkg_path = os.join_path_single(app.repo.pkg_dir, rand.uuid_v4()) - app.ldebug("Uploading $length bytes (${util.pretty_bytes(length.int())}) to '$pkg_path'.") + app.ldebug("Uploading ${length} bytes (${util.pretty_bytes(length.int())}) to '${pkg_path}'.") // This is used to time how long it takes to upload a file mut sw := time.new_stopwatch(time.StopWatchOptions{ auto_start: true }) util.reader_to_file(mut app.reader, length.int(), pkg_path) or { - app.lwarn("Failed to upload '$pkg_path'") + app.lwarn("Failed to upload '${pkg_path}'") return app.status(.internal_server_error) } sw.stop() - app.ldebug("Upload of '$pkg_path' completed in ${sw.elapsed().seconds():.3}s.") + app.ldebug("Upload of '${pkg_path}' completed in ${sw.elapsed().seconds():.3}s.") } else { app.lwarn('Tried to upload package without specifying a Content-Length.') @@ -83,14 +83,14 @@ fn (mut app App) put_package(repo string) web.Result { } res := app.repo.add_pkg_from_path(repo, pkg_path) or { - app.lerror('Error while adding package: $err.msg()') + app.lerror('Error while adding package: ${err.msg()}') - os.rm(pkg_path) or { app.lerror("Failed to remove download '$pkg_path': $err.msg()") } + os.rm(pkg_path) or { app.lerror("Failed to remove download '${pkg_path}': ${err.msg()}") } return app.status(.internal_server_error) } - app.linfo("Added '$res.name-$res.version' to '$repo (${res.archs.join(',')})'.") + app.linfo("Added '${res.name}-${res.version}' to '${repo} (${res.archs.join(',')})'.") return app.json(.ok, new_data_response(res)) } diff --git a/src/server/repo_remove.v b/src/server/repo_remove.v index 9e6d747..24baeaf 100644 --- a/src/server/repo_remove.v +++ b/src/server/repo_remove.v @@ -6,17 +6,17 @@ import web ['/:repo/:arch/:pkg'; auth; delete; markused] fn (mut app App) delete_package(repo string, arch string, pkg string) web.Result { res := app.repo.remove_pkg_from_arch_repo(repo, arch, pkg, true) or { - app.lerror('Error while deleting package: $err.msg()') + app.lerror('Error while deleting package: ${err.msg()}') return app.status(.internal_server_error) } if res { - app.linfo("Removed package '$pkg' from '$repo/$arch'") + app.linfo("Removed package '${pkg}' from '${repo}/${arch}'") return app.status(.ok) } else { - app.linfo("Tried removing package '$pkg' from '$repo/$arch', but it doesn't exist.") + app.linfo("Tried removing package '${pkg}' from '${repo}/${arch}', but it doesn't exist.") return app.status(.not_found) } @@ -26,17 +26,17 @@ fn (mut app App) delete_package(repo string, arch string, pkg string) web.Result ['/:repo/:arch'; auth; delete; markused] fn (mut app App) delete_arch_repo(repo string, arch string) web.Result { res := app.repo.remove_arch_repo(repo, arch) or { - app.lerror('Error while deleting arch-repo: $err.msg()') + app.lerror('Error while deleting arch-repo: ${err.msg()}') return app.status(.internal_server_error) } if res { - app.linfo("Removed arch-repo '$repo/$arch'") + app.linfo("Removed arch-repo '${repo}/${arch}'") return app.status(.ok) } else { - app.linfo("Tried removing '$repo/$arch', but it doesn't exist.") + app.linfo("Tried removing '${repo}/${arch}', but it doesn't exist.") return app.status(.not_found) } @@ -46,17 +46,17 @@ fn (mut app App) delete_arch_repo(repo string, arch string) web.Result { ['/:repo'; auth; delete; markused] fn (mut app App) delete_repo(repo string) web.Result { res := app.repo.remove_repo(repo) or { - app.lerror('Error while deleting repo: $err.msg()') + app.lerror('Error while deleting repo: ${err.msg()}') return app.status(.internal_server_error) } if res { - app.linfo("Removed repo '$repo'") + app.linfo("Removed repo '${repo}'") return app.status(.ok) } else { - app.linfo("Tried removing '$repo', but it doesn't exist.") + app.linfo("Tried removing '${repo}', but it doesn't exist.") return app.status(.not_found) } diff --git a/src/server/server.v b/src/server/server.v index c6bdd1a..79d93e2 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -44,11 +44,11 @@ pub fn server(conf Config) ! { } global_ce := cron.parse_expression(conf.global_schedule) or { - util.exit_with_message(1, 'Invalid global cron expression: $err.msg()') + util.exit_with_message(1, 'Invalid global cron expression: ${err.msg()}') } log_removal_ce := cron.parse_expression(conf.log_removal_schedule) or { - util.exit_with_message(1, 'Invalid log removal cron expression: $err.msg()') + util.exit_with_message(1, 'Invalid log removal cron expression: ${err.msg()}') } // Configure logger @@ -89,7 +89,7 @@ pub fn server(conf Config) ! { db_file := os.join_path_single(conf.data_dir, server.db_file_name) db := db.init(db_file) or { - util.exit_with_message(1, 'Failed to initialize database: $err.msg()') + util.exit_with_message(1, 'Failed to initialize database: ${err.msg()}') } mut collector := if conf.collect_metrics { @@ -97,9 +97,9 @@ pub fn server(conf Config) ! { } else { &metrics.MetricsCollector(metrics.new_null_collector()) } - - collector.histogram_buckets_set('http_requests_duration_seconds', [0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, - 10] ) + + collector.histogram_buckets_set('http_requests_duration_seconds', [0.001, 0.005, 0.01, 0.05, + 0.1, 0.5, 1, 5, 10]) mut app := &App{ logger: logger @@ -111,11 +111,11 @@ pub fn server(conf Config) ! { job_queue: build.new_job_queue(global_ce, conf.base_image) } app.init_job_queue() or { - util.exit_with_message(1, 'Failed to inialize job queue: $err.msg()') + util.exit_with_message(1, 'Failed to inialize job queue: ${err.msg()}') } if conf.max_log_age > 0 { - go app.log_removal_daemon(log_removal_ce) + spawn app.log_removal_daemon(log_removal_ce) } web.run(app, conf.port) diff --git a/src/util/stream.v b/src/util/stream.v index 15cc618..ec4c971 100644 --- a/src/util/stream.v +++ b/src/util/stream.v @@ -51,7 +51,7 @@ pub fn reader_to_file(mut reader io.BufferedReader, length int, path string) ! { // match_array_in_array returns how many elements of a2 overlap with a1. For // example, if a1 = "abcd" & a2 = "cd", the result will be 2. If the match is // not at the end of a1, the result is 0. -pub fn match_array_in_array(a1 []T, a2 []T) int { +pub fn match_array_in_array[T](a1 []T, a2 []T) int { mut i := 0 mut match_len := 0 diff --git a/src/web/parse.v b/src/web/parse.v index 889944b..9e26f85 100644 --- a/src/web/parse.v +++ b/src/web/parse.v @@ -10,7 +10,7 @@ const attrs_to_ignore = ['auth', 'markused'] // Parsing function attributes for methods and path. fn parse_attrs(name string, attrs []string) !([]http.Method, string) { if attrs.len == 0 { - return [http.Method.get], '/$name' + return [http.Method.get], '/${name}' } mut x := attrs.clone() @@ -45,7 +45,7 @@ fn parse_attrs(name string, attrs []string) !([]http.Method, string) { methods = [http.Method.get] } if path == '' { - path = '/$name' + path = '/${name}' } // Make path lowercase for case-insensitive comparisons return methods, path.to_lower() diff --git a/src/web/response/response.v b/src/web/response/response.v index a06a589..f736f77 100644 --- a/src/web/response/response.v +++ b/src/web/response/response.v @@ -1,6 +1,6 @@ module response -pub struct Response { +pub struct Response[T] { pub: message string data T @@ -8,8 +8,8 @@ pub: // new_response constructs a new Response object with the given message // & an empty data field. -pub fn new_response(message string) Response { - return Response{ +pub fn new_response(message string) Response[string] { + return Response[string]{ message: message data: '' } @@ -17,8 +17,8 @@ pub fn new_response(message string) Response { // new_data_response constructs a new Response object with the given data // & an empty message field. -pub fn new_data_response(data T) Response { - return Response{ +pub fn new_data_response[T](data T) Response[T] { + return Response[T]{ message: '' data: data } @@ -26,8 +26,8 @@ pub fn new_data_response(data T) Response { // new_full_response constructs a new Response object with the given // message & data. -pub fn new_full_response(message string, data T) Response { - return Response{ +pub fn new_full_response[T](message string, data T) Response[T] { + return Response[T]{ message: message data: data } diff --git a/src/web/web.v b/src/web/web.v index f0f3523..300ce32 100644 --- a/src/web/web.v +++ b/src/web/web.v @@ -158,7 +158,7 @@ pub fn (mut ctx Context) body(status http.Status, content_type string, body stri } // json HTTP_OK with json_s as payload with content-type `application/json` -pub fn (mut ctx Context) json(status http.Status, j T) Result { +pub fn (mut ctx Context) json[T](status http.Status, j T) Result { ctx.status = status ctx.content_type = 'application/json' @@ -278,14 +278,14 @@ interface DbInterface { // run runs the app [manualfree] -pub fn run(global_app &T, port int) { - mut l := net.listen_tcp(.ip6, ':$port') or { panic('failed to listen $err.code() $err') } +pub fn run[T](global_app &T, port int) { + mut l := net.listen_tcp(.ip6, ':${port}') or { panic('failed to listen ${err.code()} ${err}') } // Parsing methods attributes mut routes := map[string]Route{} $for method in T.methods { http_methods, route_path := parse_attrs(method.name, method.attrs) or { - eprintln('error parsing method attributes: $err') + eprintln('error parsing method attributes: ${err}') return } @@ -294,7 +294,7 @@ pub fn run(global_app &T, port int) { path: route_path } } - println('[Vweb] Running app on http://localhost:$port') + println('[Vweb] Running app on http://localhost:${port}') for { // Create a new app object for each connection, copy global data like db connections mut request_app := &T{} @@ -311,16 +311,16 @@ pub fn run(global_app &T, port int) { request_app.Context = global_app.Context // copy the context ref that contains static files map etc mut conn := l.accept() or { // failures should not panic - eprintln('accept() failed with error: $err.msg()') + eprintln('accept() failed with error: ${err.msg()}') continue } - go handle_conn(mut conn, mut request_app, routes) + spawn handle_conn[T](mut conn, mut request_app, routes) } } // handle_conn handles a connection [manualfree] -fn handle_conn(mut conn net.TcpConn, mut app T, routes map[string]Route) { +fn handle_conn[T](mut conn net.TcpConn, mut app T, routes map[string]Route) { conn.set_read_timeout(30 * time.second) conn.set_write_timeout(30 * time.second) @@ -362,8 +362,8 @@ fn handle_conn(mut conn net.TcpConn, mut app T, routes map[string]Route) { // Request parse head := http.parse_request_head(mut reader) or { // Prevents errors from being thrown when BufferedReader is empty - if '$err' != 'none' { - eprintln('error parsing request head: $err') + if '${err}' != 'none' { + eprintln('error parsing request head: ${err}') } return } @@ -371,7 +371,7 @@ fn handle_conn(mut conn net.TcpConn, mut app T, routes map[string]Route) { // The healthcheck spams the logs, which isn't very useful if head.url != '/health' { lock app.logger { - app.logger.debug('$head.method $head.url $head.version') + app.logger.debug('${head.method} ${head.url} ${head.version}') } } @@ -385,7 +385,7 @@ fn handle_conn(mut conn net.TcpConn, mut app T, routes map[string]Route) { // URL Parse url := urllib.parse(head.url) or { - eprintln('error parsing path: $err') + eprintln('error parsing path: ${err}') return } @@ -423,7 +423,7 @@ fn handle_conn(mut conn net.TcpConn, mut app T, routes map[string]Route) { $for method in T.methods { $if method.return_type is Result { route := routes[method.name] or { - eprintln('parsed attributes for the `$method.name` are not found, skipping...') + eprintln('parsed attributes for the `${method.name}` are not found, skipping...') Route{} } @@ -455,7 +455,7 @@ fn handle_conn(mut conn net.TcpConn, mut app T, routes map[string]Route) { method_args := params.clone() if method_args.len != method.args.len { - eprintln('warning: uneven parameters count ($method.args.len) in `$method.name`, compared to the web route `$method.attrs` ($method_args.len)') + eprintln('warning: uneven parameters count (${method.args.len}) in `${method.name}`, compared to the web route `${method.attrs}` (${method_args.len})') } app.$method(method_args) return From 91a976c63425559508688c8ffbf05c268dd070e6 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 8 Feb 2023 11:09:18 +0100 Subject: [PATCH 80/97] chore: rename db module to avoid conflict with vlib --- Makefile | 2 +- src/build/queue.v | 6 +++--- src/{db/db.v => dbms/dbms.v} | 8 ++++---- src/{db => dbms}/logs.v | 2 +- src/{db => dbms}/migrations/001-initial/down.sql | 0 src/{db => dbms}/migrations/001-initial/up.sql | 0 .../migrations/002-rename-to-targets/down.sql | 0 src/{db => dbms}/migrations/002-rename-to-targets/up.sql | 0 src/{db => dbms}/migrations/003-target-url-type/down.sql | 0 src/{db => dbms}/migrations/003-target-url-type/up.sql | 0 src/{db => dbms}/migrations/004-nullable-branch/down.sql | 0 src/{db => dbms}/migrations/004-nullable-branch/up.sql | 0 src/{db => dbms}/migrations/005-repo-path/down.sql | 0 src/{db => dbms}/migrations/005-repo-path/up.sql | 0 src/{db => dbms}/targets.v | 2 +- src/{db => dbms}/targets_iter.v | 4 ++-- src/server/api_logs.v | 1 - src/server/api_targets.v | 1 - src/server/server.v | 6 +++--- 19 files changed, 15 insertions(+), 17 deletions(-) rename src/{db/db.v => dbms/dbms.v} (94%) rename src/{db => dbms}/logs.v (99%) rename src/{db => dbms}/migrations/001-initial/down.sql (100%) rename src/{db => dbms}/migrations/001-initial/up.sql (100%) rename src/{db => dbms}/migrations/002-rename-to-targets/down.sql (100%) rename src/{db => dbms}/migrations/002-rename-to-targets/up.sql (100%) rename src/{db => dbms}/migrations/003-target-url-type/down.sql (100%) rename src/{db => dbms}/migrations/003-target-url-type/up.sql (100%) rename src/{db => dbms}/migrations/004-nullable-branch/down.sql (100%) rename src/{db => dbms}/migrations/004-nullable-branch/up.sql (100%) rename src/{db => dbms}/migrations/005-repo-path/down.sql (100%) rename src/{db => dbms}/migrations/005-repo-path/up.sql (100%) rename src/{db => dbms}/targets.v (99%) rename src/{db => dbms}/targets_iter.v (99%) diff --git a/Makefile b/Makefile index 1521676..7dda68c 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ SRC_DIR := src SRCS != find '$(SRC_DIR)' -iname '*.v' V_PATH ?= v -V := $(V_PATH) -showcc -gc boehm -W -d use_openssl -skip-unused +V := $(V_PATH) -showcc -gc boehm -d use_openssl -skip-unused all: vieter diff --git a/src/build/queue.v b/src/build/queue.v index 2aa6e7a..73068ac 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -143,7 +143,7 @@ pub fn (mut q BuildJobQueue) peek(arch string) ?BuildJob { } q.pop_invalid(arch) - job := q.queues[arch].peek()? + job := q.queues[arch].peek() or { return none } if job.timestamp < time.now() { return job @@ -162,10 +162,10 @@ pub fn (mut q BuildJobQueue) pop(arch string) ?BuildJob { } q.pop_invalid(arch) - mut job := q.queues[arch].peek()? + mut job := q.queues[arch].peek() or { return none } if job.timestamp < time.now() { - job = q.queues[arch].pop()? + job = q.queues[arch].pop() or { return none } if !job.single { q.reschedule(job, arch) diff --git a/src/db/db.v b/src/dbms/dbms.v similarity index 94% rename from src/db/db.v rename to src/dbms/dbms.v index 73a5e83..686bb52 100644 --- a/src/db/db.v +++ b/src/dbms/dbms.v @@ -1,6 +1,6 @@ -module db +module dbms -import sqlite +import db.sqlite import time pub struct VieterDb { @@ -49,8 +49,8 @@ pub fn init(db_path string) !VieterDb { } // Apply each migration in order - for i in cur_version.version .. db.migrations_up.len { - migration := db.migrations_up[i].to_string() + for i in cur_version.version .. dbms.migrations_up.len { + migration := dbms.migrations_up[i].to_string() version_num := i + 1 diff --git a/src/db/logs.v b/src/dbms/logs.v similarity index 99% rename from src/db/logs.v rename to src/dbms/logs.v index 0321183..b0786b8 100644 --- a/src/db/logs.v +++ b/src/dbms/logs.v @@ -1,4 +1,4 @@ -module db +module dbms import models { BuildLog, BuildLogFilter } import time diff --git a/src/db/migrations/001-initial/down.sql b/src/dbms/migrations/001-initial/down.sql similarity index 100% rename from src/db/migrations/001-initial/down.sql rename to src/dbms/migrations/001-initial/down.sql diff --git a/src/db/migrations/001-initial/up.sql b/src/dbms/migrations/001-initial/up.sql similarity index 100% rename from src/db/migrations/001-initial/up.sql rename to src/dbms/migrations/001-initial/up.sql diff --git a/src/db/migrations/002-rename-to-targets/down.sql b/src/dbms/migrations/002-rename-to-targets/down.sql similarity index 100% rename from src/db/migrations/002-rename-to-targets/down.sql rename to src/dbms/migrations/002-rename-to-targets/down.sql diff --git a/src/db/migrations/002-rename-to-targets/up.sql b/src/dbms/migrations/002-rename-to-targets/up.sql similarity index 100% rename from src/db/migrations/002-rename-to-targets/up.sql rename to src/dbms/migrations/002-rename-to-targets/up.sql diff --git a/src/db/migrations/003-target-url-type/down.sql b/src/dbms/migrations/003-target-url-type/down.sql similarity index 100% rename from src/db/migrations/003-target-url-type/down.sql rename to src/dbms/migrations/003-target-url-type/down.sql diff --git a/src/db/migrations/003-target-url-type/up.sql b/src/dbms/migrations/003-target-url-type/up.sql similarity index 100% rename from src/db/migrations/003-target-url-type/up.sql rename to src/dbms/migrations/003-target-url-type/up.sql diff --git a/src/db/migrations/004-nullable-branch/down.sql b/src/dbms/migrations/004-nullable-branch/down.sql similarity index 100% rename from src/db/migrations/004-nullable-branch/down.sql rename to src/dbms/migrations/004-nullable-branch/down.sql diff --git a/src/db/migrations/004-nullable-branch/up.sql b/src/dbms/migrations/004-nullable-branch/up.sql similarity index 100% rename from src/db/migrations/004-nullable-branch/up.sql rename to src/dbms/migrations/004-nullable-branch/up.sql diff --git a/src/db/migrations/005-repo-path/down.sql b/src/dbms/migrations/005-repo-path/down.sql similarity index 100% rename from src/db/migrations/005-repo-path/down.sql rename to src/dbms/migrations/005-repo-path/down.sql diff --git a/src/db/migrations/005-repo-path/up.sql b/src/dbms/migrations/005-repo-path/up.sql similarity index 100% rename from src/db/migrations/005-repo-path/up.sql rename to src/dbms/migrations/005-repo-path/up.sql diff --git a/src/db/targets.v b/src/dbms/targets.v similarity index 99% rename from src/db/targets.v rename to src/dbms/targets.v index 35ee270..a55220f 100644 --- a/src/db/targets.v +++ b/src/dbms/targets.v @@ -1,4 +1,4 @@ -module db +module dbms import models { Target, TargetArch } diff --git a/src/db/targets_iter.v b/src/dbms/targets_iter.v similarity index 99% rename from src/db/targets_iter.v rename to src/dbms/targets_iter.v index 081de1f..ca149b9 100644 --- a/src/db/targets_iter.v +++ b/src/dbms/targets_iter.v @@ -1,7 +1,7 @@ -module db +module dbms import models { Target, TargetFilter } -import sqlite +import db.sqlite // Iterator providing a filtered view into the list of targets currently stored // in the database. It replaces functionality usually performed in the database diff --git a/src/server/api_logs.v b/src/server/api_logs.v index 3e210b0..00a7e2e 100644 --- a/src/server/api_logs.v +++ b/src/server/api_logs.v @@ -3,7 +3,6 @@ module server import web import net.urllib import web.response { new_data_response, new_response } -import db import time import os import util diff --git a/src/server/api_targets.v b/src/server/api_targets.v index a8fdf37..ed121d9 100644 --- a/src/server/api_targets.v +++ b/src/server/api_targets.v @@ -2,7 +2,6 @@ module server import web import web.response { new_data_response, new_response } -import db import models { Target, TargetArch, TargetFilter } // v1_get_targets returns the current list of targets. diff --git a/src/server/server.v b/src/server/server.v index 79d93e2..e1fa0d7 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -5,7 +5,7 @@ import os import log import repo import util -import db +import dbms import build { BuildJobQueue } import cron import metrics @@ -25,7 +25,7 @@ pub mut: repo repo.RepoGroupManager [required; web_global] // Keys are the various architectures for packages job_queue BuildJobQueue [required; web_global] - db db.VieterDb + db dbms.VieterDb } // init_job_queue populates a fresh job queue with all the targets currently @@ -88,7 +88,7 @@ pub fn server(conf Config) ! { } db_file := os.join_path_single(conf.data_dir, server.db_file_name) - db := db.init(db_file) or { + db := dbms.init(db_file) or { util.exit_with_message(1, 'Failed to initialize database: ${err.msg()}') } From b9598ca046980e9cc2dbec2545e7ffef3c20cbdd Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 8 Feb 2023 11:11:28 +0100 Subject: [PATCH 81/97] chore: rewrite docstrings with generics --- src/dbms/dbms.v | 2 +- src/models/models.v | 6 +++--- src/util/stream.v | 2 +- src/web/response/response.v | 4 ++-- src/web/web.v | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/dbms/dbms.v b/src/dbms/dbms.v index 686bb52..e5676ab 100644 --- a/src/dbms/dbms.v +++ b/src/dbms/dbms.v @@ -80,7 +80,7 @@ pub fn init(db_path string) !VieterDb { } } -// row_into converts an sqlite.Row into a given type T by parsing each field +// row_into[T] converts an sqlite.Row into a given type T by parsing each field // from a string according to its type. pub fn row_into[T](row sqlite.Row) T { mut i := 0 diff --git a/src/models/models.v b/src/models/models.v index 9111286..1ed0da8 100644 --- a/src/models/models.v +++ b/src/models/models.v @@ -2,7 +2,7 @@ module models import time -// from_params creates a new instance of T from the given map by parsing all +// from_params[T] creates a new instance of T from the given map by parsing all // of its fields from the map. pub fn from_params[T](params map[string]string) ?T { mut o := T{} @@ -12,7 +12,7 @@ pub fn from_params[T](params map[string]string) ?T { return o } -// patch_from_params updates the given T object with the params defined in +// patch_from_params[T] updates the given T object with the params defined in // the map. pub fn patch_from_params[T](mut o T, params map[string]string) ? { $for field in T.fields { @@ -36,7 +36,7 @@ pub fn patch_from_params[T](mut o T, params map[string]string) ? { } } -// params_from converts a given T struct into a map of strings. +// params_from[T] converts a given T struct into a map of strings. pub fn params_from[T](o &T) map[string]string { mut out := map[string]string{} diff --git a/src/util/stream.v b/src/util/stream.v index ec4c971..4b362fc 100644 --- a/src/util/stream.v +++ b/src/util/stream.v @@ -48,7 +48,7 @@ pub fn reader_to_file(mut reader io.BufferedReader, length int, path string) ! { } } -// match_array_in_array returns how many elements of a2 overlap with a1. For +// match_array_in_array[T] returns how many elements of a2 overlap with a1. For // example, if a1 = "abcd" & a2 = "cd", the result will be 2. If the match is // not at the end of a1, the result is 0. pub fn match_array_in_array[T](a1 []T, a2 []T) int { diff --git a/src/web/response/response.v b/src/web/response/response.v index f736f77..c1475ff 100644 --- a/src/web/response/response.v +++ b/src/web/response/response.v @@ -15,7 +15,7 @@ pub fn new_response(message string) Response[string] { } } -// new_data_response constructs a new Response object with the given data +// new_data_response[T] constructs a new Response object with the given data // & an empty message field. pub fn new_data_response[T](data T) Response[T] { return Response[T]{ @@ -24,7 +24,7 @@ pub fn new_data_response[T](data T) Response[T] { } } -// new_full_response constructs a new Response object with the given +// new_full_response[T] constructs a new Response object with the given // message & data. pub fn new_full_response[T](message string, data T) Response[T] { return Response[T]{ diff --git a/src/web/web.v b/src/web/web.v index 300ce32..54801f7 100644 --- a/src/web/web.v +++ b/src/web/web.v @@ -157,7 +157,7 @@ pub fn (mut ctx Context) body(status http.Status, content_type string, body stri return Result{} } -// json HTTP_OK with json_s as payload with content-type `application/json` +// json[T] HTTP_OK with json_s as payload with content-type `application/json` pub fn (mut ctx Context) json[T](status http.Status, j T) Result { ctx.status = status ctx.content_type = 'application/json' From 8a0842390789eedcdeeace2f2713ce40ff40b2ee Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 8 Feb 2023 11:15:41 +0100 Subject: [PATCH 82/97] chore(ci): update V images --- .woodpecker/build.yml | 2 +- .woodpecker/docs.yml | 2 +- .woodpecker/gitea.yml | 2 +- .woodpecker/lint.yml | 2 +- .woodpecker/man.yml | 2 +- .woodpecker/test.yml | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.woodpecker/build.yml b/.woodpecker/build.yml index 0785392..e431081 100644 --- a/.woodpecker/build.yml +++ b/.woodpecker/build.yml @@ -1,5 +1,5 @@ variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2-alpine3.17' + - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.3-alpine3.17' matrix: PLATFORM: diff --git a/.woodpecker/docs.yml b/.woodpecker/docs.yml index e51f3d7..6561538 100644 --- a/.woodpecker/docs.yml +++ b/.woodpecker/docs.yml @@ -1,5 +1,5 @@ variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2-alpine3.17' + - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.3-alpine3.17' platform: 'linux/amd64' branches: diff --git a/.woodpecker/gitea.yml b/.woodpecker/gitea.yml index cff0eb9..8d36f8e 100644 --- a/.woodpecker/gitea.yml +++ b/.woodpecker/gitea.yml @@ -1,5 +1,5 @@ variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2-alpine3.17' + - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.3-alpine3.17' platform: 'linux/amd64' branches: [ 'main' ] diff --git a/.woodpecker/lint.yml b/.woodpecker/lint.yml index 1babcbc..76df634 100644 --- a/.woodpecker/lint.yml +++ b/.woodpecker/lint.yml @@ -1,5 +1,5 @@ variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2-alpine3.17' + - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.3-alpine3.17' # These checks already get performed on the feature branches branches: diff --git a/.woodpecker/man.yml b/.woodpecker/man.yml index 9ad8dcf..486a511 100644 --- a/.woodpecker/man.yml +++ b/.woodpecker/man.yml @@ -1,5 +1,5 @@ variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2-alpine3.17' + - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.3-alpine3.17' platform: 'linux/amd64' branches: diff --git a/.woodpecker/test.yml b/.woodpecker/test.yml index 91ef7c6..b742e08 100644 --- a/.woodpecker/test.yml +++ b/.woodpecker/test.yml @@ -1,5 +1,5 @@ variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.2-alpine3.17' + - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.3-alpine3.17' matrix: PLATFORM: From bff817ccd96fa6d920661705b915ce119212c8ae Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 8 Feb 2023 11:30:38 +0100 Subject: [PATCH 83/97] fix: add temporary fix to compile with V 0.3.3 --- src/openssl.c.v | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/openssl.c.v diff --git a/src/openssl.c.v b/src/openssl.c.v new file mode 100644 index 0000000..bff8c54 --- /dev/null +++ b/src/openssl.c.v @@ -0,0 +1,5 @@ +// With V 0.3.3, Vieter fails to compile without this fix, as provided by +// spytheman. It will get fixed in V itself, but this temporary fix allows me +// to stay on V 0.3.3. +[typedef] +pub struct C.SSL_CTX{} From 4dc82515f444e900d45747ebfcef7112eaadc4d4 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 15 Feb 2023 16:24:07 +0100 Subject: [PATCH 84/97] chore: stop shadowing import names with variables --- src/agent/cli.v | 4 +-- src/console/aur/aur.v | 4 +-- src/console/logs/logs.v | 32 ++++++++++++------------ src/console/repos/repos.v | 10 ++++---- src/console/targets/targets.v | 46 +++++++++++++++++------------------ src/openssl.c.v | 5 ---- src/server/cli.v | 4 +-- src/server/server.v | 4 +-- 8 files changed, 52 insertions(+), 57 deletions(-) delete mode 100644 src/openssl.c.v diff --git a/src/agent/cli.v b/src/agent/cli.v index 41e3421..2dee8d6 100644 --- a/src/agent/cli.v +++ b/src/agent/cli.v @@ -23,9 +23,9 @@ pub fn cmd() cli.Command { description: 'Start an agent daemon.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! + conf_ := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! - agent(conf)! + agent(conf_)! } } } diff --git a/src/console/aur/aur.v b/src/console/aur/aur.v index 6fc8513..c1c409c 100644 --- a/src/console/aur/aur.v +++ b/src/console/aur/aur.v @@ -36,12 +36,12 @@ pub fn cmd() cli.Command { required_args: 2 execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! + conf_ := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! c := aur.new() pkgs := c.info(cmd.args[1..])! - vc := client.new(conf.address, conf.api_key) + vc := client.new(conf_.address, conf_.api_key) for pkg in pkgs { vc.add_target( diff --git a/src/console/logs/logs.v b/src/console/logs/logs.v index 518b507..b8e088c 100644 --- a/src/console/logs/logs.v +++ b/src/console/logs/logs.v @@ -74,7 +74,7 @@ pub fn cmd() cli.Command { ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! + conf_ := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! mut filter := BuildLogFilter{} @@ -146,7 +146,7 @@ pub fn cmd() cli.Command { raw := cmd.flags.get_bool('raw')! - list(conf, filter, raw)! + list(conf_, filter, raw)! } }, cli.Command{ @@ -156,9 +156,9 @@ pub fn cmd() cli.Command { description: 'Remove a build log that matches the given id.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! + conf_ := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! - remove(conf, cmd.args[0])! + remove(conf_, cmd.args[0])! } }, cli.Command{ @@ -168,10 +168,10 @@ pub fn cmd() cli.Command { description: 'Show all info for a specific build log.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! + conf_ := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! id := cmd.args[0].int() - info(conf, id)! + info(conf_, id)! } }, cli.Command{ @@ -181,10 +181,10 @@ pub fn cmd() cli.Command { description: 'Output the content of a build log to stdout.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! + conf_ := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! id := cmd.args[0].int() - content(conf, id)! + content(conf_, id)! } }, ] @@ -204,16 +204,16 @@ fn print_log_list(logs []BuildLog, raw bool) ! { } // list prints a list of all build logs. -fn list(conf Config, filter BuildLogFilter, raw bool) ! { - c := client.new(conf.address, conf.api_key) +fn list(conf_ Config, filter BuildLogFilter, raw bool) ! { + c := client.new(conf_.address, conf_.api_key) logs := c.get_build_logs(filter)! print_log_list(logs, raw)! } // info print the detailed info for a given build log. -fn info(conf Config, id int) ! { - c := client.new(conf.address, conf.api_key) +fn info(conf_ Config, id int) ! { + c := client.new(conf_.address, conf_.api_key) log := c.get_build_log(id)! print(log) @@ -221,15 +221,15 @@ fn info(conf Config, id int) ! { // content outputs the contents of the log file for a given build log to // stdout. -fn content(conf Config, id int) ! { - c := client.new(conf.address, conf.api_key) +fn content(conf_ Config, id int) ! { + c := client.new(conf_.address, conf_.api_key) content := c.get_build_log_content(id)! println(content) } // remove removes a build log from the server's list. -fn remove(conf Config, id string) ! { - c := client.new(conf.address, conf.api_key) +fn remove(conf_ Config, id string) ! { + c := client.new(conf_.address, conf_.api_key) c.remove_build_log(id.int())! } diff --git a/src/console/repos/repos.v b/src/console/repos/repos.v index 0021d52..3779d33 100644 --- a/src/console/repos/repos.v +++ b/src/console/repos/repos.v @@ -28,7 +28,7 @@ pub fn cmd() cli.Command { ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! + conf_ := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! if cmd.args.len < 3 { if !cmd.flags.get_bool('force')! { @@ -36,14 +36,14 @@ pub fn cmd() cli.Command { } } - client := client.new(conf.address, conf.api_key) + client_ := client.new(conf_.address, conf_.api_key) if cmd.args.len == 1 { - client.remove_repo(cmd.args[0])! + client_.remove_repo(cmd.args[0])! } else if cmd.args.len == 2 { - client.remove_arch_repo(cmd.args[0], cmd.args[1])! + client_.remove_arch_repo(cmd.args[0], cmd.args[1])! } else { - client.remove_package(cmd.args[0], cmd.args[1], cmd.args[2])! + client_.remove_package(cmd.args[0], cmd.args[1], cmd.args[2])! } } }, diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index 1b8d4be..d1dfbe3 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -54,7 +54,7 @@ pub fn cmd() cli.Command { ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! + conf_ := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! mut filter := TargetFilter{} @@ -85,7 +85,7 @@ pub fn cmd() cli.Command { raw := cmd.flags.get_bool('raw')! - list(conf, filter, raw)! + list(conf_, filter, raw)! } }, cli.Command{ @@ -113,7 +113,7 @@ pub fn cmd() cli.Command { ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! + conf_ := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! t := NewTarget{ kind: cmd.flags.get_string('kind')! @@ -125,7 +125,7 @@ pub fn cmd() cli.Command { raw := cmd.flags.get_bool('raw')! - add(conf, t, raw)! + add(conf_, t, raw)! } }, cli.Command{ @@ -135,9 +135,9 @@ pub fn cmd() cli.Command { description: 'Remove a target that matches the given id.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! + conf_ := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! - remove(conf, cmd.args[0])! + remove(conf_, cmd.args[0])! } }, cli.Command{ @@ -147,9 +147,9 @@ pub fn cmd() cli.Command { description: 'Show detailed information for the target matching the id.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! + conf_ := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! - info(conf, cmd.args[0])! + info(conf_, cmd.args[0])! } }, cli.Command{ @@ -196,7 +196,7 @@ pub fn cmd() cli.Command { ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! + conf_ := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! found := cmd.flags.get_all_found() @@ -208,7 +208,7 @@ pub fn cmd() cli.Command { } } - patch(conf, cmd.args[0], params)! + patch(conf_, cmd.args[0], params)! } }, cli.Command{ @@ -235,7 +235,7 @@ pub fn cmd() cli.Command { ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! + conf_ := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! remote := cmd.flags.get_bool('remote')! force := cmd.flags.get_bool('force')! @@ -248,10 +248,10 @@ pub fn cmd() cli.Command { return error('When scheduling the build remotely, you have to specify an architecture.') } - c := client.new(conf.address, conf.api_key) + c := client.new(conf_.address, conf_.api_key) c.queue_job(target_id, arch, force)! } else { - build(conf, target_id, force)! + build(conf_, target_id, force)! } } }, @@ -260,8 +260,8 @@ pub fn cmd() cli.Command { } // list prints out a list of all repositories. -fn list(conf Config, filter TargetFilter, raw bool) ! { - c := client.new(conf.address, conf.api_key) +fn list(conf_ Config, filter TargetFilter, raw bool) ! { + c := client.new(conf_.address, conf_.api_key) targets := c.get_targets(filter)! data := targets.map([it.id.str(), it.kind, it.url, it.repo]) @@ -273,8 +273,8 @@ fn list(conf Config, filter TargetFilter, raw bool) ! { } // add adds a new target to the server's list. -fn add(conf Config, t &NewTarget, raw bool) ! { - c := client.new(conf.address, conf.api_key) +fn add(conf_ Config, t &NewTarget, raw bool) ! { + c := client.new(conf_.address, conf_.api_key) target_id := c.add_target(t)! if raw { @@ -285,13 +285,13 @@ fn add(conf Config, t &NewTarget, raw bool) ! { } // remove removes a target from the server's list. -fn remove(conf Config, id string) ! { - c := client.new(conf.address, conf.api_key) +fn remove(conf_ Config, id string) ! { + c := client.new(conf_.address, conf_.api_key) c.remove_target(id.int())! } // patch patches a given target with the provided params. -fn patch(conf Config, id string, params map[string]string) ! { +fn patch(conf_ Config, id string, params map[string]string) ! { // We check the cron expression first because it's useless to send an // invalid one to the server. if 'schedule' in params && params['schedule'] != '' { @@ -300,13 +300,13 @@ fn patch(conf Config, id string, params map[string]string) ! { } } - c := client.new(conf.address, conf.api_key) + c := client.new(conf_.address, conf_.api_key) c.patch_target(id.int(), params)! } // info shows detailed information for a given target. -fn info(conf Config, id string) ! { - c := client.new(conf.address, conf.api_key) +fn info(conf_ Config, id string) ! { + c := client.new(conf_.address, conf_.api_key) target := c.get_target(id.int())! println(target) } diff --git a/src/openssl.c.v b/src/openssl.c.v deleted file mode 100644 index bff8c54..0000000 --- a/src/openssl.c.v +++ /dev/null @@ -1,5 +0,0 @@ -// With V 0.3.3, Vieter fails to compile without this fix, as provided by -// spytheman. It will get fixed in V itself, but this temporary fix allows me -// to stay on V 0.3.3. -[typedef] -pub struct C.SSL_CTX{} diff --git a/src/server/cli.v b/src/server/cli.v index c272d52..08ad5f8 100644 --- a/src/server/cli.v +++ b/src/server/cli.v @@ -25,9 +25,9 @@ pub fn cmd() cli.Command { description: 'Start the Vieter server.' execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! - conf := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! + conf_ := vconf.load[Config](prefix: 'VIETER_', default_path: config_file)! - server(conf)! + server(conf_)! } } } diff --git a/src/server/server.v b/src/server/server.v index e1fa0d7..4cccb27 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -82,7 +82,7 @@ pub fn server(conf Config) ! { repo_dir := os.join_path_single(conf.data_dir, server.repo_dir_name) // This also creates the directories if needed - repo := repo.new(repo_dir, conf.pkg_dir, conf.default_arch) or { + repo_ := repo.new(repo_dir, conf.pkg_dir, conf.default_arch) or { logger.error(err.msg()) exit(1) } @@ -105,7 +105,7 @@ pub fn server(conf Config) ! { logger: logger api_key: conf.api_key conf: conf - repo: repo + repo: repo_ db: db collector: collector job_queue: build.new_job_queue(global_ce, conf.base_image) From 455f3b5f4118484b795f36ffa6be2e3c8b9a3017 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 15 Feb 2023 20:07:07 +0100 Subject: [PATCH 85/97] fix: compile with selected V version --- src/console/targets/build.v | 2 +- src/console/targets/targets.v | 2 +- src/repo/remove.v | 4 ++-- src/server/repo.v | 18 +++++++++--------- src/web/web.v | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/console/targets/build.v b/src/console/targets/build.v index b8cbe7f..a59e6a1 100644 --- a/src/console/targets/build.v +++ b/src/console/targets/build.v @@ -6,7 +6,7 @@ import os import build // build locally builds the target with the given id. -fn build(conf Config, target_id int, force bool) ! { +fn build_target(conf Config, target_id int, force bool) ! { c := client.new(conf.address, conf.api_key) target := c.get_target(target_id)! diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index d1dfbe3..676fa0a 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -251,7 +251,7 @@ pub fn cmd() cli.Command { c := client.new(conf_.address, conf_.api_key) c.queue_job(target_id, arch, force)! } else { - build(conf_, target_id, force)! + build_target(conf_, target_id, force)! } } }, diff --git a/src/repo/remove.v b/src/repo/remove.v index 63866a9..6d949c3 100644 --- a/src/repo/remove.v +++ b/src/repo/remove.v @@ -5,7 +5,7 @@ import os // remove_pkg_from_arch_repo removes a package from an arch-repo's database. It // returns false if the package wasn't present in the database. It also // optionally re-syncs the repo archives. -pub fn (r &RepoGroupManager) remove_pkg_from_arch_repo(repo string, arch string, pkg_name string, sync bool) !bool { +pub fn (r &RepoGroupManager) remove_pkg_from_arch_repo(repo string, arch string, pkg_name string, perform_sync bool) !bool { repo_dir := os.join_path(r.repos_dir, repo, arch) // If the repository doesn't exist yet, the result is automatically false @@ -39,7 +39,7 @@ pub fn (r &RepoGroupManager) remove_pkg_from_arch_repo(repo string, arch string, } // Sync the db archives if requested - if sync { + if perform_sync { r.sync(repo, arch)! } diff --git a/src/server/repo.v b/src/server/repo.v index 724d9d0..051a7c2 100644 --- a/src/server/repo.v +++ b/src/server/repo.v @@ -19,15 +19,15 @@ pub fn (mut app App) healthcheck() web.Result { // repository's archives, but also package archives or the contents of a // package's desc file. ['/:repo/:arch/:filename'; get; head; markused] -fn (mut app App) get_repo_file(repo string, arch string, filename string) web.Result { +fn (mut app App) get_repo_file(repo_ string, arch string, filename string) web.Result { mut full_path := '' db_exts := ['.db', '.files', '.db.tar.gz', '.files.tar.gz'] // There's no point in having the ability to serve db archives with wrong // filenames - if db_exts.any(filename == '${repo}${it}') { - full_path = os.join_path(app.repo.repos_dir, repo, arch, filename) + if db_exts.any(filename == '${repo_}${it}') { + full_path = os.join_path(app.repo.repos_dir, repo_, arch, filename) // repo-add does this using symlinks, but we just change the requested // path @@ -35,13 +35,13 @@ fn (mut app App) get_repo_file(repo string, arch string, filename string) web.Re full_path += '.tar.gz' } } else if filename.contains('.pkg') { - full_path = os.join_path(app.repo.pkg_dir, repo, arch, filename) + full_path = os.join_path(app.repo.pkg_dir, repo_, arch, filename) } // Default behavior is to return the desc file for the package, if present. // This can then also be used by the build system to properly check whether // a package is present in an arch-repo. else { - full_path = os.join_path(app.repo.repos_dir, repo, arch, filename, 'desc') + full_path = os.join_path(app.repo.repos_dir, repo_, arch, filename, 'desc') } return app.file(full_path) @@ -49,10 +49,10 @@ fn (mut app App) get_repo_file(repo string, arch string, filename string) web.Re // put_package handles publishing a package to a repository. ['/:repo/publish'; auth; markused; post] -fn (mut app App) put_package(repo string) web.Result { +fn (mut app App) put_package(repo_ string) web.Result { // api is a reserved keyword for api routes & should never be allowed to be // a repository. - if repo.to_lower() == 'api' { + if repo_.to_lower() == 'api' { return app.json(.bad_request, new_response("'api' is a reserved keyword & cannot be used as a repository name.")) } @@ -82,7 +82,7 @@ fn (mut app App) put_package(repo string) web.Result { return app.status(.length_required) } - res := app.repo.add_pkg_from_path(repo, pkg_path) or { + res := app.repo.add_pkg_from_path(repo_, pkg_path) or { app.lerror('Error while adding package: ${err.msg()}') os.rm(pkg_path) or { app.lerror("Failed to remove download '${pkg_path}': ${err.msg()}") } @@ -90,7 +90,7 @@ fn (mut app App) put_package(repo string) web.Result { return app.status(.internal_server_error) } - app.linfo("Added '${res.name}-${res.version}' to '${repo} (${res.archs.join(',')})'.") + app.linfo("Added '${res.name}-${res.version}' to '${repo_} (${res.archs.join(',')})'.") return app.json(.ok, new_data_response(res)) } diff --git a/src/web/web.v b/src/web/web.v index 54801f7..5c612f3 100644 --- a/src/web/web.v +++ b/src/web/web.v @@ -44,7 +44,7 @@ pub mut: // Files from multipart-form. files map[string][]http.FileData // Allows reading the request body - reader io.BufferedReader + reader &io.BufferedReader = unsafe { nil } // RESPONSE status http.Status = http.Status.ok content_type string = 'text/plain' From a3a83a94ae2d6fce4258d73851e441085b160c5c Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 15 Feb 2023 20:50:55 +0100 Subject: [PATCH 86/97] chore(ci): update CI to new Vlang version --- .woodpecker/build.yml | 2 +- .woodpecker/docs.yml | 2 +- .woodpecker/gitea.yml | 2 +- .woodpecker/lint.yml | 2 +- .woodpecker/man.yml | 2 +- .woodpecker/test.yml | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.woodpecker/build.yml b/.woodpecker/build.yml index e431081..e288bb2 100644 --- a/.woodpecker/build.yml +++ b/.woodpecker/build.yml @@ -1,5 +1,5 @@ variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.3-alpine3.17' + - &vlang_image 'git.rustybever.be/vieter/vlang:5d4c9dc9fc11bf8648541c934adb64f27cb94e37-alpine3.17' matrix: PLATFORM: diff --git a/.woodpecker/docs.yml b/.woodpecker/docs.yml index 6561538..98f5060 100644 --- a/.woodpecker/docs.yml +++ b/.woodpecker/docs.yml @@ -1,5 +1,5 @@ variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.3-alpine3.17' + - &vlang_image 'git.rustybever.be/vieter/vlang:5d4c9dc9fc11bf8648541c934adb64f27cb94e37-alpine3.17' platform: 'linux/amd64' branches: diff --git a/.woodpecker/gitea.yml b/.woodpecker/gitea.yml index 8d36f8e..6079b76 100644 --- a/.woodpecker/gitea.yml +++ b/.woodpecker/gitea.yml @@ -1,5 +1,5 @@ variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.3-alpine3.17' + - &vlang_image 'git.rustybever.be/vieter/vlang:5d4c9dc9fc11bf8648541c934adb64f27cb94e37-alpine3.17' platform: 'linux/amd64' branches: [ 'main' ] diff --git a/.woodpecker/lint.yml b/.woodpecker/lint.yml index 76df634..39918a9 100644 --- a/.woodpecker/lint.yml +++ b/.woodpecker/lint.yml @@ -1,5 +1,5 @@ variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.3-alpine3.17' + - &vlang_image 'git.rustybever.be/vieter/vlang:5d4c9dc9fc11bf8648541c934adb64f27cb94e37-alpine3.17' # These checks already get performed on the feature branches branches: diff --git a/.woodpecker/man.yml b/.woodpecker/man.yml index 486a511..23330f3 100644 --- a/.woodpecker/man.yml +++ b/.woodpecker/man.yml @@ -1,5 +1,5 @@ variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.3-alpine3.17' + - &vlang_image 'git.rustybever.be/vieter/vlang:5d4c9dc9fc11bf8648541c934adb64f27cb94e37-alpine3.17' platform: 'linux/amd64' branches: diff --git a/.woodpecker/test.yml b/.woodpecker/test.yml index b742e08..ba93957 100644 --- a/.woodpecker/test.yml +++ b/.woodpecker/test.yml @@ -1,5 +1,5 @@ variables: - - &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.3-alpine3.17' + - &vlang_image 'git.rustybever.be/vieter/vlang:5d4c9dc9fc11bf8648541c934adb64f27cb94e37-alpine3.17' matrix: PLATFORM: From beae2cebd26f719197e7727ce513e636ba36b336 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 15 Feb 2023 21:02:50 +0100 Subject: [PATCH 87/97] fix(ci): lock slate version --- .woodpecker/docs.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.woodpecker/docs.yml b/.woodpecker/docs.yml index 98f5060..c7ecd59 100644 --- a/.woodpecker/docs.yml +++ b/.woodpecker/docs.yml @@ -21,8 +21,9 @@ pipeline: - make api-docs slate-docs: - image: 'slatedocs/slate' + image: 'slatedocs/slate:v2.13.0' group: 'generate' + # Slate requires a specific directory to run in commands: - cd docs/api - bundle exec middleman build --clean From f423dcf26bac36d80bd7916cd7494f590a8534ab Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sun, 19 Feb 2023 15:54:31 +0100 Subject: [PATCH 88/97] chore: updated PKGBUILDs --- .woodpecker/arch-rel.yml | 1 + .woodpecker/arch.yml | 1 + CHANGELOG.md | 1 + PKGBUILD | 2 +- PKGBUILD.dev | 2 +- README.md | 6 +++--- 6 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.woodpecker/arch-rel.yml b/.woodpecker/arch-rel.yml index f727486..0cdf91d 100644 --- a/.woodpecker/arch-rel.yml +++ b/.woodpecker/arch-rel.yml @@ -10,6 +10,7 @@ skip_clone: true pipeline: build: image: 'git.rustybever.be/vieter-v/vieter-builder' + pull: true commands: # Add the vieter repository so we can use the compiler - echo -e '[vieter]\nServer = https://arch.r8r.be/$repo/$arch\nSigLevel = Optional' >> /etc/pacman.conf diff --git a/.woodpecker/arch.yml b/.woodpecker/arch.yml index f5f8432..7295065 100644 --- a/.woodpecker/arch.yml +++ b/.woodpecker/arch.yml @@ -10,6 +10,7 @@ skip_clone: true pipeline: build: image: 'git.rustybever.be/vieter-v/vieter-builder' + pull: true commands: # Add the vieter repository so we can use the compiler - echo -e '[vieter]\nServer = https://arch.r8r.be/$repo/$arch\nSigLevel = Optional' >> /etc/pacman.conf diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c572bf..a95d6c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed * Rewrote cron expression logic in C +* Updated codebase to V commit after 0.3.3 ### Removed diff --git a/PKGBUILD b/PKGBUILD index bf9c621..7438390 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -7,7 +7,7 @@ pkgver='0.5.0' pkgrel=1 pkgdesc="Lightweight Arch repository server & package build system" depends=('glibc' 'openssl' 'libarchive' 'sqlite') -makedepends=('git' 'vlang') +makedepends=('git' 'vieter-vlang') arch=('x86_64' 'aarch64') url='https://git.rustybever.be/vieter-v/vieter' license=('AGPL3') diff --git a/PKGBUILD.dev b/PKGBUILD.dev index b07585a..4ea213d 100644 --- a/PKGBUILD.dev +++ b/PKGBUILD.dev @@ -7,7 +7,7 @@ pkgver=0.2.0.r25.g20112b8 pkgrel=1 pkgdesc="Lightweight Arch repository server & package build system (development version)" depends=('glibc' 'openssl' 'libarchive' 'sqlite') -makedepends=('git' 'vlang') +makedepends=('git' 'vieter-vlang') arch=('x86_64' 'aarch64') url='https://git.rustybever.be/vieter-v/vieter' license=('AGPL3') diff --git a/README.md b/README.md index 637d4c1..6b487b6 100644 --- a/README.md +++ b/README.md @@ -48,9 +48,9 @@ update`. ### Compiler -I used to maintain a mirror that tracked the latest master, but nowadays, I -maintain a Docker image containing the specific compiler version that Vieter -builds with. Currently, this is V 0.3.2. +V is developed using a specific compiler commit that is usually updated +whenever a new version is released. Information on this can be found in the +[tools](https://git.rustybever.be/vieter-v/tools) repository. ## Contributing From 3b24ad0f2c06908661e1b8ea595f0f98716f0f42 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sun, 19 Feb 2023 16:38:53 +0100 Subject: [PATCH 89/97] fix(web): don't log new metric for every query param --- src/web/web.v | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/web/web.v b/src/web/web.v index 5c612f3..775354a 100644 --- a/src/web/web.v +++ b/src/web/web.v @@ -332,9 +332,10 @@ fn handle_conn[T](mut conn net.TcpConn, mut app T, routes map[string]Route) { } // Record how long request took to process + path := urllib.parse(app.req.url) or { urllib.URL{} }.path labels := [ ['method', app.req.method.str()]!, - ['path', app.req.url]!, + ['path', path]!, // Not all methods properly set this value yet I think ['status', app.status.int().str()]!, ] From 094634084b741cce218364c8a5d5a28b5aaa9434 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 5 Apr 2023 10:50:30 +0200 Subject: [PATCH 90/97] feat(agent): use worker thread approach --- src/agent/daemon.v | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/agent/daemon.v b/src/agent/daemon.v index 4364a92..d49b45e 100644 --- a/src/agent/daemon.v +++ b/src/agent/daemon.v @@ -20,11 +20,13 @@ struct AgentDaemon { client client.Client mut: images ImageManager - // Which builds are currently running; length is conf.max_concurrent_builds - builds []BuildConfig // Atomic variables used to detect when a build has finished; length is - // conf.max_concurrent_builds + // conf.max_concurrent_builds. This approach is used as the difference + // between a recently finished build and an empty build slot is important + // for knowing whether the agent is currently "active". atomics []u64 + // Channel used to send builds to worker threads + build_channel chan BuildConfig } // agent_init initializes a new agent @@ -34,8 +36,8 @@ fn agent_init(logger log.Log, conf Config) AgentDaemon { client: client.new(conf.address, conf.api_key) conf: conf images: new_image_manager(conf.image_rebuild_frequency * 60) - builds: []BuildConfig{len: conf.max_concurrent_builds} atomics: []u64{len: conf.max_concurrent_builds} + build_channel: chan BuildConfig{cap: conf.max_concurrent_builds} } return d @@ -43,6 +45,11 @@ fn agent_init(logger log.Log, conf Config) AgentDaemon { // run starts the actual agent daemon. This function will run forever. pub fn (mut d AgentDaemon) run() { + // Spawn worker threads + for builder_index in 0 .. d.conf.max_concurrent_builds { + spawn d.builder_thread(d.build_channel, builder_index) + } + // This is just so that the very first time the loop is ran, the jobs are // always polled mut last_poll_time := time.now().add_seconds(-d.conf.polling_frequency) @@ -107,10 +114,10 @@ pub fn (mut d AgentDaemon) run() { // It's technically still possible that the build image is // removed in the very short period between building the // builder image and starting a build container with it. If - // this happens, faith really just didn't want you to do this + // this happens, fate really just didn't want you to do this // build. - d.start_build(config) + d.build_channel <- config running++ } } @@ -147,22 +154,6 @@ fn (mut d AgentDaemon) update_atomics() (int, int) { return finished, empty } -// start_build starts a build for the given BuildConfig. -fn (mut d AgentDaemon) start_build(config BuildConfig) bool { - for i in 0 .. d.atomics.len { - if stdatomic.load_u64(&d.atomics[i]) == agent.build_empty { - stdatomic.store_u64(&d.atomics[i], agent.build_running) - d.builds[i] = config - - spawn d.run_build(i, config) - - return true - } - } - - return false -} - // run_build actually starts the build process for a given target. fn (mut d AgentDaemon) run_build(build_index int, config BuildConfig) { d.linfo('started build: ${config}') @@ -195,3 +186,12 @@ fn (mut d AgentDaemon) run_build(build_index int, config BuildConfig) { stdatomic.store_u64(&d.atomics[build_index], agent.build_done) } + +// builder_thread is a thread that constantly listens for builds to process +fn (mut d AgentDaemon) builder_thread(ch chan BuildConfig, builder_index int) { + for { + build_config := <-ch or { break } + + d.run_build(builder_index, build_config) + } +} From 7595eb7bbea77479d59fdbdf5782e3ca34b5b7c9 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 27 Apr 2023 13:23:58 +0200 Subject: [PATCH 91/97] fix: error when upload failed before all bytes received --- src/server/repo.v | 2 +- src/util/stream.v | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/server/repo.v b/src/server/repo.v index 051a7c2..8f8270d 100644 --- a/src/server/repo.v +++ b/src/server/repo.v @@ -68,7 +68,7 @@ fn (mut app App) put_package(repo_ string) web.Result { mut sw := time.new_stopwatch(time.StopWatchOptions{ auto_start: true }) util.reader_to_file(mut app.reader, length.int(), pkg_path) or { - app.lwarn("Failed to upload '${pkg_path}'") + app.lwarn("Failed to upload '${pkg_path}': ${err.msg()}") return app.status(.internal_server_error) } diff --git a/src/util/stream.v b/src/util/stream.v index 4b362fc..ef6e872 100644 --- a/src/util/stream.v +++ b/src/util/stream.v @@ -46,6 +46,10 @@ pub fn reader_to_file(mut reader io.BufferedReader, length int, path string) ! { to_write = to_write - bytes_written } } + + if bytes_left > 0 { + return error('Not all bytes were received.') + } } // match_array_in_array[T] returns how many elements of a2 overlap with a1. For From ac3a89500bbbbf9a4b95278913b61e4490ff5558 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 2 May 2023 11:46:19 +0200 Subject: [PATCH 92/97] feat: add non-functional build timeout setting --- src/build/build.v | 4 ++-- src/build/queue.v | 7 +++++-- src/console/targets/build.v | 4 ++-- src/console/targets/targets.v | 9 ++++++++- src/models/builds.v | 3 ++- src/models/targets.v | 3 ++- src/server/cli.v | 23 ++++++++++++----------- src/server/server.v | 2 +- 8 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/build/build.v b/src/build/build.v index c69a613..756e8f6 100644 --- a/src/build/build.v +++ b/src/build/build.v @@ -94,8 +94,8 @@ pub: } // build_target builds the given target. Internally it calls `build_config`. -pub fn build_target(address string, api_key string, base_image_id string, target &Target, force bool) !BuildResult { - config := target.as_build_config(base_image_id, force) +pub fn build_target(address string, api_key string, base_image_id string, target &Target, force bool, timeout int) !BuildResult { + config := target.as_build_config(base_image_id, force, timeout) return build_config(address, api_key, config) } diff --git a/src/build/queue.v b/src/build/queue.v index 73068ac..bc4db9d 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -33,6 +33,8 @@ pub struct BuildJobQueue { default_schedule &cron.Expression // Base image to use for targets without defined base image default_base_image string + // After how many minutes a build should be forcefully cancelled + default_build_timeout int mut: mutex shared util.Dummy // For each architecture, a priority queue is tracked @@ -44,10 +46,11 @@ mut: } // new_job_queue initializes a new job queue -pub fn new_job_queue(default_schedule &cron.Expression, default_base_image string) BuildJobQueue { +pub fn new_job_queue(default_schedule &cron.Expression, default_base_image string, default_build_timeout int) BuildJobQueue { return BuildJobQueue{ default_schedule: unsafe { default_schedule } default_base_image: default_base_image + default_build_timeout: default_build_timeout invalidated: map[int]time.Time{} } } @@ -80,7 +83,7 @@ pub fn (mut q BuildJobQueue) insert(input InsertConfig) ! { mut job := BuildJob{ created: time.now() single: input.single - config: input.target.as_build_config(q.default_base_image, input.force) + config: input.target.as_build_config(q.default_base_image, input.force, q.default_build_timeout) } if !input.now { diff --git a/src/console/targets/build.v b/src/console/targets/build.v index a59e6a1..93464af 100644 --- a/src/console/targets/build.v +++ b/src/console/targets/build.v @@ -6,7 +6,7 @@ import os import build // build locally builds the target with the given id. -fn build_target(conf Config, target_id int, force bool) ! { +fn build_target(conf Config, target_id int, force bool, timeout int) ! { c := client.new(conf.address, conf.api_key) target := c.get_target(target_id)! @@ -16,7 +16,7 @@ fn build_target(conf Config, target_id int, force bool) ! { image_id := build.create_build_image(conf.base_image)! println('Running build...') - res := build.build_target(conf.address, conf.api_key, image_id, target, force)! + res := build.build_target(conf.address, conf.api_key, image_id, target, force, timeout)! println('Removing build image...') diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index 676fa0a..80fc36b 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -232,6 +232,12 @@ pub fn cmd() cli.Command { description: 'Architecture to schedule build for. Required when using -remote.' flag: cli.FlagType.string }, + cli.Flag{ + name: 'timeout' + description: 'After how many minutes to cancel the build. Only applies to local builds.' + flag: cli.FlagType.int + default_value: ['60'] + }, ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! @@ -239,6 +245,7 @@ pub fn cmd() cli.Command { remote := cmd.flags.get_bool('remote')! force := cmd.flags.get_bool('force')! + timeout := cmd.flags.get_int('timeout')! target_id := cmd.args[0].int() if remote { @@ -251,7 +258,7 @@ pub fn cmd() cli.Command { c := client.new(conf_.address, conf_.api_key) c.queue_job(target_id, arch, force)! } else { - build_target(conf_, target_id, force)! + build_target(conf_, target_id, force, timeout)! } } }, diff --git a/src/models/builds.v b/src/models/builds.v index be2910c..6923115 100644 --- a/src/models/builds.v +++ b/src/models/builds.v @@ -10,9 +10,10 @@ pub: repo string base_image string force bool + timeout int } // str return a single-line string representation of a build log pub fn (c BuildConfig) str() string { - return '{ target: ${c.target_id}, kind: ${c.kind}, url: ${c.url}, branch: ${c.branch}, path: ${c.path}, repo: ${c.repo}, base_image: ${c.base_image}, force: ${c.force} }' + return '{ target: ${c.target_id}, kind: ${c.kind}, url: ${c.url}, branch: ${c.branch}, path: ${c.path}, repo: ${c.repo}, base_image: ${c.base_image}, force: ${c.force}, timeout: ${c.timeout} }' } diff --git a/src/models/targets.v b/src/models/targets.v index 3c0c9cf..14cc8a6 100644 --- a/src/models/targets.v +++ b/src/models/targets.v @@ -54,7 +54,7 @@ pub fn (t &Target) str() string { // as_build_config converts a Target into a BuildConfig, given some extra // needed information. -pub fn (t &Target) as_build_config(base_image string, force bool) BuildConfig { +pub fn (t &Target) as_build_config(base_image string, force bool, timeout int) BuildConfig { return BuildConfig{ target_id: t.id kind: t.kind @@ -64,6 +64,7 @@ pub fn (t &Target) as_build_config(base_image string, force bool) BuildConfig { repo: t.repo base_image: base_image force: force + timeout: timeout } } diff --git a/src/server/cli.v b/src/server/cli.v index 08ad5f8..c24812d 100644 --- a/src/server/cli.v +++ b/src/server/cli.v @@ -5,17 +5,18 @@ import conf as vconf struct Config { pub: - port int = 8000 - log_level string = 'WARN' - pkg_dir string - data_dir string - api_key string - default_arch string - global_schedule string = '0 3' - base_image string = 'archlinux:base-devel' - max_log_age int [empty_default] - log_removal_schedule string = '0 0' - collect_metrics bool [empty_default] + port int = 8000 + log_level string = 'WARN' + pkg_dir string + data_dir string + api_key string + default_arch string + global_schedule string = '0 3' + base_image string = 'archlinux:base-devel' + max_log_age int [empty_default] + log_removal_schedule string = '0 0' + collect_metrics bool [empty_default] + default_build_timeout int = 60 } // cmd returns the cli submodule that handles starting the server diff --git a/src/server/server.v b/src/server/server.v index 4cccb27..e1516fa 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -108,7 +108,7 @@ pub fn server(conf Config) ! { repo: repo_ db: db collector: collector - job_queue: build.new_job_queue(global_ce, conf.base_image) + job_queue: build.new_job_queue(global_ce, conf.base_image, conf.default_build_timeout) } app.init_job_queue() or { util.exit_with_message(1, 'Failed to inialize job queue: ${err.msg()}') From afb38256ac7ab48900582b13becefcc2df99d79d Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 2 May 2023 14:49:32 +0200 Subject: [PATCH 93/97] feat: implement build timeout --- src/build/build.v | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/build/build.v b/src/build/build.v index 756e8f6..b864792 100644 --- a/src/build/build.v +++ b/src/build/build.v @@ -136,9 +136,17 @@ pub fn build_config(address string, api_key string, config BuildConfig) !BuildRe dd.container_start(id)! mut data := dd.container_inspect(id)! + start_time := time.now() // This loop waits until the container has stopped, so we can remove it after for data.state.running { + if time.now() - start_time > config.timeout * time.second { + dd.container_kill(id)! + dd.container_remove(id)! + + return error('Build killed due to timeout (${config.timeout}s)') + } + time.sleep(1 * time.second) data = dd.container_inspect(id)! From b278ebd73fe2a5096acd5e2a164b325a4b981035 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 2 May 2023 14:51:49 +0200 Subject: [PATCH 94/97] fix: set default timeout to 60 minutes --- src/console/targets/targets.v | 2 +- src/server/cli.v | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index 80fc36b..f85c4c0 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -236,7 +236,7 @@ pub fn cmd() cli.Command { name: 'timeout' description: 'After how many minutes to cancel the build. Only applies to local builds.' flag: cli.FlagType.int - default_value: ['60'] + default_value: ['3600'] }, ] execute: fn (cmd cli.Command) ! { diff --git a/src/server/cli.v b/src/server/cli.v index c24812d..abb5fe3 100644 --- a/src/server/cli.v +++ b/src/server/cli.v @@ -16,7 +16,7 @@ pub: max_log_age int [empty_default] log_removal_schedule string = '0 0' collect_metrics bool [empty_default] - default_build_timeout int = 60 + default_build_timeout int = 3600 } // cmd returns the cli submodule that handles starting the server From 076ee24b1b7170e8a10fe054ec0c994b6cd3e2ca Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 4 May 2023 09:36:03 +0200 Subject: [PATCH 95/97] chore: update changelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a95d6c1..9747a16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,11 +12,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Metrics endpoint for Prometheus integration * Search in list of targets using API & CLI * Allow filtering targets by arch value +* Configurable global timeout for builds ### Changed * Rewrote cron expression logic in C * Updated codebase to V commit after 0.3.3 +* Agents now use worker threads and no longer spawn a new thread for every + build + +### Fixed + +* Package upload now fails if TCP connection is closed before all bytes have + been received ### Removed From 47c0f0405b536a1e28d5713554cd6fe3473baa0a Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Mon, 17 Jul 2023 13:26:38 +0200 Subject: [PATCH 96/97] chore: update versions to 0.6.0 --- CHANGELOG.md | 2 ++ PKGBUILD | 2 +- src/main.v | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9747a16..871877e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://git.rustybever.be/vieter-v/vieter/src/branch/dev) +## [0.6.0](https://git.rustybever.be/vieter-v/vieter/src/tag/0.6.0) + ### Added * Metrics endpoint for Prometheus integration diff --git a/PKGBUILD b/PKGBUILD index 7438390..05a3e73 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -3,7 +3,7 @@ pkgbase='vieter' pkgname='vieter' -pkgver='0.5.0' +pkgver='0.6.0' pkgrel=1 pkgdesc="Lightweight Arch repository server & package build system" depends=('glibc' 'openssl' 'libarchive' 'sqlite') diff --git a/src/main.v b/src/main.v index ce9ec81..e3b8a1a 100644 --- a/src/main.v +++ b/src/main.v @@ -20,7 +20,7 @@ fn main() { mut app := cli.Command{ name: 'vieter' description: 'Vieter is a lightweight implementation of an Arch repository server.' - version: '0.5.0' + version: '0.6.0' posix_mode: true flags: [ cli.Flag{ From 1a992806faf5e3678af9eb5c9dd30939203451cf Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Mon, 17 Jul 2023 13:53:20 +0200 Subject: [PATCH 97/97] fix: update PKGBUILD to use libvieter --- PKGBUILD | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/PKGBUILD b/PKGBUILD index 05a3e73..e5cde95 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -11,13 +11,23 @@ makedepends=('git' 'vieter-vlang') arch=('x86_64' 'aarch64') url='https://git.rustybever.be/vieter-v/vieter' license=('AGPL3') -source=("$pkgname::git+https://git.rustybever.be/vieter-v/vieter#tag=${pkgver//_/-}") -md5sums=('SKIP') +source=( + "$pkgname::git+https://git.rustybever.be/vieter-v/vieter#tag=${pkgver//_/-}" + "libvieter::git+https://git.rustybever.be/vieter-v/libvieter" +) +md5sums=('SKIP' 'SKIP') prepare() { - export VMODULES="$srcdir/.vmodules" + cd "${pkgname}" - cd "$pkgname/src" && v install + # Add the libvieter submodule + git submodule init + git config submodules.src/libvieter.url "${srcdir}/libvieter" + git -c protocol.file.allow=always submodule update + + export VMODULES="${srcdir}/.vmodules" + + cd src && v install } build() {