vieter/src/agent/images.v

65 lines
1.6 KiB
Coq
Raw Normal View History

module agent
import time
import docker
2022-12-12 21:50:34 +01:00
import build
struct ImageManager {
2022-12-12 21:50:34 +01:00
mut:
refresh_frequency int
images map[string][]string [required]
timestamps map[string]time.Time [required]
}
2022-12-12 21:50:34 +01:00
fn new_image_manager(refresh_frequency int) ImageManager {
return ImageManager{
refresh_frequency: refresh_frequency
images: map[string][]string{}
timestamps: map[string]time.Time{}
}
}
pub fn (m &ImageManager) get(base_image string) string {
return m.images[base_image].last()
}
2022-12-12 21:50:34 +01:00
fn (mut m ImageManager) refresh_image(base_image string) ! {
// No need to refresh the image if the previous one is still new enough
if base_image in m.timestamps
&& m.timestamps[base_image].add_seconds(m.refresh_frequency) > time.now() {
return
}
2022-12-12 21:50:34 +01:00
// TODO use better image tags for built images
new_image := build.create_build_image(base_image) or {
return error('Failed to build builder image from base image $base_image')
}
2022-12-12 21:50:34 +01:00
m.images[base_image] << new_image
m.timestamps[base_image] = time.now()
}
2022-12-12 21:50:34 +01:00
// clean_old_images tries to remove any old but still present builder images.
fn (mut m ImageManager) clean_old_images() {
mut dd := docker.new_conn() or { return }
2022-12-12 21:50:34 +01:00
defer {
dd.close() or {}
}
2022-12-12 21:50:34 +01:00
mut i := 0
2022-12-12 21:50:34 +01:00
for image in m.images.keys() {
i = 0
for i < m.images[image].len - 1 {
// For each builder image, we try to remove it by calling the Docker
// API. If the function returns an error or false, that means the image
// wasn't deleted. Therefore, we move the index over. If the function
// returns true, the array's length has decreased by one so we don't
// move the index.
dd.remove_image(m.images[image][i]) or { i += 1 }
}
}
}