1use crate::{
2 Error,
3 cdk::api::canister_self,
4 interface::ic::get_current_subnet_pid,
5 log::Topic,
6 ops::{
7 config::ConfigOps,
8 model::memory::{EnvOps, topology::SubnetCanisterRegistryOps},
9 prelude::*,
10 request::{CreateCanisterParent, create_canister_request},
11 },
12};
13
14pub async fn root_set_subnet_id() {
15 if let Ok(Some(subnet_pid)) = get_current_subnet_pid().await {
17 EnvOps::set_subnet_pid(subnet_pid);
18 return;
19 }
20
21 let fallback = canister_self();
23 EnvOps::set_subnet_pid(fallback);
24 log!(
25 Topic::Topology,
26 Warn,
27 "get_current_subnet_pid unavailable; using self as subnet: {fallback}"
28 );
29}
30
31pub async fn root_create_canisters() -> Result<(), Error> {
33 let subnet_cfg = ConfigOps::current_subnet()?;
34
35 for ty in &subnet_cfg.auto_create {
37 create_canister_request::<()>(ty, CreateCanisterParent::Root, None).await?;
38 }
39
40 for canister in SubnetCanisterRegistryOps::export() {
42 log!(Topic::Init, Info, "🥫 {} ({})", canister.ty, canister.pid);
43 }
44
45 Ok(())
46}