mirror of
https://github.com/moghtech/komodo.git
synced 2026-03-11 17:44:19 -05:00
move some libraries out
This commit is contained in:
21
Cargo.lock
generated
21
Cargo.lock
generated
@@ -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",
|
||||
|
||||
@@ -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"] }
|
||||
|
||||
@@ -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 {
|
||||
|
||||
17
bin/core/src/api/execute/sync.rs
Normal file
17
bin/core/src/api/execute/sync.rs
Normal file
@@ -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<RunSync, (User, Update)> for State {
|
||||
async fn resolve(
|
||||
&self,
|
||||
RunSync { sync }: RunSync,
|
||||
(user, update): (User, Update),
|
||||
) -> anyhow::Result<Update> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
@@ -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),
|
||||
|
||||
123
bin/core/src/api/read/sync.rs
Normal file
123
bin/core/src/api/read/sync.rs
Normal file
@@ -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<GetResourceSync, User> for State {
|
||||
async fn resolve(
|
||||
&self,
|
||||
GetResourceSync { sync }: GetResourceSync,
|
||||
user: User,
|
||||
) -> anyhow::Result<ResourceSync> {
|
||||
resource::get_check_permissions::<ResourceSync>(
|
||||
&sync,
|
||||
&user,
|
||||
PermissionLevel::Read,
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
impl Resolve<ListResourceSyncs, User> for State {
|
||||
async fn resolve(
|
||||
&self,
|
||||
ListResourceSyncs { query }: ListResourceSyncs,
|
||||
user: User,
|
||||
) -> anyhow::Result<Vec<ResourceSyncListItem>> {
|
||||
resource::list_for_user::<ResourceSync>(query, &user).await
|
||||
}
|
||||
}
|
||||
|
||||
impl Resolve<ListFullResourceSyncs, User> for State {
|
||||
async fn resolve(
|
||||
&self,
|
||||
ListFullResourceSyncs { query }: ListFullResourceSyncs,
|
||||
user: User,
|
||||
) -> anyhow::Result<ListFullResourceSyncsResponse> {
|
||||
resource::list_full_for_user::<ResourceSync>(query, &user).await
|
||||
}
|
||||
}
|
||||
|
||||
impl Resolve<GetResourceSyncActionState, User> for State {
|
||||
async fn resolve(
|
||||
&self,
|
||||
GetResourceSyncActionState { sync }: GetResourceSyncActionState,
|
||||
user: User,
|
||||
) -> anyhow::Result<ResourceSyncActionState> {
|
||||
let sync = resource::get_check_permissions::<ResourceSync>(
|
||||
&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<GetResourceSyncsSummary, User> for State {
|
||||
async fn resolve(
|
||||
&self,
|
||||
GetResourceSyncsSummary {}: GetResourceSyncsSummary,
|
||||
user: User,
|
||||
) -> anyhow::Result<GetResourceSyncsSummaryResponse> {
|
||||
let resource_syncs =
|
||||
resource::list_full_for_user::<ResourceSync>(
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -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),
|
||||
|
||||
61
bin/core/src/api/write/sync.rs
Normal file
61
bin/core/src/api/write/sync.rs
Normal file
@@ -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<CreateResourceSync, User> for State {
|
||||
#[instrument(name = "CreateResourceSync", skip(self, user))]
|
||||
async fn resolve(
|
||||
&self,
|
||||
CreateResourceSync { name, config }: CreateResourceSync,
|
||||
user: User,
|
||||
) -> anyhow::Result<ResourceSync> {
|
||||
resource::create::<ResourceSync>(&name, config, &user).await
|
||||
}
|
||||
}
|
||||
|
||||
impl Resolve<CopyResourceSync, User> for State {
|
||||
#[instrument(name = "CopyResourceSync", skip(self, user))]
|
||||
async fn resolve(
|
||||
&self,
|
||||
CopyResourceSync { name, id }: CopyResourceSync,
|
||||
user: User,
|
||||
) -> anyhow::Result<ResourceSync> {
|
||||
let ResourceSync { config, .. } =
|
||||
resource::get_check_permissions::<ResourceSync>(
|
||||
&id,
|
||||
&user,
|
||||
PermissionLevel::Write,
|
||||
)
|
||||
.await?;
|
||||
resource::create::<ResourceSync>(&name, config.into(), &user)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
impl Resolve<DeleteResourceSync, User> for State {
|
||||
#[instrument(name = "DeleteResourceSync", skip(self, user))]
|
||||
async fn resolve(
|
||||
&self,
|
||||
DeleteResourceSync { id }: DeleteResourceSync,
|
||||
user: User,
|
||||
) -> anyhow::Result<ResourceSync> {
|
||||
resource::delete::<ResourceSync>(&id, &user).await
|
||||
}
|
||||
}
|
||||
|
||||
impl Resolve<UpdateResourceSync, User> for State {
|
||||
#[instrument(name = "UpdateResourceSync", skip(self, user))]
|
||||
async fn resolve(
|
||||
&self,
|
||||
UpdateResourceSync { id, config }: UpdateResourceSync,
|
||||
user: User,
|
||||
) -> anyhow::Result<ResourceSync> {
|
||||
resource::update::<ResourceSync>(&id, config, &user).await
|
||||
}
|
||||
}
|
||||
@@ -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::<ResourceSync>(&data.sync).await?.id,
|
||||
),
|
||||
),
|
||||
};
|
||||
let mut update = make_update(target, operation, user);
|
||||
update.in_progress();
|
||||
|
||||
@@ -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::Config, Self::Info>,
|
||||
) -> 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<bool> {
|
||||
@@ -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<Self::Config, Self::Info>,
|
||||
update: &mut Update,
|
||||
_created: &Resource<Self::Config, Self::Info>,
|
||||
_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<Self::Config, Self::Info>,
|
||||
update: &mut Update,
|
||||
_updated: &Resource<Self::Config, Self::Info>,
|
||||
_update: &mut Update,
|
||||
) -> anyhow::Result<()> {
|
||||
todo!()
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// DELETE
|
||||
@@ -97,15 +111,15 @@ impl super::MonitorResource for ResourceSync {
|
||||
}
|
||||
|
||||
async fn pre_delete(
|
||||
resource: &Resource<Self::Config, Self::Info>,
|
||||
update: &mut Update,
|
||||
_resource: &Resource<Self::Config, Self::Info>,
|
||||
_update: &mut Update,
|
||||
) -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn post_delete(
|
||||
resource: &Resource<Self::Config, Self::Info>,
|
||||
update: &mut Update,
|
||||
_resource: &Resource<Self::Config, Self::Info>,
|
||||
_update: &mut Update,
|
||||
) -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -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<String, ResourceSyncState>;
|
||||
|
||||
pub fn resource_sync_state_cache() -> &'static ResourceSyncStateCache
|
||||
{
|
||||
static RESOURCE_SYNC_STATE_CACHE: OnceLock<ResourceSyncStateCache> =
|
||||
OnceLock::new();
|
||||
RESOURCE_SYNC_STATE_CACHE.get_or_init(Default::default)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
|
||||
@@ -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<GetLatestCommit, ()> for State {
|
||||
async fn resolve(
|
||||
&self,
|
||||
GetLatestCommit { name }: GetLatestCommit,
|
||||
_: (),
|
||||
) -> anyhow::Result<GetLatestCommitResponse> {
|
||||
) -> anyhow::Result<LatestCommit> {
|
||||
let repo_path = periphery_config().repo_dir.join(name);
|
||||
if !repo_path.is_dir() {
|
||||
return Err(anyhow!(
|
||||
@@ -31,7 +34,14 @@ impl Resolve<CloneRepo> for State {
|
||||
CloneRepo { args, github_token }: CloneRepo,
|
||||
_: (),
|
||||
) -> anyhow::Result<Vec<Log>> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -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 {
|
||||
@@ -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;
|
||||
@@ -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,
|
||||
@@ -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<String>,
|
||||
) -> anyhow::Result<Option<String>> {
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ use termination_signal::tokio::immediate_term_handle;
|
||||
|
||||
mod api;
|
||||
mod config;
|
||||
mod docker;
|
||||
mod guard;
|
||||
mod handler;
|
||||
mod helpers;
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<Log>)]
|
||||
pub struct CloneRepo {
|
||||
|
||||
12
lib/command/Cargo.toml
Normal file
12
lib/command/Cargo.toml
Normal file
@@ -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
|
||||
29
lib/command/src/lib.rs
Normal file
29
lib/command/src/lib.rs
Normal file
@@ -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(),
|
||||
}
|
||||
}
|
||||
17
lib/git/Cargo.toml
Normal file
17
lib/git/Cargo.toml
Normal file
@@ -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
|
||||
@@ -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<String>,
|
||||
@@ -66,9 +63,10 @@ pub async fn pull(
|
||||
logs
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
#[tracing::instrument(skip(repo_dir, github_token))]
|
||||
pub async fn clone<T>(
|
||||
clone_args: T,
|
||||
repo_dir: &Path,
|
||||
github_token: Option<String>,
|
||||
) -> anyhow::Result<Vec<Log>>
|
||||
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<GetLatestCommitResponse> {
|
||||
) -> anyhow::Result<LatestCommit> {
|
||||
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<Log> {
|
||||
#[instrument(level = "debug")]
|
||||
pub async fn get_commit_hash_log(
|
||||
repo_dir: &Path,
|
||||
) -> anyhow::Result<Log> {
|
||||
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;
|
||||
Reference in New Issue
Block a user