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
57 changes: 1 addition & 56 deletions src/dfx/src/commands/canister/create.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
use crate::lib::environment::Environment;
use crate::lib::error::{DfxError, DfxResult};
use crate::lib::message::UserMessage;
use crate::lib::models::canister_id_store::CanisterIdStore;
use crate::lib::progress_bar::ProgressBar;
use crate::lib::provider::get_network_context;
use crate::lib::waiter::create_waiter;
use crate::lib::operations::canister::create_canister;

use clap::{App, Arg, ArgMatches, SubCommand};
use ic_agent::ManagementCanister;
use std::format;
use tokio::runtime::Runtime;

pub fn construct() -> App<'static, 'static> {
SubCommand::with_name("create")
Expand All @@ -30,55 +24,6 @@ pub fn construct() -> App<'static, 'static> {
)
}

fn create_canister(env: &dyn Environment, canister_name: &str) -> DfxResult {
let message = format!("Creating canister {:?}...", canister_name);
let b = ProgressBar::new_spinner(&message);

env.get_config()
.ok_or(DfxError::CommandMustBeRunInAProject)?;

let mgr = ManagementCanister::new(
env.get_agent()
.ok_or(DfxError::CommandMustBeRunInAProject)?,
);
let mut runtime = Runtime::new().expect("Unable to create a runtime");

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

let network_name = get_network_context()?;

let non_default_network = if network_name == "local" {
format!("")
} else {
format!("on network {:?} ", network_name)
};

match canister_id_store.find(&canister_name) {
Some(canister_id) => {
let message = format!(
"{:?} canister was already created {}and has canister id: {:?}",
canister_name,
non_default_network,
canister_id.to_text()
);
b.finish_with_message(&message);
Ok(())
}
None => {
let cid = runtime.block_on(mgr.create_canister(create_waiter()))?;
let canister_id = cid.to_text();
let message = format!(
"{:?} canister created {}with canister id: {:?}",
canister_name, non_default_network, canister_id
);
b.finish_with_message(&message);
canister_id_store.add(&canister_name, canister_id)
}
}?;

Ok(())
}

pub fn exec(env: &dyn Environment, args: &ArgMatches<'_>) -> DfxResult {
let config = env
.get_config()
Expand Down
49 changes: 2 additions & 47 deletions src/dfx/src/commands/canister/install.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use crate::lib::canister_info::CanisterInfo;
use crate::lib::environment::Environment;
use crate::lib::error::{DfxError, DfxResult};
use crate::lib::installers::assets::post_install_store_assets;
use crate::lib::message::UserMessage;
use crate::lib::models::canister_id_store::CanisterIdStore;
use crate::lib::waiter::create_waiter;
use crate::lib::operations::canister::install_canister;

use clap::{App, Arg, ArgMatches, SubCommand};
use ic_agent::{Agent, CanisterAttributes, ComputeAllocation, InstallMode, ManagementCanister};
use slog::info;
use ic_agent::{ComputeAllocation, InstallMode};
use std::convert::TryFrom;
use std::str::FromStr;
use tokio::runtime::Runtime;
Expand Down Expand Up @@ -55,49 +53,6 @@ pub fn construct() -> App<'static, 'static> {
)
}

async fn install_canister(
env: &dyn Environment,
agent: &Agent,
canister_info: &CanisterInfo,
compute_allocation: Option<ComputeAllocation>,
mode: InstallMode,
) -> DfxResult {
let mgr = ManagementCanister::new(agent);
let log = env.get_logger();
let canister_id = canister_info.get_canister_id().map_err(|_| {
DfxError::CannotFindBuildOutputForCanister(canister_info.get_name().to_owned())
})?;

info!(
log,
"Installing code for canister {}, with canister_id {}",
canister_info.get_name(),
canister_id.to_text(),
);

let wasm_path = canister_info
.get_output_wasm_path()
.expect("Cannot get WASM output path.");
let wasm = std::fs::read(wasm_path)?;

mgr.install_code(
create_waiter(),
&canister_id,
mode,
&wasm,
&[],
&CanisterAttributes { compute_allocation },
)
.await
.map_err(DfxError::from)?;

if canister_info.get_type() == "assets" {
post_install_store_assets(&canister_info, &agent).await?;
}

Ok(())
}

fn compute_allocation_validator(compute_allocation: String) -> Result<(), String> {
if let Ok(num) = compute_allocation.parse::<u64>() {
if num <= 100 {
Expand Down
1 change: 1 addition & 0 deletions src/dfx/src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod logger;
pub mod message;
pub mod models;
pub mod network;
pub mod operations;
pub mod package_arguments;
pub mod progress_bar;
pub mod provider;
Expand Down
59 changes: 59 additions & 0 deletions src/dfx/src/lib/operations/canister/create_canister.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::lib::environment::Environment;
use crate::lib::error::{DfxError, DfxResult};
use crate::lib::models::canister_id_store::CanisterIdStore;
use crate::lib::progress_bar::ProgressBar;
use crate::lib::provider::get_network_context;
use crate::lib::waiter::create_waiter;

use ic_agent::ManagementCanister;
use std::format;
use tokio::runtime::Runtime;

pub fn create_canister(env: &dyn Environment, canister_name: &str) -> DfxResult {
let message = format!("Creating canister {:?}...", canister_name);
let b = ProgressBar::new_spinner(&message);

env.get_config()
.ok_or(DfxError::CommandMustBeRunInAProject)?;

let mgr = ManagementCanister::new(
env.get_agent()
.ok_or(DfxError::CommandMustBeRunInAProject)?,
);
let mut runtime = Runtime::new().expect("Unable to create a runtime");

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

let network_name = get_network_context()?;

let non_default_network = if network_name == "local" {
format!("")
} else {
format!("on network {:?} ", network_name)
};

match canister_id_store.find(&canister_name) {
Some(canister_id) => {
let message = format!(
"{:?} canister was already created {}and has canister id: {:?}",
canister_name,
non_default_network,
canister_id.to_text()
);
b.finish_with_message(&message);
Ok(())
}
None => {
let cid = runtime.block_on(mgr.create_canister(create_waiter()))?;
let canister_id = cid.to_text();
let message = format!(
"{:?} canister created {}with canister id: {:?}",
canister_name, non_default_network, canister_id
);
b.finish_with_message(&message);
canister_id_store.add(&canister_name, canister_id)
}
}?;

Ok(())
}
51 changes: 51 additions & 0 deletions src/dfx/src/lib/operations/canister/install_canister.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::lib::canister_info::CanisterInfo;
use crate::lib::environment::Environment;
use crate::lib::error::{DfxError, DfxResult};
use crate::lib::installers::assets::post_install_store_assets;
use crate::lib::waiter::create_waiter;

use ic_agent::{Agent, CanisterAttributes, ComputeAllocation, InstallMode, ManagementCanister};
use slog::info;

pub async fn install_canister(
env: &dyn Environment,
agent: &Agent,
canister_info: &CanisterInfo,
compute_allocation: Option<ComputeAllocation>,
mode: InstallMode,
) -> DfxResult {
let mgr = ManagementCanister::new(agent);
let log = env.get_logger();
let canister_id = canister_info.get_canister_id().map_err(|_| {
DfxError::CannotFindBuildOutputForCanister(canister_info.get_name().to_owned())
})?;

info!(
log,
"Installing code for canister {}, with canister_id {}",
canister_info.get_name(),
canister_id.to_text(),
);

let wasm_path = canister_info
.get_output_wasm_path()
.expect("Cannot get WASM output path.");
let wasm = std::fs::read(wasm_path)?;

mgr.install_code(
create_waiter(),
&canister_id,
mode,
&wasm,
&[],
&CanisterAttributes { compute_allocation },
)
.await
.map_err(DfxError::from)?;

if canister_info.get_type() == "assets" {
post_install_store_assets(&canister_info, &agent).await?;
}

Ok(())
}
5 changes: 5 additions & 0 deletions src/dfx/src/lib/operations/canister/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod create_canister;
mod install_canister;

pub use create_canister::create_canister;
pub use install_canister::install_canister;
1 change: 1 addition & 0 deletions src/dfx/src/lib/operations/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod canister;