From 65ec5273aa51d19282cffdd6321c047696aaceb8 Mon Sep 17 00:00:00 2001 From: Nathaniel Clark Date: Tue, 17 Nov 2020 07:54:44 -0500 Subject: [PATCH 1/2] Add ha_resource_move action Move resource, wait for move completion, then remove move constraint. Signed-off-by: Nathaniel Clark --- iml-agent/src/action_plugins/action_plugin.rs | 1 + .../action_plugins/high_availability/mod.rs | 42 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/iml-agent/src/action_plugins/action_plugin.rs b/iml-agent/src/action_plugins/action_plugin.rs index fa704dca89..5fe6f52c2c 100644 --- a/iml-agent/src/action_plugins/action_plugin.rs +++ b/iml-agent/src/action_plugins/action_plugin.rs @@ -49,6 +49,7 @@ pub fn create_registry() -> action_plugins::Actions { .add_plugin("remove_fstab_entry", lustre::client::remove_fstab_entry) .add_plugin("ha_resource_start", high_availability::start_resource) .add_plugin("ha_resource_stop", high_availability::stop_resource) + .add_plugin("ha_resource_move", high_availability::move_resource) .add_plugin( "ha_resource_create", high_availability::create_single_resource, diff --git a/iml-agent/src/action_plugins/high_availability/mod.rs b/iml-agent/src/action_plugins/high_availability/mod.rs index fd86df8575..4a507a1a76 100644 --- a/iml-agent/src/action_plugins/high_availability/mod.rs +++ b/iml-agent/src/action_plugins/high_availability/mod.rs @@ -27,6 +27,9 @@ pub use crate::high_availability::{crm_attribute, pcs}; const NO_EXTRA: Vec = vec![]; +const WAIT_SEC: u64 = 300; +const WAIT_DELAY: u64 = 2; + fn create(elem: &Element) -> ResourceAgentInfo { ResourceAgentInfo { agent: { @@ -76,8 +79,8 @@ fn create(elem: &Element) -> ResourceAgentInfo { } async fn wait_resource(resource: &str, running: bool) -> Result<(), ImlAgentError> { - let sec_to_wait = 300; - let sec_delay = 2; + let sec_to_wait = WAIT_SEC; + let sec_delay = WAIT_DELAY; let delay_duration = Duration::new(sec_delay, 0); for _ in 0..(sec_to_wait / sec_delay) { let output = crm_mon_cmd().checked_output().await?; @@ -619,6 +622,41 @@ pub async fn stop_resource(resource: String) -> Result<(), ImlAgentError> { Ok(()) } +pub async fn move_resource((resource, dest_host): (String, String)) -> Result<(), ImlAgentError> { + let delay_duration = Duration::new(WAIT_DELAY, 0); + + crm_resource(&["--resource", &resource, "--move", "--node", &dest_host]).await?; + + let mut counter = 0; + let rc = loop { + let output = crm_mon_cmd().checked_output().await?; + let cluster = read_crm_output(&output.stdout)?; + + let res = cluster + .resources + .into_iter() + .filter(|r| r.id == resource) + .next(); + if let Some(r) = res { + if r.active_node_name.unwrap_or("".into()) == dest_host { + break Ok(()); + } + } + if counter >= WAIT_SEC { + break Err(ImlAgentError::from(RequiredError(format!( + "Waiting for resource {} to move to {} failed after {} sec", + resource, dest_host, WAIT_SEC, + )))); + } + counter += WAIT_DELAY; + delay_for(delay_duration).await; + }; + + crm_resource(&["--resource", &resource, "--un-move", "--node", &dest_host]).await?; + + rc +} + #[cfg(test)] mod tests { use super::{process_resource, PacemakerOperations, ResourceAgentInfo, ResourceAgentType}; From 3920ef82237dc99cc90750382c8370ae2c6182ec Mon Sep 17 00:00:00 2001 From: Nathaniel Clark Date: Tue, 17 Nov 2020 10:34:12 -0500 Subject: [PATCH 2/2] Fixup: Fix clippy warning Signed-off-by: Nathaniel Clark --- iml-agent/src/action_plugins/high_availability/mod.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/iml-agent/src/action_plugins/high_availability/mod.rs b/iml-agent/src/action_plugins/high_availability/mod.rs index 4a507a1a76..93300889b3 100644 --- a/iml-agent/src/action_plugins/high_availability/mod.rs +++ b/iml-agent/src/action_plugins/high_availability/mod.rs @@ -632,13 +632,9 @@ pub async fn move_resource((resource, dest_host): (String, String)) -> Result<() let output = crm_mon_cmd().checked_output().await?; let cluster = read_crm_output(&output.stdout)?; - let res = cluster - .resources - .into_iter() - .filter(|r| r.id == resource) - .next(); + let res = cluster.resources.into_iter().find(|r| r.id == resource); if let Some(r) = res { - if r.active_node_name.unwrap_or("".into()) == dest_host { + if r.active_node_name.unwrap_or_else(|| "".into()) == dest_host { break Ok(()); } }