forked from vieter-v/vieter
WIP: write agent
parent
18c94c8d0b
commit
6edd1b475d
|
|
@ -4,6 +4,7 @@ import log
|
|||
import sync.stdatomic
|
||||
import build { BuildConfig }
|
||||
import client
|
||||
import time
|
||||
|
||||
const (
|
||||
build_empty = 0
|
||||
|
|
@ -14,6 +15,7 @@ const (
|
|||
struct AgentDaemon {
|
||||
logger shared log.Log
|
||||
conf Config
|
||||
mut:
|
||||
images ImageManager
|
||||
// Which builds are currently running; length is same as
|
||||
// conf.max_concurrent_builds
|
||||
|
|
@ -41,13 +43,33 @@ pub fn (mut d AgentDaemon) run() {
|
|||
for {
|
||||
free_builds := d.update_atomics()
|
||||
|
||||
if free_builds > 0 {
|
||||
// All build slots are taken, so there's nothing to be done
|
||||
if free_builds == 0 {
|
||||
time.sleep(1 * time.second)
|
||||
continue
|
||||
}
|
||||
|
||||
// Builds have finished, so old builder images might have freed up.
|
||||
d.images.clean_old_images()
|
||||
|
||||
// Poll for new jobs
|
||||
new_configs := d.client.poll_jobs(free_builds) or {
|
||||
d.lerror('Failed to poll jobs: $err.msg()')
|
||||
|
||||
time.sleep(1 * time.second)
|
||||
continue
|
||||
}
|
||||
|
||||
// Schedule new jobs
|
||||
for config in new_configs {
|
||||
d.start_build(config)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// update_atomics checks for each build whether it's completed, and sets it to
|
||||
// free again if so. The return value is how many fields are now set to free.
|
||||
// free again if so. The return value is how many build slots are currently
|
||||
// free.
|
||||
fn (mut d AgentDaemon) update_atomics() int {
|
||||
mut count := 0
|
||||
|
||||
|
|
@ -62,3 +84,49 @@ fn (mut d AgentDaemon) update_atomics() int {
|
|||
|
||||
return count
|
||||
}
|
||||
|
||||
// start_build starts a build for the given BuildConfig object.
|
||||
fn (mut d AgentDaemon) start_build(config BuildConfig) bool {
|
||||
for i in 0 .. d.atomics.len {
|
||||
if stdatomic.load_u64(&d.atomics[i]) == agent.build_empty {
|
||||
stdatomic.store_u64(&d.atomics[i], agent.build_running)
|
||||
d.builds[i] = config
|
||||
|
||||
go d.run_build(i, config)
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// run_build actually starts the build process for a given target.
|
||||
fn (mut d AgentDaemon) run_build(build_index int, config BuildConfig) {
|
||||
d.linfo('started build: $config.url -> $config.repo')
|
||||
|
||||
// 0 means success, 1 means failure
|
||||
mut status := 0
|
||||
|
||||
res := build.build_target(d.client.address, d.client.api_key, d.builder_images.last(),
|
||||
&sb.target) or {
|
||||
d.ldebug('build_target error: $err.msg()')
|
||||
status = 1
|
||||
|
||||
build.BuildResult{}
|
||||
}
|
||||
|
||||
if status == 0 {
|
||||
d.linfo('finished build: $sb.target.url -> $sb.target.repo; uploading logs...')
|
||||
|
||||
build_arch := os.uname().machine
|
||||
d.client.add_build_log(sb.target.id, res.start_time, res.end_time, build_arch,
|
||||
res.exit_code, res.logs) or {
|
||||
d.lerror('Failed to upload logs for build: $sb.target.url -> $sb.target.repo')
|
||||
}
|
||||
} else {
|
||||
d.linfo('an error occured during build: $sb.target.url -> $sb.target.repo')
|
||||
}
|
||||
|
||||
stdatomic.store_u64(&d.atomics[build_index], agent.build_done)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -103,10 +103,23 @@ pub:
|
|||
logs string
|
||||
}
|
||||
|
||||
pub fn build_target(address string, api_key string, base_image_id string, target &Target) !BuildResult {
|
||||
config := BuildConfig{
|
||||
target_id: target.id
|
||||
kind: target.kind
|
||||
url: target.url
|
||||
branch: target.branch
|
||||
repo: target.repo
|
||||
base_image: base_image_id
|
||||
}
|
||||
|
||||
return build_config(address, api_key, config)
|
||||
}
|
||||
|
||||
// build_target builds, packages & publishes a given Arch package based on the
|
||||
// provided target. The base image ID should be of an image previously created
|
||||
// by create_build_image. It returns the logs of the container.
|
||||
pub fn build_target(address string, api_key string, base_image_id string, target &Target) !BuildResult {
|
||||
pub fn build_config(address string, api_key string, config BuildConfig) !BuildResult {
|
||||
mut dd := docker.new_conn()!
|
||||
|
||||
defer {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
module client
|
||||
|
||||
import build { BuildConfig }
|
||||
|
||||
pub fn (c &Client) poll_jobs(max int) ![]BuildConfig {
|
||||
data := c.send_request<[]BuildConfig>(.get, '/api/v1/jobs/poll', {
|
||||
'max': max.str()
|
||||
})!
|
||||
|
||||
return data.data
|
||||
}
|
||||
Loading…
Reference in New Issue