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 = "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.

View file

@ -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(())
}

View file

@ -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::<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(
container_host: &Url,
suite: &crate::complaince::suite::Suite,

View file

@ -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))]