2022-02-17 22:00:46 +01:00
|
|
|
module main
|
|
|
|
|
|
|
|
import docker
|
2022-02-20 20:26:39 +01:00
|
|
|
import encoding.base64
|
|
|
|
import rand
|
2022-02-20 21:09:06 +01:00
|
|
|
import time
|
|
|
|
import os
|
2022-02-20 21:19:31 +01:00
|
|
|
import json
|
|
|
|
import git
|
2022-02-20 20:26:39 +01:00
|
|
|
|
|
|
|
const container_build_dir = '/build'
|
|
|
|
|
2022-02-20 21:09:06 +01:00
|
|
|
fn build(key string, repo_dir string) ? {
|
2022-02-20 22:15:10 +01:00
|
|
|
server_url := os.getenv_opt('VIETER_ADDRESS') or {
|
|
|
|
exit_with_message(1, 'No Vieter server address was provided.')
|
|
|
|
}
|
2022-02-20 21:19:31 +01:00
|
|
|
|
|
|
|
// 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) ?
|
|
|
|
|
2022-02-20 20:26:39 +01:00
|
|
|
mut commands := [
|
|
|
|
// Update repos & install required packages
|
|
|
|
'pacman -Syu --needed --noconfirm base-devel git'
|
|
|
|
// Add a non-root user to run makepkg
|
2022-02-20 22:15:10 +01:00
|
|
|
'groupadd -g 1000 builder',
|
2022-02-20 20:26:39 +01:00
|
|
|
'useradd -mg builder builder'
|
|
|
|
// Make sure they can use sudo without a password
|
|
|
|
"echo 'builder ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers"
|
|
|
|
// Create the directory for the builds & make it writeable for the
|
|
|
|
// build user
|
2022-02-20 22:15:10 +01:00
|
|
|
'mkdir /build',
|
|
|
|
'chown -R builder:builder /build',
|
2022-02-20 20:26:39 +01:00
|
|
|
]
|
|
|
|
|
2022-02-20 21:09:06 +01:00
|
|
|
// Each repo gets a unique UUID to avoid naming conflicts when cloning
|
|
|
|
mut uuids := []string{}
|
|
|
|
|
2022-02-20 20:26:39 +01:00
|
|
|
for repo in repos {
|
|
|
|
mut uuid := rand.uuid_v4()
|
|
|
|
|
|
|
|
// Just to be sure we don't have any collisions
|
|
|
|
for uuids.contains(uuid) {
|
|
|
|
uuid = rand.uuid_v4()
|
|
|
|
}
|
|
|
|
|
|
|
|
uuids << uuid
|
|
|
|
|
|
|
|
commands << "su builder -c 'git clone --single-branch --depth 1 --branch $repo.branch $repo.url /build/$uuid'"
|
2022-02-20 21:09:06 +01:00
|
|
|
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\''
|
2022-02-20 20:26:39 +01:00
|
|
|
}
|
|
|
|
|
2022-02-20 21:09:06 +01:00
|
|
|
// We convert the list of commands into a base64 string, which then gets
|
|
|
|
// passed to the container as an env var
|
2022-02-20 20:26:39 +01:00
|
|
|
cmds_str := base64.encode_str(commands.join('\n'))
|
|
|
|
|
|
|
|
c := docker.NewContainer{
|
|
|
|
image: 'archlinux:latest'
|
2022-02-20 21:09:06 +01:00
|
|
|
env: ['BUILD_SCRIPT=$cmds_str', 'API_KEY=$key']
|
2022-02-20 20:26:39 +01:00
|
|
|
entrypoint: ['/bin/sh', '-c']
|
|
|
|
cmd: ['echo \$BUILD_SCRIPT | base64 -d | /bin/sh -e']
|
|
|
|
}
|
|
|
|
|
2022-02-20 21:09:06 +01:00
|
|
|
// 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) ?
|
2022-02-17 22:00:46 +01:00
|
|
|
}
|