diff --git a/Dockerfile.ci b/Dockerfile.ci index e0dd5da..24f2bef 100644 --- a/Dockerfile.ci +++ b/Dockerfile.ci @@ -36,14 +36,11 @@ HEALTHCHECK --interval=30s \ CMD /bin/wget --spider http://localhost:8000/health || exit 1 RUN mkdir /data && \ - chown -R www-data:www-data /data && \ - mkdir -p '/var/spool/cron/crontabs' && \ - echo '0 3 * * * /bin/vieter build' >> /var/spool/cron/crontabs/www-data && \ - chown www-data:www-data /var/spool/cron/crontabs/www-data + chown -R www-data:www-data /data WORKDIR /data USER www-data:www-data ENTRYPOINT ["/bin/dumb-init", "--"] -CMD ["/bin/vieter", "server"] +CMD ["/bin/vieter"] diff --git a/src/build.v b/src/build.v index 49ec48f..f303635 100644 --- a/src/build.v +++ b/src/build.v @@ -3,20 +3,22 @@ module main import docker import encoding.base64 import rand -import time -import os -import json -import git const container_build_dir = '/build' -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.') } +struct GitRepo { + url string [required] + branch string [required] +} - // Read in the repos from a json file - filename := os.join_path_single(repo_dir, 'repos.json') - txt := os.read_file(filename) ? - repos := json.decode([]git.GitRepo, txt) ? +fn build() { + // println(docker.pull('nginx', 'latest') or { panic('yeetus') }) + // println(docker.containers() or { panic('heet') }) + 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 @@ -30,11 +32,10 @@ fn build(key string, repo_dir string) ? { // 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() @@ -46,37 +47,20 @@ fn build(key string, repo_dir string) ? { 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" $server_url/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" https://arch.r8r.be/publish; done\'' } + println(commands) - // We convert the list of commands into a base64 string, which then gets - // passed to the container as an env var + // We convert the list of commands into a base64 string cmds_str := base64.encode_str(commands.join('\n')) c := docker.NewContainer{ image: 'archlinux:latest' - env: ['BUILD_SCRIPT=$cmds_str', 'API_KEY=$key'] + env: ['BUILD_SCRIPT=$cmds_str'] entrypoint: ['/bin/sh', '-c'] cmd: ['echo \$BUILD_SCRIPT | base64 -d | /bin/sh -e'] } - // 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) ? + id := docker.create_container(c) or { panic('aaaahh') } + print(docker.start_container(id) or { panic('yikes') }) } diff --git a/src/docker/containers.v b/src/docker/containers.v index 5952ef3..f7eb8c7 100644 --- a/src/docker/containers.v +++ b/src/docker/containers.v @@ -37,32 +37,7 @@ pub fn create_container(c &NewContainer) ?string { pub fn start_container(id string) ?bool { res := request('POST', urllib.parse('/containers/$id/start') ?) ? - - 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') ?) ? + println(res) return res.status_code == 204 } diff --git a/src/docker/docker.v b/src/docker/docker.v index 75d03ac..9eda41f 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(image string, tag string) ?http.Response { +pub fn pull(image string, tag string) ?http.Response { return request('POST', urllib.parse('/images/create?fromImage=$image&tag=$tag') ?) } diff --git a/src/git.v b/src/git.v deleted file mode 100644 index 097e2e5..0000000 --- a/src/git.v +++ /dev/null @@ -1,7 +0,0 @@ -module git - -pub struct GitRepo { -pub: - url string [required] - branch string [required] -} diff --git a/src/main.v b/src/main.v index cc66b59..5d8c072 100644 --- a/src/main.v +++ b/src/main.v @@ -9,6 +9,8 @@ const port = 8000 const buf_size = 1_000_000 +const db_name = 'pieter.db' + struct App { web.Context pub: @@ -51,18 +53,13 @@ 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(key, repo_dir) } - 'build' { build(key, repo_dir) ? } + 'server' { server() } + 'build' { build() } else { exit_with_message(1, 'Unknown action: ${os.args[1]}') } } } diff --git a/src/server.v b/src/server.v index e0f22ec..0e6ba45 100644 --- a/src/server.v +++ b/src/server.v @@ -5,7 +5,7 @@ import os import log import repo -fn server(key string, repo_dir string) { +fn server() { // Configure logger log_level_str := os.getenv_opt('LOG_LEVEL') or { 'WARN' } log_level := log.level_from_tag(log_level_str) or { @@ -27,6 +27,10 @@ fn server(key string, repo_dir string) { } // 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.') }