2022-04-06 16:52:31 +02:00
|
|
|
module build
|
2022-02-17 22:00:46 +01:00
|
|
|
|
2022-11-01 21:43:25 +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-04-07 14:40:49 +02:00
|
|
|
import os
|
2022-05-15 09:56:23 +02:00
|
|
|
import strings
|
|
|
|
import util
|
2022-06-14 22:25:40 +02:00
|
|
|
import models { Target }
|
2022-02-20 20:26:39 +01:00
|
|
|
|
2022-05-16 14:22:53 +02:00
|
|
|
const (
|
|
|
|
container_build_dir = '/build'
|
|
|
|
build_image_repo = 'vieter-build'
|
2022-06-17 20:19:15 +02:00
|
|
|
// Contents of PATH variable in build containers
|
|
|
|
path_dirs = ['/sbin', '/bin', '/usr/sbin', '/usr/bin', '/usr/local/sbin',
|
|
|
|
'/usr/local/bin', '/usr/bin/site_perl', '/usr/bin/vendor_perl', '/usr/bin/core_perl']
|
2022-05-16 14:22:53 +02:00
|
|
|
)
|
2022-02-20 20:26:39 +01:00
|
|
|
|
2022-12-06 13:50:25 +01:00
|
|
|
pub struct BuildConfig {
|
|
|
|
pub:
|
2022-12-06 14:11:17 +01:00
|
|
|
target_id int
|
|
|
|
kind string
|
|
|
|
url string
|
|
|
|
branch string
|
|
|
|
repo string
|
2022-12-06 13:50:25 +01:00
|
|
|
base_image string
|
|
|
|
}
|
|
|
|
|
2022-04-30 20:22:03 +02:00
|
|
|
// create_build_image creates a builder image given some base image which can
|
|
|
|
// then be used to build & package Arch images. It mostly just updates the
|
|
|
|
// system, install some necessary packages & creates a non-root user to run
|
|
|
|
// makepkg with. The base image should be some Linux distribution that uses
|
|
|
|
// Pacman as its package manager.
|
2022-11-01 21:10:45 +01:00
|
|
|
pub fn create_build_image(base_image string) !string {
|
|
|
|
mut dd := docker.new_conn()!
|
2022-05-13 22:03:06 +02:00
|
|
|
|
2022-05-15 09:56:23 +02:00
|
|
|
defer {
|
|
|
|
dd.close() or {}
|
|
|
|
}
|
|
|
|
|
2022-02-25 20:52:30 +01:00
|
|
|
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{
|
2022-04-08 13:22:29 +02:00
|
|
|
image: base_image
|
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-04-08 13:22:29 +02:00
|
|
|
// This check is needed so the user can pass "archlinux" without passing a
|
|
|
|
// tag & make it still work
|
|
|
|
image_parts := base_image.split_nth(':', 2)
|
|
|
|
image_name := image_parts[0]
|
|
|
|
image_tag := if image_parts.len > 1 { image_parts[1] } else { 'latest' }
|
|
|
|
|
|
|
|
// We pull the provided image
|
2022-11-01 21:10:45 +01:00
|
|
|
dd.pull_image(image_name, image_tag)!
|
2022-02-20 21:09:06 +01:00
|
|
|
|
2022-11-01 21:10:45 +01:00
|
|
|
id := dd.container_create(c)!.id
|
|
|
|
// id := docker.create_container(c)!
|
|
|
|
dd.container_start(id)!
|
2022-02-20 21:09:06 +01:00
|
|
|
|
|
|
|
// This loop waits until the container has stopped, so we can remove it after
|
|
|
|
for {
|
2022-11-01 21:10:45 +01:00
|
|
|
data := dd.container_inspect(id)!
|
2022-02-20 21:09:06 +01:00
|
|
|
|
|
|
|
if !data.state.running {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-04-30 17:56:35 +02:00
|
|
|
time.sleep(1 * time.second)
|
2022-02-20 21:09:06 +01:00
|
|
|
}
|
|
|
|
|
2022-02-25 20:52:30 +01:00
|
|
|
// Finally, we create the image from the container
|
|
|
|
// As the tag, we use the epoch value
|
2022-04-30 17:56:35 +02:00
|
|
|
// TODO also add the base image's name into the image name to prevent
|
|
|
|
// conflicts.
|
2022-02-25 20:52:30 +01:00
|
|
|
tag := time.sys_mono_now().str()
|
2022-11-01 21:10:45 +01:00
|
|
|
image := dd.create_image_from_container(id, 'vieter-build', tag)!
|
|
|
|
dd.container_remove(id)!
|
2022-02-25 20:52:30 +01:00
|
|
|
|
|
|
|
return image.id
|
|
|
|
}
|
|
|
|
|
2022-05-08 15:07:54 +02:00
|
|
|
pub struct BuildResult {
|
|
|
|
pub:
|
2022-05-08 13:17:54 +02:00
|
|
|
start_time time.Time
|
2022-05-08 14:53:35 +02:00
|
|
|
end_time time.Time
|
|
|
|
exit_code int
|
|
|
|
logs string
|
2022-05-08 13:17:54 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 16:23:47 +02:00
|
|
|
// build_target builds, packages & publishes a given Arch package based on the
|
2022-06-14 22:25:40 +02:00
|
|
|
// provided target. The base image ID should be of an image previously created
|
2022-05-08 13:17:54 +02:00
|
|
|
// by create_build_image. It returns the logs of the container.
|
2022-11-01 21:10:45 +01:00
|
|
|
pub fn build_target(address string, api_key string, base_image_id string, target &Target) !BuildResult {
|
|
|
|
mut dd := docker.new_conn()!
|
2022-05-15 09:56:23 +02:00
|
|
|
|
|
|
|
defer {
|
|
|
|
dd.close() or {}
|
|
|
|
}
|
|
|
|
|
2022-04-30 17:56:35 +02:00
|
|
|
build_arch := os.uname().machine
|
2022-06-17 16:23:47 +02:00
|
|
|
build_script := create_build_script(address, target, build_arch)
|
2022-04-30 17:56:35 +02:00
|
|
|
|
2022-06-01 20:34:36 +02:00
|
|
|
// We convert the build script into a base64 string, which then gets passed
|
|
|
|
// to the container as an env var
|
|
|
|
base64_script := base64.encode_str(build_script)
|
2022-04-30 17:56:35 +02:00
|
|
|
|
|
|
|
c := docker.NewContainer{
|
|
|
|
image: '$base_image_id'
|
2022-06-17 20:19:15 +02:00
|
|
|
env: [
|
|
|
|
'BUILD_SCRIPT=$base64_script',
|
|
|
|
'API_KEY=$api_key',
|
|
|
|
// `archlinux:base-devel` does not correctly set the path variable,
|
|
|
|
// causing certain builds to fail. This fixes it.
|
|
|
|
'PATH=${build.path_dirs.join(':')}',
|
|
|
|
]
|
2022-04-30 17:56:35 +02:00
|
|
|
entrypoint: ['/bin/sh', '-c']
|
|
|
|
cmd: ['echo \$BUILD_SCRIPT | base64 -d | /bin/bash -e']
|
|
|
|
work_dir: '/build'
|
2022-06-01 20:34:36 +02:00
|
|
|
user: '0:0'
|
2022-04-30 17:56:35 +02:00
|
|
|
}
|
|
|
|
|
2022-11-01 21:10:45 +01:00
|
|
|
id := dd.container_create(c)!.id
|
|
|
|
dd.container_start(id)!
|
2022-04-30 17:56:35 +02:00
|
|
|
|
2022-11-01 21:10:45 +01:00
|
|
|
mut data := dd.container_inspect(id)!
|
2022-05-08 13:17:54 +02:00
|
|
|
|
2022-04-30 17:56:35 +02:00
|
|
|
// This loop waits until the container has stopped, so we can remove it after
|
2022-05-09 15:05:53 +02:00
|
|
|
for data.state.running {
|
2022-04-30 17:56:35 +02:00
|
|
|
time.sleep(1 * time.second)
|
2022-05-08 13:17:54 +02:00
|
|
|
|
2022-11-01 21:10:45 +01:00
|
|
|
data = dd.container_inspect(id)!
|
2022-04-30 17:56:35 +02:00
|
|
|
}
|
|
|
|
|
2022-11-01 21:10:45 +01:00
|
|
|
mut logs_stream := dd.container_get_logs(id)!
|
2022-05-08 13:17:54 +02:00
|
|
|
|
2022-05-15 09:56:23 +02:00
|
|
|
// Read in the entire stream
|
|
|
|
mut logs_builder := strings.new_builder(10 * 1024)
|
2022-11-01 21:10:45 +01:00
|
|
|
util.reader_to_writer(mut logs_stream, mut logs_builder)!
|
2022-05-15 09:56:23 +02:00
|
|
|
|
2022-11-01 21:10:45 +01:00
|
|
|
dd.container_remove(id)!
|
2022-05-08 13:17:54 +02:00
|
|
|
|
|
|
|
return BuildResult{
|
|
|
|
start_time: data.state.start_time
|
|
|
|
end_time: data.state.end_time
|
|
|
|
exit_code: data.state.exit_code
|
2022-05-15 09:56:23 +02:00
|
|
|
logs: logs_builder.str()
|
2022-05-08 13:17:54 +02:00
|
|
|
}
|
2022-04-30 17:56:35 +02:00
|
|
|
}
|