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
7 changes: 5 additions & 2 deletions src/dfx/src/commands/canister/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::lib::error::{DfxError, DfxResult};
use crate::lib::operations::canister::create_canister;
use crate::util::expiry_duration;
use clap::{App, ArgMatches, Clap, FromArgMatches, IntoApp};
use tokio::runtime::Runtime;

/// Creates an empty canister on the Internet Computer and
/// associates the Internet Computer assigned Canister ID to the canister name.
Expand All @@ -29,14 +30,16 @@ pub fn exec(env: &dyn Environment, args: &ArgMatches) -> DfxResult {

let timeout = expiry_duration();

let mut runtime = Runtime::new().expect("Unable to create a runtime");

if let Some(canister_name) = opts.canister_name {
create_canister(env, canister_name.as_str(), timeout)?;
runtime.block_on(create_canister(env, canister_name.as_str(), timeout))?;
Ok(())
} else if opts.all {
// Create all canisters.
if let Some(canisters) = &config.get_config().canisters {
for canister_name in canisters.keys() {
create_canister(env, canister_name, timeout)?;
runtime.block_on(create_canister(env, canister_name, timeout))?;
}
}
Ok(())
Expand Down
11 changes: 10 additions & 1 deletion src/dfx/src/commands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::lib::operations::canister::deploy_canisters;
use crate::lib::provider::create_agent_environment;
use crate::util::expiry_duration;
use clap::{App, ArgMatches, Clap, FromArgMatches, IntoApp};
use tokio::runtime::Runtime;

/// Deploys all or a specific canister from the code in your project. By default, all canisters are deployed.
#[derive(Clap)]
Expand Down Expand Up @@ -39,5 +40,13 @@ pub fn exec(env: &dyn Environment, args: &ArgMatches) -> DfxResult {
let argument = opts.argument.as_deref();
let argument_type = opts.argument_type.as_deref();

deploy_canisters(&env, canister_name, argument, argument_type, timeout)
let mut runtime = Runtime::new().expect("Unable to create a runtime");

runtime.block_on(deploy_canisters(
&env,
canister_name,
argument,
argument_type,
timeout,
))
}
16 changes: 9 additions & 7 deletions src/dfx/src/lib/operations/canister/create_canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ use ic_utils::interfaces::ManagementCanister;
use slog::info;
use std::format;
use std::time::Duration;
use tokio::runtime::Runtime;

pub fn create_canister(env: &dyn Environment, canister_name: &str, timeout: Duration) -> DfxResult {
pub async fn create_canister(
env: &dyn Environment,
canister_name: &str,
timeout: Duration,
) -> DfxResult {
let log = env.get_logger();
info!(log, "Creating canister {:?}...", canister_name);

Expand Down Expand Up @@ -45,11 +48,10 @@ pub fn create_canister(env: &dyn Environment, canister_name: &str, timeout: Dura
.ok_or(DfxError::CommandMustBeRunInAProject)?,
);

let mut runtime = Runtime::new().expect("Unable to create a runtime");
let (cid,) = runtime.block_on(
mgr.create_canister()
.call_and_wait(waiter_with_timeout(timeout)),
)?;
let (cid,) = mgr
.create_canister()
.call_and_wait(waiter_with_timeout(timeout))
.await?;
let canister_id = cid.to_text();
info!(
log,
Expand Down
26 changes: 13 additions & 13 deletions src/dfx/src/lib/operations/canister/deploy_canisters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ use ic_utils::interfaces::management_canister::{ComputeAllocation, InstallMode,
use slog::{info, warn};
use std::convert::TryFrom;
use std::time::Duration;
use tokio::runtime::Runtime;

pub fn deploy_canisters(
pub async fn deploy_canisters(
env: &dyn Environment,
some_canister: Option<&str>,
argument: Option<&str>,
Expand All @@ -37,7 +36,7 @@ pub fn deploy_canisters(
info!(log, "Deploying all canisters.");
}

register_canisters(env, &canister_names, &initial_canister_id_store, timeout)?;
register_canisters(env, &canister_names, &initial_canister_id_store, timeout).await?;

build_canisters(env, &canister_names, &config)?;

Expand All @@ -49,7 +48,8 @@ pub fn deploy_canisters(
argument,
argument_type,
timeout,
)?;
)
.await?;

info!(log, "Deployed canisters.");

Expand All @@ -64,7 +64,7 @@ fn canisters_to_deploy(config: &Config, some_canister: Option<&str>) -> DfxResul
Ok(canister_names)
}

fn register_canisters(
async fn register_canisters(
env: &dyn Environment,
canister_names: &[String],
canister_id_store: &CanisterIdStore,
Expand All @@ -80,7 +80,7 @@ fn register_canisters(
} else {
info!(env.get_logger(), "Creating canisters...");
for canister_name in &canisters_to_create {
create_canister(env, &canister_name, timeout)?;
create_canister(env, &canister_name, timeout).await?;
}
}
Ok(())
Expand All @@ -94,7 +94,7 @@ fn build_canisters(env: &dyn Environment, canister_names: &[String], config: &Co
canister_pool.build_or_fail(BuildConfig::from_config(&config)?)
}

fn install_canisters(
async fn install_canisters(
env: &dyn Environment,
canister_names: &[String],
initial_canister_id_store: &CanisterIdStore,
Expand All @@ -109,8 +109,6 @@ fn install_canisters(
.get_agent()
.ok_or(DfxError::CommandMustBeRunInAProject)?;

let mut runtime = Runtime::new().expect("Unable to create a runtime");

let canister_id_store = CanisterIdStore::for_env(env)?;

for canister_name in canister_names {
Expand Down Expand Up @@ -143,7 +141,7 @@ fn install_canisters(
.expect("Memory allocation must be between 0 and 2^48 (i.e 256TB), inclusively.")
});

let result = runtime.block_on(install_canister(
let result = install_canister(
env,
&agent,
&canister_info,
Expand All @@ -152,7 +150,8 @@ fn install_canisters(
first_mode,
memory_allocation,
timeout,
));
)
.await;
match result {
Err(DfxError::AgentError(AgentError::ReplicaError {
reject_code,
Expand All @@ -168,7 +167,7 @@ fn install_canisters(
env.get_logger(),
"replica error. attempting {}", mode_description
);
runtime.block_on(install_canister(
install_canister(
env,
&agent,
&canister_info,
Expand All @@ -177,7 +176,8 @@ fn install_canisters(
second_mode,
memory_allocation,
timeout,
))
)
.await
}
other => other,
}?;
Expand Down