Skip to content

Commit

Permalink
fix: use the actual config/settings root
Browse files Browse the repository at this point in the history
  • Loading branch information
kylezs committed Oct 11, 2023
1 parent 9b02636 commit db855d6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 28 deletions.
5 changes: 3 additions & 2 deletions api/bin/chainflip-cli/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use chainflip_api::primitives::{AccountRole, Asset, ForeignChain};
pub use chainflip_engine::settings::StateChain;
use chainflip_engine::{
constants::{CONFIG_ROOT, DEFAULT_CONFIG_ROOT},
settings::{CfSettings, Eth, EthOptions, StateChainOptions},
settings::{CfSettings, Eth, EthOptions, StateChainOptions, DEFAULT_SETTINGS_DIR},
};
use clap::Parser;
use config::{ConfigBuilder, ConfigError, Source, Value};
Expand Down Expand Up @@ -232,7 +232,7 @@ impl CLISettings {
/// New settings loaded from "$base_config_path/config/Settings.toml",
/// environment and `CommandLineOptions`
pub fn new(opts: CLICommandLineOptions) -> Result<Self, ConfigError> {
Self::load_settings_from_all_sources(opts.config_root.clone(), opts)
Self::load_settings_from_all_sources(opts.config_root.clone(), DEFAULT_SETTINGS_DIR, opts)
}
}

Expand All @@ -258,6 +258,7 @@ mod tests {

let settings = CLISettings::load_settings_from_all_sources(
DEFAULT_CONFIG_ROOT.to_owned(),
DEFAULT_SETTINGS_DIR,
CLICommandLineOptions::default(),
)
.unwrap();
Expand Down
24 changes: 13 additions & 11 deletions engine/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use chainflip_engine::{
dot::retry_rpc::DotRetryRpcClient,
eth::retry_rpc::EthersRetryRpcClient,
health, p2p,
settings::{CommandLineOptions, Settings},
settings::{CommandLineOptions, Settings, DEFAULT_SETTINGS_DIR},
settings_migrate::migrate_settings0_9_3_to_0_10_0,
state_chain_observer::{
self,
Expand Down Expand Up @@ -82,14 +82,15 @@ async fn ensure_cfe_version_record_up_to_date(
async fn main() -> anyhow::Result<()> {
use_chainflip_account_id_encoding();

let mut opts = CommandLineOptions::parse();
let provided_config_root = PathBuf::from(opts.config_root.clone());
let opts = CommandLineOptions::parse();

let used_config_root = migrate_settings0_9_3_to_0_10_0(opts.config_root.clone())?;
opts.config_root =
used_config_root.to_str().context("Invalid config-root provided")?.to_string();
let config_root_path = PathBuf::from(&opts.config_root);

let settings = Settings::new(opts).context("Error reading settings")?;
// the settings directory from opts.config_root that we'll use to read the settings file
let settings_dir = migrate_settings0_9_3_to_0_10_0(opts.config_root.clone())?;

let settings =
Settings::new_with_settings_dir(settings_dir, opts).context("Error reading settings")?;

// Note: the greeting should only be printed in normal mode (i.e. not for short-lived commands
// like `--version`), so we execute it only after the settings have been parsed.
Expand Down Expand Up @@ -155,10 +156,11 @@ async fn main() -> anyhow::Result<()> {
task_scope(|scope| start(scope, settings, state_chain_stream, state_chain_client).boxed())
);

// Effectively, we want to move the files from the temp-migrated location, to the location
// that the user has specified they want the file to be - updating it in-place.
// Note: the backup was already created and is in this folder too.
std::fs::rename(&used_config_root, &provided_config_root)
// Effectively, we want to move the files from the temp-migrated location, to the standard location
// - updating it in-place.
// Note: the backup was already created and is in the temp folder (which will become the new standard folder) too

std::fs::rename(&config_root_path.join(settings_dir), &config_root_path.join(DEFAULT_SETTINGS_DIR))
.context("Unable to replace old settings with temp settings")?;

cfe_status = CfeStatus::Active(handle);
Expand Down
17 changes: 15 additions & 2 deletions engine/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use utilities::{metrics::Prometheus, redact_endpoint_secret::SecretUrl, Port};

use crate::constants::{CONFIG_ROOT, DEFAULT_CONFIG_ROOT};

pub const DEFAULT_SETTINGS_DIR: &str = "config";

#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
pub struct P2P {
#[serde(deserialize_with = "deser_path")]
Expand Down Expand Up @@ -352,6 +354,8 @@ where
/// 4 - Default value
fn load_settings_from_all_sources(
config_root: String,
// config_root/settings_path/Settings.toml is the location of the settings that we'll read.
settings_path: &str,
opts: Self::CommandLineOptions,
) -> Result<Self, ConfigError> {
// Set the default settings
Expand All @@ -360,7 +364,8 @@ where
// If the file does not exist we will try and continue anyway.
// Because if all of the settings are covered in the environment, cli options and defaults,
// then we don't need it.
let settings_file = PathBuf::from(config_root.clone()).join(SETTINGS_IN_CONFIG_ROOT);
let settings_file =
PathBuf::from(config_root.clone()).join(settings_path).join("Settings.toml");
let file_present = settings_file.is_file();
if file_present {
builder = builder.add_source(File::from(settings_file.clone()));
Expand Down Expand Up @@ -613,13 +618,21 @@ impl Settings {
/// New settings loaded from "$base_config_path/config/Settings.toml",
/// environment and `CommandLineOptions`
pub fn new(opts: CommandLineOptions) -> Result<Self, ConfigError> {
Self::load_settings_from_all_sources(opts.config_root.clone(), opts)
Self::new_with_settings_dir(DEFAULT_SETTINGS_DIR, opts)
}

pub fn new_with_settings_dir(
settings_dir: &str,
opts: CommandLineOptions,
) -> Result<Self, ConfigError> {
Self::load_settings_from_all_sources(opts.config_root.clone(), settings_dir, opts)
}

#[cfg(test)]
pub fn new_test() -> Result<Self, ConfigError> {
Settings::load_settings_from_all_sources(
"config/testing/".to_owned(),
DEFAULT_SETTINGS_DIR,
CommandLineOptions::default(),
)
}
Expand Down
26 changes: 13 additions & 13 deletions engine/src/settings_migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@ use toml::{map::Map, Table, Value};

use anyhow::{Context, Result};

use crate::settings::SETTINGS_IN_CONFIG_ROOT;
use crate::settings::DEFAULT_SETTINGS_DIR;

const PRIVATE_KEY_FILE: &str = "private_key_file";
const WS_NODE_ENDPOINT: &str = "ws_node_endpoint";
const HTTP_NODE_ENDPOINT: &str = "http_node_endpoint";
const RPC: &str = "rpc";

const MIGRATED_SETTINGS_DIR: &str = "config-migrated";

// Returns the path to the "config root" where the Settings.toml file to be used is located.
pub fn migrate_settings0_9_3_to_0_10_0(config_root: String) -> Result<PathBuf> {
pub fn migrate_settings0_9_3_to_0_10_0(config_root: String) -> Result<&'static str> {
let config_root = PathBuf::from(config_root);
let settings_file = config_root.join(SETTINGS_IN_CONFIG_ROOT);
let settings_root = config_root.join(DEFAULT_SETTINGS_DIR);
let settings_file = settings_root.join("Settings.toml");

if !settings_file.is_file() {
tracing::warn!("Please check that the Settings.toml file exists at {settings_file:?}");
return Ok(config_root)
return Ok(DEFAULT_SETTINGS_DIR)
}

std::fs::copy(settings_file.clone(), settings_file.with_extension("toml.0_9_2.backup"))
.context("Unable to create backup of Settings.toml")?;

let old_settings_table = std::fs::read_to_string(&settings_file)
.context("Unable to read Settings.toml for migration")?
.parse::<Table>()?;
Expand All @@ -71,7 +71,7 @@ pub fn migrate_settings0_9_3_to_0_10_0(config_root: String) -> Result<PathBuf> {
};

if !migrate {
return Ok(config_root)
return Ok(DEFAULT_SETTINGS_DIR)
}

tracing::info!("Migrating settings to 0.9.3");
Expand Down Expand Up @@ -120,20 +120,20 @@ pub fn migrate_settings0_9_3_to_0_10_0(config_root: String) -> Result<PathBuf> {

rename_btc_rpc_user_and_rpc_password(&mut new_settings_table);

let tmp_config_root = config_root.join("migrated");
let tmp_config_settings = tmp_config_root.join(SETTINGS_IN_CONFIG_ROOT);
let tmp_settings_root = config_root.join(MIGRATED_SETTINGS_DIR);
let tmp_settings = tmp_settings_root.join("Settings.toml");

// backup the old settings into the temp location
std::fs::copy(settings_file.clone(), tmp_config_settings.with_extension("toml.0_9_3.backup"))
std::fs::copy(settings_file.clone(), tmp_settings.with_extension("toml.0_9_3.backup"))
.context("Unable to create backup of Settings.toml")?;

fs::write(
&tmp_config_settings,
&tmp_settings,
toml::to_string(&new_settings_table).context("Unable to serialize new Settings to TOML")?,
)
.context("Unable to write to {settings_file} for migration")?;

Ok(tmp_config_root)
Ok(MIGRATED_SETTINGS_DIR)
}

fn remove_node_from_endpoint_names(settings_table: &mut Map<String, Value>) {
Expand Down

0 comments on commit db855d6

Please sign in to comment.