From 00459f9e3d818f0fb84160862f02898d64f98110 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Thu, 14 Jul 2022 20:53:08 +0200 Subject: [PATCH] create a configuration to hold docker registry url --- planetwars-server/src/lib.rs | 11 ++++++-- planetwars-server/src/modules/bot_api.rs | 29 ++++++++++++--------- planetwars-server/src/modules/matches.rs | 33 ++++++++++++++++-------- planetwars-server/src/modules/ranking.rs | 15 ++++++++--- planetwars-server/src/routes/demo.rs | 28 ++++++++++++-------- 5 files changed, 76 insertions(+), 40 deletions(-) diff --git a/planetwars-server/src/lib.rs b/planetwars-server/src/lib.rs index fdaf800..eb69c82 100644 --- a/planetwars-server/src/lib.rs +++ b/planetwars-server/src/lib.rs @@ -10,13 +10,14 @@ pub mod util; use std::net::SocketAddr; use std::ops::Deref; +use std::sync::Arc; use bb8::{Pool, PooledConnection}; use bb8_diesel::{self, DieselConnectionManager}; use config::ConfigError; use diesel::{Connection, PgConnection}; -use modules::ranking::run_ranker; use modules::registry::registry_service; +use modules::{matches::MatchRunnerConfig, ranking::run_ranker}; use serde::Deserialize; use axum::{ @@ -120,12 +121,18 @@ pub async fn run_app() { let configuration = get_config().unwrap(); let db_pool = prepare_db(&configuration.database_url).await; - tokio::spawn(run_ranker(db_pool.clone())); + let runner_config = Arc::new(MatchRunnerConfig { + python_runner_image: "python:3.10-slim-buster".to_string(), + container_registry_url: "localhost:9001".to_string(), + }); + + tokio::spawn(run_ranker(runner_config.clone(), db_pool.clone())); tokio::spawn(run_registry(db_pool.clone())); let api_service = Router::new() .nest("/api", api()) .layer(Extension(db_pool)) + .layer(Extension(runner_config)) .into_make_service(); // TODO: put in config diff --git a/planetwars-server/src/modules/bot_api.rs b/planetwars-server/src/modules/bot_api.rs index 0ee9357..4e7d737 100644 --- a/planetwars-server/src/modules/bot_api.rs +++ b/planetwars-server/src/modules/bot_api.rs @@ -21,10 +21,11 @@ use crate::db; use crate::util::gen_alphanumeric; use crate::ConnectionPool; -use super::matches::{MatchPlayer, RunMatch}; +use super::matches::{MatchPlayer, MatchRunnerConfig, RunMatch}; pub struct BotApiServer { conn_pool: ConnectionPool, + runner_config: Arc, router: PlayerRouter, } @@ -113,15 +114,18 @@ impl pb::bot_api_service_server::BotApiService for BotApiServer { player_key: player_key.clone(), router: self.router.clone(), }); - let mut run_match = RunMatch::from_players(vec![ - MatchPlayer::BotSpec { - spec: remote_bot_spec, - }, - MatchPlayer::BotVersion { - bot: Some(opponent_bot), - version: opponent_bot_version, - }, - ]); + let run_match = RunMatch::from_players( + self.runner_config.clone(), + vec![ + MatchPlayer::BotSpec { + spec: remote_bot_spec, + }, + MatchPlayer::BotVersion { + bot: Some(opponent_bot), + version: opponent_bot_version, + }, + ], + ); let (created_match, _) = run_match .run(self.conn_pool.clone()) .await @@ -261,11 +265,12 @@ async fn schedule_timeout( .resolve_request(request_id, Err(RequestError::Timeout)); } -pub async fn run_bot_api(pool: ConnectionPool) { +pub async fn run_bot_api(runner_config: Arc, pool: ConnectionPool) { let router = PlayerRouter::new(); let server = BotApiServer { router, - conn_pool: pool.clone(), + conn_pool: pool, + runner_config, }; let addr = SocketAddr::from(([127, 0, 0, 1], 50051)); diff --git a/planetwars-server/src/modules/matches.rs b/planetwars-server/src/modules/matches.rs index 6caa8c2..07dc68b 100644 --- a/planetwars-server/src/modules/matches.rs +++ b/planetwars-server/src/modules/matches.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::{path::PathBuf, sync::Arc}; use diesel::{PgConnection, QueryResult}; use planetwars_matchrunner::{self as runner, docker_runner::DockerBotSpec, BotSpec, MatchConfig}; @@ -14,11 +14,16 @@ use crate::{ ConnectionPool, BOTS_DIR, MAPS_DIR, MATCHES_DIR, }; -const PYTHON_IMAGE: &str = "python:3.10-slim-buster"; +// TODO: add all paths +pub struct MatchRunnerConfig { + pub python_runner_image: String, + pub container_registry_url: String, +} pub struct RunMatch { log_file_name: String, players: Vec, + runner_config: Arc, } pub enum MatchPlayer { @@ -32,15 +37,16 @@ pub enum MatchPlayer { } impl RunMatch { - pub fn from_players(players: Vec) -> Self { + pub fn from_players(runner_config: Arc, players: Vec) -> Self { let log_file_name = format!("{}.log", gen_alphanumeric(16)); RunMatch { + runner_config, log_file_name, players, } } - pub fn into_runner_config(self) -> runner::MatchConfig { + fn into_runner_config(self) -> runner::MatchConfig { runner::MatchConfig { map_path: PathBuf::from(MAPS_DIR).join("hex.json"), map_name: "hex".to_string(), @@ -51,7 +57,7 @@ impl RunMatch { .map(|player| runner::MatchPlayer { bot_spec: match player { MatchPlayer::BotVersion { bot, version } => { - bot_version_to_botspec(bot.as_ref(), &version) + bot_version_to_botspec(&self.runner_config, bot.as_ref(), &version) } MatchPlayer::BotSpec { spec } => spec, }, @@ -98,16 +104,18 @@ impl RunMatch { } pub fn bot_version_to_botspec( + runner_config: &Arc, bot: Option<&db::bots::Bot>, bot_version: &db::bots::BotVersion, ) -> Box { if let Some(code_bundle_path) = &bot_version.code_bundle_path { - python_docker_bot_spec(code_bundle_path) + python_docker_bot_spec(runner_config, code_bundle_path) } else if let (Some(container_digest), Some(bot)) = (&bot_version.container_digest, bot) { - // TODO: put this in config - let registry_url = "localhost:9001"; Box::new(DockerBotSpec { - image: format!("{}/{}@{}", registry_url, bot.name, container_digest), + image: format!( + "{}/{}@{}", + runner_config.container_registry_url, bot.name, container_digest + ), binds: None, argv: None, working_dir: None, @@ -118,14 +126,17 @@ pub fn bot_version_to_botspec( } } -fn python_docker_bot_spec(code_bundle_path: &str) -> Box { +fn python_docker_bot_spec( + runner_config: &Arc, + code_bundle_path: &str, +) -> Box { 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 { - image: PYTHON_IMAGE.to_string(), + image: runner_config.python_runner_image.clone(), 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()), diff --git a/planetwars-server/src/modules/ranking.rs b/planetwars-server/src/modules/ranking.rs index 1c35394..e483d1c 100644 --- a/planetwars-server/src/modules/ranking.rs +++ b/planetwars-server/src/modules/ranking.rs @@ -6,12 +6,15 @@ use diesel::{PgConnection, QueryResult}; use rand::seq::SliceRandom; use std::collections::HashMap; use std::mem; +use std::sync::Arc; use std::time::{Duration, Instant}; use tokio; +use super::matches::MatchRunnerConfig; + const RANKER_INTERVAL: u64 = 60; -pub async fn run_ranker(db_pool: DbPool) { +pub async fn run_ranker(runner_config: Arc, db_pool: DbPool) { // TODO: make this configurable // play at most one match every n seconds let mut interval = tokio::time::interval(Duration::from_secs(RANKER_INTERVAL)); @@ -30,12 +33,16 @@ pub async fn run_ranker(db_pool: DbPool) { let mut rng = &mut rand::thread_rng(); bots.choose_multiple(&mut rng, 2).cloned().collect() }; - play_ranking_match(selected_bots, db_pool.clone()).await; + play_ranking_match(runner_config.clone(), selected_bots, db_pool.clone()).await; recalculate_ratings(&db_conn).expect("could not recalculate ratings"); } } -async fn play_ranking_match(selected_bots: Vec, db_pool: DbPool) { +async fn play_ranking_match( + runner_config: Arc, + selected_bots: Vec, + db_pool: DbPool, +) { let db_conn = db_pool.get().await.expect("could not get db pool"); let mut players = Vec::new(); for bot in &selected_bots { @@ -48,7 +55,7 @@ async fn play_ranking_match(selected_bots: Vec, db_pool: DbPool) { players.push(player); } - let (_, handle) = RunMatch::from_players(players) + let (_, handle) = RunMatch::from_players(runner_config, players) .run(db_pool.clone()) .await .expect("failed to run match"); diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs index 5ff02c7..6f2d5e6 100644 --- a/planetwars-server/src/routes/demo.rs +++ b/planetwars-server/src/routes/demo.rs @@ -1,7 +1,9 @@ +use std::sync::Arc; + use crate::db; use crate::db::matches::{FullMatchData, FullMatchPlayerData}; use crate::modules::bots::save_code_string; -use crate::modules::matches::{MatchPlayer, RunMatch}; +use crate::modules::matches::{MatchPlayer, MatchRunnerConfig, RunMatch}; use crate::ConnectionPool; use axum::extract::Extension; use axum::Json; @@ -30,6 +32,7 @@ pub struct SubmitBotResponse { pub async fn submit_bot( Json(params): Json, Extension(pool): Extension, + Extension(runner_config): Extension>, ) -> Result, StatusCode> { let conn = pool.get().await.expect("could not get database connection"); @@ -46,16 +49,19 @@ pub async fn submit_bot( // TODO: can we recover from this? .expect("could not save bot code"); - let run_match = RunMatch::from_players(vec![ - MatchPlayer::BotVersion { - bot: None, - version: player_bot_version.clone(), - }, - MatchPlayer::BotVersion { - bot: Some(opponent_bot.clone()), - version: opponent_bot_version.clone(), - }, - ]); + let run_match = RunMatch::from_players( + runner_config, + vec![ + MatchPlayer::BotVersion { + bot: None, + version: player_bot_version.clone(), + }, + MatchPlayer::BotVersion { + bot: Some(opponent_bot.clone()), + version: opponent_bot_version.clone(), + }, + ], + ); let (match_data, _) = run_match .run(pool.clone()) .await