feat: optionally start nginx proxy

This commit is contained in:
Aravinth Manivannan 2023-10-04 00:37:59 +05:30
parent 5c6db4bccd
commit 7d93129bbf
Signed by: realaravinth
GPG Key ID: F8F50389936984FF
4 changed files with 61 additions and 5 deletions

View File

@ -3,10 +3,11 @@ allow_registration = true
# source code of your copy of pages server. # source code of your copy of pages server.
source_code = "https://git.batsense.net/ForgeFlux/ftest" source_code = "https://git.batsense.net/ForgeFlux/ftest"
support_email = "support@forgeflux.org" support_email = "support@forgeflux.org"
docker_proxy = true
[server] [server]
# The port at which you want Pages to listen to # 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 address. Enter 0.0.0.0 to listen on all availale addresses
ip= "0.0.0.0" ip= "0.0.0.0"
# The number of worker threads that must be spun up by the Pages server. # The number of worker threads that must be spun up by the Pages server.

View File

@ -71,13 +71,21 @@ async fn main() -> std::io::Result<()> {
settings.init(); settings.init();
let ctx = AppCtx::new(Arc::new(Ctx::new(settings.clone()).await)); let ctx = AppCtx::new(Arc::new(Ctx::new(settings.clone()).await));
ctx.db.migrate().await.unwrap(); 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(()) Ok(())
} }
async fn serve(settings: Settings, ctx: AppCtx) -> std::io::Result<()> { async fn serve(ctx: AppCtx) -> std::io::Result<()> {
let ip = settings.server.get_ip(); let ip = ctx.settings.server.get_ip();
let workers = settings.server.workers.unwrap_or_else(num_cpus::get); let workers = ctx.settings.server.workers.unwrap_or_else(num_cpus::get);
let scheduler = runner::scheduler::Scheduler::spawn(ctx.clone()).await; let scheduler = runner::scheduler::Scheduler::spawn(ctx.clone()).await;
info!("Starting server on: http://{}", ip); info!("Starting server on: http://{}", ip);
@ -101,6 +109,7 @@ async fn serve(settings: Settings, ctx: AppCtx) -> std::io::Result<()> {
.unwrap() .unwrap()
.run() .run()
.await?; .await?;
info!("Stopping job runner");
scheduler.stop().await; scheduler.stop().await;
Ok(()) Ok(())
} }

View File

@ -25,6 +25,8 @@ pub struct TestRunnerState {
test: Test, test: Test,
} }
const FTEST_NGINX_PROXY: &str = "ftest";
impl SuiteRunnerState { impl SuiteRunnerState {
pub async fn run( pub async fn run(
container_host: &Url, container_host: &Url,
@ -35,6 +37,49 @@ impl SuiteRunnerState {
state.collect_results(ctx).await 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::<Vec<&str>>();
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( fn launch_suite(
container_host: &Url, container_host: &Url,
suite: &crate::complaince::suite::Suite, suite: &crate::complaince::suite::Suite,

View File

@ -75,6 +75,7 @@ pub struct Settings {
pub source_code: String, pub source_code: String,
pub database: Database, pub database: Database,
pub repository: Repository, pub repository: Repository,
pub docker_proxy: bool,
} }
#[cfg(not(tarpaulin_include))] #[cfg(not(tarpaulin_include))]