2022-04-06 16:52:31 +02:00
|
|
|
module build
|
2022-02-17 22:00:46 +01:00
|
|
|
|
|
|
|
import docker
|
2022-02-20 20:26:39 +01:00
|
|
|
import encoding.base64
|
2022-02-20 21:09:06 +01:00
|
|
|
import time
|
2022-02-21 22:58:05 +01:00
|
|
|
import net.http
|
2022-04-06 16:52:31 +02:00
|
|
|
import git
|
2022-04-06 17:57:05 +02:00
|
|
|
import json
|
2022-02-20 20:26:39 +01:00
|
|
|
|
|
|
|
const container_build_dir = '/build'
|
2022-02-25 21:54:16 +01:00
|
|
|
|
2022-02-25 20:52:30 +01:00
|
|
|
const build_image_repo = 'vieter-build'
|
2022-02-20 20:26:39 +01:00
|
|
|
|
2022-02-25 20:52:30 +01:00
|
|
|
fn create_build_image() ?string {
|
|
|
|
commands := [
|
2022-02-20 20:26:39 +01:00
|
|
|
// 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
|
|
|
]
|
|
|
|
cmds_str := base64.encode_str(commands.join('\n'))
|
|
|
|
|
|
|
|
c := docker.NewContainer{
|
|
|
|
image: 'archlinux:latest'
|
2022-02-25 20:52:30 +01:00
|
|
|
env: ['BUILD_SCRIPT=$cmds_str']
|
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)
|
|
|
|
}
|
|
|
|
|
2022-02-25 20:52:30 +01:00
|
|
|
// Finally, we create the image from the container
|
|
|
|
// As the tag, we use the epoch value
|
|
|
|
tag := time.sys_mono_now().str()
|
|
|
|
image := docker.create_image_from_container(id, 'vieter-build', tag) ?
|
2022-02-20 21:09:06 +01:00
|
|
|
docker.remove_container(id) ?
|
2022-02-25 20:52:30 +01:00
|
|
|
|
|
|
|
return image.id
|
|
|
|
}
|
|
|
|
|
2022-04-06 18:20:14 +02:00
|
|
|
fn build(conf Config) ? {
|
2022-02-25 20:52:30 +01:00
|
|
|
// We get the repos list from the Vieter instance
|
|
|
|
mut req := http.new_request(http.Method.get, '$conf.address/api/repos', '') ?
|
|
|
|
req.add_custom_header('X-Api-Key', conf.api_key) ?
|
|
|
|
|
|
|
|
res := req.do() ?
|
2022-04-06 16:52:31 +02:00
|
|
|
repos := json.decode([]git.GitRepo, res.text) ?
|
2022-02-25 20:52:30 +01:00
|
|
|
|
|
|
|
// No point in doing work if there's no repos present
|
|
|
|
if repos.len == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, we create a base image which has updated repos n stuff
|
|
|
|
image_id := create_build_image() ?
|
|
|
|
|
|
|
|
for repo in repos {
|
2022-02-25 21:47:28 +01:00
|
|
|
// TODO what to do with PKGBUILDs that build multiple packages?
|
2022-02-25 20:52:30 +01:00
|
|
|
commands := [
|
2022-02-25 21:54:16 +01:00
|
|
|
'git clone --single-branch --depth 1 --branch $repo.branch $repo.url repo',
|
|
|
|
'cd repo',
|
|
|
|
'makepkg --nobuild --nodeps',
|
|
|
|
'source PKGBUILD',
|
2022-02-25 21:47:28 +01:00
|
|
|
// The build container checks whether the package is already present on the server
|
2022-02-25 21:54:16 +01:00
|
|
|
'curl --head --fail $conf.address/\$pkgname-\$pkgver-\$pkgrel-\$(uname -m).pkg.tar.zst && exit 0',
|
|
|
|
'MAKEFLAGS="-j\$(nproc)" makepkg -s --noconfirm --needed && for pkg in \$(ls -1 *.pkg*); do curl -XPOST -T "\$pkg" -H "X-API-KEY: \$API_KEY" $conf.address/publish; done',
|
2022-02-25 20:52:30 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
// 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: '$image_id'
|
|
|
|
env: ['BUILD_SCRIPT=$cmds_str', 'API_KEY=$conf.api_key']
|
|
|
|
entrypoint: ['/bin/sh', '-c']
|
2022-02-25 21:47:28 +01:00
|
|
|
cmd: ['echo \$BUILD_SCRIPT | base64 -d | /bin/bash -e']
|
|
|
|
work_dir: '/build'
|
|
|
|
user: 'builder:builder'
|
2022-02-25 20:52:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-02-25 21:50:07 +01:00
|
|
|
docker.remove_container(id) ?
|
2022-02-25 20:52:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we remove the builder image
|
|
|
|
docker.remove_image(image_id) ?
|
2022-02-17 22:00:46 +01:00
|
|
|
}
|