-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcreate.rs
More file actions
262 lines (225 loc) · 9.01 KB
/
create.rs
File metadata and controls
262 lines (225 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
use anyhow::anyhow;
use candid::{Nat, Principal};
use clap::{ArgGroup, Args, Parser};
use icp::context::Context;
use icp::parsers::{CyclesAmount, DurationAmount, MemoryAmount};
use icp::{Canister, context::CanisterSelection, prelude::*};
use icp_canister_interfaces::management_canister::CanisterSettingsArg;
use crate::{commands::args, operations::create::CreateOperation};
pub(crate) const DEFAULT_CANISTER_CYCLES: u128 = 2 * TRILLION;
#[derive(Clone, Debug, Default, Args)]
pub(crate) struct CanisterSettings {
/// Optional compute allocation (0 to 100). Represents guaranteed compute capacity.
#[arg(long)]
pub(crate) compute_allocation: Option<u64>,
/// Optional memory allocation in bytes. If unset, memory is allocated dynamically.
/// Supports suffixes: kb, kib, mb, mib, gb, gib (e.g. "4gib" or "2.5kb").
#[arg(long)]
pub(crate) memory_allocation: Option<MemoryAmount>,
/// Optional freezing threshold. Controls how long a canister can be inactive before being frozen.
/// Supports duration suffixes: s (seconds), m (minutes), h (hours), d (days), w (weeks).
/// A bare number is treated as seconds.
#[arg(long)]
pub(crate) freezing_threshold: Option<DurationAmount>,
/// Optional upper limit on cycles reserved for future resource payments.
/// Memory allocations that would push the reserved balance above this limit will fail.
/// Supports suffixes: k (thousand), m (million), b (billion), t (trillion).
#[arg(long)]
pub(crate) reserved_cycles_limit: Option<CyclesAmount>,
}
/// Create a canister on a network.
#[derive(Debug, Parser)]
#[command(after_long_help = "\
This command can be used to create canisters defined in a project
or a \"detached\" canister on a network.
Examples:
# Create on a network by url
icp canister create -n http://localhost:8000 -k $ROOT_KEY --detached
# Create on mainnet outside of a project context
icp canister create -n ic --detached
# Create a detached canister inside the scope of a project
icp canister create -n mynetwork --detached
")]
#[command(group(
ArgGroup::new("canister_sel")
.args(["canister", "detached"])
.required(true)
))]
pub(crate) struct CreateArgs {
#[command(flatten)]
pub(crate) cmd_args: args::OptionalCanisterCommandArgs,
/// One or more controllers for the canister. Repeat `--controller` to specify multiple.
#[arg(long)]
pub(crate) controller: Vec<Principal>,
// Resource-related settings and thresholds for the new canister.
#[command(flatten)]
pub(crate) settings: CanisterSettings,
/// Suppress human-readable output; print only canister IDs, one per line, to stdout.
#[arg(long, short = 'q')]
pub(crate) quiet: bool,
/// Cycles to fund canister creation.
/// Supports suffixes: k (thousand), m (million), b (billion), t (trillion).
#[arg(long, default_value_t = CyclesAmount::from(DEFAULT_CANISTER_CYCLES))]
pub(crate) cycles: CyclesAmount,
/// The subnet to create canisters on.
#[arg(long)]
pub(crate) subnet: Option<Principal>,
/// Create a canister detached from any project configuration. The canister id will be
/// printed out but not recorded in the project configuration. Not valid if `Canister`
/// is provided.
#[arg(
long,
conflicts_with = "canister",
required_unless_present = "canister"
)]
pub detached: bool,
}
impl CreateArgs {
pub(crate) fn canister_settings_with_default(&self, default: &Canister) -> CanisterSettingsArg {
CanisterSettingsArg {
freezing_threshold: self
.settings
.freezing_threshold
.clone()
.or(default.settings.freezing_threshold.clone())
.map(|d| Nat::from(d.get())),
controllers: if self.controller.is_empty() {
None
} else {
Some(self.controller.clone())
},
reserved_cycles_limit: self
.settings
.reserved_cycles_limit
.clone()
.or(default.settings.reserved_cycles_limit.clone())
.map(|c| Nat::from(c.get())),
// TODO This should be configurable from the CLI
log_visibility: default.settings.log_visibility.clone().map(Into::into),
memory_allocation: self
.settings
.memory_allocation
.clone()
.or(default.settings.memory_allocation.clone())
.map(|m| Nat::from(m.get())),
compute_allocation: self
.settings
.compute_allocation
.or(default.settings.compute_allocation)
.map(Nat::from),
}
}
pub(crate) fn canister_settings(&self) -> CanisterSettingsArg {
CanisterSettingsArg {
freezing_threshold: self
.settings
.freezing_threshold
.clone()
.map(|d| Nat::from(d.get())),
controllers: if self.controller.is_empty() {
None
} else {
Some(self.controller.clone())
},
reserved_cycles_limit: self
.settings
.reserved_cycles_limit
.clone()
.map(|c| Nat::from(c.get())),
// TODO This should be configurable from the CLI
log_visibility: None,
memory_allocation: self
.settings
.memory_allocation
.clone()
.map(|m| Nat::from(m.get())),
compute_allocation: self.settings.compute_allocation.map(Nat::from),
}
}
}
// Creates canister(s) by asking the cycles ledger to create them.
// The cycles ledger will take cycles out of the user's account, and attaches them to a call to CMC::create_canister.
// The CMC will then pick a subnet according to the user's preferences and permissions, and create a canister on that subnet.
pub(crate) async fn exec(ctx: &Context, args: &CreateArgs) -> Result<(), anyhow::Error> {
if args.detached {
create_canister(ctx, args).await
} else {
create_project_canister(ctx, args).await
}
}
// Attemtps to create a canister on the target network without recording it in the project metadata
async fn create_canister(ctx: &Context, args: &CreateArgs) -> Result<(), anyhow::Error> {
let selections = args.cmd_args.selections();
assert!(
selections.canister.is_none(),
"This path should not be called if canister is_some()"
);
let agent = ctx
.get_agent(
&selections.identity,
&selections.network,
&selections.environment,
)
.await?;
let create_operation = CreateOperation::new(agent, args.subnet, args.cycles.get(), vec![]);
let canister_settings = args.canister_settings();
let id = create_operation.create(&canister_settings).await?;
if args.quiet {
let _ = ctx.term.write_line(&format!("{id}"));
} else {
let _ = ctx
.term
.write_line(&format!("Created canister with ID {id}"));
}
Ok(())
}
// Attempts to create a canister and record it in the project metadata
async fn create_project_canister(ctx: &Context, args: &CreateArgs) -> Result<(), anyhow::Error> {
let selections = args.cmd_args.selections();
let canister = match selections
.canister
.expect("Canister must be Some() when --detached is not used")
{
CanisterSelection::Named(name) => name,
CanisterSelection::Principal(_) => Err(anyhow!("Cannot create a canister by principal"))?,
};
let env = ctx.get_environment(&selections.environment).await?;
let (_, canister_info) = env.get_canister_info(&canister).map_err(|e| anyhow!(e))?;
if ctx
.get_canister_id_for_env(
&icp::context::CanisterSelection::Named(canister.clone()),
&selections.environment,
)
.await
.is_ok()
{
let _ = ctx
.term
.write_line(&format!("Canister {canister} already exists"));
return Ok(());
}
let agent = ctx
.get_agent_for_env(&selections.identity, &selections.environment)
.await?;
let existing_canisters = ctx
.ids_by_environment(&selections.environment)
.await
.map_err(|e| anyhow!(e))?
.into_values()
.collect();
let create_operation =
CreateOperation::new(agent, args.subnet, args.cycles.get(), existing_canisters);
let canister_settings = args.canister_settings_with_default(&canister_info);
let id = create_operation.create(&canister_settings).await?;
ctx.set_canister_id_for_env(&canister, id, &selections.environment)
.await?;
ctx.update_custom_domains(&selections.environment).await;
if args.quiet {
let _ = ctx.term.write_line(&format!("{id}"));
} else {
let _ = ctx
.term
.write_line(&format!("Created canister {canister} with ID {id}"));
}
Ok(())
}