create a configuration to hold docker registry url
This commit is contained in:
parent
668409e76d
commit
00459f9e3d
5 changed files with 76 additions and 40 deletions
|
@ -10,13 +10,14 @@ pub mod util;
|
||||||
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use bb8::{Pool, PooledConnection};
|
use bb8::{Pool, PooledConnection};
|
||||||
use bb8_diesel::{self, DieselConnectionManager};
|
use bb8_diesel::{self, DieselConnectionManager};
|
||||||
use config::ConfigError;
|
use config::ConfigError;
|
||||||
use diesel::{Connection, PgConnection};
|
use diesel::{Connection, PgConnection};
|
||||||
use modules::ranking::run_ranker;
|
|
||||||
use modules::registry::registry_service;
|
use modules::registry::registry_service;
|
||||||
|
use modules::{matches::MatchRunnerConfig, ranking::run_ranker};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
|
@ -120,12 +121,18 @@ pub async fn run_app() {
|
||||||
let configuration = get_config().unwrap();
|
let configuration = get_config().unwrap();
|
||||||
let db_pool = prepare_db(&configuration.database_url).await;
|
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()));
|
tokio::spawn(run_registry(db_pool.clone()));
|
||||||
|
|
||||||
let api_service = Router::new()
|
let api_service = Router::new()
|
||||||
.nest("/api", api())
|
.nest("/api", api())
|
||||||
.layer(Extension(db_pool))
|
.layer(Extension(db_pool))
|
||||||
|
.layer(Extension(runner_config))
|
||||||
.into_make_service();
|
.into_make_service();
|
||||||
|
|
||||||
// TODO: put in config
|
// TODO: put in config
|
||||||
|
|
|
@ -21,10 +21,11 @@ use crate::db;
|
||||||
use crate::util::gen_alphanumeric;
|
use crate::util::gen_alphanumeric;
|
||||||
use crate::ConnectionPool;
|
use crate::ConnectionPool;
|
||||||
|
|
||||||
use super::matches::{MatchPlayer, RunMatch};
|
use super::matches::{MatchPlayer, MatchRunnerConfig, RunMatch};
|
||||||
|
|
||||||
pub struct BotApiServer {
|
pub struct BotApiServer {
|
||||||
conn_pool: ConnectionPool,
|
conn_pool: ConnectionPool,
|
||||||
|
runner_config: Arc<MatchRunnerConfig>,
|
||||||
router: PlayerRouter,
|
router: PlayerRouter,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,15 +114,18 @@ impl pb::bot_api_service_server::BotApiService for BotApiServer {
|
||||||
player_key: player_key.clone(),
|
player_key: player_key.clone(),
|
||||||
router: self.router.clone(),
|
router: self.router.clone(),
|
||||||
});
|
});
|
||||||
let mut run_match = RunMatch::from_players(vec![
|
let run_match = RunMatch::from_players(
|
||||||
MatchPlayer::BotSpec {
|
self.runner_config.clone(),
|
||||||
spec: remote_bot_spec,
|
vec![
|
||||||
},
|
MatchPlayer::BotSpec {
|
||||||
MatchPlayer::BotVersion {
|
spec: remote_bot_spec,
|
||||||
bot: Some(opponent_bot),
|
},
|
||||||
version: opponent_bot_version,
|
MatchPlayer::BotVersion {
|
||||||
},
|
bot: Some(opponent_bot),
|
||||||
]);
|
version: opponent_bot_version,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
);
|
||||||
let (created_match, _) = run_match
|
let (created_match, _) = run_match
|
||||||
.run(self.conn_pool.clone())
|
.run(self.conn_pool.clone())
|
||||||
.await
|
.await
|
||||||
|
@ -261,11 +265,12 @@ async fn schedule_timeout(
|
||||||
.resolve_request(request_id, Err(RequestError::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<MatchRunnerConfig>, pool: ConnectionPool) {
|
||||||
let router = PlayerRouter::new();
|
let router = PlayerRouter::new();
|
||||||
let server = BotApiServer {
|
let server = BotApiServer {
|
||||||
router,
|
router,
|
||||||
conn_pool: pool.clone(),
|
conn_pool: pool,
|
||||||
|
runner_config,
|
||||||
};
|
};
|
||||||
|
|
||||||
let addr = SocketAddr::from(([127, 0, 0, 1], 50051));
|
let addr = SocketAddr::from(([127, 0, 0, 1], 50051));
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::path::PathBuf;
|
use std::{path::PathBuf, sync::Arc};
|
||||||
|
|
||||||
use diesel::{PgConnection, QueryResult};
|
use diesel::{PgConnection, QueryResult};
|
||||||
use planetwars_matchrunner::{self as runner, docker_runner::DockerBotSpec, BotSpec, MatchConfig};
|
use planetwars_matchrunner::{self as runner, docker_runner::DockerBotSpec, BotSpec, MatchConfig};
|
||||||
|
@ -14,11 +14,16 @@ use crate::{
|
||||||
ConnectionPool, BOTS_DIR, MAPS_DIR, MATCHES_DIR,
|
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 {
|
pub struct RunMatch {
|
||||||
log_file_name: String,
|
log_file_name: String,
|
||||||
players: Vec<MatchPlayer>,
|
players: Vec<MatchPlayer>,
|
||||||
|
runner_config: Arc<MatchRunnerConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum MatchPlayer {
|
pub enum MatchPlayer {
|
||||||
|
@ -32,15 +37,16 @@ pub enum MatchPlayer {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RunMatch {
|
impl RunMatch {
|
||||||
pub fn from_players(players: Vec<MatchPlayer>) -> Self {
|
pub fn from_players(runner_config: Arc<MatchRunnerConfig>, players: Vec<MatchPlayer>) -> Self {
|
||||||
let log_file_name = format!("{}.log", gen_alphanumeric(16));
|
let log_file_name = format!("{}.log", gen_alphanumeric(16));
|
||||||
RunMatch {
|
RunMatch {
|
||||||
|
runner_config,
|
||||||
log_file_name,
|
log_file_name,
|
||||||
players,
|
players,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_runner_config(self) -> runner::MatchConfig {
|
fn into_runner_config(self) -> runner::MatchConfig {
|
||||||
runner::MatchConfig {
|
runner::MatchConfig {
|
||||||
map_path: PathBuf::from(MAPS_DIR).join("hex.json"),
|
map_path: PathBuf::from(MAPS_DIR).join("hex.json"),
|
||||||
map_name: "hex".to_string(),
|
map_name: "hex".to_string(),
|
||||||
|
@ -51,7 +57,7 @@ impl RunMatch {
|
||||||
.map(|player| runner::MatchPlayer {
|
.map(|player| runner::MatchPlayer {
|
||||||
bot_spec: match player {
|
bot_spec: match player {
|
||||||
MatchPlayer::BotVersion { bot, version } => {
|
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,
|
MatchPlayer::BotSpec { spec } => spec,
|
||||||
},
|
},
|
||||||
|
@ -98,16 +104,18 @@ impl RunMatch {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bot_version_to_botspec(
|
pub fn bot_version_to_botspec(
|
||||||
|
runner_config: &Arc<MatchRunnerConfig>,
|
||||||
bot: Option<&db::bots::Bot>,
|
bot: Option<&db::bots::Bot>,
|
||||||
bot_version: &db::bots::BotVersion,
|
bot_version: &db::bots::BotVersion,
|
||||||
) -> Box<dyn BotSpec> {
|
) -> Box<dyn BotSpec> {
|
||||||
if let Some(code_bundle_path) = &bot_version.code_bundle_path {
|
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) {
|
} 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 {
|
Box::new(DockerBotSpec {
|
||||||
image: format!("{}/{}@{}", registry_url, bot.name, container_digest),
|
image: format!(
|
||||||
|
"{}/{}@{}",
|
||||||
|
runner_config.container_registry_url, bot.name, container_digest
|
||||||
|
),
|
||||||
binds: None,
|
binds: None,
|
||||||
argv: None,
|
argv: None,
|
||||||
working_dir: None,
|
working_dir: None,
|
||||||
|
@ -118,14 +126,17 @@ pub fn bot_version_to_botspec(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn python_docker_bot_spec(code_bundle_path: &str) -> Box<dyn BotSpec> {
|
fn python_docker_bot_spec(
|
||||||
|
runner_config: &Arc<MatchRunnerConfig>,
|
||||||
|
code_bundle_path: &str,
|
||||||
|
) -> Box<dyn BotSpec> {
|
||||||
let code_bundle_rel_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_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();
|
let code_bundle_path_str = code_bundle_abs_path.as_os_str().to_str().unwrap();
|
||||||
|
|
||||||
// TODO: it would be good to simplify this configuration
|
// TODO: it would be good to simplify this configuration
|
||||||
Box::new(DockerBotSpec {
|
Box::new(DockerBotSpec {
|
||||||
image: PYTHON_IMAGE.to_string(),
|
image: runner_config.python_runner_image.clone(),
|
||||||
binds: Some(vec![format!("{}:{}", code_bundle_path_str, "/workdir")]),
|
binds: Some(vec![format!("{}:{}", code_bundle_path_str, "/workdir")]),
|
||||||
argv: Some(vec!["python".to_string(), "bot.py".to_string()]),
|
argv: Some(vec!["python".to_string(), "bot.py".to_string()]),
|
||||||
working_dir: Some("/workdir".to_string()),
|
working_dir: Some("/workdir".to_string()),
|
||||||
|
|
|
@ -6,12 +6,15 @@ use diesel::{PgConnection, QueryResult};
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
use std::sync::Arc;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
use tokio;
|
use tokio;
|
||||||
|
|
||||||
|
use super::matches::MatchRunnerConfig;
|
||||||
|
|
||||||
const RANKER_INTERVAL: u64 = 60;
|
const RANKER_INTERVAL: u64 = 60;
|
||||||
|
|
||||||
pub async fn run_ranker(db_pool: DbPool) {
|
pub async fn run_ranker(runner_config: Arc<MatchRunnerConfig>, db_pool: DbPool) {
|
||||||
// TODO: make this configurable
|
// TODO: make this configurable
|
||||||
// play at most one match every n seconds
|
// play at most one match every n seconds
|
||||||
let mut interval = tokio::time::interval(Duration::from_secs(RANKER_INTERVAL));
|
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();
|
let mut rng = &mut rand::thread_rng();
|
||||||
bots.choose_multiple(&mut rng, 2).cloned().collect()
|
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");
|
recalculate_ratings(&db_conn).expect("could not recalculate ratings");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn play_ranking_match(selected_bots: Vec<Bot>, db_pool: DbPool) {
|
async fn play_ranking_match(
|
||||||
|
runner_config: Arc<MatchRunnerConfig>,
|
||||||
|
selected_bots: Vec<Bot>,
|
||||||
|
db_pool: DbPool,
|
||||||
|
) {
|
||||||
let db_conn = db_pool.get().await.expect("could not get db pool");
|
let db_conn = db_pool.get().await.expect("could not get db pool");
|
||||||
let mut players = Vec::new();
|
let mut players = Vec::new();
|
||||||
for bot in &selected_bots {
|
for bot in &selected_bots {
|
||||||
|
@ -48,7 +55,7 @@ async fn play_ranking_match(selected_bots: Vec<Bot>, db_pool: DbPool) {
|
||||||
players.push(player);
|
players.push(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
let (_, handle) = RunMatch::from_players(players)
|
let (_, handle) = RunMatch::from_players(runner_config, players)
|
||||||
.run(db_pool.clone())
|
.run(db_pool.clone())
|
||||||
.await
|
.await
|
||||||
.expect("failed to run match");
|
.expect("failed to run match");
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::db;
|
use crate::db;
|
||||||
use crate::db::matches::{FullMatchData, FullMatchPlayerData};
|
use crate::db::matches::{FullMatchData, FullMatchPlayerData};
|
||||||
use crate::modules::bots::save_code_string;
|
use crate::modules::bots::save_code_string;
|
||||||
use crate::modules::matches::{MatchPlayer, RunMatch};
|
use crate::modules::matches::{MatchPlayer, MatchRunnerConfig, RunMatch};
|
||||||
use crate::ConnectionPool;
|
use crate::ConnectionPool;
|
||||||
use axum::extract::Extension;
|
use axum::extract::Extension;
|
||||||
use axum::Json;
|
use axum::Json;
|
||||||
|
@ -30,6 +32,7 @@ pub struct SubmitBotResponse {
|
||||||
pub async fn submit_bot(
|
pub async fn submit_bot(
|
||||||
Json(params): Json<SubmitBotParams>,
|
Json(params): Json<SubmitBotParams>,
|
||||||
Extension(pool): Extension<ConnectionPool>,
|
Extension(pool): Extension<ConnectionPool>,
|
||||||
|
Extension(runner_config): Extension<Arc<MatchRunnerConfig>>,
|
||||||
) -> Result<Json<SubmitBotResponse>, StatusCode> {
|
) -> Result<Json<SubmitBotResponse>, StatusCode> {
|
||||||
let conn = pool.get().await.expect("could not get database connection");
|
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?
|
// TODO: can we recover from this?
|
||||||
.expect("could not save bot code");
|
.expect("could not save bot code");
|
||||||
|
|
||||||
let run_match = RunMatch::from_players(vec![
|
let run_match = RunMatch::from_players(
|
||||||
MatchPlayer::BotVersion {
|
runner_config,
|
||||||
bot: None,
|
vec![
|
||||||
version: player_bot_version.clone(),
|
MatchPlayer::BotVersion {
|
||||||
},
|
bot: None,
|
||||||
MatchPlayer::BotVersion {
|
version: player_bot_version.clone(),
|
||||||
bot: Some(opponent_bot.clone()),
|
},
|
||||||
version: opponent_bot_version.clone(),
|
MatchPlayer::BotVersion {
|
||||||
},
|
bot: Some(opponent_bot.clone()),
|
||||||
]);
|
version: opponent_bot_version.clone(),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
);
|
||||||
let (match_data, _) = run_match
|
let (match_data, _) = run_match
|
||||||
.run(pool.clone())
|
.run(pool.clone())
|
||||||
.await
|
.await
|
||||||
|
|
Loading…
Reference in a new issue