forked from github-starred/komodo
pass core config as toml
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,5 +4,6 @@ dist
|
||||
.env
|
||||
|
||||
config.json
|
||||
config.toml
|
||||
secrets.json
|
||||
secrets.toml
|
||||
47
Cargo.lock
generated
47
Cargo.lock
generated
@@ -250,10 +250,9 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"axum",
|
||||
"docker",
|
||||
"dotenv",
|
||||
"envy",
|
||||
"git",
|
||||
"helpers",
|
||||
"mungos",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
@@ -526,19 +525,6 @@ dependencies = [
|
||||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "docker"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async_timing_util",
|
||||
"axum",
|
||||
"bollard",
|
||||
"run_command",
|
||||
"thiserror",
|
||||
"types",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dotenv"
|
||||
version = "0.15.0"
|
||||
@@ -742,15 +728,6 @@ dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "git"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"async_timing_util",
|
||||
"run_command",
|
||||
"types",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "h2"
|
||||
version = "0.3.14"
|
||||
@@ -792,6 +769,22 @@ version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
|
||||
|
||||
[[package]]
|
||||
name = "helpers"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async_timing_util",
|
||||
"axum",
|
||||
"bollard",
|
||||
"run_command",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
"serde_json",
|
||||
"toml",
|
||||
"types",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.1.19"
|
||||
@@ -1239,10 +1232,9 @@ dependencies = [
|
||||
"axum-extra",
|
||||
"bcrypt",
|
||||
"db_client",
|
||||
"docker",
|
||||
"dotenv",
|
||||
"envy",
|
||||
"git",
|
||||
"helpers",
|
||||
"hmac",
|
||||
"jwt",
|
||||
"mungos",
|
||||
@@ -1265,10 +1257,9 @@ dependencies = [
|
||||
"anyhow",
|
||||
"axum",
|
||||
"bollard",
|
||||
"docker",
|
||||
"dotenv",
|
||||
"envy",
|
||||
"git",
|
||||
"helpers",
|
||||
"run_command",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
|
||||
@@ -6,8 +6,7 @@ members = [
|
||||
"periphery",
|
||||
"builder",
|
||||
"lib/db_client",
|
||||
"lib/docker",
|
||||
"lib/git",
|
||||
"lib/helpers",
|
||||
"lib/periphery_client",
|
||||
"lib/types"
|
||||
"lib/types",
|
||||
]
|
||||
@@ -6,8 +6,7 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
docker = { path = "../lib/docker" }
|
||||
git = { path = "../lib/git" }
|
||||
helpers = { path = "../lib/helpers" }
|
||||
types = { path = "../lib/types" }
|
||||
tokio = { version = "1.21", features = ["full"] }
|
||||
axum = { version = "0.5", features = ["ws"] }
|
||||
|
||||
@@ -6,8 +6,7 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
docker = { path = "../lib/docker" }
|
||||
git = { path = "../lib/git" }
|
||||
helpers = { path = "../lib/helpers" }
|
||||
db = { package = "db_client", path = "../lib/db_client" }
|
||||
types = { path = "../lib/types" }
|
||||
tokio = { version = "1.21", features = ["full"] }
|
||||
|
||||
18
core/config.example.json
Normal file
18
core/config.example.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"port": 9000,
|
||||
"docker_accounts": {},
|
||||
"github_accounts": {},
|
||||
"github_oauth": {
|
||||
"id": "your_client_id",
|
||||
"secret": "your_client_secret"
|
||||
},
|
||||
"github_webhook_secret": "your_random_webhook_secret",
|
||||
"jwt_secret": "your_jwt_secret",
|
||||
"jwt_valid_for": "1-wk",
|
||||
"slack_url": "your_slack_app_webhook_url",
|
||||
"mongo": {
|
||||
"uri": "your_mongo_uri",
|
||||
"app_name": "monitor_core",
|
||||
"db_name": "monitor"
|
||||
}
|
||||
}
|
||||
20
core/config.example.toml
Normal file
20
core/config.example.toml
Normal file
@@ -0,0 +1,20 @@
|
||||
port = 9000
|
||||
jwt_secret = "your_jwt_secret"
|
||||
jwt_valid_for = "1-wk"
|
||||
slack_url = "your_slack_app_webhook_url"
|
||||
github_webhook_secret = "your_random_webhook_secret"
|
||||
|
||||
[github_oauth]
|
||||
id = "your_client_id"
|
||||
secret = "your_client_secret"
|
||||
|
||||
[mongo]
|
||||
uri = "your_mongo_uri"
|
||||
app_name = "monitor_core"
|
||||
db_name = "monitor"
|
||||
|
||||
[github_accounts]
|
||||
github_username = "github_token"
|
||||
|
||||
[docker_accounts]
|
||||
docker_username = "docker_token"
|
||||
@@ -1,17 +0,0 @@
|
||||
{
|
||||
"PORT": 9000,
|
||||
"PASSKEY": "your-default-passkey",
|
||||
"DOCKER_ACCOUNTS": {},
|
||||
"GITHUB_ACCOUNTS": {},
|
||||
"GITHUB_OAUTH": {
|
||||
"ID": "your_client_id",
|
||||
"SECRET": "your_client_secret"
|
||||
},
|
||||
"GITHUB_WEBHOOK_SECRET": "your_random_webhook_secret",
|
||||
"JWT_SECRET": "your_jwt_secret",
|
||||
"JWT_VALID_FOR": "1-wk",
|
||||
"SLACK_URL": "your_slack_app_webhook_url",
|
||||
"MONGO_URI": "your_mongo_uri",
|
||||
"MONGO_APP_NAME": "monitor_core",
|
||||
"MONGO_DB_NAME": "monitor"
|
||||
}
|
||||
@@ -8,6 +8,7 @@ use std::{
|
||||
|
||||
use db::{DbClient, DbExtension};
|
||||
use dotenv::dotenv;
|
||||
use helpers::parse_config_file;
|
||||
use mungos::{Deserialize, Mungos};
|
||||
use types::CoreConfig;
|
||||
|
||||
@@ -20,8 +21,7 @@ struct Env {
|
||||
pub fn load() -> CoreConfig {
|
||||
dotenv().ok();
|
||||
let env: Env = envy::from_env().expect("failed to parse environment variables");
|
||||
let file = File::open(&env.config_path).expect("failed to open config file");
|
||||
serde_json::from_reader(file).unwrap()
|
||||
parse_config_file(&env.config_path)
|
||||
}
|
||||
|
||||
pub fn default_config_path() -> String {
|
||||
|
||||
@@ -3,14 +3,15 @@
|
||||
use auth::JwtClient;
|
||||
use axum::{http::StatusCode, Router};
|
||||
use db::DbClient;
|
||||
use docker::DockerClient;
|
||||
use helpers::get_socket_addr;
|
||||
use ::helpers::docker::DockerClient;
|
||||
|
||||
mod api;
|
||||
mod auth;
|
||||
mod config;
|
||||
mod helpers;
|
||||
|
||||
use crate::helpers::get_socket_addr;
|
||||
|
||||
type ResponseResult<T> = Result<T, (StatusCode, String)>;
|
||||
|
||||
#[tokio::main]
|
||||
@@ -21,7 +22,7 @@ async fn main() {
|
||||
.nest("/api", api::router())
|
||||
.nest("/auth", auth::router(&config))
|
||||
.layer(JwtClient::extension(&config))
|
||||
.layer(DbClient::extension((&config).into()).await);
|
||||
.layer(DbClient::extension(config.mongo.clone()).await);
|
||||
|
||||
println!("starting monitor_core on localhost:{}", config.port);
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ use collections::{
|
||||
updates_collection, users_collection,
|
||||
};
|
||||
use mungos::{Collection, Mungos};
|
||||
use types::{Build, CoreConfig, Deployment, Procedure, Server, Update, User};
|
||||
use types::{Build, Deployment, Procedure, Server, Update, User, MongoConfig};
|
||||
|
||||
mod collections;
|
||||
|
||||
@@ -21,28 +21,12 @@ pub struct DbClient {
|
||||
pub updates: Collection<Update>,
|
||||
}
|
||||
|
||||
pub struct DbConfig {
|
||||
mongo_uri: String,
|
||||
mongo_app_name: String,
|
||||
mongo_db_name: String,
|
||||
}
|
||||
|
||||
impl From<&CoreConfig> for DbConfig {
|
||||
fn from(config: &CoreConfig) -> DbConfig {
|
||||
DbConfig {
|
||||
mongo_uri: config.mongo_uri.clone(),
|
||||
mongo_app_name: config.mongo_app_name.clone(),
|
||||
mongo_db_name: config.mongo_db_name.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DbClient {
|
||||
pub async fn extension(config: DbConfig) -> DbExtension {
|
||||
let db_name = &config.mongo_db_name;
|
||||
pub async fn extension(config: MongoConfig) -> DbExtension {
|
||||
let db_name = &config.db_name;
|
||||
let mungos = Mungos::new(
|
||||
&config.mongo_uri,
|
||||
&config.mongo_app_name,
|
||||
&config.uri,
|
||||
&config.app_name,
|
||||
Duration::from_secs(3),
|
||||
None,
|
||||
)
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
[package]
|
||||
name = "git"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
types = { path = "../types" }
|
||||
run_command = "0.0.5"
|
||||
async_timing_util = "0.1.11"
|
||||
@@ -1,15 +1,18 @@
|
||||
[package]
|
||||
name = "docker"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
types = { path = "../types" }
|
||||
run_command = { version = "0.0.5", features = ["async_tokio"] }
|
||||
bollard = "0.13"
|
||||
anyhow = "1.0"
|
||||
thiserror = "1.0"
|
||||
axum = "0.5"
|
||||
async_timing_util = "0.1.11"
|
||||
[package]
|
||||
name = "helpers"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
types = { path = "../types" }
|
||||
run_command = "0.0.5"
|
||||
async_timing_util = "0.1.11"
|
||||
bollard = "0.13"
|
||||
anyhow = "1.0"
|
||||
axum = "0.5"
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
serde_json = "1.0"
|
||||
toml = "0.5"
|
||||
@@ -1,5 +1,3 @@
|
||||
#![allow(unused)]
|
||||
|
||||
use async_timing_util::unix_timestamp_ms;
|
||||
use run_command::async_run_command;
|
||||
use types::{Build, Deployment, Log};
|
||||
@@ -13,10 +11,13 @@ pub async fn clone_build_repo(
|
||||
on_clone,
|
||||
..
|
||||
}: &Build,
|
||||
) {
|
||||
) -> Log {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub async fn clone_deployment_repo(Deployment { .. }: &Deployment) {}
|
||||
pub async fn clone_deployment_repo(Deployment { .. }: &Deployment) -> Log {
|
||||
todo!()
|
||||
}
|
||||
|
||||
async fn clone(
|
||||
repo: &str,
|
||||
19
lib/helpers/src/lib.rs
Normal file
19
lib/helpers/src/lib.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use std::{fs::File, io::Read};
|
||||
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
pub mod docker;
|
||||
pub mod git;
|
||||
|
||||
pub fn parse_config_file<T: DeserializeOwned>(path: &str) -> T {
|
||||
let mut file = File::open(&path).expect(&format!("failed to find config at {path}"));
|
||||
if path.ends_with("toml") {
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents).expect(&format!("failed to read toml at {path}"));
|
||||
toml::from_str(&contents).expect(&format!("failed to parse toml at {path}"))
|
||||
} else if path.ends_with("json") {
|
||||
serde_json::from_reader(file).expect(&format!("failed to parse json at {path}"))
|
||||
} else {
|
||||
panic!("unsupported config file type: {}", path)
|
||||
}
|
||||
}
|
||||
@@ -225,14 +225,10 @@ pub struct OauthCredentials {
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
#[serde(rename_all = "UPPERCASE")]
|
||||
pub struct CoreConfig {
|
||||
// port the core web server runs on
|
||||
pub port: u16,
|
||||
|
||||
// default periphery passkey
|
||||
pub passkey: String,
|
||||
|
||||
// docker integration
|
||||
pub docker_accounts: DockerAccounts,
|
||||
|
||||
@@ -248,12 +244,17 @@ pub struct CoreConfig {
|
||||
// integration with slack app
|
||||
pub slack_url: Option<String>,
|
||||
|
||||
//mongo config
|
||||
pub mongo_uri: String,
|
||||
// mongo config
|
||||
pub mongo: MongoConfig,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct MongoConfig {
|
||||
pub uri: String,
|
||||
#[serde(default = "default_core_mongo_app_name")]
|
||||
pub mongo_app_name: String,
|
||||
pub app_name: String,
|
||||
#[serde(default = "default_core_mongo_db_name")]
|
||||
pub mongo_db_name: String,
|
||||
pub db_name: String,
|
||||
}
|
||||
|
||||
fn default_core_mongo_app_name() -> String {
|
||||
@@ -266,7 +267,6 @@ fn default_core_mongo_db_name() -> String {
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct PeripherySecrets {
|
||||
pub passkey: String,
|
||||
#[serde(default)]
|
||||
pub docker_accounts: DockerAccounts,
|
||||
#[serde(default)]
|
||||
|
||||
@@ -6,9 +6,8 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
helpers = { path = "../lib/helpers" }
|
||||
types = { path = "../lib/types" }
|
||||
docker = { path = "../lib/docker" }
|
||||
git = { path = "../lib/git" }
|
||||
run_command = { version = "0.0.5", features = ["async_tokio"] }
|
||||
tokio = { version = "1.21", features = ["full"] }
|
||||
axum = { version = "0.5" }
|
||||
|
||||
@@ -4,7 +4,7 @@ use axum::{
|
||||
routing::{get, post},
|
||||
Extension, Json, Router,
|
||||
};
|
||||
use docker::{
|
||||
use helpers::docker::{
|
||||
deploy, docker_start, docker_stop, docker_stop_and_remove, parse_container_name, DockerClient,
|
||||
DockerExtension,
|
||||
};
|
||||
|
||||
@@ -3,6 +3,7 @@ use std::{fs::File, io::Read};
|
||||
use dotenv::dotenv;
|
||||
use serde::Deserialize;
|
||||
use types::PeripherySecrets;
|
||||
use helpers::parse_config_file;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Env {
|
||||
@@ -15,7 +16,7 @@ struct Env {
|
||||
pub fn load() -> (u16, PeripherySecrets) {
|
||||
dotenv().ok();
|
||||
let env: Env = envy::from_env().expect("failed to parse env");
|
||||
let secrets = read_secrets(&env.secrets_path);
|
||||
let secrets = parse_config_file(&env.secrets_path);
|
||||
(env.port, secrets)
|
||||
}
|
||||
|
||||
@@ -25,17 +26,4 @@ fn default_port() -> u16 {
|
||||
|
||||
fn default_secrets_path() -> String {
|
||||
"/secrets/secrets.toml".to_string()
|
||||
}
|
||||
|
||||
fn read_secrets(secrets_path: &str) -> PeripherySecrets {
|
||||
let mut secrets_file = File::open(&secrets_path).expect("failed to find secrets");
|
||||
if secrets_path.ends_with("toml") {
|
||||
let mut contents = String::new();
|
||||
secrets_file.read_to_string(&mut contents);
|
||||
toml::from_str(&contents).expect("failed to parse secrets toml")
|
||||
} else if secrets_path.ends_with("json") {
|
||||
serde_json::from_reader(secrets_file).expect("failed to parse secrets json")
|
||||
} else {
|
||||
panic!("unsupported secrets file type: {}", secrets_path)
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
#![allow(unused)]
|
||||
|
||||
use axum::{extract::Path, http::StatusCode, routing::get, Extension, Json, Router};
|
||||
use docker::{DockerClient, DockerExtension};
|
||||
use helpers::{get_socket_addr, handle_anyhow_error};
|
||||
|
||||
mod api;
|
||||
mod config;
|
||||
mod helpers;
|
||||
|
||||
use crate::helpers::get_socket_addr;
|
||||
|
||||
use api::*;
|
||||
|
||||
#[tokio::main]
|
||||
|
||||
Reference in New Issue
Block a user