start implementing the periphery client

This commit is contained in:
beckerinj
2022-11-06 22:50:01 -05:00
parent fd13240134
commit 841b458f05
14 changed files with 2813 additions and 2708 deletions
Generated
+2667 -2654
View File
File diff suppressed because it is too large Load Diff
+6 -1
View File
@@ -4,5 +4,10 @@ members = [
"cli",
"core",
"periphery",
"builder"
"builder",
"lib/db_client",
"lib/docker_client",
"lib/git_client",
"lib/periphery_client",
"lib/types"
]
+1 -1
View File
@@ -17,10 +17,10 @@ tower = { version = "0.4", features = ["full"] }
tower-http = { version = "0.3", features = ["cors"] }
slack = { package = "slack_client_rs", version = "0.0.7" }
mungos = "0.2.24"
dotenv = "0.15"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
dotenv = "0.15"
envy = "0.4"
oauth2 = "4.2.3"
anyhow = "1.0"
+2 -1
View File
@@ -33,7 +33,8 @@ pub struct JwtClient {
impl JwtClient {
pub fn extension(config: &CoreConfig) -> JwtExtension {
let key = Hmac::new_from_slice(config.jwt_secret.as_bytes()).unwrap();
let key = Hmac::new_from_slice(config.jwt_secret.as_bytes())
.expect("failed at taking HmacSha256 of jwt secret");
let client = JwtClient {
key,
valid_for_ms: get_timelength_in_ms(config.jwt_valid_for),
+13 -2
View File
@@ -11,8 +11,19 @@ use dotenv::dotenv;
use mungos::{Deserialize, Mungos};
use types::CoreConfig;
#[derive(Deserialize, Debug)]
struct Env {
#[serde(default = "default_config_path")]
pub config_path: String,
}
pub fn load() -> CoreConfig {
let config_path = env::var("CONFIG_PATH").unwrap_or("./config.json".to_string());
let file = File::open(config_path).expect("failed to open config file");
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()
}
pub fn default_config_path() -> String {
"./config.json".to_string()
}
+1 -1
View File
@@ -28,5 +28,5 @@ async fn main() {
axum::Server::bind(&get_socket_addr(&config))
.serve(app.into_make_service())
.await
.expect("server crashed");
.expect("monitor core axum server crashed");
}
+2 -1
View File
@@ -10,4 +10,5 @@ types = { path = "../types" }
run_command = { version = "0.0.5", features = ["async_tokio"] }
bollard = "0.13"
anyhow = "1.0"
thiserror = "1.0"
thiserror = "1.0"
axum = "0.5"
+53 -44
View File
@@ -1,44 +1,53 @@
use anyhow::anyhow;
use bollard::{container::ListContainersOptions, Docker};
use types::BasicContainerInfo;
mod build;
mod deploy;
pub struct DockerClient {
client: Docker,
}
impl DockerClient {
pub fn new() -> anyhow::Result<DockerClient> {
Ok(DockerClient {
client: Docker::connect_with_local_defaults()?,
})
}
pub async fn list_containers(&self) -> anyhow::Result<Vec<BasicContainerInfo>> {
let res = self
.client
.list_containers(Some(ListContainersOptions::<String> {
all: true,
..Default::default()
}))
.await?
.into_iter()
.map(|s| {
let info = BasicContainerInfo {
name: s
.names
.ok_or(anyhow!("no names on container"))?
.pop()
.ok_or(anyhow!("no names on container (empty vec)"))?
.replace("/", ""),
state: s.state.unwrap().parse().unwrap(),
status: s.status,
};
Ok::<_, anyhow::Error>(info)
})
.collect::<anyhow::Result<Vec<BasicContainerInfo>>>()?;
Ok(res)
}
}
#![allow(unused)]
use std::sync::Arc;
use anyhow::anyhow;
use axum::Extension;
use bollard::{container::ListContainersOptions, Docker};
use types::BasicContainerInfo;
mod build;
mod deploy;
pub type DockerExtenstion = Extension<Arc<DockerClient>>;
pub struct DockerClient {
client: Docker,
}
impl DockerClient {
pub fn extension() -> DockerExtenstion {
let client = DockerClient {
client: Docker::connect_with_local_defaults()
.expect("failed to connect to docker daemon"),
};
Extension(Arc::new(client))
}
pub async fn list_containers(&self) -> anyhow::Result<Vec<BasicContainerInfo>> {
let res = self
.client
.list_containers(Some(ListContainersOptions::<String> {
all: true,
..Default::default()
}))
.await?
.into_iter()
.map(|s| {
let info = BasicContainerInfo {
name: s
.names
.ok_or(anyhow!("no names on container"))?
.pop()
.ok_or(anyhow!("no names on container (empty vec)"))?
.replace("/", ""),
state: s.state.unwrap().parse().unwrap(),
status: s.status,
};
Ok::<_, anyhow::Error>(info)
})
.collect::<anyhow::Result<Vec<BasicContainerInfo>>>()?;
Ok(res)
}
}
+9
View File
@@ -0,0 +1,9 @@
[package]
name = "periphery_client"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
reqwest = "0.11"
+1
View File
@@ -0,0 +1 @@
+6 -1
View File
@@ -6,6 +6,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
types = { path = "../lib/types" }
docker = { package = "docker_client", path = "../lib/docker_client" }
git = { package = "git_client", path = "../lib/git_client" }
tokio = { version = "1.21", features = ["full"] }
axum = { version = "0.5" }
tower = { version = "0.4", features = ["full"] }
@@ -13,4 +16,6 @@ dotenv = "0.15"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
bollard = "0.13"
bollard = "0.13"
anyhow = "1.0"
envy = "0.4"
+28
View File
@@ -0,0 +1,28 @@
use std::fs::File;
use serde::Deserialize;
use types::PeripherySecrets;
#[derive(Deserialize)]
struct Env {
#[serde(default = "default_port")]
port: u16,
#[serde(default = "default_secrets_path")]
secrets_path: String,
}
pub fn load() -> (u16, PeripherySecrets) {
let env: Env = envy::from_env().expect("failed to parse env");
let secrets_file = File::open(&env.secrets_path).expect("failed to find secrets");
let secrets: PeripherySecrets =
serde_json::from_reader(secrets_file).expect("failed to parse secrets file");
(env.port, secrets)
}
fn default_port() -> u16 {
8000
}
fn default_secrets_path() -> String {
"/secrets/secrets.json".to_string()
}
+5
View File
@@ -0,0 +1,5 @@
use std::{net::SocketAddr, str::FromStr};
pub fn get_socket_addr(port: u16) -> SocketAddr {
SocketAddr::from_str(&format!("0.0.0.0:{}", port)).expect("failed to parse socket addr")
}
+19 -2
View File
@@ -1,2 +1,19 @@
#[tokio::main]
async fn main() {}
use axum::Router;
use docker::DockerClient;
use helpers::get_socket_addr;
mod config;
mod helpers;
#[tokio::main]
async fn main() {
let (port, secrets) = config::load();
let app = Router::new()
.layer(DockerClient::extension());
axum::Server::bind(&get_socket_addr(port))
.serve(app.into_make_service())
.await
.expect("monitor periphery axum server crashed");
}