forked from vieter-v/vieter
Compare commits
No commits in common. "36693900a3db4d0943ac3725100ec1cf2260167f" and "4f705f5fb5cb618eac3d9d484f850109d79e7cc2" have entirely different histories.
36693900a3
...
4f705f5fb5
|
|
@ -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"]
|
||||
|
|
|
|||
56
src/build.v
56
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') })
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
||||
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') ?)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
module git
|
||||
|
||||
pub struct GitRepo {
|
||||
pub:
|
||||
url string [required]
|
||||
branch string [required]
|
||||
}
|
||||
11
src/main.v
11
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]}') }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.')
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue