Skip to content

Commit

Permalink
chore: migrate settings for CFE upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
kylezs committed Oct 11, 2023
1 parent f9120e3 commit 9b02636
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
18 changes: 15 additions & 3 deletions engine/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ use clap::Parser;
use futures::FutureExt;
use jsonrpsee::core::client::ClientT;
use multisig::{self, bitcoin::BtcSigning, eth::EthSigning, polkadot::PolkadotSigning};
use std::sync::{atomic::AtomicBool, Arc};
use std::{
path::PathBuf,
sync::{atomic::AtomicBool, Arc},
};
use tracing::info;
use utilities::{
make_periodic_tick, metrics,
Expand Down Expand Up @@ -79,9 +82,12 @@ async fn ensure_cfe_version_record_up_to_date(
async fn main() -> anyhow::Result<()> {
use_chainflip_account_id_encoding();

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

migrate_settings0_9_3_to_0_10_0(opts.config_root.clone())?;
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 settings = Settings::new(opts).context("Error reading settings")?;

Expand Down Expand Up @@ -149,6 +155,12 @@ 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)
.context("Unable to replace old settings with temp settings")?;

cfe_status = CfeStatus::Active(handle);
} else {
tracing::info!("Current runtime is not compatible with this CFE version ({:?})", *CFE_VERSION);
Expand Down
4 changes: 3 additions & 1 deletion engine/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ where
deserializer.deserialize_any(PathVisitor)
}

pub const SETTINGS_IN_CONFIG_ROOT: &str = "config/Settings.toml";

/// Describes behaviour required by a struct to be used for as settings/configuration
pub trait CfSettings
where
Expand All @@ -358,7 +360,7 @@ 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("config/Settings.toml");
let settings_file = PathBuf::from(config_root.clone()).join(SETTINGS_IN_CONFIG_ROOT);
let file_present = settings_file.is_file();
if file_present {
builder = builder.add_source(File::from(settings_file.clone()));
Expand Down
23 changes: 17 additions & 6 deletions engine/src/settings_migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@ use toml::{map::Map, Table, Value};

use anyhow::{Context, Result};

use crate::settings::SETTINGS_IN_CONFIG_ROOT;

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";

pub fn migrate_settings0_9_3_to_0_10_0(config_root: String) -> Result<()> {
let settings_file = PathBuf::from(config_root).join("config/Settings.toml");
// 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> {
let config_root = PathBuf::from(config_root);
let settings_file = config_root.join(SETTINGS_IN_CONFIG_ROOT);

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

std::fs::copy(settings_file.clone(), settings_file.with_extension("toml.0_9_2.backup"))
Expand Down Expand Up @@ -67,7 +71,7 @@ pub fn migrate_settings0_9_3_to_0_10_0(config_root: String) -> Result<()> {
};

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

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

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);

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

fs::write(
settings_file,
&tmp_config_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(())
Ok(tmp_config_root)
}

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

0 comments on commit 9b02636

Please sign in to comment.