Skip to content

Commit

Permalink
fix: settings migration
Browse files Browse the repository at this point in the history
  • Loading branch information
dandanlen committed Oct 17, 2023
1 parent 9f83f25 commit 63274d2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 30 deletions.
46 changes: 31 additions & 15 deletions engine/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,13 @@ async fn main() -> anyhow::Result<()> {
let config_root_path = PathBuf::from(&opts.config_root);

// 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 migrated_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")?;
let settings = Settings::new_with_settings_dir(
migrated_settings_dir.unwrap_or(DEFAULT_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 @@ -148,19 +151,32 @@ async fn main() -> anyhow::Result<()> {
// Wait until SCC has started, to ensure old engine has stopped
start_logger_server_fn.take().expect("only called once")(scope);

if *CFE_VERSION == (SemVer { major: 0, minor: 10, patch: 0 }) &&
settings_dir != DEFAULT_SETTINGS_DIR
if *CFE_VERSION == (SemVer { major: 0, minor: 10, patch: 0 })
{
// 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")?;
if let Some(migrated_settings_dir) = migrated_settings_dir {
// Back up the old settings.
std::fs::copy(
config_root_path.join(DEFAULT_SETTINGS_DIR).join("Settings.toml"),
config_root_path.join(DEFAULT_SETTINGS_DIR).join("Settings.backup-0.9.3.toml"),
)
.context("Unable to back up old settings. Please ensure the chainflip-engine has write permissions in the config directories.")?;

// Replace with the migrated settings.
std::fs::rename(
config_root_path.join(migrated_settings_dir).join("Settings.toml"),
config_root_path.join(DEFAULT_SETTINGS_DIR).join("Settings.toml"),
)
.context("Unable to replace old settings with migrated settings. Please ensure the chainflip-engine has write permissions in the config directories.")?;

// Remove the migration dir.
std::fs::remove_dir_all(config_root_path.join(migrated_settings_dir))
.unwrap_or_else(|e| {
tracing::warn!(
"Unable to remove migration dir: {e:?}. Please remove it manually.",
e = e
)
});
}
}

if let Some(health_check_settings) = &settings.health_check {
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 @@ -35,14 +35,14 @@ const RPC: &str = "rpc";

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

// Returns the directory inside 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<&'static str> {
// Returns the migrated settings dir if it was migrated, or None if it wasn't.
pub fn migrate_settings0_9_3_to_0_10_0(config_root: String) -> Result<Option<&'static str>> {
println!("Attempting settings migration 0.9 -> 0.10");
let config_root = PathBuf::from(config_root);
let settings_file = config_root.join(DEFAULT_SETTINGS_DIR).join("Settings.toml");

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

let old_settings_table = std::fs::read_to_string(&settings_file)
Expand Down Expand Up @@ -70,10 +70,11 @@ pub fn migrate_settings0_9_3_to_0_10_0(config_root: String) -> Result<&'static s
};

if !migrate {
return Ok(DEFAULT_SETTINGS_DIR)
println!("Settings migration not required.");
return Ok(None)
}

tracing::info!("Migrating settings to 0.9.3");
println!("Migrating settings to 0.10");

let mut new_settings_table = old_settings_table.clone();

Expand Down Expand Up @@ -119,19 +120,18 @@ pub fn migrate_settings0_9_3_to_0_10_0(config_root: String) -> Result<&'static s

rename_btc_rpc_user_and_rpc_password(&mut new_settings_table);

let tmp_settings = config_root.join(MIGRATED_SETTINGS_DIR).join("Settings.toml");

// backup the old settings into the temp location
std::fs::copy(settings_file.clone(), tmp_settings.with_extension("toml.0_9_3.backup"))
.context("Unable to create backup of Settings.toml")?;
let migration_dir = config_root.join(MIGRATED_SETTINGS_DIR);
if !migration_dir.exists() {
std::fs::create_dir_all(&migration_dir)?
}

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

Ok(MIGRATED_SETTINGS_DIR)
Ok(Some(MIGRATED_SETTINGS_DIR))
}

fn remove_node_from_endpoint_names(settings_table: &mut Map<String, Value>) {
Expand Down
4 changes: 2 additions & 2 deletions engine/src/state_chain_observer/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use futures::{StreamExt, TryStreamExt};
use sp_core::{Pair, H256};
use state_chain_runtime::AccountId;
use std::{sync::Arc, time::Duration};
use tracing::{info, warn};
use tracing::info;

use utilities::{
make_periodic_tick, read_clean_and_decode_hex_str_file,
Expand Down Expand Up @@ -358,7 +358,7 @@ impl<BaseRpcClient: base_rpc_api::BaseRpcApi + Send + Sync + 'static, SignedExtr
.boxed();

incompatible_blocks.try_for_each(move |(block_hash, current_release_version)| futures::future::ready({
warn!("This version '{}' is incompatible with the current release '{}' at block: {}. WAITING for a compatible release version.", required_version, current_release_version, block_hash);
info!("This version '{}' is incompatible with the current release '{}' at block: {}. WAITING for a compatible release version.", required_version, current_release_version, block_hash);
Ok::<_, anyhow::Error>(())
})).await?;
} else {
Expand Down

0 comments on commit 63274d2

Please sign in to comment.