-
Notifications
You must be signed in to change notification settings - Fork 94
New Feature: dfx canister install #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
994d4c5
feat: add canister subcommand
c1dd7d8
refactor: rename Request/Response for read endpoint
70f7acd
tool: add client timestamp to version when running in debug
hansl 10d796f
feat: add canister install command
37de2d5
refactor: use URLs instead of passing strings around
hansl d616773
test: fix the build test
hansl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,4 @@ wabt = "0.9.2" | |
[dev-dependencies] | ||
env_logger = "0.6" | ||
mockito = "0.20.0" | ||
tempfile = "3.1.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use crate::lib::api_client::{install_code, Blob}; | ||
use crate::lib::env::{ClientEnv, ProjectConfigEnv}; | ||
use crate::lib::error::DfxResult; | ||
use clap::{App, Arg, ArgMatches, SubCommand}; | ||
use std::path::PathBuf; | ||
use tokio::runtime::Runtime; | ||
|
||
fn is_number(v: String) -> Result<(), String> { | ||
v.parse::<u64>() | ||
.map_err(|_| String::from("The value must be a number.")) | ||
.map(|_| ()) | ||
} | ||
|
||
pub fn construct() -> App<'static, 'static> { | ||
SubCommand::with_name("install") | ||
.about("Install a canister.") | ||
.arg( | ||
Arg::with_name("canister") | ||
.takes_value(true) | ||
.help("The canister ID (a number).") | ||
.required(true) | ||
.validator(is_number), | ||
) | ||
.arg( | ||
Arg::with_name("wasm") | ||
.help("The wasm file to use.") | ||
.required(true), | ||
) | ||
} | ||
|
||
pub fn exec<T>(env: &T, args: &ArgMatches<'_>) -> DfxResult | ||
where | ||
T: ClientEnv + ProjectConfigEnv, | ||
{ | ||
// Read the config. | ||
let config = env.get_config().unwrap(); | ||
let project_root = config.get_path().parent().unwrap(); | ||
|
||
let canister_id = args.value_of("canister").unwrap().parse::<u64>()?; | ||
let wasm_path = args.value_of("wasm").unwrap(); | ||
let wasm_path = PathBuf::from(project_root).join(wasm_path); | ||
hansl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let wasm = std::fs::read(wasm_path)?; | ||
let client = env.get_client(); | ||
|
||
let install = install_code(client, canister_id, Blob(wasm), None); | ||
|
||
let mut runtime = Runtime::new().expect("Unable to create a runtime"); | ||
runtime.block_on(install)?; | ||
hansl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use crate::commands::CliCommand; | ||
use crate::lib::env::{ClientEnv, ProjectConfigEnv}; | ||
use crate::lib::error::{DfxError, DfxResult}; | ||
use clap::{App, ArgMatches, SubCommand}; | ||
|
||
mod install; | ||
|
||
fn builtins<T>() -> Vec<CliCommand<T>> | ||
where | ||
T: ClientEnv + ProjectConfigEnv, | ||
{ | ||
vec![CliCommand::new(install::construct(), install::exec)] | ||
} | ||
|
||
pub fn construct<T>() -> App<'static, 'static> | ||
where | ||
T: ClientEnv + ProjectConfigEnv, | ||
{ | ||
SubCommand::with_name("canister") | ||
.about("Manage canisters from a network.") | ||
.subcommands( | ||
builtins::<T>() | ||
.into_iter() | ||
.map(|x| x.get_subcommand().clone()), | ||
) | ||
} | ||
|
||
pub fn exec<T>(env: &T, args: &ArgMatches<'_>) -> DfxResult | ||
where | ||
T: ClientEnv + ProjectConfigEnv, | ||
{ | ||
let subcommand = args.subcommand(); | ||
|
||
if let (name, Some(subcommand_args)) = subcommand { | ||
match builtins().into_iter().find(|x| name == x.get_name()) { | ||
Some(cmd) => cmd.execute(env, subcommand_args), | ||
None => Err(DfxError::UnknownCommand(format!( | ||
"Command {} not found.", | ||
name | ||
))), | ||
} | ||
} else { | ||
construct::<T>().write_help(&mut std::io::stderr())?; | ||
println!(); | ||
println!(); | ||
Ok(()) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,31 @@ | ||
pub mod cache; | ||
pub mod dfinity; | ||
|
||
pub const DFX_VERSION: &str = env!("CARGO_PKG_VERSION"); | ||
static mut DFX_VERSION: Option<String> = None; | ||
/// Returns the version of DFX that was built. | ||
/// In debug, add a timestamp of the upstream compilation at the end of version to ensure all | ||
/// debug runs are unique (and cached uniquely). | ||
/// That timestamp is taken from the DFX_TIMESTAMP_DEBUG_MODE_ONLY env var that is set in | ||
/// Nix. | ||
pub fn dfx_version() -> &'static str { | ||
unsafe { | ||
match &DFX_VERSION { | ||
Some(x) => x.as_str(), | ||
None => { | ||
let version = env!("CARGO_PKG_VERSION"); | ||
|
||
#[cfg(debug_assertions)] | ||
{ | ||
DFX_VERSION = Some(format!( | ||
"{}-{}", | ||
version, | ||
std::env::var("DFX_TIMESTAMP_DEBUG_MODE_ONLY") | ||
.unwrap_or_else(|_| "local-debug".to_owned()) | ||
)); | ||
} | ||
|
||
dfx_version() | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are assigning names to canisters in the project config, can we just provide the name of the canister we want to install instead?
The about text implies that a canister will be built so I'm a bit confused why we'd need to provide a wasm file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is where the flow breaks a bit. Right now we only have a canister 42.
create_canister
will return a "random" ID so we might not want to store it in the JSON (since restarting the net from nothing would give non-deterministic IDs). The best location would probably be to have our own coordinator on the test net that will be a store of name to canister ID and do a query before we do every submit. That's the best I could come up with.Do you have any ideas to improve this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep the ID and wasm. I added a task for implementing the
env.production.json
mapping we discussed: https://dfinity.atlassian.net/browse/SDK-447There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the config file format (the last time I looked) I was thinking that something like
dfx install hello
would amount to looking at thecanisters.hello.main
field, building that, and then installing the resulting Wasm file.