From 40c62234bb06c934cc4e94d9b5672057a172a356 Mon Sep 17 00:00:00 2001 From: beckerinj Date: Fri, 11 Nov 2022 04:11:27 -0500 Subject: [PATCH] write the basic git clone function --- Cargo.lock | 5 ++++ lib/git/Cargo.toml | 3 +++ lib/git/src/lib.rs | 48 ++++++++++++++++++++++++++++++--- lib/periphery_client/src/lib.rs | 14 ++++------ periphery/src/api/mod.rs | 12 +++++++-- periphery/src/main.rs | 4 +-- 6 files changed, 69 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c8e98b5be..0c9bbdd11 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -745,6 +745,11 @@ dependencies = [ [[package]] name = "git" version = "0.1.0" +dependencies = [ + "async_timing_util", + "run_command", + "types", +] [[package]] name = "h2" diff --git a/lib/git/Cargo.toml b/lib/git/Cargo.toml index c582ebe1f..21c5eb1df 100644 --- a/lib/git/Cargo.toml +++ b/lib/git/Cargo.toml @@ -6,3 +6,6 @@ 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" \ No newline at end of file diff --git a/lib/git/src/lib.rs b/lib/git/src/lib.rs index c4a2b9454..559de54e1 100644 --- a/lib/git/src/lib.rs +++ b/lib/git/src/lib.rs @@ -1,3 +1,45 @@ -pub struct GitClient; - -impl GitClient {} +#![allow(unused)] + +use async_timing_util::unix_timestamp_ms; +use run_command::async_run_command; +use types::Log; + +pub async fn clone_build_repo() {} + +pub async fn clone_deployment_repo() {} + +async fn clone( + repo: &str, + destination: &str, + branch: Option, + access_token: Option, +) -> Log { + let _ = std::fs::remove_dir_all(destination); + let access_token = match access_token { + Some(token) => { + format!("{token}@") + } + None => { + format!("") + } + }; + let branch = branch.unwrap_or("main".to_string()); + let repo_url = format!("https://{access_token}github.com/{repo}.git"); + let command = format!("git clone {repo_url} {destination} -b {branch}"); + let start_ts = unix_timestamp_ms() as i64; + let output = async_run_command(&command).await; + let command = if access_token.len() > 0 { + command.replace(&access_token, "") + } else { + command + }; + Log { + stage: "clone repo".to_string(), + command, + success: output.success(), + stdout: output.stdout, + stderr: output.stderr, + start_ts, + end_ts: unix_timestamp_ms() as i64, + } +} diff --git a/lib/periphery_client/src/lib.rs b/lib/periphery_client/src/lib.rs index c9a18301c..cc37eab07 100644 --- a/lib/periphery_client/src/lib.rs +++ b/lib/periphery_client/src/lib.rs @@ -2,7 +2,7 @@ use anyhow::{anyhow, Context}; use reqwest::StatusCode; use serde::{de::DeserializeOwned, Serialize}; use serde_json::json; -use types::{BasicContainerInfo, Server, Log, Deployment}; +use types::{BasicContainerInfo, Deployment, Log, Server}; pub struct PeripheryClient { http_client: reqwest::Client, @@ -32,7 +32,7 @@ impl PeripheryClient { .await } - pub async fn container_stop( + pub async fn container_stop( &self, server: &Server, container_name: &str, @@ -45,7 +45,7 @@ impl PeripheryClient { .await } - pub async fn container_remove( + pub async fn container_remove( &self, server: &Server, container_name: &str, @@ -59,12 +59,8 @@ impl PeripheryClient { } pub async fn deploy(&self, server: &Server, deployment: &Deployment) -> anyhow::Result { - self.post_json( - server, - &format!("/container/deploy"), - deployment, - ) - .await + self.post_json(server, &format!("/container/deploy"), deployment) + .await } async fn get_json( diff --git a/periphery/src/api/mod.rs b/periphery/src/api/mod.rs index 5be99b1f5..54a63b8af 100644 --- a/periphery/src/api/mod.rs +++ b/periphery/src/api/mod.rs @@ -1,2 +1,10 @@ -pub mod container; -pub mod stats; +use axum::Router; + +mod container; +mod stats; + +pub fn router() -> Router { + Router::new() + .nest("/container", container::router()) + .nest("/stats", stats::router()) +} diff --git a/periphery/src/main.rs b/periphery/src/main.rs index 3211a4265..0f54bec0a 100644 --- a/periphery/src/main.rs +++ b/periphery/src/main.rs @@ -14,9 +14,7 @@ use api::*; async fn main() { let (port, secrets) = config::load(); - let app = Router::new() - .nest("/container", container::router()) - .nest("/stats", stats::router()); + let app = api::router(); println!("starting montior periphery on port {port}");