From 941b30e7d20556c4bb3f6de653fdf695ed107b03 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sun, 20 Feb 2022 21:09:06 +0100 Subject: [PATCH] Fully functional hardcoded build command --- src/build.v | 43 ++++++++++++++++++++++++++++------------- src/docker/containers.v | 27 +++++++++++++++++++++++++- src/docker/docker.v | 2 +- src/main.v | 11 +++++++---- src/server.v | 6 +----- 5 files changed, 65 insertions(+), 24 deletions(-) diff --git a/src/build.v b/src/build.v index f303635..d664c8e 100644 --- a/src/build.v +++ b/src/build.v @@ -3,6 +3,8 @@ module main import docker import encoding.base64 import rand +import time +import os const container_build_dir = '/build' @@ -11,15 +13,12 @@ struct GitRepo { branch string [required] } -fn build() { - // println(docker.pull('nginx', 'latest') or { panic('yeetus') }) - // println(docker.containers() or { panic('heet') }) +fn build(key string, repo_dir string) ? { + server_url := os.getenv_opt('VIETER_ADDRESS') or { exit_with_message(1, 'No Vieter server address was provided.') } repos := [ GitRepo{'https://git.rustybever.be/Chewing_Bever/st', 'master'} GitRepo{'https://aur.archlinux.org/libxft-bgra.git', 'master'} ] - mut uuids := []string{} - mut commands := [ // Update repos & install required packages 'pacman -Syu --needed --noconfirm base-devel git' @@ -32,10 +31,11 @@ fn build() { // build user 'mkdir /build' 'chown -R builder:builder /build' - // "su builder -c 'git clone https://git.rustybever.be/Chewing_Bever/st /build/st'" - // 'su builder -c \'cd /build/st && makepkg -s --noconfirm --needed && for pkg in \$(ls -1 *.pkg*); do curl -XPOST -T "\${pkg}" -H "X-API-KEY: \$API_KEY" https://arch.r8r.be/publish; done\'' ] + // Each repo gets a unique UUID to avoid naming conflicts when cloning + mut uuids := []string{} + for repo in repos { mut uuid := rand.uuid_v4() @@ -47,20 +47,37 @@ fn build() { uuids << uuid commands << "su builder -c 'git clone --single-branch --depth 1 --branch $repo.branch $repo.url /build/$uuid'" - commands << 'su builder -c \'cd /build/$uuid && makepkg -s --noconfirm --needed && for pkg in \$(ls -1 *.pkg*); do curl -XPOST -T "\${pkg}" -H "X-API-KEY: \$API_KEY" https://arch.r8r.be/publish; done\'' + commands << 'su builder -c \'cd /build/$uuid && makepkg -s --noconfirm --needed && for pkg in \$(ls -1 *.pkg*); do curl -XPOST -T "\${pkg}" -H "X-API-KEY: \$API_KEY" $server_url/publish; done\'' } - println(commands) - // We convert the list of commands into a base64 string + // We convert the list of commands into a base64 string, which then gets + // passed to the container as an env var cmds_str := base64.encode_str(commands.join('\n')) c := docker.NewContainer{ image: 'archlinux:latest' - env: ['BUILD_SCRIPT=$cmds_str'] + env: ['BUILD_SCRIPT=$cmds_str', 'API_KEY=$key'] entrypoint: ['/bin/sh', '-c'] cmd: ['echo \$BUILD_SCRIPT | base64 -d | /bin/sh -e'] } - id := docker.create_container(c) or { panic('aaaahh') } - print(docker.start_container(id) or { panic('yikes') }) + // First, we pull the latest archlinux image + docker.pull_image('archlinux', 'latest') ? + + id := docker.create_container(c) ? + docker.start_container(id) ? + + // This loop waits until the container has stopped, so we can remove it after + for { + data := docker.inspect_container(id) ? + + if !data.state.running { + break + } + + // Wait for 5 seconds + time.sleep(5000000000) + } + + docker.remove_container(id) ? } diff --git a/src/docker/containers.v b/src/docker/containers.v index f7eb8c7..5952ef3 100644 --- a/src/docker/containers.v +++ b/src/docker/containers.v @@ -37,7 +37,32 @@ pub fn create_container(c &NewContainer) ?string { pub fn start_container(id string) ?bool { res := request('POST', urllib.parse('/containers/$id/start') ?) ? - println(res) + + return res.status_code == 204 +} + +struct ContainerInspect { +pub: + state ContainerState [json: State] +} + +struct ContainerState { +pub: + running bool [json: Running] +} + +pub fn inspect_container(id string) ?ContainerInspect { + res := request('GET', urllib.parse('/containers/$id/json') ?) ? + + if res.status_code != 200 { + return error("Failed to inspect container.") + } + + return json.decode(ContainerInspect, res.text) +} + +pub fn remove_container(id string) ?bool { + res := request('DELETE', urllib.parse('/containers/$id') ?) ? return res.status_code == 204 } diff --git a/src/docker/docker.v b/src/docker/docker.v index 9eda41f..75d03ac 100644 --- a/src/docker/docker.v +++ b/src/docker/docker.v @@ -82,6 +82,6 @@ pub fn request_with_json(method string, url urllib.URL, data &T) ?http.Respon return request_with_body(method, url, 'application/json', body) } -pub fn pull(image string, tag string) ?http.Response { +pub fn pull_image(image string, tag string) ?http.Response { return request('POST', urllib.parse('/images/create?fromImage=$image&tag=$tag') ?) } diff --git a/src/main.v b/src/main.v index 5d8c072..cc66b59 100644 --- a/src/main.v +++ b/src/main.v @@ -9,8 +9,6 @@ const port = 8000 const buf_size = 1_000_000 -const db_name = 'pieter.db' - struct App { web.Context pub: @@ -53,13 +51,18 @@ fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? { } fn main() { + key := os.getenv_opt('API_KEY') or { exit_with_message(1, 'No API key was provided.') } + repo_dir := os.getenv_opt('REPO_DIR') or { + exit_with_message(1, 'No repo directory was configured.') + } + if os.args.len == 1 { exit_with_message(1, 'No action provided.') } match os.args[1] { - 'server' { server() } - 'build' { build() } + 'server' { server(key, repo_dir) } + 'build' { build(key, repo_dir) ? } else { exit_with_message(1, 'Unknown action: ${os.args[1]}') } } } diff --git a/src/server.v b/src/server.v index 0e6ba45..e0f22ec 100644 --- a/src/server.v +++ b/src/server.v @@ -5,7 +5,7 @@ import os import log import repo -fn server() { +fn server(key string, repo_dir string) { // Configure logger log_level_str := os.getenv_opt('LOG_LEVEL') or { 'WARN' } log_level := log.level_from_tag(log_level_str) or { @@ -27,10 +27,6 @@ fn server() { } // Configure web server - key := os.getenv_opt('API_KEY') or { exit_with_message(1, 'No API key was provided.') } - repo_dir := os.getenv_opt('REPO_DIR') or { - exit_with_message(1, 'No repo directory was configured.') - } pkg_dir := os.getenv_opt('PKG_DIR') or { exit_with_message(1, 'No package directory was configured.') }