From 93c4306b1015594bb6d7e08d03138c12229ac598 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Sun, 24 Jul 2022 23:08:51 +0200 Subject: pull docker bots before running them --- planetwars-matchrunner/src/docker_runner.rs | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'planetwars-matchrunner') diff --git a/planetwars-matchrunner/src/docker_runner.rs b/planetwars-matchrunner/src/docker_runner.rs index 2d93273..6de9bb1 100644 --- a/planetwars-matchrunner/src/docker_runner.rs +++ b/planetwars-matchrunner/src/docker_runner.rs @@ -15,12 +15,22 @@ use crate::match_context::{EventBus, PlayerHandle, RequestError, RequestMessage} use crate::match_log::{MatchLogMessage, MatchLogger, StdErrMessage}; use crate::BotSpec; +// TODO: this API needs a better design with respect to pulling +// and general container management #[derive(Clone, Debug)] pub struct DockerBotSpec { pub image: String, pub binds: Option>, pub argv: Option>, pub working_dir: Option, + pub pull: bool, + pub credentials: Option, +} + +#[derive(Clone, Debug)] +pub struct Credentials { + pub username: String, + pub password: String, } #[async_trait] @@ -43,6 +53,30 @@ async fn spawn_docker_process( ) -> Result { let docker = Docker::connect_with_socket_defaults()?; + if params.pull { + let mut create_image_stream = docker.create_image( + Some(bollard::image::CreateImageOptions { + from_image: params.image.as_str(), + ..Default::default() + }), + None, + params + .credentials + .as_ref() + .map(|credentials| bollard::auth::DockerCredentials { + username: Some(credentials.username.clone()), + password: Some(credentials.password.clone()), + ..Default::default() + }), + ); + + while let Some(item) = create_image_stream.next().await { + // just consume the stream for now, + // and make noise when something breaks + let _info = item.expect("hit error in docker pull"); + } + } + let memory_limit = 512 * 1024 * 1024; // 512MB let config = container::Config { image: Some(params.image.clone()), -- cgit v1.2.3