Skip to content

Commit

Permalink
fix: extract correct directory
Browse files Browse the repository at this point in the history
  • Loading branch information
dandanlen committed Oct 17, 2023
1 parent 6948086 commit f12f730
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
21 changes: 6 additions & 15 deletions engine/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,10 @@ 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 migrated_settings_file_path = 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(
&migrated_settings_file_path
.clone()
.map(|path| {
path.parent()
.expect("settings should not be in root")
.to_str()
.expect("valid unicode path")
.to_owned()
})
.unwrap_or(DEFAULT_SETTINGS_DIR.to_owned()),
migrated_settings_dir.unwrap_or(DEFAULT_SETTINGS_DIR),
opts,
)
.context("Error reading settings")?;
Expand Down Expand Up @@ -162,7 +153,7 @@ async fn main() -> anyhow::Result<()> {

if *CFE_VERSION == (SemVer { major: 0, minor: 10, patch: 0 })
{
if let Some(migrated_settings_file_path) = migrated_settings_file_path {
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"),
Expand All @@ -171,14 +162,14 @@ async fn main() -> anyhow::Result<()> {
.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::copy(
config_root_path.join(&migrated_settings_file_path),
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(migrated_settings_file_path.parent().expect("settings should not be in root"))
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.",
Expand Down
15 changes: 8 additions & 7 deletions engine/src/settings_migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ const WS_NODE_ENDPOINT: &str = "ws_node_endpoint";
const HTTP_NODE_ENDPOINT: &str = "http_node_endpoint";
const RPC: &str = "rpc";

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

// Returns the path to the migrated settings file 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<PathBuf>> {
// 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");

Expand Down Expand Up @@ -69,10 +70,11 @@ pub fn migrate_settings0_9_3_to_0_10_0(config_root: String) -> Result<Option<Pat
};

if !migrate {
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 @@ -123,14 +125,13 @@ pub fn migrate_settings0_9_3_to_0_10_0(config_root: String) -> Result<Option<Pat
std::fs::create_dir_all(&migration_dir)?
}

let migrated_file_path = migration_dir.join("Settings.toml");
fs::write(
&migrated_file_path,
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(Some(migrated_file_path))
Ok(Some(MIGRATED_SETTINGS_DIR))
}

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

0 comments on commit f12f730

Please sign in to comment.