From 1a45fffe75fd5e1bf882a61a8c0e1f84b93d7366 Mon Sep 17 00:00:00 2001 From: mbecker20 Date: Wed, 5 Jun 2024 23:44:06 -0700 Subject: [PATCH] move some libraries out --- Cargo.lock | 21 +++ Cargo.toml | 2 + bin/core/src/api/execute/mod.rs | 4 + bin/core/src/api/execute/sync.rs | 17 +++ bin/core/src/api/read/mod.rs | 8 ++ bin/core/src/api/read/sync.rs | 123 ++++++++++++++++++ bin/core/src/api/write/mod.rs | 7 + bin/core/src/api/write/sync.rs | 61 +++++++++ bin/core/src/helpers/update.rs | 9 ++ bin/core/src/resource/sync.rs | 52 +++++--- bin/core/src/state.rs | 10 ++ bin/periphery/Cargo.toml | 2 + bin/periphery/src/api/build.rs | 2 +- bin/periphery/src/api/container.rs | 2 +- bin/periphery/src/api/git.rs | 22 +++- bin/periphery/src/api/mod.rs | 4 +- bin/periphery/src/api/network.rs | 2 +- .../src/{helpers => }/docker/build.rs | 6 +- .../src/{helpers => }/docker/client.rs | 0 .../src/{helpers => }/docker/container.rs | 11 +- bin/periphery/src/{helpers => }/docker/mod.rs | 3 +- .../src/{helpers => }/docker/network.rs | 3 +- .../src/{helpers/mod.rs => helpers.rs} | 37 +----- bin/periphery/src/main.rs | 1 + client/core/rs/src/entities/mod.rs | 6 + client/core/rs/src/entities/sync.rs | 4 +- client/periphery/rs/src/api/git.rs | 10 +- lib/command/Cargo.toml | 12 ++ lib/command/src/lib.rs | 29 +++++ lib/git/Cargo.toml | 17 +++ .../src/helpers/git.rs => lib/git/src/lib.rs | 63 ++++----- 31 files changed, 429 insertions(+), 121 deletions(-) create mode 100644 bin/core/src/api/execute/sync.rs create mode 100644 bin/core/src/api/read/sync.rs create mode 100644 bin/core/src/api/write/sync.rs rename bin/periphery/src/{helpers => }/docker/build.rs (97%) rename bin/periphery/src/{helpers => }/docker/client.rs (100%) rename bin/periphery/src/{helpers => }/docker/container.rs (98%) rename bin/periphery/src/{helpers => }/docker/mod.rs (97%) rename bin/periphery/src/{helpers => }/docker/network.rs (94%) rename bin/periphery/src/{helpers/mod.rs => helpers.rs} (50%) create mode 100644 lib/command/Cargo.toml create mode 100644 lib/command/src/lib.rs create mode 100644 lib/git/Cargo.toml rename bin/periphery/src/helpers/git.rs => lib/git/src/lib.rs (83%) diff --git a/Cargo.lock b/Cargo.lock index 924827056..e65f35218 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -921,6 +921,14 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "command" +version = "1.6.2" +dependencies = [ + "monitor_client", + "run_command", +] + [[package]] name = "convert_case" version = "0.4.0" @@ -1413,6 +1421,17 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +[[package]] +name = "git" +version = "1.6.2" +dependencies = [ + "anyhow", + "command", + "monitor_client", + "run_command", + "tracing", +] + [[package]] name = "glob" version = "0.3.1" @@ -2248,8 +2267,10 @@ dependencies = [ "axum-extra", "bollard", "clap", + "command", "dotenv", "envy", + "git", "logger", "merge_config_files", "monitor_client", diff --git a/Cargo.toml b/Cargo.toml index e649bb2cc..2f6bb7ade 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,9 @@ monitor_client = { path = "client/core/rs" } # LOCAL monitor_client = "1.6.2" periphery_client = { path = "client/periphery/rs" } +command = { path = "lib/command" } logger = { path = "lib/logger" } +git = { path = "lib/git" } # MOGH run_command = { version = "0.0.6", features = ["async_tokio"] } diff --git a/bin/core/src/api/execute/mod.rs b/bin/core/src/api/execute/mod.rs index 00d9f788b..0f778bd78 100644 --- a/bin/core/src/api/execute/mod.rs +++ b/bin/core/src/api/execute/mod.rs @@ -28,6 +28,7 @@ mod procedure; mod repo; mod server; mod server_template; +mod sync; #[typeshare] #[derive(Serialize, Deserialize, Debug, Clone, Resolver)] @@ -60,6 +61,9 @@ pub enum ExecuteRequest { // ==== SERVER TEMPLATE ==== LaunchServer(LaunchServer), + + // ==== SYNC ==== + RunSync(RunSync), } pub fn router() -> Router { diff --git a/bin/core/src/api/execute/sync.rs b/bin/core/src/api/execute/sync.rs new file mode 100644 index 000000000..a3bbcc33c --- /dev/null +++ b/bin/core/src/api/execute/sync.rs @@ -0,0 +1,17 @@ +use monitor_client::{ + api::execute::RunSync, + entities::{update::Update, user::User}, +}; +use resolver_api::Resolve; + +use crate::state::State; + +impl Resolve for State { + async fn resolve( + &self, + RunSync { sync }: RunSync, + (user, update): (User, Update), + ) -> anyhow::Result { + todo!() + } +} diff --git a/bin/core/src/api/read/mod.rs b/bin/core/src/api/read/mod.rs index 9288ade36..e1c3f45aa 100644 --- a/bin/core/src/api/read/mod.rs +++ b/bin/core/src/api/read/mod.rs @@ -23,6 +23,7 @@ mod repo; mod search; mod server; mod server_template; +mod sync; mod tag; mod toml; mod update; @@ -114,6 +115,13 @@ enum ReadRequest { ListFullRepos(ListFullRepos), GetRepoActionState(GetRepoActionState), + // ==== SYNC ==== + GetResourceSyncsSummary(GetResourceSyncsSummary), + GetResourceSync(GetResourceSync), + ListResourceSyncs(ListResourceSyncs), + ListFullResourceSyncs(ListFullResourceSyncs), + GetResourceSyncActionState(GetResourceSyncActionState), + // ==== BUILDER ==== GetBuildersSummary(GetBuildersSummary), GetBuilder(GetBuilder), diff --git a/bin/core/src/api/read/sync.rs b/bin/core/src/api/read/sync.rs new file mode 100644 index 000000000..773abb1d7 --- /dev/null +++ b/bin/core/src/api/read/sync.rs @@ -0,0 +1,123 @@ +use anyhow::Context; +use monitor_client::{ + api::read::*, + entities::{ + permission::PermissionLevel, + sync::{ + ResourceSync, ResourceSyncActionState, ResourceSyncListItem, + ResourceSyncState, + }, + user::User, + }, +}; +use resolver_api::Resolve; + +use crate::{ + resource, + state::{action_states, resource_sync_state_cache, State}, +}; + +impl Resolve for State { + async fn resolve( + &self, + GetResourceSync { sync }: GetResourceSync, + user: User, + ) -> anyhow::Result { + resource::get_check_permissions::( + &sync, + &user, + PermissionLevel::Read, + ) + .await + } +} + +impl Resolve for State { + async fn resolve( + &self, + ListResourceSyncs { query }: ListResourceSyncs, + user: User, + ) -> anyhow::Result> { + resource::list_for_user::(query, &user).await + } +} + +impl Resolve for State { + async fn resolve( + &self, + ListFullResourceSyncs { query }: ListFullResourceSyncs, + user: User, + ) -> anyhow::Result { + resource::list_full_for_user::(query, &user).await + } +} + +impl Resolve for State { + async fn resolve( + &self, + GetResourceSyncActionState { sync }: GetResourceSyncActionState, + user: User, + ) -> anyhow::Result { + let sync = resource::get_check_permissions::( + &sync, + &user, + PermissionLevel::Read, + ) + .await?; + let action_state = action_states() + .resource_sync + .get(&sync.id) + .await + .unwrap_or_default() + .get()?; + Ok(action_state) + } +} + +impl Resolve for State { + async fn resolve( + &self, + GetResourceSyncsSummary {}: GetResourceSyncsSummary, + user: User, + ) -> anyhow::Result { + let resource_syncs = + resource::list_full_for_user::( + Default::default(), + &user, + ) + .await + .context("failed to get resource_syncs from db")?; + + let mut res = GetResourceSyncsSummaryResponse::default(); + + let cache = resource_sync_state_cache(); + let action_states = action_states(); + + for resource_sync in resource_syncs { + res.total += 1; + + match ( + cache.get(&resource_sync.id).await.unwrap_or_default(), + action_states + .resource_sync + .get(&resource_sync.id) + .await + .unwrap_or_default() + .get()?, + ) { + (_, action_states) if action_states.syncing => { + res.syncing += 1; + } + (ResourceSyncState::Ok, _) => res.ok += 1, + (ResourceSyncState::Failed, _) => res.failed += 1, + (ResourceSyncState::Unknown, _) => res.unknown += 1, + // will never come off the cache in the building state, since that comes from action states + (ResourceSyncState::Syncing, _) => { + unreachable!() + } + } + } + + Ok(res) + } +} diff --git a/bin/core/src/api/write/mod.rs b/bin/core/src/api/write/mod.rs index 4ca55c3fa..3d7fb4130 100644 --- a/bin/core/src/api/write/mod.rs +++ b/bin/core/src/api/write/mod.rs @@ -23,6 +23,7 @@ mod repo; mod server; mod server_template; mod service_user; +mod sync; mod tag; mod user_group; mod variable; @@ -105,6 +106,12 @@ enum WriteRequest { DeleteProcedure(DeleteProcedure), UpdateProcedure(UpdateProcedure), + // ==== SYNC ==== + CreateResourceSync(CreateResourceSync), + CopyResourceSync(CopyResourceSync), + DeleteResourceSync(DeleteResourceSync), + UpdateResourceSync(UpdateResourceSync), + // ==== TAG ==== CreateTag(CreateTag), DeleteTag(DeleteTag), diff --git a/bin/core/src/api/write/sync.rs b/bin/core/src/api/write/sync.rs new file mode 100644 index 000000000..2fcd62491 --- /dev/null +++ b/bin/core/src/api/write/sync.rs @@ -0,0 +1,61 @@ +use monitor_client::{ + api::write::*, + entities::{ + permission::PermissionLevel, sync::ResourceSync, user::User, + }, +}; +use resolver_api::Resolve; + +use crate::{resource, state::State}; + +impl Resolve for State { + #[instrument(name = "CreateResourceSync", skip(self, user))] + async fn resolve( + &self, + CreateResourceSync { name, config }: CreateResourceSync, + user: User, + ) -> anyhow::Result { + resource::create::(&name, config, &user).await + } +} + +impl Resolve for State { + #[instrument(name = "CopyResourceSync", skip(self, user))] + async fn resolve( + &self, + CopyResourceSync { name, id }: CopyResourceSync, + user: User, + ) -> anyhow::Result { + let ResourceSync { config, .. } = + resource::get_check_permissions::( + &id, + &user, + PermissionLevel::Write, + ) + .await?; + resource::create::(&name, config.into(), &user) + .await + } +} + +impl Resolve for State { + #[instrument(name = "DeleteResourceSync", skip(self, user))] + async fn resolve( + &self, + DeleteResourceSync { id }: DeleteResourceSync, + user: User, + ) -> anyhow::Result { + resource::delete::(&id, &user).await + } +} + +impl Resolve for State { + #[instrument(name = "UpdateResourceSync", skip(self, user))] + async fn resolve( + &self, + UpdateResourceSync { id, config }: UpdateResourceSync, + user: User, + ) -> anyhow::Result { + resource::update::(&id, config, &user).await + } +} diff --git a/bin/core/src/helpers/update.rs b/bin/core/src/helpers/update.rs index 09da2799e..c7bafb1de 100644 --- a/bin/core/src/helpers/update.rs +++ b/bin/core/src/helpers/update.rs @@ -7,6 +7,7 @@ use monitor_client::entities::{ repo::Repo, server::Server, server_template::ServerTemplate, + sync::ResourceSync, update::{ResourceTarget, Update, UpdateListItem}, user::User, Operation, @@ -205,6 +206,14 @@ pub async fn init_execution_update( .id, ), ), + + // Resource Sync + ExecuteRequest::RunSync(data) => ( + Operation::RunSync, + ResourceTarget::ResourceSync( + resource::get::(&data.sync).await?.id, + ), + ), }; let mut update = make_update(target, operation, user); update.in_progress(); diff --git a/bin/core/src/resource/sync.rs b/bin/core/src/resource/sync.rs index 4418ea824..bae085614 100644 --- a/bin/core/src/resource/sync.rs +++ b/bin/core/src/resource/sync.rs @@ -3,7 +3,8 @@ use monitor_client::entities::{ sync::{ PartialResourceSyncConfig, ResourceSync, ResourceSyncConfig, ResourceSyncConfigDiff, ResourceSyncInfo, ResourceSyncListItem, - ResourceSyncQuerySpecifics, + ResourceSyncListItemInfo, ResourceSyncQuerySpecifics, + ResourceSyncState, }, update::{ResourceTargetVariant, Update}, user::User, @@ -33,7 +34,20 @@ impl super::MonitorResource for ResourceSync { async fn to_list_item( resource_sync: Resource, ) -> Self::ListItem { - todo!() + ResourceSyncListItem { + id: resource_sync.id, + name: resource_sync.name, + tags: resource_sync.tags, + resource_type: ResourceTargetVariant::ResourceSync, + info: ResourceSyncListItemInfo { + repo: resource_sync.config.repo, + branch: resource_sync.config.branch, + last_sync_ts: 0, + last_sync_hash: String::new(), + last_sync_message: String::new(), + state: ResourceSyncState::Unknown, + }, + } } async fn busy(id: &String) -> anyhow::Result { @@ -56,17 +70,17 @@ impl super::MonitorResource for ResourceSync { } async fn validate_create_config( - config: &mut Self::PartialConfig, - user: &User, + _config: &mut Self::PartialConfig, + _user: &User, ) -> anyhow::Result<()> { - todo!() + Ok(()) } async fn post_create( - created: &Resource, - update: &mut Update, + _created: &Resource, + _update: &mut Update, ) -> anyhow::Result<()> { - todo!() + Ok(()) } // UPDATE @@ -76,18 +90,18 @@ impl super::MonitorResource for ResourceSync { } async fn validate_update_config( - id: &str, - config: &mut Self::PartialConfig, - user: &User, + _id: &str, + _config: &mut Self::PartialConfig, + _user: &User, ) -> anyhow::Result<()> { - todo!() + Ok(()) } async fn post_update( - updated: &Resource, - update: &mut Update, + _updated: &Resource, + _update: &mut Update, ) -> anyhow::Result<()> { - todo!() + Ok(()) } // DELETE @@ -97,15 +111,15 @@ impl super::MonitorResource for ResourceSync { } async fn pre_delete( - resource: &Resource, - update: &mut Update, + _resource: &Resource, + _update: &mut Update, ) -> anyhow::Result<()> { Ok(()) } async fn post_delete( - resource: &Resource, - update: &mut Update, + _resource: &Resource, + _update: &mut Update, ) -> anyhow::Result<()> { Ok(()) } diff --git a/bin/core/src/state.rs b/bin/core/src/state.rs index 4e6eae358..1eaa4254d 100644 --- a/bin/core/src/state.rs +++ b/bin/core/src/state.rs @@ -3,6 +3,7 @@ use std::sync::{Arc, OnceLock}; use monitor_client::entities::{ build::BuildState, deployment::DeploymentState, procedure::ProcedureState, repo::RepoState, + sync::ResourceSyncState, }; use tokio::sync::OnceCell; @@ -89,3 +90,12 @@ pub fn procedure_state_cache() -> &'static ProcedureStateCache { OnceLock::new(); PROCEDURE_STATE_CACHE.get_or_init(Default::default) } + +pub type ResourceSyncStateCache = Cache; + +pub fn resource_sync_state_cache() -> &'static ResourceSyncStateCache +{ + static RESOURCE_SYNC_STATE_CACHE: OnceLock = + OnceLock::new(); + RESOURCE_SYNC_STATE_CACHE.get_or_init(Default::default) +} diff --git a/bin/periphery/Cargo.toml b/bin/periphery/Cargo.toml index d6fab9d8e..6ef4fc009 100644 --- a/bin/periphery/Cargo.toml +++ b/bin/periphery/Cargo.toml @@ -17,7 +17,9 @@ path = "src/main.rs" # local monitor_client = { workspace = true, features = ["docker"] } periphery_client.workspace = true +command.workspace = true logger.workspace = true +git.workspace = true # mogh serror = { workspace = true, features = ["axum"] } async_timing_util.workspace = true diff --git a/bin/periphery/src/api/build.rs b/bin/periphery/src/api/build.rs index 9cc0ec2fe..c8e547fec 100644 --- a/bin/periphery/src/api/build.rs +++ b/bin/periphery/src/api/build.rs @@ -7,7 +7,7 @@ use periphery_client::api::build::{ use resolver_api::Resolve; use crate::{ - helpers::docker::{self, client::docker_client}, + docker::{self, client::docker_client}, State, }; diff --git a/bin/periphery/src/api/container.rs b/bin/periphery/src/api/container.rs index 5a4588202..ce5d11c9a 100644 --- a/bin/periphery/src/api/container.rs +++ b/bin/periphery/src/api/container.rs @@ -7,7 +7,7 @@ use periphery_client::api::container::*; use resolver_api::Resolve; use crate::{ - helpers::docker::{self, client::docker_client}, + docker::{self, client::docker_client}, State, }; diff --git a/bin/periphery/src/api/git.rs b/bin/periphery/src/api/git.rs index 5fb739613..71e0cebd9 100644 --- a/bin/periphery/src/api/git.rs +++ b/bin/periphery/src/api/git.rs @@ -1,19 +1,22 @@ use anyhow::anyhow; -use monitor_client::entities::{to_monitor_name, update::Log}; +use monitor_client::entities::{ + to_monitor_name, update::Log, CloneArgs, LatestCommit, +}; use periphery_client::api::git::{ - CloneRepo, DeleteRepo, GetLatestCommit, GetLatestCommitResponse, - PullRepo, + CloneRepo, DeleteRepo, GetLatestCommit, PullRepo, }; use resolver_api::Resolve; -use crate::{config::periphery_config, helpers::git, State}; +use crate::{ + config::periphery_config, helpers::get_github_token, State, +}; impl Resolve for State { async fn resolve( &self, GetLatestCommit { name }: GetLatestCommit, _: (), - ) -> anyhow::Result { + ) -> anyhow::Result { let repo_path = periphery_config().repo_dir.join(name); if !repo_path.is_dir() { return Err(anyhow!( @@ -31,7 +34,14 @@ impl Resolve for State { CloneRepo { args, github_token }: CloneRepo, _: (), ) -> anyhow::Result> { - git::clone(args, github_token).await + let CloneArgs { github_account, .. } = &args; + let github_token = + match (github_token, get_github_token(github_account)) { + (Some(token), _) => Some(token), + (None, Ok(token)) => token, + (None, Err(e)) => return Err(e), + }; + git::clone(args, &periphery_config().repo_dir, github_token).await } } diff --git a/bin/periphery/src/api/mod.rs b/bin/periphery/src/api/mod.rs index d835b2bf2..12e9d4503 100644 --- a/bin/periphery/src/api/mod.rs +++ b/bin/periphery/src/api/mod.rs @@ -1,4 +1,5 @@ use anyhow::Context; +use command::run_monitor_command; use monitor_client::entities::{update::Log, SystemCommand}; use periphery_client::api::{ build::*, container::*, git::*, network::*, stats::*, GetAccounts, @@ -10,8 +11,7 @@ use serde::{Deserialize, Serialize}; use crate::{ config::{accounts_response, secrets_response}, - helpers::{docker, run_monitor_command}, - State, + docker, State, }; mod build; diff --git a/bin/periphery/src/api/network.rs b/bin/periphery/src/api/network.rs index 6f38f4b1b..5bda56c31 100644 --- a/bin/periphery/src/api/network.rs +++ b/bin/periphery/src/api/network.rs @@ -7,7 +7,7 @@ use periphery_client::api::network::{ use resolver_api::Resolve; use crate::{ - helpers::docker::{self, client::docker_client}, + docker::{self, client::docker_client}, State, }; diff --git a/bin/periphery/src/helpers/docker/build.rs b/bin/periphery/src/docker/build.rs similarity index 97% rename from bin/periphery/src/helpers/docker/build.rs rename to bin/periphery/src/docker/build.rs index e1e4d6fa4..d74809bbf 100644 --- a/bin/periphery/src/helpers/docker/build.rs +++ b/bin/periphery/src/docker/build.rs @@ -1,4 +1,5 @@ use anyhow::Context; +use command::run_monitor_command; use monitor_client::entities::{ build::{Build, BuildConfig}, optional_string, to_monitor_name, @@ -7,10 +8,7 @@ use monitor_client::entities::{ }; use serror::serialize_error_pretty; -use crate::{ - config::periphery_config, - helpers::{get_docker_token, run_monitor_command}, -}; +use crate::{config::periphery_config, helpers::get_docker_token}; use super::{docker_login, parse_extra_args, parse_labels}; diff --git a/bin/periphery/src/helpers/docker/client.rs b/bin/periphery/src/docker/client.rs similarity index 100% rename from bin/periphery/src/helpers/docker/client.rs rename to bin/periphery/src/docker/client.rs diff --git a/bin/periphery/src/helpers/docker/container.rs b/bin/periphery/src/docker/container.rs similarity index 98% rename from bin/periphery/src/helpers/docker/container.rs rename to bin/periphery/src/docker/container.rs index 65f19050d..b1b81d57f 100644 --- a/bin/periphery/src/helpers/docker/container.rs +++ b/bin/periphery/src/docker/container.rs @@ -1,4 +1,5 @@ use anyhow::{anyhow, Context}; +use command::run_monitor_command; use monitor_client::entities::{ deployment::{ Conversion, Deployment, DeploymentConfig, DeploymentImage, @@ -11,15 +12,9 @@ use monitor_client::entities::{ use run_command::async_run_command; use serror::serialize_error_pretty; -use crate::{ - config::periphery_config, - helpers::{ - docker::{parse_extra_args, parse_labels}, - get_docker_token, run_monitor_command, - }, -}; +use crate::{config::periphery_config, helpers::get_docker_token}; -use super::docker_login; +use super::{docker_login, parse_extra_args, parse_labels}; #[instrument(level = "debug")] pub async fn container_log(container_name: &str, tail: u64) -> Log { diff --git a/bin/periphery/src/helpers/docker/mod.rs b/bin/periphery/src/docker/mod.rs similarity index 97% rename from bin/periphery/src/helpers/docker/mod.rs rename to bin/periphery/src/docker/mod.rs index 43b4fec0c..a857a9db7 100644 --- a/bin/periphery/src/helpers/docker/mod.rs +++ b/bin/periphery/src/docker/mod.rs @@ -1,9 +1,8 @@ use anyhow::anyhow; +use command::run_monitor_command; use monitor_client::entities::{update::Log, EnvironmentVar}; use run_command::async_run_command; -use super::run_monitor_command; - pub mod build; pub mod client; pub mod container; diff --git a/bin/periphery/src/helpers/docker/network.rs b/bin/periphery/src/docker/network.rs similarity index 94% rename from bin/periphery/src/helpers/docker/network.rs rename to bin/periphery/src/docker/network.rs index 04edd90cb..b65811836 100644 --- a/bin/periphery/src/helpers/docker/network.rs +++ b/bin/periphery/src/docker/network.rs @@ -1,7 +1,6 @@ +use command::run_monitor_command; use monitor_client::entities::update::Log; -use crate::helpers::run_monitor_command; - #[instrument] pub async fn create_network( name: &str, diff --git a/bin/periphery/src/helpers/mod.rs b/bin/periphery/src/helpers.rs similarity index 50% rename from bin/periphery/src/helpers/mod.rs rename to bin/periphery/src/helpers.rs index f0c29b979..f7104c5cc 100644 --- a/bin/periphery/src/helpers/mod.rs +++ b/bin/periphery/src/helpers.rs @@ -1,13 +1,7 @@ use anyhow::anyhow; -use async_timing_util::unix_timestamp_ms; -use monitor_client::entities::update::Log; -use run_command::{async_run_command, CommandOutput}; use crate::config::periphery_config; -pub mod docker; -pub mod git; - pub fn get_github_token( github_account: &Option, ) -> anyhow::Result> { @@ -32,37 +26,10 @@ pub fn get_docker_token( match periphery_config().docker_accounts.get(account) { Some(token) => Ok(Some(token.to_owned())), None => Err(anyhow!( - "did not find token in config for docker account {account}" - )), + "did not find token in config for docker account {account}" + )), } } None => Ok(None), } } - -pub async fn run_monitor_command( - stage: &str, - command: String, -) -> Log { - let start_ts = unix_timestamp_ms() as i64; - let output = async_run_command(&command).await; - output_into_log(stage, command, start_ts, output) -} - -pub fn output_into_log( - stage: &str, - command: String, - start_ts: i64, - output: CommandOutput, -) -> Log { - let success = output.success(); - Log { - stage: stage.to_string(), - stdout: output.stdout, - stderr: output.stderr, - command, - success, - start_ts, - end_ts: unix_timestamp_ms() as i64, - } -} diff --git a/bin/periphery/src/main.rs b/bin/periphery/src/main.rs index d06349459..e5b6291fd 100644 --- a/bin/periphery/src/main.rs +++ b/bin/periphery/src/main.rs @@ -9,6 +9,7 @@ use termination_signal::tokio::immediate_term_handle; mod api; mod config; +mod docker; mod guard; mod handler; mod helpers; diff --git a/client/core/rs/src/entities/mod.rs b/client/core/rs/src/entities/mod.rs index d4ac96801..edda01e9a 100644 --- a/client/core/rs/src/entities/mod.rs +++ b/client/core/rs/src/entities/mod.rs @@ -237,6 +237,12 @@ pub struct EnvironmentVar { pub value: String, } +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct LatestCommit { + pub hash: String, + pub message: String, +} + #[typeshare] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub struct CloneArgs { diff --git a/client/core/rs/src/entities/sync.rs b/client/core/rs/src/entities/sync.rs index 611a61727..8316317ca 100644 --- a/client/core/rs/src/entities/sync.rs +++ b/client/core/rs/src/entities/sync.rs @@ -29,14 +29,14 @@ pub struct ResourceSyncListItemInfo { /// The branch of the repo pub branch: String, /// State of the sync. Reflects whether most recent sync successful. - pub state: SyncState, + pub state: ResourceSyncState, } #[typeshare] #[derive( Debug, Clone, Copy, Default, Serialize, Deserialize, Display, )] -pub enum SyncState { +pub enum ResourceSyncState { /// Last sync successful (or never synced) Ok, /// Last sync failed diff --git a/client/periphery/rs/src/api/git.rs b/client/periphery/rs/src/api/git.rs index bba518d65..f84632565 100644 --- a/client/periphery/rs/src/api/git.rs +++ b/client/periphery/rs/src/api/git.rs @@ -1,21 +1,15 @@ use monitor_client::entities::{ - update::Log, CloneArgs, SystemCommand, + update::Log, CloneArgs, LatestCommit, SystemCommand, }; use resolver_api::derive::Request; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize, Request)] -#[response(GetLatestCommitResponse)] +#[response(LatestCommit)] pub struct GetLatestCommit { pub name: String, } -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct GetLatestCommitResponse { - pub hash: String, - pub message: String, -} - #[derive(Serialize, Deserialize, Debug, Clone, Request)] #[response(Vec)] pub struct CloneRepo { diff --git a/lib/command/Cargo.toml b/lib/command/Cargo.toml new file mode 100644 index 000000000..232420aeb --- /dev/null +++ b/lib/command/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "command" +version.workspace = true +edition.workspace = true +authors.workspace = true +license.workspace = true +repository.workspace = true +homepage.workspace = true + +[dependencies] +monitor_client.workspace = true +run_command.workspace = true \ No newline at end of file diff --git a/lib/command/src/lib.rs b/lib/command/src/lib.rs new file mode 100644 index 000000000..0816ab73c --- /dev/null +++ b/lib/command/src/lib.rs @@ -0,0 +1,29 @@ +use monitor_client::entities::{monitor_timestamp, update::Log}; +use run_command::{async_run_command, CommandOutput}; + +pub async fn run_monitor_command( + stage: &str, + command: String, +) -> Log { + let start_ts = monitor_timestamp(); + let output = async_run_command(&command).await; + output_into_log(stage, command, start_ts, output) +} + +pub fn output_into_log( + stage: &str, + command: String, + start_ts: i64, + output: CommandOutput, +) -> Log { + let success = output.success(); + Log { + stage: stage.to_string(), + stdout: output.stdout, + stderr: output.stderr, + command, + success, + start_ts, + end_ts: monitor_timestamp(), + } +} diff --git a/lib/git/Cargo.toml b/lib/git/Cargo.toml new file mode 100644 index 000000000..c1a62828f --- /dev/null +++ b/lib/git/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "git" +version.workspace = true +edition.workspace = true +authors.workspace = true +license.workspace = true +repository.workspace = true +homepage.workspace = true + +[dependencies] +monitor_client.workspace = true +command.workspace = true +# +run_command.workspace = true +# +tracing.workspace = true +anyhow.workspace = true \ No newline at end of file diff --git a/bin/periphery/src/helpers/git.rs b/lib/git/src/lib.rs similarity index 83% rename from bin/periphery/src/helpers/git.rs rename to lib/git/src/lib.rs index a33fc7963..a4c72a298 100644 --- a/bin/periphery/src/helpers/git.rs +++ b/lib/git/src/lib.rs @@ -1,18 +1,15 @@ use std::path::Path; use anyhow::Context; -use async_timing_util::unix_timestamp_ms; +use command::run_monitor_command; use monitor_client::entities::{ all_logs_success, monitor_timestamp, to_monitor_name, update::Log, - CloneArgs, SystemCommand, + CloneArgs, LatestCommit, SystemCommand, }; -use periphery_client::api::git::GetLatestCommitResponse; use run_command::async_run_command; +use tracing::instrument; -use crate::config::periphery_config; - -use super::{get_github_token, run_monitor_command}; - +#[tracing::instrument] pub async fn pull( path: &Path, branch: &Option, @@ -66,9 +63,10 @@ pub async fn pull( logs } -#[instrument] +#[tracing::instrument(skip(repo_dir, github_token))] pub async fn clone( clone_args: T, + repo_dir: &Path, github_token: Option, ) -> anyhow::Result> where @@ -81,31 +79,31 @@ where commit, on_clone, on_pull, - github_account, + .. } = clone_args.into(); - let access_token = - match (github_token, get_github_token(&github_account)) { - (Some(token), _) => Some(token), - (None, Ok(token)) => token, - (None, Err(e)) => return Err(e), - }; + // let access_token = + // match (github_token, get_github_token(&github_account)) { + // (Some(token), _) => Some(token), + // (None, Ok(token)) => token, + // (None, Err(e)) => return Err(e), + // }; let repo = repo.as_ref().context("build has no repo attached")?; let name = to_monitor_name(&name); - let repo_dir = periphery_config().repo_dir.join(name); + let repo_dir = repo_dir.join(name); let mut logs = - clone_inner(repo, &repo_dir, &branch, &commit, access_token) + clone_inner(repo, &repo_dir, &branch, &commit, github_token) .await; if !all_logs_success(&logs) { - warn!("repo at {repo_dir:?} failed to clone"); + tracing::warn!("repo at {repo_dir:?} failed to clone"); return Ok(logs); } - info!("repo at {repo_dir:?} cloned with clone_inner"); + tracing::info!("repo at {repo_dir:?} cloned with clone_inner"); let commit_hash_log = get_commit_hash_log(&repo_dir).await?; logs.push(commit_hash_log); @@ -122,9 +120,10 @@ where ), ) .await; - info!( + tracing::info!( "run repo on_clone command | command: {} | cwd: {:?}", - command.command, on_clone_path + command.command, + on_clone_path ); logs.push(on_clone_log); } @@ -141,9 +140,10 @@ where ), ) .await; - info!( + tracing::info!( "run repo on_pull command | command: {} | cwd: {:?}", - command.command, on_pull_path + command.command, + on_pull_path ); logs.push(on_pull_log); } @@ -151,7 +151,7 @@ where Ok(logs) } -#[instrument] +#[tracing::instrument(skip(destination, access_token))] async fn clone_inner( repo: &str, destination: &Path, @@ -172,7 +172,7 @@ async fn clone_inner( format!("https://{access_token_at}github.com/{repo}.git"); let command = format!("git clone {repo_url} {}{branch}", destination.display()); - let start_ts = unix_timestamp_ms() as i64; + let start_ts = monitor_timestamp(); let output = async_run_command(&command).await; let success = output.success(); let (command, stderr) = if !access_token_at.is_empty() { @@ -192,7 +192,7 @@ async fn clone_inner( stdout: output.stdout, stderr, start_ts, - end_ts: unix_timestamp_ms() as i64, + end_ts: monitor_timestamp(), }]; if !logs[0].success { @@ -214,9 +214,10 @@ async fn clone_inner( logs } +#[instrument(level = "debug")] pub async fn get_commit_hash_info( repo_dir: &Path, -) -> anyhow::Result { +) -> anyhow::Result { let command = format!("cd {} && git rev-parse --short HEAD && git rev-parse HEAD && git log -1 --pretty=%B", repo_dir.display()); let output = async_run_command(&command).await; let mut split = output.stdout.split('\n'); @@ -231,11 +232,13 @@ pub async fn get_commit_hash_info( .context("failed to get commit message")? .to_string(), ); - Ok(GetLatestCommitResponse { hash, message }) + Ok(LatestCommit { hash, message }) } -#[instrument] -async fn get_commit_hash_log(repo_dir: &Path) -> anyhow::Result { +#[instrument(level = "debug")] +pub async fn get_commit_hash_log( + repo_dir: &Path, +) -> anyhow::Result { let start_ts = monitor_timestamp(); let command = format!("cd {} && git rev-parse --short HEAD && git rev-parse HEAD && git log -1 --pretty=%B", repo_dir.display()); let output = async_run_command(&command).await;