tying it together: execute docker bots

This commit is contained in:
Ilion Beyst 2022-07-11 20:43:10 +02:00
parent ec1d50f655
commit 0b9a9f0eaa
2 changed files with 23 additions and 14 deletions

View file

@ -1,5 +1,4 @@
use std::io;
use std::path::PathBuf;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
@ -19,8 +18,9 @@ use crate::BotSpec;
#[derive(Clone, Debug)]
pub struct DockerBotSpec {
pub image: String,
pub code_path: PathBuf,
pub argv: Vec<String>,
pub binds: Option<Vec<String>>,
pub argv: Option<Vec<String>>,
pub working_dir: Option<String>,
}
#[async_trait]
@ -42,14 +42,12 @@ async fn spawn_docker_process(
params: &DockerBotSpec,
) -> Result<ContainerProcess, bollard::errors::Error> {
let docker = Docker::connect_with_socket_defaults()?;
let bot_code_dir = std::fs::canonicalize(&params.code_path).unwrap();
let code_dir_str = bot_code_dir.as_os_str().to_str().unwrap();
let memory_limit = 512 * 1024 * 1024; // 512MB
let config = container::Config {
image: Some(params.image.clone()),
host_config: Some(bollard::models::HostConfig {
binds: Some(vec![format!("{}:{}", code_dir_str, "/workdir")]),
binds: params.binds.clone(),
network_mode: Some("none".to_string()),
memory: Some(memory_limit),
memory_swap: Some(memory_limit),
@ -59,8 +57,8 @@ async fn spawn_docker_process(
// cpu_quota: Some(10_000),
..Default::default()
}),
working_dir: Some("/workdir".to_string()),
cmd: Some(params.argv.clone()),
working_dir: params.working_dir.clone(),
cmd: params.argv.clone(),
attach_stdin: Some(true),
attach_stdout: Some(true),
attach_stderr: Some(true),

View file

@ -111,25 +111,36 @@ impl RunMatch {
}
pub fn bot_version_to_botspec(
_bot: &db::bots::Bot,
bot: &db::bots::Bot,
bot_version: &db::bots::BotVersion,
) -> Box<dyn BotSpec> {
if let Some(code_bundle_path) = &bot_version.code_bundle_path {
python_docker_bot_spec(code_bundle_path)
} else if let Some(_container_digest) = &bot_version.container_digest {
unimplemented!()
} else if let Some(container_digest) = &bot_version.container_digest {
// TODO: put this in config
let registry_url = "localhost:9001";
Box::new(DockerBotSpec {
image: format!("{}/{}@{}", registry_url, bot.name, container_digest),
binds: None,
argv: None,
working_dir: None,
})
} else {
panic!("bad bot version")
}
}
fn python_docker_bot_spec(code_bundle_path: &str) -> Box<dyn BotSpec> {
let code_bundle_abs_path = PathBuf::from(BOTS_DIR).join(code_bundle_path);
let code_bundle_rel_path = PathBuf::from(BOTS_DIR).join(code_bundle_path);
let code_bundle_abs_path = std::fs::canonicalize(&code_bundle_rel_path).unwrap();
let code_bundle_path_str = code_bundle_abs_path.as_os_str().to_str().unwrap();
// TODO: it would be good to simplify this configuration
Box::new(DockerBotSpec {
code_path: code_bundle_abs_path,
image: PYTHON_IMAGE.to_string(),
argv: vec!["python".to_string(), "bot.py".to_string()],
binds: Some(vec![format!("{}:{}", code_bundle_path_str, "/workdir")]),
argv: Some(vec!["python".to_string(), "bot.py".to_string()]),
working_dir: Some("/workdir".to_string()),
})
}