Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce code duplication in setup steps #650

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bin/scheduler/setup/steps/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl SetupStep for SetupStepSuccess {
}
}

pub fn skip(plans: Vec<Plan>) -> (Box<dyn SetupStep>, Vec<Plan>) {
pub fn skip(plans: Vec<Plan>) -> StepWithPlans {
(Box::new(SetupStepSuccess {}), plans)
}

Expand Down
47 changes: 23 additions & 24 deletions src/bin/scheduler/setup/steps/directories.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::api::{self, skip, SetupStep, StepWithPlans};
use super::plans_by_sessions;
use super::{
partition_into_rcc_and_system_plans, plans_by_sessions, rcc_working_directory_for_session,
};

use crate::internal_config::{GlobalConfig, Plan, Source};
#[cfg(windows)]
Expand Down Expand Up @@ -82,9 +84,8 @@ impl SetupStep for StepRobocorpHomeBase {

#[cfg(windows)]
pub fn gather_robocorp_home_base(config: &GlobalConfig, plans: Vec<Plan>) -> Vec<StepWithPlans> {
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) = plans
.into_iter()
.partition(|plan| matches!(plan.environment, Environment::Rcc(_)));
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) =
partition_into_rcc_and_system_plans(plans);
vec![
(
Box::new(StepRobocorpHomeBase {
Expand All @@ -101,10 +102,9 @@ pub fn gather_robocorp_home_per_user(
config: &GlobalConfig,
plans: Vec<Plan>,
) -> Vec<StepWithPlans> {
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) = plans
.into_iter()
.partition(|plan| matches!(plan.environment, Environment::Rcc(_)));
let mut setup_steps: Vec<StepWithPlans> = Vec::new();
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) =
partition_into_rcc_and_system_plans(plans);
let mut setup_steps: Vec<StepWithPlans> = vec![skip(system_plans)];
for (session, plans_in_session) in plans_by_sessions(rcc_plans) {
setup_steps.push((
Box::new(StepCreateWithAccess {
Expand All @@ -114,7 +114,6 @@ pub fn gather_robocorp_home_per_user(
plans_in_session,
));
}
setup_steps.push(skip(system_plans));
setup_steps
}

Expand Down Expand Up @@ -159,9 +158,8 @@ pub fn gather_environment_building_directories(
}

pub fn gather_rcc_working_base(config: &GlobalConfig, plans: Vec<Plan>) -> Vec<StepWithPlans> {
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) = plans
.into_iter()
.partition(|plan| matches!(plan.environment, Environment::Rcc(_)));
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) =
partition_into_rcc_and_system_plans(plans);
vec![
(
Box::new(StepCreate {
Expand All @@ -174,20 +172,21 @@ pub fn gather_rcc_working_base(config: &GlobalConfig, plans: Vec<Plan>) -> Vec<S
}

pub fn gather_rcc_working_per_user(config: &GlobalConfig, plans: Vec<Plan>) -> Vec<StepWithPlans> {
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) = plans
.into_iter()
.partition(|plan| matches!(plan.environment, Environment::Rcc(_)));
let mut setup_steps: Vec<StepWithPlans> = Vec::new();
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) =
partition_into_rcc_and_system_plans(plans);
let mut setup_steps: Vec<StepWithPlans> = vec![skip(system_plans)];
for (session, plans_in_session) in plans_by_sessions(rcc_plans) {
setup_steps.push((
Box::new(StepCreateWithAccess {
target: config.working_directory_rcc_setup_steps.join(session.id()),
target: rcc_working_directory_for_session(
&config.working_directory_rcc_setup_steps,
&session,
),
session,
}),
plans_in_session,
));
}
setup_steps.push(skip(system_plans));
setup_steps
}

Expand All @@ -197,15 +196,15 @@ pub fn gather_rcc_longpath_directory(
plans: Vec<Plan>,
) -> Vec<StepWithPlans> {
use robotmk::session::CurrentSession;
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) = plans
.into_iter()
.partition(|plan| matches!(plan.environment, Environment::Rcc(_)));
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) =
partition_into_rcc_and_system_plans(plans);
vec![
(
Box::new(StepCreate {
target: config
.working_directory_rcc_setup_steps
.join(CurrentSession {}.id()),
target: rcc_working_directory_for_session(
&config.working_directory_rcc_setup_steps,
&Session::Current(CurrentSession {}),
),
}),
rcc_plans,
),
Expand Down
16 changes: 16 additions & 0 deletions src/bin/scheduler/setup/steps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ mod rcc;
pub mod run;

use crate::internal_config::Plan;

use camino::{Utf8Path, Utf8PathBuf};
use robotmk::environment::Environment;
use robotmk::session::Session;
use std::collections::HashMap;

Expand All @@ -17,3 +20,16 @@ fn plans_by_sessions(plans: Vec<Plan>) -> HashMap<Session, Vec<Plan>> {
}
plans_by_session
}

fn partition_into_rcc_and_system_plans(plans: Vec<Plan>) -> (Vec<Plan>, Vec<Plan>) {
plans
.into_iter()
.partition(|plan| matches!(plan.environment, Environment::Rcc(_)))
}

fn rcc_working_directory_for_session(
working_directory_rcc_setup_steps: &Utf8Path,
session: &Session,
) -> Utf8PathBuf {
working_directory_rcc_setup_steps.join(session.id())
}
83 changes: 37 additions & 46 deletions src/bin/scheduler/setup/steps/rcc.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use super::api::{self, skip, SetupStep, StepWithPlans};
use super::plans_by_sessions;
use super::{
partition_into_rcc_and_system_plans, plans_by_sessions, rcc_working_directory_for_session,
};

use crate::internal_config::{GlobalConfig, Plan};
use crate::logging::log_and_return_error;
#[cfg(windows)]
use crate::setup::windows_permissions::run_icacls_command;

use robotmk::config::RCCProfileConfig;
use robotmk::environment::{Environment, RCCEnvironment};
use robotmk::environment::RCCEnvironment;
#[cfg(windows)]
use robotmk::session::CurrentSession;
use robotmk::session::{RunSpec, Session};
Expand Down Expand Up @@ -99,10 +101,11 @@ impl SetupStep for StepRCCCommand {
let run_spec = RunSpec {
id: &format!("robotmk_{}", self.id),
command_spec: &command_spec,
runtime_base_path: &self
.working_directory
.join(self.session.id())
.join(&self.id),
runtime_base_path: &rcc_working_directory_for_session(
&self.working_directory,
&self.session,
)
.join(&self.id),
timeout: 120,
cancellation_token: &self.cancellation_token,
};
Expand Down Expand Up @@ -190,7 +193,11 @@ impl SetupStep for StepDisableSharedHolotree {
let run_spec = &RunSpec {
id: &format!("robotmk_{name}_{}", self.session.id()),
command_spec: &command_spec,
runtime_base_path: &self.working_directory.join(self.session.id()).join(name),
runtime_base_path: &rcc_working_directory_for_session(
&self.working_directory,
&self.session,
)
.join(name),
timeout: 120,
cancellation_token: &self.cancellation_token,
};
Expand Down Expand Up @@ -246,10 +253,9 @@ pub fn gather_rcc_binary_permissions(
config: &GlobalConfig,
plans: Vec<Plan>,
) -> Vec<StepWithPlans> {
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) = plans
.into_iter()
.partition(|plan| matches!(plan.environment, Environment::Rcc(_)));
let mut steps: Vec<StepWithPlans> = Vec::new();
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) =
partition_into_rcc_and_system_plans(plans);
let mut steps: Vec<StepWithPlans> = vec![skip(system_plans)];
for (session, plans_in_session) in plans_by_sessions(rcc_plans) {
steps.push((
Box::new(StepFilePermissions {
Expand All @@ -260,7 +266,6 @@ pub fn gather_rcc_binary_permissions(
plans_in_session,
));
}
steps.push(skip(system_plans));
steps
}

Expand All @@ -269,11 +274,9 @@ pub fn gather_rcc_profile_permissions(
config: &GlobalConfig,
plans: Vec<Plan>,
) -> Vec<StepWithPlans> {
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) = plans
.into_iter()
.partition(|plan| matches!(plan.environment, Environment::Rcc(_)));
let mut steps: Vec<StepWithPlans> = Vec::new();

let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) =
partition_into_rcc_and_system_plans(plans);
let mut steps: Vec<StepWithPlans> = vec![skip(system_plans)];
match &config.rcc_config.profile_config {
RCCProfileConfig::Default => steps.push(skip(rcc_plans)),
RCCProfileConfig::Custom(custom_profile) => {
Expand All @@ -289,15 +292,13 @@ pub fn gather_rcc_profile_permissions(
}
}
}
steps.push(skip(system_plans));
steps
}

pub fn gather_disable_rcc_telemetry(config: &GlobalConfig, plans: Vec<Plan>) -> Vec<StepWithPlans> {
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) = plans
.into_iter()
.partition(|plan| matches!(plan.environment, Environment::Rcc(_)));
let mut steps: Vec<StepWithPlans> = Vec::new();
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) =
partition_into_rcc_and_system_plans(plans);
let mut steps: Vec<StepWithPlans> = vec![skip(system_plans)];
for (session, plans_in_session) in plans_by_sessions(rcc_plans) {
steps.push((
Box::new(StepRCCCommand::new_from_config(
Expand All @@ -310,7 +311,6 @@ pub fn gather_disable_rcc_telemetry(config: &GlobalConfig, plans: Vec<Plan>) ->
plans_in_session,
));
}
steps.push(skip(system_plans));
steps
}

Expand All @@ -321,10 +321,9 @@ pub fn gather_configure_default_rcc_profile(
if !matches!(config.rcc_config.profile_config, RCCProfileConfig::Default) {
return vec![skip(plans)];
}
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) = plans
.into_iter()
.partition(|plan| matches!(plan.environment, Environment::Rcc(_)));
let mut steps: Vec<StepWithPlans> = Vec::new();
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) =
partition_into_rcc_and_system_plans(plans);
let mut steps: Vec<StepWithPlans> = vec![skip(system_plans)];
for (session, plans_in_session) in plans_by_sessions(rcc_plans) {
steps.push((
Box::new(StepRCCCommand::new_from_config(
Expand All @@ -337,7 +336,6 @@ pub fn gather_configure_default_rcc_profile(
plans_in_session,
));
}
steps.push(skip(system_plans));
steps
}

Expand All @@ -351,10 +349,9 @@ pub fn gather_import_custom_rcc_profile(
custom_rcc_profile_config.path.clone()
}
};
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) = plans
.into_iter()
.partition(|plan| matches!(plan.environment, Environment::Rcc(_)));
let mut steps: Vec<StepWithPlans> = Vec::new();
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) =
partition_into_rcc_and_system_plans(plans);
let mut steps: Vec<StepWithPlans> = vec![skip(system_plans)];
for (session, plans_in_session) in plans_by_sessions(rcc_plans) {
steps.push((
Box::new(StepRCCCommand::new_from_config(
Expand All @@ -372,7 +369,6 @@ pub fn gather_import_custom_rcc_profile(
plans_in_session,
));
}
steps.push(skip(system_plans));
steps
}

Expand All @@ -386,10 +382,9 @@ pub fn gather_switch_to_custom_rcc_profile(
custom_rcc_profile_config.name.clone()
}
};
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) = plans
.into_iter()
.partition(|plan| matches!(plan.environment, Environment::Rcc(_)));
let mut steps: Vec<StepWithPlans> = Vec::new();
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) =
partition_into_rcc_and_system_plans(plans);
let mut steps: Vec<StepWithPlans> = vec![skip(system_plans)];
for (session, plans_in_session) in plans_by_sessions(rcc_plans) {
steps.push((
Box::new(StepRCCCommand::new_from_config(
Expand All @@ -407,7 +402,6 @@ pub fn gather_switch_to_custom_rcc_profile(
plans_in_session,
));
}
steps.push(skip(system_plans));
steps
}

Expand All @@ -416,9 +410,8 @@ pub fn gather_enable_rcc_long_path_support(
config: &GlobalConfig,
plans: Vec<Plan>,
) -> Vec<StepWithPlans> {
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) = plans
.into_iter()
.partition(|plan| matches!(plan.environment, Environment::Rcc(_)));
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) =
partition_into_rcc_and_system_plans(plans);
vec![
(
Box::new(StepRCCCommand::new_from_config(
Expand All @@ -438,10 +431,9 @@ pub fn gather_disable_rcc_shared_holotree(
global_config: &GlobalConfig,
plans: Vec<Plan>,
) -> Vec<StepWithPlans> {
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) = plans
.into_iter()
.partition(|plan| matches!(plan.environment, Environment::Rcc(_)));
let mut steps: Vec<StepWithPlans> = Vec::new();
let (rcc_plans, system_plans): (Vec<Plan>, Vec<Plan>) =
partition_into_rcc_and_system_plans(plans);
let mut steps: Vec<StepWithPlans> = vec![skip(system_plans)];
for (session, plans_in_session) in plans_by_sessions(rcc_plans) {
steps.push((
Box::new(StepDisableSharedHolotree::new_from_config(
Expand All @@ -451,6 +443,5 @@ pub fn gather_disable_rcc_shared_holotree(
plans_in_session,
));
}
steps.push(skip(system_plans));
steps
}
4 changes: 2 additions & 2 deletions src/bin/scheduler/setup/steps/run.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::api::{run_steps, SetupStep};
use super::api::{run_steps, StepWithPlans};
use super::{directories, rcc};
use crate::internal_config::{sort_plans_by_grouping, GlobalConfig, Plan};
use robotmk::results::SetupFailure;
Expand Down Expand Up @@ -27,7 +27,7 @@ pub fn run(
Ok((plans, failures))
}

type Gatherer = fn(&GlobalConfig, Vec<Plan>) -> Vec<(Box<dyn SetupStep>, Vec<Plan>)>;
type Gatherer = fn(&GlobalConfig, Vec<Plan>) -> Vec<StepWithPlans>;
#[cfg(unix)]
type Steps = [Gatherer; 10];
#[cfg(windows)]
Expand Down