From 7d93129bbf82c8d1b89ad7fd2cd4e9150048c167 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Wed, 4 Oct 2023 00:37:59 +0530 Subject: [PATCH] feat: optionally start nginx proxy --- config/default.toml | 3 ++- src/main.rs | 17 +++++++++++++---- src/runner/suite.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/settings.rs | 1 + 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/config/default.toml b/config/default.toml index de2c5d6..cb4dffe 100644 --- a/config/default.toml +++ b/config/default.toml @@ -3,10 +3,11 @@ allow_registration = true # source code of your copy of pages server. source_code = "https://git.batsense.net/ForgeFlux/ftest" support_email = "support@forgeflux.org" +docker_proxy = true [server] # The port at which you want Pages to listen to -port = 9000 +port = 29130 #IP address. Enter 0.0.0.0 to listen on all availale addresses ip= "0.0.0.0" # The number of worker threads that must be spun up by the Pages server. diff --git a/src/main.rs b/src/main.rs index 74bc3db..2550159 100644 --- a/src/main.rs +++ b/src/main.rs @@ -71,13 +71,21 @@ async fn main() -> std::io::Result<()> { settings.init(); let ctx = AppCtx::new(Arc::new(Ctx::new(settings.clone()).await)); ctx.db.migrate().await.unwrap(); - serve(settings, ctx).await?; + + if ctx.settings.docker_proxy { + crate::runner::suite::SuiteRunnerState::run_proxy(&ctx) + } + + serve(ctx.clone()).await?; + if ctx.settings.docker_proxy { + crate::runner::suite::SuiteRunnerState::stop_proxy(&ctx); + } Ok(()) } -async fn serve(settings: Settings, ctx: AppCtx) -> std::io::Result<()> { - let ip = settings.server.get_ip(); - let workers = settings.server.workers.unwrap_or_else(num_cpus::get); +async fn serve(ctx: AppCtx) -> std::io::Result<()> { + let ip = ctx.settings.server.get_ip(); + let workers = ctx.settings.server.workers.unwrap_or_else(num_cpus::get); let scheduler = runner::scheduler::Scheduler::spawn(ctx.clone()).await; info!("Starting server on: http://{}", ip); @@ -101,6 +109,7 @@ async fn serve(settings: Settings, ctx: AppCtx) -> std::io::Result<()> { .unwrap() .run() .await?; + info!("Stopping job runner"); scheduler.stop().await; Ok(()) } diff --git a/src/runner/suite.rs b/src/runner/suite.rs index e77e897..353df6c 100644 --- a/src/runner/suite.rs +++ b/src/runner/suite.rs @@ -25,6 +25,8 @@ pub struct TestRunnerState { test: Test, } +const FTEST_NGINX_PROXY: &str = "ftest"; + impl SuiteRunnerState { pub async fn run( container_host: &Url, @@ -35,6 +37,49 @@ impl SuiteRunnerState { state.collect_results(ctx).await } + pub fn run_proxy(ctx: &crate::ctx::ArcCtx) { + Self::stop_proxy(ctx); + + let mut default = ""; + let nets = std::process::Command::new("/sbin/ip") + .arg("route") + .output() + .unwrap(); + let nets = String::from_utf8(nets.stdout).unwrap(); + for l in nets.lines() { + let f = l.split(' ').collect::>(); + if let Some("default") = f.get(0).map(|s| *s) { + default = f.get(8).unwrap(); + log::info!("Found default net: {default}"); + } + } + + let args = [ + "run", + "-d", + "-p", + "9080:9000", + "--name", + FTEST_NGINX_PROXY, + "--network", + "ftest", + "--add-host", + &format!("ftest_backend:{default}"), + "forgeflux/ftest-nginx-proxy", + ]; + let mut child = std::process::Command::new("docker") + .args(&args) + .spawn() + .expect("unable to obtain Docker version"); + child.wait().unwrap(); + log::info!("Started {FTEST_NGINX_PROXY} nginx proxy"); + } + + pub fn stop_proxy(ctx: &crate::ctx::ArcCtx) { + ctx.docker.rm_container(FTEST_NGINX_PROXY, true); + log::info!("Stopped {FTEST_NGINX_PROXY} nginx proxy"); + } + fn launch_suite( container_host: &Url, suite: &crate::complaince::suite::Suite, diff --git a/src/settings.rs b/src/settings.rs index 9081abb..c2b8a3a 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -75,6 +75,7 @@ pub struct Settings { pub source_code: String, pub database: Database, pub repository: Repository, + pub docker_proxy: bool, } #[cfg(not(tarpaulin_include))]