Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion crates/icp-cli/src/commands/network/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ pub(crate) enum CommandError {
#[error("network '{name}' must be a managed network")]
Unmanaged { name: String },

#[error("failed to create network directory")]
CreateNetworkDir { source: icp::fs::Error },

#[error("failed to cleanup canister ID store for environment '{env}'")]
CleanupCanisterIdStore {
source: icp::store_id::CleanupError,
env: String,
},

#[error(transparent)]
NetworkAccess(#[from] icp::network::AccessError),

Expand Down Expand Up @@ -98,7 +107,20 @@ pub(crate) async fn exec(ctx: &Context, args: &RunArgs) -> Result<(), CommandErr
// Network directory
let nd = ctx.network.get_network_directory(network)?;
nd.ensure_exists()
.map_err(|e| RunNetworkError::CreateDirFailed { source: e })?;
.map_err(|e| CommandError::CreateNetworkDir { source: e })?;

// Clean up any existing canister ID mappings of which environment is on this network
for env in p.environments.values() {
if env.network == *network {
// It's been ensured that the network is managed, so is_cache is true.
ctx.ids.cleanup(true, env.name.as_str()).map_err(|e| {
CommandError::CleanupCanisterIdStore {
source: e,
env: env.name.to_owned(),
}
})?;
}
}

// Identities
let ids = ctx
Expand Down
34 changes: 32 additions & 2 deletions crates/icp/src/store_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use std::{io::ErrorKind, sync::Mutex};
use ic_agent::export::Principal;
use snafu::{ResultExt, Snafu};

use crate::fs::create_dir_all;
use crate::{
CACHE_DIR, DATA_DIR, ICP_BASE,
fs::json,
fs::{create_dir_all, json, remove_file},
manifest::{ProjectRootLocate, ProjectRootLocateError},
prelude::*,
};
Expand Down Expand Up @@ -55,6 +54,9 @@ pub trait Access: Sync + Send {

/// Lookup all canister IDs for a given environment.
fn lookup_by_environment(&self, is_cache: bool, env: &str) -> Result<IdMapping, LookupIdError>;

/// Remove all canister ID mappings for a given environment.
fn cleanup(&self, is_cache: bool, env: &str) -> Result<(), CleanupError>;
}

#[derive(Debug, Snafu)]
Expand Down Expand Up @@ -99,6 +101,15 @@ pub enum LookupIdError {
EnvironmentNotFound { name: String },
}

#[derive(Debug, Snafu)]
pub enum CleanupError {
#[snafu(transparent)]
ProjectRootLocate { source: ProjectRootLocateError },

#[snafu(transparent)]
DeleteFile { source: crate::fs::Error },
}

/// Store of canister ID mappings for environments.
///
/// Each environment has a separate file storing its canister IDs mapping.
Expand Down Expand Up @@ -181,6 +192,15 @@ impl Access for AccessImpl {
env: env.to_owned(),
})
}

fn cleanup(&self, is_cache: bool, env: &str) -> Result<(), CleanupError> {
let _g = self.lock.lock().expect("failed to acquire id store lock");
let fpath = self.get_fpath_for_env(is_cache, env)?;
if fpath.exists() {
remove_file(&fpath)?;
}
Ok(())
}
}

impl AccessImpl {
Expand Down Expand Up @@ -303,5 +323,15 @@ pub(crate) mod mock {
}),
}
}

fn cleanup(&self, is_cache: bool, env: &str) -> Result<(), CleanupError> {
let mut store = if is_cache {
self.cache.lock().unwrap()
} else {
self.data.lock().unwrap()
};
store.remove(env);
Ok(())
}
}
}