2023-09-29 19:18:33 +05:30
|
|
|
// Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
|
|
|
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
2023-09-05 19:21:30 +05:30
|
|
|
use std::env;
|
2023-10-04 18:33:38 +05:30
|
|
|
use std::path::PathBuf;
|
2023-09-29 19:18:33 +05:30
|
|
|
use std::sync::Arc;
|
2023-09-05 19:21:30 +05:30
|
|
|
|
|
|
|
use actix_web::{
|
|
|
|
error::InternalError, http::StatusCode, middleware as actix_middleware, web::Data as WebData,
|
|
|
|
web::JsonConfig, App, HttpServer,
|
|
|
|
};
|
2023-10-04 18:33:38 +05:30
|
|
|
use clap::{Args, Parser, Subcommand, ValueEnum};
|
2023-09-05 19:21:30 +05:30
|
|
|
//use static_assets::FileMap;
|
|
|
|
use tracing::info;
|
|
|
|
use tracing_actix_web::TracingLogger;
|
|
|
|
//
|
|
|
|
//pub use crate::api::v1::ROUTES as V1_API_ROUTES;
|
2023-10-04 18:33:38 +05:30
|
|
|
use ctx::CliCtx;
|
|
|
|
use ctx::DaemonCtx;
|
|
|
|
use ctx::MinAppContext;
|
2023-09-05 19:21:30 +05:30
|
|
|
pub use settings::Settings;
|
|
|
|
|
|
|
|
pub const CACHE_AGE: u32 = 604800;
|
|
|
|
|
|
|
|
pub const GIT_COMMIT_HASH: &str = env!("GIT_HASH");
|
|
|
|
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
|
|
|
pub const PKG_NAME: &str = env!("CARGO_PKG_NAME");
|
|
|
|
pub const PKG_DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
|
|
|
|
pub const PKG_HOMEPAGE: &str = env!("CARGO_PKG_HOMEPAGE");
|
|
|
|
|
2023-10-04 02:14:47 +05:30
|
|
|
pub type AppFullCtx = WebData<ctx::ArcFullCtx>;
|
|
|
|
pub type AppMinCtx = WebData<ctx::ArcMinCtx>;
|
2023-09-05 19:21:30 +05:30
|
|
|
|
2023-09-29 19:18:33 +05:30
|
|
|
mod api;
|
|
|
|
mod complaince;
|
2023-09-05 19:21:30 +05:30
|
|
|
mod ctx;
|
|
|
|
mod db;
|
|
|
|
mod docker;
|
2023-09-29 19:18:33 +05:30
|
|
|
mod docker_compose;
|
2023-09-05 19:21:30 +05:30
|
|
|
mod errors;
|
|
|
|
mod git;
|
2023-10-04 21:07:07 +05:30
|
|
|
mod pages;
|
2023-09-29 19:18:33 +05:30
|
|
|
mod runner;
|
2023-09-05 19:21:30 +05:30
|
|
|
mod settings;
|
|
|
|
mod utils;
|
|
|
|
|
|
|
|
//lazy_static::lazy_static! {
|
|
|
|
// pub static ref FILES: FileMap = FileMap::new();
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//#[derive(Parser)]
|
|
|
|
//#[clap(author, version, about, long_about = None)]
|
2023-10-04 18:33:38 +05:30
|
|
|
#[derive(Parser)]
|
|
|
|
#[clap(author, version, about, long_about = None)]
|
|
|
|
struct Cli {
|
|
|
|
#[command(subcommand)]
|
|
|
|
command: Command,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Subcommand)]
|
|
|
|
enum Command {
|
|
|
|
Daemon,
|
|
|
|
Verify { path: PathBuf },
|
|
|
|
Test { path: PathBuf },
|
|
|
|
}
|
2023-09-05 19:21:30 +05:30
|
|
|
|
|
|
|
#[actix_web::main]
|
|
|
|
#[cfg(not(tarpaulin_include))]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
|
|
|
if env::var("RUST_LOG").is_err() {
|
|
|
|
env::set_var("RUST_LOG", "info");
|
|
|
|
}
|
|
|
|
|
|
|
|
pretty_env_logger::init();
|
2023-10-04 18:33:38 +05:30
|
|
|
let cli = Cli::parse();
|
2023-09-05 19:21:30 +05:30
|
|
|
|
|
|
|
info!(
|
|
|
|
"{}: {}.\nFor more information, see: {}\nBuild info:\nVersion: {} commit: {}",
|
|
|
|
PKG_NAME, PKG_DESCRIPTION, PKG_HOMEPAGE, VERSION, GIT_COMMIT_HASH
|
|
|
|
);
|
|
|
|
|
2023-10-04 18:33:38 +05:30
|
|
|
match cli.command {
|
|
|
|
Command::Daemon => run_daemon().await,
|
|
|
|
Command::Verify { path } => {
|
|
|
|
let s = std::fs::read_to_string(path).unwrap();
|
|
|
|
let _: complaince::target::Target = toml::from_str(&s).unwrap();
|
|
|
|
println!("Syntax OK");
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Command::Test { path } => {
|
|
|
|
let ctx: Arc<dyn MinAppContext> = Arc::new(CliCtx::new());
|
2023-10-04 19:04:47 +05:30
|
|
|
let serv = basic_server(ctx.clone()).await;
|
2023-10-04 19:20:16 +05:30
|
|
|
|
|
|
|
loop {
|
|
|
|
log::info!("Waiting for server to start...");
|
|
|
|
let res = reqwest::get(&format!(
|
|
|
|
"http://localhost:29130{}",
|
|
|
|
api::v1::meta::META.build_details
|
|
|
|
))
|
|
|
|
.await;
|
|
|
|
if res.is_ok() {
|
|
|
|
log::info!("Waiting server started");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tokio::time::sleep(std::time::Duration::new(2, 0)).await;
|
|
|
|
}
|
|
|
|
|
2023-10-04 18:33:38 +05:30
|
|
|
crate::runner::suite::SuiteRunnerState::run_proxy(ctx.as_ref());
|
2023-10-05 00:57:36 +05:30
|
|
|
let (suite_results, init_containers, specimen_logs) =
|
2023-10-04 18:33:38 +05:30
|
|
|
crate::runner::target::run_target(ctx.as_ref(), path.clone()).await;
|
|
|
|
let content = crate::runner::results::ArchivableResult {
|
|
|
|
commit: "".into(),
|
|
|
|
suites: suite_results,
|
|
|
|
init_containers,
|
2023-10-05 00:57:36 +05:30
|
|
|
specimen_logs,
|
2023-10-04 18:33:38 +05:30
|
|
|
};
|
2023-10-04 21:07:07 +05:30
|
|
|
let results_file = path.join("results.json");
|
2023-10-04 18:33:38 +05:30
|
|
|
println!("Writing results to: {:?}", path.canonicalize());
|
|
|
|
std::fs::write(results_file, serde_json::to_string(&content).unwrap()).unwrap();
|
2023-10-04 21:07:07 +05:30
|
|
|
let results_html_file = path.join("results.html");
|
|
|
|
let contents = pages::ViewResult::new(pages::Payload { results: content }).render();
|
|
|
|
std::fs::write(results_html_file, contents).unwrap();
|
|
|
|
println!("Writing results to: {:?}", path.canonicalize());
|
2023-10-04 18:33:38 +05:30
|
|
|
|
|
|
|
crate::runner::suite::SuiteRunnerState::stop_proxy(ctx.as_ref());
|
2023-10-04 21:07:07 +05:30
|
|
|
|
2023-10-04 19:04:47 +05:30
|
|
|
serv.stop(true).await;
|
2023-10-04 18:33:38 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn run_daemon() -> std::io::Result<()> {
|
2023-09-05 19:21:30 +05:30
|
|
|
let settings = Settings::new().unwrap();
|
2023-09-29 19:18:33 +05:30
|
|
|
settings.init();
|
2023-10-04 18:33:38 +05:30
|
|
|
let inner_ctx = Arc::new(DaemonCtx::new(settings.clone()).await);
|
2023-10-04 02:14:47 +05:30
|
|
|
let ctx = AppFullCtx::new(inner_ctx.clone());
|
|
|
|
let ctx2 = AppMinCtx::new(inner_ctx);
|
|
|
|
ctx.db().migrate().await.unwrap();
|
2023-10-04 00:37:59 +05:30
|
|
|
|
2023-10-04 02:14:47 +05:30
|
|
|
if ctx.settings().docker_proxy {
|
|
|
|
crate::runner::suite::SuiteRunnerState::run_proxy(ctx.as_ref());
|
2023-10-04 00:37:59 +05:30
|
|
|
}
|
|
|
|
|
2023-10-04 18:33:38 +05:30
|
|
|
daemon(ctx.clone(), ctx2).await?;
|
2023-10-04 02:14:47 +05:30
|
|
|
if ctx.settings().docker_proxy {
|
|
|
|
crate::runner::suite::SuiteRunnerState::stop_proxy(ctx.as_ref());
|
2023-10-04 00:37:59 +05:30
|
|
|
}
|
2023-09-05 19:21:30 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-04 19:04:47 +05:30
|
|
|
async fn basic_server(ctx: Arc<dyn MinAppContext>) -> actix_web::dev::ServerHandle {
|
|
|
|
let ctx = AppMinCtx::new(ctx);
|
|
|
|
info!("Starting server on: http://0.0.0.0:29130");
|
|
|
|
let serv = HttpServer::new(move || {
|
2023-10-04 18:33:38 +05:30
|
|
|
App::new()
|
|
|
|
.wrap(TracingLogger::default())
|
|
|
|
.wrap(actix_middleware::Compress::default())
|
2023-10-04 19:04:47 +05:30
|
|
|
.app_data(ctx.clone())
|
2023-10-04 18:33:38 +05:30
|
|
|
.app_data(get_json_err())
|
|
|
|
.wrap(
|
|
|
|
actix_middleware::DefaultHeaders::new()
|
|
|
|
.add(("Permissions-Policy", "interest-cohort=()")),
|
|
|
|
)
|
|
|
|
.wrap(actix_middleware::NormalizePath::new(
|
|
|
|
actix_middleware::TrailingSlash::Trim,
|
|
|
|
))
|
|
|
|
.configure(services)
|
|
|
|
})
|
2023-10-04 19:04:47 +05:30
|
|
|
.bind("0.0.0.0:29130")
|
2023-10-04 18:33:38 +05:30
|
|
|
.unwrap()
|
2023-10-04 19:04:47 +05:30
|
|
|
.run();
|
|
|
|
let handle = serv.handle();
|
|
|
|
tokio::spawn(serv);
|
|
|
|
handle
|
2023-10-04 18:33:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
async fn daemon(ctx: AppFullCtx, ctx2: AppMinCtx) -> std::io::Result<()> {
|
2023-10-04 02:14:47 +05:30
|
|
|
let ip = ctx.settings().server.get_ip();
|
|
|
|
let workers = ctx.settings().server.workers.unwrap_or_else(num_cpus::get);
|
2023-09-29 19:34:21 +05:30
|
|
|
let scheduler = runner::scheduler::Scheduler::spawn(ctx.clone()).await;
|
2023-09-05 19:21:30 +05:30
|
|
|
|
|
|
|
info!("Starting server on: http://{}", ip);
|
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
|
|
|
.wrap(TracingLogger::default())
|
|
|
|
.wrap(actix_middleware::Compress::default())
|
|
|
|
.app_data(ctx.clone())
|
2023-10-04 02:14:47 +05:30
|
|
|
.app_data(ctx2.clone())
|
2023-09-05 19:21:30 +05:30
|
|
|
.app_data(get_json_err())
|
|
|
|
.wrap(
|
|
|
|
actix_middleware::DefaultHeaders::new()
|
|
|
|
.add(("Permissions-Policy", "interest-cohort=()")),
|
|
|
|
)
|
|
|
|
.wrap(actix_middleware::NormalizePath::new(
|
|
|
|
actix_middleware::TrailingSlash::Trim,
|
|
|
|
))
|
|
|
|
.configure(services)
|
|
|
|
})
|
|
|
|
.workers(workers)
|
|
|
|
.bind(ip)
|
|
|
|
.unwrap()
|
|
|
|
.run()
|
2023-09-29 19:18:33 +05:30
|
|
|
.await?;
|
2023-10-04 00:37:59 +05:30
|
|
|
info!("Stopping job runner");
|
2023-09-29 19:18:33 +05:30
|
|
|
scheduler.stop().await;
|
|
|
|
Ok(())
|
2023-09-05 19:21:30 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(tarpaulin_include))]
|
|
|
|
pub fn get_json_err() -> JsonConfig {
|
|
|
|
JsonConfig::default().error_handler(|err, _| {
|
|
|
|
//debug!("JSON deserialization error: {:?}", &err);
|
|
|
|
InternalError::new(err, StatusCode::BAD_REQUEST).into()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
|
2023-09-29 19:18:33 +05:30
|
|
|
crate::api::v1::services(cfg);
|
2023-09-05 19:21:30 +05:30
|
|
|
}
|