From d09ab36696ce1ca6e34152ef9d31be6dcd763168 Mon Sep 17 00:00:00 2001 From: mbecker20 Date: Sat, 8 Jun 2024 04:34:09 -0700 Subject: [PATCH] any sync error shows up in log --- bin/core/src/helpers/sync/resource.rs | 40 +++++++++++++++++++----- bin/core/src/helpers/sync/resources.rs | 35 ++++++++++++++------- bin/core/src/helpers/sync/user_groups.rs | 34 +++++++++++++++++--- bin/core/src/helpers/sync/variables.rs | 12 ++++++- 4 files changed, 95 insertions(+), 26 deletions(-) diff --git a/bin/core/src/helpers/sync/resource.rs b/bin/core/src/helpers/sync/resource.rs index 5e1d9aa72..d0b6e6712 100644 --- a/bin/core/src/helpers/sync/resource.rs +++ b/bin/core/src/helpers/sync/resource.rs @@ -73,6 +73,7 @@ pub trait ResourceSync: MonitorResource + Sized { return None; } + let mut has_error = false; let mut log = format!("running updates on {}s", Self::resource_type()); @@ -89,6 +90,7 @@ pub trait ResourceSync: MonitorResource + Sized { { Ok(resource) => resource.id, Err(e) => { + has_error = true; log.push_str(&format!( "\n{}: failed to create {} '{}' | {e:#}", colored("ERROR", "red"), @@ -98,13 +100,20 @@ pub trait ResourceSync: MonitorResource + Sized { continue; } }; - run_update_tags::(id.clone(), &name, tags, &mut log) - .await; + run_update_tags::( + id.clone(), + &name, + tags, + &mut log, + &mut has_error, + ) + .await; run_update_description::( id, &name, description, &mut log, + &mut has_error, ) .await; log.push_str(&format!( @@ -134,13 +143,20 @@ pub trait ResourceSync: MonitorResource + Sized { &name, description, &mut log, + &mut has_error, ) .await; } if update_tags { - run_update_tags::(id.clone(), &name, tags, &mut log) - .await; + run_update_tags::( + id.clone(), + &name, + tags, + &mut log, + &mut has_error, + ) + .await; } if !resource.config.is_none() { @@ -151,6 +167,7 @@ pub trait ResourceSync: MonitorResource + Sized { ) .await { + has_error = true; log.push_str(&format!( "\n{}: failed to update config on {} '{}' | {e:#}", colored("ERROR", "red"), @@ -173,6 +190,7 @@ pub trait ResourceSync: MonitorResource + Sized { if let Err(e) = crate::resource::delete::(&resource, sync_user()).await { + has_error = true; log.push_str(&format!( "\n{}: failed to delete {} '{}' | {e:#}", colored("ERROR", "red"), @@ -190,10 +208,12 @@ pub trait ResourceSync: MonitorResource + Sized { } } - Some(Log::simple( - &format!("Update {}s", Self::resource_type()), - log, - )) + let stage = format!("Update {}s", Self::resource_type()); + Some(if has_error { + Log::error(&stage, log) + } else { + Log::simple(&stage, log) + }) } } @@ -423,6 +443,7 @@ pub async fn run_update_tags( name: &str, tags: Vec, log: &mut String, + has_error: &mut bool, ) { // Update tags if let Err(e) = State @@ -435,6 +456,7 @@ pub async fn run_update_tags( ) .await { + *has_error = true; log.push_str(&format!( "\n{}: failed to update tags on {} '{}' | {e:#}", colored("ERROR", "red"), @@ -457,6 +479,7 @@ pub async fn run_update_description( name: &str, description: String, log: &mut String, + has_error: &mut bool, ) { if let Err(e) = State .resolve( @@ -468,6 +491,7 @@ pub async fn run_update_description( ) .await { + *has_error = true; log.push_str(&format!( "\n{}: failed to update description on {} '{}' | {e:#}", colored("ERROR", "red"), diff --git a/bin/core/src/helpers/sync/resources.rs b/bin/core/src/helpers/sync/resources.rs index 5ccf84617..74b3cf4ba 100644 --- a/bin/core/src/helpers/sync/resources.rs +++ b/bin/core/src/helpers/sync/resources.rs @@ -317,6 +317,7 @@ impl ResourceSync for Procedure { return None; } + let mut has_error = false; let mut log = format!("running updates on {}s", Self::resource_type()); @@ -324,6 +325,7 @@ impl ResourceSync for Procedure { if let Err(e) = crate::resource::delete::(&name, sync_user()).await { + has_error = true; log.push_str(&format!( "{}: failed to delete {} '{}' | {e:#}", colored("ERROR", "red"), @@ -342,10 +344,12 @@ impl ResourceSync for Procedure { } if to_update.is_empty() && to_create.is_empty() { - return Some(Log::simple( - &format!("Update {}s", Self::resource_type()), - log, - )); + let stage = "Update Procedures"; + return Some(if has_error { + Log::error(stage, log) + } else { + Log::simple(stage, log) + }); } for i in 0..10 { @@ -367,6 +371,7 @@ impl ResourceSync for Procedure { &name, description, &mut log, + &mut has_error, ) .await; } @@ -376,6 +381,7 @@ impl ResourceSync for Procedure { &name, tags, &mut log, + &mut has_error, ) .await; } @@ -388,9 +394,9 @@ impl ResourceSync for Procedure { .await { if i == 9 { - log.push('\n'); + has_error = true; log.push_str(&format!( - "{}: failed to update {} '{}' | {e:#}", + "\n{}: failed to update {} '{}' | {e:#}", colored("ERROR", "red"), Self::resource_type(), bold(&name) @@ -400,9 +406,8 @@ impl ResourceSync for Procedure { } } - log.push('\n'); log.push_str(&format!( - "{}: {} '{}' updated", + "\n{}: {} '{}' updated", muted("INFO"), Self::resource_type(), bold(&name) @@ -428,9 +433,9 @@ impl ResourceSync for Procedure { Ok(resource) => resource.id, Err(e) => { if i == 9 { - log.push('\n'); + has_error = true; log.push_str(&format!( - "{}: failed to create {} '{}' | {e:#}", + "\n{}: failed to create {} '{}' | {e:#}", colored("ERROR", "red"), Self::resource_type(), bold(&name) @@ -444,6 +449,7 @@ impl ResourceSync for Procedure { &name, tags, &mut log, + &mut has_error, ) .await; run_update_description::( @@ -451,6 +457,7 @@ impl ResourceSync for Procedure { &name, description, &mut log, + &mut has_error, ) .await; log.push_str(&format!( @@ -465,8 +472,12 @@ impl ResourceSync for Procedure { to_create.retain(|resource| !to_pull.contains(&resource.name)); if to_update.is_empty() && to_create.is_empty() { - // info!("all procedures synced"); - return Some(Log::simple("Update Procedures", log)); + let stage = "Update Procedures"; + return Some(if has_error { + Log::error(stage, log) + } else { + Log::simple(stage, log) + }); } } warn!("procedure sync loop exited after max iterations"); diff --git a/bin/core/src/helpers/sync/user_groups.rs b/bin/core/src/helpers/sync/user_groups.rs index e2e971bf6..48f056f27 100644 --- a/bin/core/src/helpers/sync/user_groups.rs +++ b/bin/core/src/helpers/sync/user_groups.rs @@ -477,6 +477,7 @@ pub async fn run_updates( return None; } + let mut has_error = false; let mut log = String::from("running updates on UserGroups"); // Create the non-existant user groups @@ -491,6 +492,7 @@ pub async fn run_updates( ) .await { + has_error = true; log.push_str(&format!( "\n{}: failed to create user group '{}' | {e:#}", colored("ERROR", "red"), @@ -506,12 +508,18 @@ pub async fn run_updates( )) }; - set_users(user_group.name.clone(), user_group.users, &mut log) - .await; + set_users( + user_group.name.clone(), + user_group.users, + &mut log, + &mut has_error, + ) + .await; run_update_permissions( user_group.name, user_group.permissions, &mut log, + &mut has_error, ) .await; } @@ -524,14 +532,20 @@ pub async fn run_updates( } in to_update { if update_users { - set_users(user_group.name.clone(), user_group.users, &mut log) - .await; + set_users( + user_group.name.clone(), + user_group.users, + &mut log, + &mut has_error, + ) + .await; } if update_permissions { run_update_permissions( user_group.name, user_group.permissions, &mut log, + &mut has_error, ) .await; } @@ -545,6 +559,7 @@ pub async fn run_updates( ) .await { + has_error = true; log.push_str(&format!( "\n{}: failed to delete user group '{}' | {e:#}", colored("ERROR", "red"), @@ -560,13 +575,19 @@ pub async fn run_updates( } } - Some(Log::simple("Update UserGroups", log)) + let stage = "Update UserGroups"; + Some(if has_error { + Log::error(stage, log) + } else { + Log::simple(stage, log) + }) } async fn set_users( user_group: String, users: Vec, log: &mut String, + has_error: &mut bool, ) { if let Err(e) = State .resolve( @@ -578,6 +599,7 @@ async fn set_users( ) .await { + *has_error = true; log.push_str(&format!( "\n{}: failed to set users in group {} | {e:#}", colored("ERROR", "red"), @@ -597,6 +619,7 @@ async fn run_update_permissions( user_group: String, permissions: Vec, log: &mut String, + has_error: &mut bool, ) { for PermissionToml { target, level } in permissions { if let Err(e) = State @@ -610,6 +633,7 @@ async fn run_update_permissions( ) .await { + *has_error = true; log.push_str(&format!( "\n{}: failed to set permssion in group {} | target: {target:?} | {e:#}", colored("ERROR", "red"), diff --git a/bin/core/src/helpers/sync/variables.rs b/bin/core/src/helpers/sync/variables.rs index 0245a8483..5dbdb7732 100644 --- a/bin/core/src/helpers/sync/variables.rs +++ b/bin/core/src/helpers/sync/variables.rs @@ -184,6 +184,7 @@ pub async fn run_updates( return None; } + let mut has_error = false; let mut log = String::from("running updates on Variables"); for variable in to_create { @@ -198,6 +199,7 @@ pub async fn run_updates( ) .await { + has_error = true; log.push_str(&format!( "\n{}: failed to create variable '{}' | {e:#}", colored("ERROR", "red"), @@ -230,6 +232,7 @@ pub async fn run_updates( ) .await { + has_error = true; log.push_str(&format!( "\n{}: failed to update variable value for '{}' | {e:#}", colored("ERROR", "red"), @@ -255,6 +258,7 @@ pub async fn run_updates( ) .await { + has_error = true; log.push_str(&format!( "\n{}: failed to update variable description for '{}' | {e:#}", colored("ERROR", "red"), @@ -281,6 +285,7 @@ pub async fn run_updates( ) .await { + has_error = true; log.push_str(&format!( "\n{}: failed to delete variable '{}' | {e:#}", colored("ERROR", "red"), @@ -296,5 +301,10 @@ pub async fn run_updates( } } - Some(Log::simple("Update Variables", log)) + let stage = "Update Variables"; + Some(if has_error { + Log::error(stage, log) + } else { + Log::simple(stage, log) + }) }