-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcreate.rs
More file actions
406 lines (328 loc) · 12.8 KB
/
create.rs
File metadata and controls
406 lines (328 loc) · 12.8 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use std::collections::HashMap;
use anyhow::anyhow;
use candid::{Decode, Encode, Nat};
use clap::Args;
use futures::{StreamExt, stream::FuturesOrdered};
use ic_agent::{Agent, AgentError, export::Principal};
use icp::{
agent,
context::{GetAgentForEnvError, GetEnvironmentError},
identity, network,
prelude::*,
};
use icp_canister_interfaces::{
cycles_ledger::{
CYCLES_LEDGER_PRINCIPAL, CanisterSettingsArg, CreateCanisterArgs, CreateCanisterResponse,
CreationArgs, SubnetSelectionArg,
},
cycles_minting_canister::CYCLES_MINTING_CANISTER_PRINCIPAL,
registry::{GetSubnetForCanisterRequest, GetSubnetForCanisterResult, REGISTRY_PRINCIPAL},
};
use rand::seq::IndexedRandom;
use icp::context::Context;
use crate::{
options::{EnvironmentOpt, IdentityOpt},
progress::{ProgressManager, ProgressManagerSettings},
};
use icp::store_id::{Key, LookupIdError, RegisterError};
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.
#[arg(long)]
pub(crate) memory_allocation: Option<u64>,
/// Optional freezing threshold in seconds. Controls how long a canister can be inactive before being frozen.
#[arg(long)]
pub(crate) freezing_threshold: Option<u64>,
/// Optional reserved cycles limit. If set, the canister cannot consume more than this many cycles.
#[arg(long)]
pub(crate) reserved_cycles_limit: Option<u64>,
}
#[derive(Clone, Debug, Args)]
pub(crate) struct CreateArgs {
/// The names of the canister within the current project
pub(crate) names: Vec<String>,
#[command(flatten)]
pub(crate) identity: IdentityOpt,
#[command(flatten)]
pub(crate) environment: EnvironmentOpt,
/// 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 (in raw cycles).
#[arg(long, default_value_t = DEFAULT_CANISTER_CYCLES)]
pub(crate) cycles: u128,
/// The subnet to create canisters on.
#[arg(long)]
pub(crate) subnet: Option<Principal>,
}
#[derive(Debug, thiserror::Error)]
pub(crate) enum CommandError {
#[error(transparent)]
Project(#[from] icp::LoadError),
#[error(transparent)]
Identity(#[from] identity::LoadError),
#[error(transparent)]
Access(#[from] network::AccessError),
#[error(transparent)]
Agent(#[from] agent::CreateError),
#[error("project does not contain a canister named '{name}'")]
CanisterNotFound { name: String },
#[error("environment '{environment}' does not include canister '{canister}'")]
EnvironmentCanister {
environment: String,
canister: String,
},
#[error("canister exists already: {principal}")]
CanisterExists { principal: Principal },
#[error(transparent)]
CreateCanister(#[from] AgentError),
#[error(transparent)]
RegisterCanister(#[from] RegisterError),
#[error(transparent)]
Candid(#[from] candid::Error),
#[error("{err}")]
LedgerCreate { err: String },
#[error("Failed to get subnet for canister: {err}")]
GetSubnet { err: String },
#[error(transparent)]
Unexpected(#[from] anyhow::Error),
#[error(transparent)]
GetAgentForEnv(#[from] GetAgentForEnvError),
#[error(transparent)]
GetEnvironment(#[from] GetEnvironmentError),
}
// 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<(), CommandError> {
// Load project
let p = ctx.project.load().await?;
// Load target environment
let env = ctx.get_environment(args.environment.name()).await?;
let target_canisters = match args.names.is_empty() {
true => env.get_canister_names(),
false => args.names.clone(),
};
for name in &target_canisters {
if !p.canisters.contains_key(name) {
return Err(CommandError::CanisterNotFound {
name: name.to_owned(),
});
}
if !env.canisters.contains_key(name) {
return Err(CommandError::EnvironmentCanister {
environment: env.name.to_owned(),
canister: name.to_owned(),
});
}
}
let canister_infos = env
.canisters
.iter()
.filter(|(k, _)| target_canisters.contains(k))
.collect::<HashMap<_, _>>();
// Do we have any already existing canisters?
let cexist: Vec<_> = env
.canisters
.values()
.filter_map(|(_, c)| {
ctx.ids
.lookup(&Key {
environment: env.name.to_owned(),
canister: c.name.to_owned(),
})
.ok()
})
.collect();
// Agent
let agent = ctx
.get_agent_for_env(&args.identity.clone().into(), args.environment.name())
.await?;
// Select which subnet to deploy the canisters to
//
// If we don't specify a subnet, then the CMC will choose a random subnet
// for each canister. Ideally, a project's canister should all live on the same subnet.
let subnet = match args.subnet {
// Target specified subnet
Some(v) => v,
// No subnet specified, and no canisters exist
// Target a random subnet
None if cexist.is_empty() => {
let vs = get_available_subnets(&agent).await?;
// Choose a random subnet
vs.choose(&mut rand::rng())
.expect("missing subnet id")
.to_owned()
}
// No subnet specified, and some canisters exist
// Target the same subnet as the first canister
None => {
get_canister_subnet(
&agent, // agent
cexist.first().expect("missing canister id"), // id
)
.await?
}
};
// Prepare a futures set for concurrent operations
let mut futs = FuturesOrdered::new();
let progress_manager = ProgressManager::new(ProgressManagerSettings { hidden: ctx.debug });
let env_ref = &env;
for (name, (_path, info)) in canister_infos.iter() {
// Create progress bar with standard configuration
let pb = progress_manager.create_progress_bar(name);
// Create an async closure that handles the operation for this specific canister
let create_fn = {
let cmd = args.clone();
let agent = agent.clone();
let pb = pb.clone();
async move {
// Indicate to user that the canister is created
pb.set_message("Creating...");
// Create canister-environment association-key
let k = Key {
environment: env_ref.name.to_owned(),
canister: name.to_string(),
};
match ctx.ids.lookup(&k) {
// Exists (skip)
Ok(principal) => {
return Err(CommandError::CanisterExists { principal });
}
// Doesn't exist (include)
Err(LookupIdError::IdNotFound { .. }) => {}
// Lookup failed
Err(err) => panic!("{err}"),
};
// Build cycles ledger create_canister args
let settings = CanisterSettingsArg {
freezing_threshold: cmd
.settings
.freezing_threshold
.or(info.settings.freezing_threshold)
.map(Nat::from),
controllers: if cmd.controller.is_empty() {
None
} else {
Some(cmd.controller.clone())
},
reserved_cycles_limit: cmd
.settings
.reserved_cycles_limit
.or(info.settings.reserved_cycles_limit)
.map(Nat::from),
memory_allocation: cmd
.settings
.memory_allocation
.or(info.settings.memory_allocation)
.map(Nat::from),
compute_allocation: cmd
.settings
.compute_allocation
.or(info.settings.compute_allocation)
.map(Nat::from),
};
let creation_args = CreationArgs {
subnet_selection: Some(SubnetSelectionArg::Subnet { subnet }),
settings: Some(settings),
};
let arg = CreateCanisterArgs {
from_subaccount: None,
created_at_time: None,
amount: Nat::from(cmd.cycles),
creation_args: Some(creation_args),
};
// Call cycles ledger create_canister
let resp = agent
.update(&CYCLES_LEDGER_PRINCIPAL, "create_canister")
.with_arg(Encode!(&arg)?)
.call_and_wait()
.await?;
let resp: CreateCanisterResponse = Decode!(&resp, CreateCanisterResponse)?;
let cid = match resp {
CreateCanisterResponse::Ok { canister_id, .. } => canister_id,
CreateCanisterResponse::Err(err) => {
return Err(CommandError::LedgerCreate {
err: err.format_error(cmd.cycles),
});
}
};
// Register the canister ID
ctx.ids.register(&k, &cid)?;
Ok::<_, CommandError>(())
}
};
futs.push_back(async move {
// Execute the create function with custom progress tracking
let mut result = ProgressManager::execute_with_custom_progress(
&pb,
create_fn,
|| "Created successfully".to_string(),
|err| match err {
CommandError::CanisterExists { principal } => {
format!("Canister already created: {principal}")
}
_ => format!("Failed to create canister: {err}"),
},
|err| matches!(err, CommandError::CanisterExists { .. }),
)
.await;
// If canister already exists, it is not considered an error
if let Err(CommandError::CanisterExists { .. }) = result {
result = Ok(());
}
result
});
}
// Consume the set of futures and abort if an error occurs
while let Some(res) = futs.next().await {
// TODO(or.ricon): Handle canister creation failures
res?;
}
Ok(())
}
async fn get_canister_subnet(agent: &Agent, id: &Principal) -> Result<Principal, anyhow::Error> {
let args = &GetSubnetForCanisterRequest {
principal: Some(*id),
};
let bs = agent
.query(®ISTRY_PRINCIPAL, "get_subnet_for_canister")
.with_arg(Encode!(args)?)
.call()
.await
.map_err(|err| CommandError::GetSubnet {
err: err.to_string(),
})?;
let resp = Decode!(&bs, GetSubnetForCanisterResult)?;
let out = resp
.map_err(|err| CommandError::GetSubnet { err })?
.subnet_id
.ok_or(anyhow!("missing subnet id"))?;
Ok(out)
}
async fn get_available_subnets(agent: &Agent) -> Result<Vec<Principal>, CommandError> {
let bs = agent
.query(&CYCLES_MINTING_CANISTER_PRINCIPAL, "get_default_subnets")
.with_arg(Encode!(&())?)
.call()
.await
.map_err(|err| CommandError::GetSubnet {
err: err.to_string(),
})?;
let resp = Decode!(&bs, Vec<Principal>)?;
// Check if any subnets are available
if resp.is_empty() {
return Err(anyhow!("no available subnets found").into());
}
Ok(resp)
}