Fully functional hardcoded build command

main^2
Jef Roosens 2022-02-20 21:09:06 +01:00
parent 4f705f5fb5
commit 941b30e7d2
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
5 changed files with 65 additions and 24 deletions

View File

@ -3,6 +3,8 @@ module main
import docker import docker
import encoding.base64 import encoding.base64
import rand import rand
import time
import os
const container_build_dir = '/build' const container_build_dir = '/build'
@ -11,15 +13,12 @@ struct GitRepo {
branch string [required] branch string [required]
} }
fn build() { fn build(key string, repo_dir string) ? {
// println(docker.pull('nginx', 'latest') or { panic('yeetus') }) server_url := os.getenv_opt('VIETER_ADDRESS') or { exit_with_message(1, 'No Vieter server address was provided.') }
// println(docker.containers() or { panic('heet') })
repos := [ repos := [
GitRepo{'https://git.rustybever.be/Chewing_Bever/st', 'master'} GitRepo{'https://git.rustybever.be/Chewing_Bever/st', 'master'}
GitRepo{'https://aur.archlinux.org/libxft-bgra.git', 'master'} GitRepo{'https://aur.archlinux.org/libxft-bgra.git', 'master'}
] ]
mut uuids := []string{}
mut commands := [ mut commands := [
// Update repos & install required packages // Update repos & install required packages
'pacman -Syu --needed --noconfirm base-devel git' 'pacman -Syu --needed --noconfirm base-devel git'
@ -32,10 +31,11 @@ fn build() {
// build user // build user
'mkdir /build' 'mkdir /build'
'chown -R builder:builder /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 { for repo in repos {
mut uuid := rand.uuid_v4() mut uuid := rand.uuid_v4()
@ -47,20 +47,37 @@ fn build() {
uuids << uuid uuids << uuid
commands << "su builder -c 'git clone --single-branch --depth 1 --branch $repo.branch $repo.url /build/$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')) cmds_str := base64.encode_str(commands.join('\n'))
c := docker.NewContainer{ c := docker.NewContainer{
image: 'archlinux:latest' image: 'archlinux:latest'
env: ['BUILD_SCRIPT=$cmds_str'] env: ['BUILD_SCRIPT=$cmds_str', 'API_KEY=$key']
entrypoint: ['/bin/sh', '-c'] entrypoint: ['/bin/sh', '-c']
cmd: ['echo \$BUILD_SCRIPT | base64 -d | /bin/sh -e'] cmd: ['echo \$BUILD_SCRIPT | base64 -d | /bin/sh -e']
} }
id := docker.create_container(c) or { panic('aaaahh') } // First, we pull the latest archlinux image
print(docker.start_container(id) or { panic('yikes') }) 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) ?
} }

View File

@ -37,7 +37,32 @@ pub fn create_container(c &NewContainer) ?string {
pub fn start_container(id string) ?bool { pub fn start_container(id string) ?bool {
res := request('POST', urllib.parse('/containers/$id/start') ?) ? 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 return res.status_code == 204
} }

View File

@ -82,6 +82,6 @@ pub fn request_with_json<T>(method string, url urllib.URL, data &T) ?http.Respon
return request_with_body(method, url, 'application/json', body) 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') ?) return request('POST', urllib.parse('/images/create?fromImage=$image&tag=$tag') ?)
} }

View File

@ -9,8 +9,6 @@ const port = 8000
const buf_size = 1_000_000 const buf_size = 1_000_000
const db_name = 'pieter.db'
struct App { struct App {
web.Context web.Context
pub: pub:
@ -53,13 +51,18 @@ fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? {
} }
fn main() { 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 { if os.args.len == 1 {
exit_with_message(1, 'No action provided.') exit_with_message(1, 'No action provided.')
} }
match os.args[1] { match os.args[1] {
'server' { server() } 'server' { server(key, repo_dir) }
'build' { build() } 'build' { build(key, repo_dir) ? }
else { exit_with_message(1, 'Unknown action: ${os.args[1]}') } else { exit_with_message(1, 'Unknown action: ${os.args[1]}') }
} }
} }

View File

@ -5,7 +5,7 @@ import os
import log import log
import repo import repo
fn server() { fn server(key string, repo_dir string) {
// Configure logger // Configure logger
log_level_str := os.getenv_opt('LOG_LEVEL') or { 'WARN' } log_level_str := os.getenv_opt('LOG_LEVEL') or { 'WARN' }
log_level := log.level_from_tag(log_level_str) or { log_level := log.level_from_tag(log_level_str) or {
@ -27,10 +27,6 @@ fn server() {
} }
// Configure web 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 { pkg_dir := os.getenv_opt('PKG_DIR') or {
exit_with_message(1, 'No package directory was configured.') exit_with_message(1, 'No package directory was configured.')
} }