From c0f58ddc77e2db11dcf7d54d07210934015aaffd Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 28 Dec 2022 16:09:00 +0100 Subject: [PATCH 01/39] 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 02/39] 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 03/39] 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 04/39] 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 05/39] 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 06/39] 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 07/39] 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 08/39] 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 09/39] 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 10/39] 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 11/39] 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 12/39] 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 13/39] 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 14/39] 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 15/39] 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 16/39] 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 17/39] 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 18/39] 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 19/39] 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 20/39] 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 21/39] 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 22/39] 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 23/39] 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 24/39] 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 25/39] 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 26/39] 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 27/39] 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 28/39] 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 29/39] 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 30/39] 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 31/39] 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 32/39] 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 33/39] 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 34/39] 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 35/39] 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 36/39] 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 37/39] 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 38/39] 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 39/39] 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{