From 81af07ee115d9ddac5a997de765d9e44cb586354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Herbel?= Date: Tue, 19 Nov 2024 11:27:49 +0100 Subject: [PATCH] Migrate RCC profile configuration CMK-20276 --- src/bin/scheduler/setup/rcc.rs | 235 +++++++++++++++------------------ 1 file changed, 107 insertions(+), 128 deletions(-) diff --git a/src/bin/scheduler/setup/rcc.rs b/src/bin/scheduler/setup/rcc.rs index 1b8a7841..24e3b5ee 100644 --- a/src/bin/scheduler/setup/rcc.rs +++ b/src/bin/scheduler/setup/rcc.rs @@ -14,7 +14,7 @@ use robotmk::termination::{Cancelled, Outcome}; use anyhow::{anyhow, Context}; use camino::{Utf8Path, Utf8PathBuf}; use log::{debug, error}; -use robotmk::config::{CustomRCCProfileConfig, RCCProfileConfig}; +use robotmk::config::RCCProfileConfig; use std::vec; use tokio_util::sync::CancellationToken; @@ -59,6 +59,9 @@ fn run_setup_steps(config: &GlobalConfig, mut plans: Vec) -> (Vec, V #[cfg(windows)] gather_rcc_profile_permissions, gather_disable_rcc_telemetry, + gather_configure_default_rcc_profile, + gather_import_custom_rcc_profile, + gather_switch_to_custom_rcc_profile, ]; let mut failures = Vec::new(); @@ -248,92 +251,123 @@ fn gather_disable_rcc_telemetry(config: &GlobalConfig, plans: Vec) -> Vec< steps } -fn rcc_setup( - global_config: &GlobalConfig, - rcc_plans: Vec, -) -> Result<(Vec, Vec), Cancelled> { - let mut sucessful_plans: Vec; - let mut all_failures = vec![]; - let mut current_failures: Vec; - - debug!("Configuring RCC profile"); - (sucessful_plans, current_failures) = configure_rcc_profile(global_config, rcc_plans)?; - all_failures.extend(current_failures); - - #[cfg(windows)] - { - debug!("Enabling support for long paths"); - (sucessful_plans, current_failures) = - enable_long_path_support(global_config, sucessful_plans)?; - all_failures.extend(current_failures); +fn gather_configure_default_rcc_profile( + config: &GlobalConfig, + plans: Vec, +) -> Vec { + if !matches!(config.rcc_config.profile_config, RCCProfileConfig::Default) { + return vec![skip(plans)]; } - - debug!("Disabling shared holotree"); - (sucessful_plans, current_failures) = holotree_disable_sharing(global_config, sucessful_plans)?; - all_failures.extend(current_failures); - - Ok((sucessful_plans, all_failures)) + let (rcc_plans, system_plans): (Vec, Vec) = plans + .into_iter() + .partition(|plan| matches!(plan.environment, Environment::Rcc(_))); + let mut steps: Vec = Vec::new(); + for (session, plans_in_session) in plans_by_sessions(rcc_plans) { + steps.push(( + Box::new(StepRCCCommand::new_from_config( + config, + session, + &["configuration", "switch", "--noprofile"], + "default_profile_switch", + "Switching to default RCC profile failed", + )), + plans_in_session, + )); + } + steps.push(skip(system_plans)); + steps } -fn configure_rcc_profile( - global_config: &GlobalConfig, - plans: Vec, -) -> Result<(Vec, Vec), Cancelled> { - match &global_config.rcc_config.profile_config { - RCCProfileConfig::Default => configure_default_rcc_profile(global_config, plans), +fn gather_import_custom_rcc_profile(config: &GlobalConfig, plans: Vec) -> Vec { + let custom_rcc_profile_path = match &config.rcc_config.profile_config { + RCCProfileConfig::Default => return vec![skip(plans)], RCCProfileConfig::Custom(custom_rcc_profile_config) => { - configure_custom_rcc_profile(custom_rcc_profile_config, global_config, plans) + custom_rcc_profile_config.path.clone() } + }; + let (rcc_plans, system_plans): (Vec, Vec) = plans + .into_iter() + .partition(|plan| matches!(plan.environment, Environment::Rcc(_))); + let mut steps: Vec = Vec::new(); + for (session, plans_in_session) in plans_by_sessions(rcc_plans) { + steps.push(( + Box::new(StepRCCCommand::new_from_config( + config, + session.clone(), + &[ + "configuration", + "import", + "--filename", + custom_rcc_profile_path.as_str(), + ], + "custom_profile_import", + "Importing custom RCC profile failed", + )), + plans_in_session, + )); } + steps.push(skip(system_plans)); + steps } -fn configure_default_rcc_profile( - global_config: &GlobalConfig, +fn gather_switch_to_custom_rcc_profile( + config: &GlobalConfig, plans: Vec, -) -> Result<(Vec, Vec), Cancelled> { - run_rcc_per_session( - global_config, - plans, - ["configuration", "switch", "--noprofile"], - "default_profile_switch", - "Switching to default RCC profile failed", - ) +) -> Vec { + let custom_rcc_profile_name = match &config.rcc_config.profile_config { + RCCProfileConfig::Default => return vec![skip(plans)], + RCCProfileConfig::Custom(custom_rcc_profile_config) => { + custom_rcc_profile_config.name.clone() + } + }; + let (rcc_plans, system_plans): (Vec, Vec) = plans + .into_iter() + .partition(|plan| matches!(plan.environment, Environment::Rcc(_))); + let mut steps: Vec = Vec::new(); + for (session, plans_in_session) in plans_by_sessions(rcc_plans) { + steps.push(( + Box::new(StepRCCCommand::new_from_config( + config, + session, + &[ + "configuration", + "switch", + "--profile", + custom_rcc_profile_name.as_str(), + ], + "custom_profile_switch", + "Switching to custom RCC porfile failed", + )), + plans_in_session, + )); + } + steps.push(skip(system_plans)); + steps } -fn configure_custom_rcc_profile( - custom_rcc_profile_config: &CustomRCCProfileConfig, +fn rcc_setup( global_config: &GlobalConfig, - plans: Vec, + rcc_plans: Vec, ) -> Result<(Vec, Vec), Cancelled> { - let (sucessful_plans_import, failures_import) = run_rcc_per_session( - global_config, - plans, - [ - "configuration", - "import", - "--filename", - custom_rcc_profile_config.path.as_str(), - ], - "custom_profile_import", - "Importing custom RCC profile failed", - )?; - let (sucessful_plans_switch, failures_switch) = run_rcc_per_session( - global_config, - sucessful_plans_import, - [ - "configuration", - "switch", - "--profile", - custom_rcc_profile_config.name.as_str(), - ], - "custom_profile_switch", - "Switching to custom RCC porfile failed", - )?; + let mut all_failures = vec![]; - Ok(( - sucessful_plans_switch, - failures_import.into_iter().chain(failures_switch).collect(), - )) + #[cfg(windows)] + let successful_plans = { + debug!("Enabling support for long paths"); + let (successful_plans, current_failures) = + enable_long_path_support(global_config, rcc_plans)?; + all_failures.extend(current_failures); + successful_plans + }; + #[cfg(unix)] + let successful_plans = rcc_plans; + + debug!("Disabling shared holotree"); + let (sucessful_plans, current_failures) = + holotree_disable_sharing(global_config, successful_plans)?; + all_failures.extend(current_failures); + + Ok((sucessful_plans, all_failures)) } #[cfg(windows)] @@ -494,61 +528,6 @@ fn run_command_spec_once_in_current_session( ) } -fn run_rcc_per_session( - global_config: &GlobalConfig, - plans: Vec, - arguments: impl IntoIterator + Copy, - id: &str, - failure_summary: &str, -) -> Result<(Vec, Vec), Cancelled> -where - T: AsRef, -{ - let mut succesful_plans = vec![]; - let mut failures = vec![]; - - for (session, plans) in plans_by_sessions(plans) { - let mut command_spec = RCCEnvironment::bundled_command_spec( - &global_config.rcc_config.binary_path, - session - .robocorp_home(&global_config.rcc_config.robocorp_home_base) - .to_string(), - ); - command_spec.add_arguments(arguments); - debug!("Running {} for `{}`", command_spec, &session); - match execute_run_spec_in_session( - &session, - &RunSpec { - id: &format!("robotmk_{id}"), - command_spec: &command_spec, - runtime_base_path: &rcc_setup_working_directory(&global_config.working_directory) - .join(session.id()) - .join(id), - timeout: 120, - cancellation_token: &global_config.cancellation_token, - }, - )? { - Some(error_msg) => { - for plan in plans { - error!( - "Plan {}: {failure_summary}. Plan won't be scheduled. - Error: {error_msg}", - plan.id - ); - failures.push(SetupFailure { - plan_id: plan.id.clone(), - summary: failure_summary.to_string(), - details: error_msg.clone(), - }); - } - } - None => succesful_plans.extend(plans), - } - } - - Ok((succesful_plans, failures)) -} - fn execute_run_spec_in_session( session: &Session, run_spec: &RunSpec,