Skip to content

Commit

Permalink
EDGAR Setup -> Prompt for WriteConfiguration upfront.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfm committed Oct 23, 2024
1 parent 53f393f commit 3ce32d4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 51 deletions.
13 changes: 0 additions & 13 deletions opendut-edgar/src/common/task/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,3 @@ fn print_outcome(task_name: String, outcome: Outcome) {
println!("{}", message(&task_name, &outcome, console::user_attended()));
info!("{}", message(&task_name, &outcome, false));
}


#[cfg(test)]
pub mod test {
use crate::common::task::{Task, TaskFulfilled};

pub async fn unchecked(task: impl Task) -> anyhow::Result<()> {
assert_eq!(task.check_fulfilled().await?, TaskFulfilled::Unchecked);
task.execute().await?;
assert_eq!(task.check_fulfilled().await?, TaskFulfilled::Unchecked);
Ok(())
}
}
2 changes: 2 additions & 0 deletions opendut-edgar/src/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ mod plugin;
mod tasks;

mod util;
pub mod write_configuration;

pub use util::user_confirmation_prompt;

#[derive(Clone, Debug)]
Expand Down
17 changes: 9 additions & 8 deletions opendut-edgar/src/setup/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use url::Url;
use crate::common::task::runner::RunMode;
use crate::common::task::{runner, Task};
use crate::service::network_interface::manager::NetworkInterfaceManager;
use crate::setup::tasks::write_configuration;
use crate::setup::write_configuration;
use crate::setup::util::running_in_docker;
use crate::setup::{tasks, Leader, User};
use opendut_types::peer::PeerSetup;
Expand All @@ -31,6 +31,14 @@ pub async fn managed(run_mode: RunMode, no_confirm: bool, setup_string: String,
println!("Using PeerId: {}", peer_setup.id);
println!("Will connect to CARL at: {}", peer_setup.carl);

write_configuration::WriteConfiguration::with_override(
write_configuration::ConfigOverride {
peer_id: peer_setup.id,
carl_url: peer_setup.carl,
auth_config: peer_setup.auth_config,
}
).execute().await?;

let mut tasks: Vec<Box<dyn Task>> = vec![];

#[cfg(target_arch = "arm")]
Expand All @@ -45,13 +53,6 @@ pub async fn managed(run_mode: RunMode, no_confirm: bool, setup_string: String,
tasks.append(&mut vec![
Box::new(tasks::WriteCaCertificate::with_certificate(peer_setup.ca)),
Box::new(tasks::CheckCommandLinePrograms),
Box::new(tasks::WriteConfiguration::with_override(
write_configuration::ConfigOverride {
peer_id: peer_setup.id,
carl_url: peer_setup.carl,
auth_config: peer_setup.auth_config,
}),
),
Box::new(tasks::CheckCarlReachable),
Box::new(tasks::CopyExecutable),
Box::new(tasks::copy_rperf::CopyRperf),
Expand Down
4 changes: 0 additions & 4 deletions opendut-edgar/src/setup/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ pub use request_linux_network_capability::RequestLinuxNetworkCapability;
mod restart_service;
pub use restart_service::RestartService;

pub mod write_configuration;

pub use write_configuration::WriteConfiguration;

pub mod load_kernel_modules;
pub use load_kernel_modules::LoadKernelModules;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::path::{Path, PathBuf};
use std::str::FromStr;

use anyhow::{anyhow, Context};
use async_trait::async_trait;
use tracing::{debug, error, info};
use url::Url;

Expand All @@ -13,30 +12,21 @@ use opendut_types::util::net::AuthConfig;

use crate::common::settings;
use crate::setup::constants;
use crate::common::task::{Success, Task, TaskFulfilled};

pub struct ConfigOverride {
pub peer_id: PeerId,
pub carl_url: Url,
pub auth_config: AuthConfig,
}

pub struct WriteConfiguration {
config_file_to_write_to: PathBuf,
config_merge_suggestion_file: PathBuf,
config_override: ConfigOverride,
user_attended: bool,
}
pub struct ConfigOverride {
pub peer_id: PeerId,
pub carl_url: Url,
pub auth_config: AuthConfig,
}

#[async_trait]
impl Task for WriteConfiguration {
fn description(&self) -> String {
String::from("Write Configuration")
}
async fn check_fulfilled(&self) -> anyhow::Result<TaskFulfilled> {
Ok(TaskFulfilled::Unchecked)
}
async fn execute(&self) -> anyhow::Result<Success> {
impl WriteConfiguration {
pub async fn execute(&self) -> anyhow::Result<()> {
let original_settings = self.load_current_settings()
.unwrap_or_else(|| {
debug!("Could not load settings from configuration file at '{}'. Continuing as if no previous configuration exists.", self.config_file_to_write_to.display());
Expand Down Expand Up @@ -102,7 +92,7 @@ impl Task for WriteConfiguration {

if original_settings.to_string() == new_settings_string {
debug!("The configuration on disk already matches the overrides we wanted to apply.");
return Ok(Success::message("Configuration on disk matches."))
return Ok(())
}

let target_file_empty =
Expand All @@ -122,7 +112,7 @@ impl Task for WriteConfiguration {
.context("Error while writing new configuration file.")?;

info!("Successfully wrote peer configuration to: {}", self.config_file_to_write_to.display());
Ok(Success::default())
Ok(())
} else {
write_settings(&self.config_merge_suggestion_file, &new_settings_string)
.context("Error while writing configuration merge suggestion file.")?;
Expand Down Expand Up @@ -193,8 +183,6 @@ mod tests {
use googletest::prelude::*;
use opendut_types::util::net::{ClientId, ClientSecret, OAuthScope};

use crate::common::task::runner;

use super::*;

const HOST: &str = "example.com";
Expand All @@ -214,7 +202,7 @@ mod tests {

let path = write_configuration.config_file_to_write_to.clone();

runner::test::unchecked(write_configuration).await?;
write_configuration.execute().await?;

assert!(predicate::path::exists().eval(&path));
let file_content = fs::read_to_string(&path)?;
Expand Down Expand Up @@ -249,7 +237,7 @@ mod tests {

assert!(predicate::path::missing().eval(&path));

runner::test::unchecked(write_configuration).await?;
write_configuration.execute().await?;

assert!(predicate::path::exists().eval(&path));
let file_content = fs::read_to_string(&path)?;
Expand Down Expand Up @@ -290,7 +278,7 @@ mod tests {
assert!(predicate::str::is_empty().not().eval(&file_content));
assert!(predicate::path::missing().eval(&config_merge_suggestion_file));

let result = runner::test::unchecked(write_configuration).await;
let result = write_configuration.execute().await;
assert!(result.is_err());

assert!(predicate::path::exists().eval(&config_merge_suggestion_file));
Expand Down Expand Up @@ -326,7 +314,7 @@ mod tests {
assert!(predicate::str::is_empty().not().eval(&file_content));
assert!(predicate::path::missing().eval(&config_merge_suggestion_file));

let result = runner::test::unchecked(write_configuration).await;
let result = write_configuration.execute().await;
assert!(result.is_err());

assert!(predicate::path::exists().eval(&config_merge_suggestion_file));
Expand Down Expand Up @@ -368,7 +356,7 @@ mod tests {
assert!(predicate::str::is_empty().not().eval(&file_content));
assert!(predicate::path::missing().eval(&config_merge_suggestion_file));

let result = runner::test::unchecked(write_configuration).await;
let result = write_configuration.execute().await;
assert!(result.is_ok());
assert!(predicate::path::missing().eval(&config_merge_suggestion_file));

Expand Down

0 comments on commit 3ce32d4

Please sign in to comment.