1use crate::dfx;
2use crate::release_set::{
3 configured_fleet_name, configured_install_targets, dfx_call, dfx_root,
4 emit_root_release_set_manifest_with_config, load_root_release_set_manifest,
5 resume_root_bootstrap, stage_root_release_set, workspace_root,
6};
7use canic_core::{cdk::types::Principal, protocol};
8use config_selection::resolve_install_config_path;
9use serde::Deserialize;
10use serde_json::Value;
11use std::{
12 env,
13 path::Path,
14 process::Command,
15 thread,
16 time::{Duration, Instant, SystemTime, UNIX_EPOCH},
17};
18
19mod config_selection;
20mod state;
21
22pub use state::{
23 FleetSummary, InstallState, list_current_fleets, read_current_install_state,
24 read_current_or_fleet_install_state, select_current_fleet,
25};
26use state::{INSTALL_STATE_SCHEMA_VERSION, validate_fleet_name, write_install_state};
27
28#[cfg(test)]
29mod tests;
30
31#[cfg(test)]
32use config_selection::{config_selection_error, discover_canic_config_choices};
33#[cfg(test)]
34use state::{
35 current_fleet_path, fleet_install_state_path, list_fleets, read_fleet_install_state,
36 read_install_state,
37};
38
39#[derive(Clone, Debug)]
44pub struct InstallRootOptions {
45 pub root_canister: String,
46 pub root_build_target: String,
47 pub network: String,
48 pub ready_timeout_seconds: u64,
49 pub config_path: Option<String>,
50 pub interactive_config_selection: bool,
51}
52
53#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
58struct BootstrapStatusSnapshot {
59 ready: bool,
60 phase: String,
61 last_error: Option<String>,
62}
63
64#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
69struct InstallTimingSummary {
70 create_canisters: Duration,
71 build_all: Duration,
72 emit_manifest: Duration,
73 fabricate_cycles: Duration,
74 install_root: Duration,
75 stage_release_set: Duration,
76 resume_bootstrap: Duration,
77 wait_ready: Duration,
78}
79
80const LOCAL_ROOT_TARGET_CYCLES: u128 = 9_000_000_000_000_000;
81const LOCAL_DFX_READY_TIMEOUT_SECONDS: u64 = 30;
82
83pub fn install_root(options: InstallRootOptions) -> Result<(), Box<dyn std::error::Error>> {
85 let workspace_root = workspace_root()?;
86 let dfx_root = dfx_root()?;
87 let config_path = resolve_install_config_path(
88 &workspace_root,
89 options.config_path.as_deref(),
90 options.interactive_config_selection,
91 )?;
92 let fleet_name = configured_fleet_name(&config_path)?;
93 validate_fleet_name(&fleet_name)?;
94 let total_started_at = Instant::now();
95 let mut timings = InstallTimingSummary::default();
96
97 println!(
98 "Installing fleet {} against DFX_NETWORK={}",
99 fleet_name, options.network
100 );
101 ensure_dfx_running(&dfx_root, &options.network)?;
102 let mut create = Command::new("dfx");
103 create
104 .current_dir(&dfx_root)
105 .args(["canister", "create", "--all", "-qq"]);
106 let create_started_at = Instant::now();
107 run_command(&mut create)?;
108 timings.create_canisters = create_started_at.elapsed();
109
110 let build_targets = configured_install_targets(&config_path, &options.root_build_target)?;
111 let build_session_id = install_build_session_id();
112 let build_started_at = Instant::now();
113 run_dfx_build_targets(&dfx_root, &build_targets, &build_session_id, &config_path)?;
114 timings.build_all = build_started_at.elapsed();
115
116 let emit_manifest_started_at = Instant::now();
117 let manifest_path = emit_root_release_set_manifest_with_config(
118 &workspace_root,
119 &dfx_root,
120 &options.network,
121 &config_path,
122 )?;
123 timings.emit_manifest = emit_manifest_started_at.elapsed();
124
125 timings.fabricate_cycles =
126 maybe_fabricate_local_cycles(&dfx_root, &options.root_canister, &options.network)?;
127
128 let mut install = Command::new("dfx");
129 install.current_dir(&dfx_root).args([
130 "canister",
131 "install",
132 &options.root_canister,
133 "--mode=reinstall",
134 "-y",
135 "--argument",
136 "(variant { Prime })",
137 ]);
138 let install_started_at = Instant::now();
139 run_command(&mut install)?;
140 timings.install_root = install_started_at.elapsed();
141
142 let manifest = load_root_release_set_manifest(&manifest_path)?;
143 let stage_started_at = Instant::now();
144 stage_root_release_set(&dfx_root, &options.root_canister, &manifest)?;
145 timings.stage_release_set = stage_started_at.elapsed();
146 let resume_started_at = Instant::now();
147 resume_root_bootstrap(&options.root_canister)?;
148 timings.resume_bootstrap = resume_started_at.elapsed();
149 let ready_started_at = Instant::now();
150 let ready_result = wait_for_root_ready(&options.root_canister, options.ready_timeout_seconds);
151 timings.wait_ready = ready_started_at.elapsed();
152 if let Err(err) = ready_result {
153 print_install_timing_summary(&timings, total_started_at.elapsed());
154 return Err(err);
155 }
156
157 print_install_timing_summary(&timings, total_started_at.elapsed());
158 let state = build_install_state(
159 &options,
160 &workspace_root,
161 &dfx_root,
162 &config_path,
163 &manifest_path,
164 &fleet_name,
165 )?;
166 let state_path = write_install_state(&dfx_root, &options.network, &state)?;
167 print_install_result_summary(&options.network, &state.fleet, &state_path);
168 Ok(())
169}
170
171fn build_install_state(
173 options: &InstallRootOptions,
174 workspace_root: &Path,
175 dfx_root: &Path,
176 config_path: &Path,
177 release_set_manifest_path: &Path,
178 fleet_name: &str,
179) -> Result<InstallState, Box<dyn std::error::Error>> {
180 Ok(InstallState {
181 schema_version: INSTALL_STATE_SCHEMA_VERSION,
182 fleet: fleet_name.to_string(),
183 installed_at_unix_secs: current_unix_secs()?,
184 network: options.network.clone(),
185 root_target: options.root_canister.clone(),
186 root_canister_id: resolve_root_canister_id(dfx_root, &options.root_canister)?,
187 root_build_target: options.root_build_target.clone(),
188 workspace_root: workspace_root.display().to_string(),
189 dfx_root: dfx_root.display().to_string(),
190 config_path: config_path.display().to_string(),
191 release_set_manifest_path: release_set_manifest_path.display().to_string(),
192 })
193}
194
195fn resolve_root_canister_id(
197 dfx_root: &Path,
198 root_canister: &str,
199) -> Result<String, Box<dyn std::error::Error>> {
200 if Principal::from_text(root_canister).is_ok() {
201 return Ok(root_canister.to_string());
202 }
203
204 let mut command = Command::new("dfx");
205 command
206 .current_dir(dfx_root)
207 .args(["canister", "id", root_canister]);
208 Ok(run_command_stdout(&mut command)?.trim().to_string())
209}
210
211fn current_unix_secs() -> Result<u64, Box<dyn std::error::Error>> {
213 Ok(SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs())
214}
215
216fn run_dfx_build_targets(
218 dfx_root: &Path,
219 targets: &[String],
220 build_session_id: &str,
221 config_path: &Path,
222) -> Result<(), Box<dyn std::error::Error>> {
223 println!("Build artifacts:");
224 println!("{:<16} {:<18} {:>10}", "CANISTER", "PROGRESS", "ELAPSED");
225
226 for (index, target) in targets.iter().enumerate() {
227 let mut command = dfx_build_target_command(dfx_root, target, build_session_id);
228 command.env("CANIC_CONFIG_PATH", config_path);
229 let started_at = Instant::now();
230 let output = command.output()?;
231 let elapsed = started_at.elapsed();
232
233 if !output.status.success() {
234 return Err(format!(
235 "dfx build failed for {target}: {}\nstdout:\n{}\nstderr:\n{}",
236 output.status,
237 String::from_utf8_lossy(&output.stdout).trim(),
238 String::from_utf8_lossy(&output.stderr).trim()
239 )
240 .into());
241 }
242
243 println!(
244 "{:<16} {:<18} {:>9.2}s",
245 target,
246 progress_bar(index + 1, targets.len(), 10),
247 elapsed.as_secs_f64()
248 );
249 }
250
251 println!();
252 Ok(())
253}
254
255fn dfx_build_target_command(dfx_root: &Path, target: &str, build_session_id: &str) -> Command {
258 let mut command = Command::new("dfx");
259 command
260 .current_dir(dfx_root)
261 .env("CANIC_BUILD_CONTEXT_SESSION", build_session_id)
262 .args(["build", "-qq", target]);
263 command
264}
265
266fn install_build_session_id() -> String {
267 let unique = SystemTime::now()
268 .duration_since(UNIX_EPOCH)
269 .map_or(0, |duration| duration.as_nanos());
270 format!("install-root-{}-{unique}", std::process::id())
271}
272
273fn maybe_fabricate_local_cycles(
275 dfx_root: &Path,
276 root_canister: &str,
277 network: &str,
278) -> Result<Duration, Box<dyn std::error::Error>> {
279 if network != "local" {
280 return Ok(Duration::ZERO);
281 }
282
283 let current_balance = root_cycle_balance(dfx_root, root_canister)?;
284 let Some(fabricate_cycles) = required_local_cycle_topup(current_balance) else {
285 println!(
286 "Skipping local cycle fabrication for {root_canister}; balance {} already meets target {}",
287 format_cycles(current_balance),
288 format_cycles(LOCAL_ROOT_TARGET_CYCLES)
289 );
290 return Ok(Duration::ZERO);
291 };
292
293 println!(
294 "Fabricating {} cycles locally for {root_canister} to reach target {} (current balance {})",
295 format_cycles(fabricate_cycles),
296 format_cycles(LOCAL_ROOT_TARGET_CYCLES),
297 format_cycles(current_balance)
298 );
299
300 let mut fabricate = Command::new("dfx");
301 fabricate.current_dir(dfx_root);
302 fabricate.args([
303 "ledger",
304 "fabricate-cycles",
305 "--canister",
306 root_canister,
307 "--cycles",
308 &fabricate_cycles.to_string(),
309 ]);
310 let fabricate_started_at = Instant::now();
311 let _ = run_command_allow_failure(&mut fabricate)?;
312
313 Ok(fabricate_started_at.elapsed())
314}
315
316fn root_cycle_balance(
318 dfx_root: &Path,
319 root_canister: &str,
320) -> Result<u128, Box<dyn std::error::Error>> {
321 let mut command = Command::new("dfx");
322 command
323 .current_dir(dfx_root)
324 .args(["canister", "status", root_canister]);
325 let stdout = dfx::run_output(&mut command)?;
326 parse_canister_status_cycles(&stdout)
327 .ok_or_else(|| "could not parse cycle balance from `dfx canister status` output".into())
328}
329
330fn parse_canister_status_cycles(status_output: &str) -> Option<u128> {
332 status_output
333 .lines()
334 .find_map(parse_canister_status_balance_line)
335}
336
337fn parse_canister_status_balance_line(line: &str) -> Option<u128> {
338 let (label, value) = line.trim().split_once(':')?;
339 let label = label.trim().to_ascii_lowercase();
340 if label != "balance" && label != "cycle balance" {
341 return None;
342 }
343
344 let digits = value
345 .chars()
346 .filter(char::is_ascii_digit)
347 .collect::<String>();
348 if digits.is_empty() {
349 return None;
350 }
351
352 digits.parse::<u128>().ok()
353}
354
355fn required_local_cycle_topup(current_balance: u128) -> Option<u128> {
357 (current_balance < LOCAL_ROOT_TARGET_CYCLES)
358 .then_some(LOCAL_ROOT_TARGET_CYCLES.saturating_sub(current_balance))
359 .filter(|cycles| *cycles > 0)
360}
361
362fn format_cycles(value: u128) -> String {
363 let digits = value.to_string();
364 let mut out = String::with_capacity(digits.len() + (digits.len().saturating_sub(1) / 3));
365 for (index, ch) in digits.chars().enumerate() {
366 if index > 0 && (digits.len() - index).is_multiple_of(3) {
367 out.push('_');
368 }
369 out.push(ch);
370 }
371 out
372}
373
374fn progress_bar(current: usize, total: usize, width: usize) -> String {
375 if total == 0 || width == 0 {
376 return "[] 0/0".to_string();
377 }
378
379 let filled = current.saturating_mul(width).div_ceil(total);
380 let filled = filled.min(width);
381 format!(
382 "[{}{}] {current}/{total}",
383 "#".repeat(filled),
384 " ".repeat(width - filled)
385 )
386}
387
388fn ensure_dfx_running(dfx_root: &Path, network: &str) -> Result<(), Box<dyn std::error::Error>> {
390 if dfx_ping(network)? {
391 return Ok(());
392 }
393
394 if network == "local" && local_dfx_autostart_enabled() {
395 println!("Local dfx replica is not reachable; starting a clean local replica");
396 let mut stop = dfx_stop_command(dfx_root);
397 let _ = run_command_allow_failure(&mut stop)?;
398
399 let mut start = dfx_start_local_command(dfx_root);
400 run_command(&mut start)?;
401 wait_for_dfx_ping(
402 network,
403 Duration::from_secs(LOCAL_DFX_READY_TIMEOUT_SECONDS),
404 )?;
405 return Ok(());
406 }
407
408 Err(format!(
409 "dfx replica is not running for network '{network}'\nStart the target replica externally and rerun."
410 )
411 .into())
412}
413
414fn dfx_ping(network: &str) -> Result<bool, Box<dyn std::error::Error>> {
416 Ok(Command::new("dfx")
417 .args(["ping", network])
418 .output()?
419 .status
420 .success())
421}
422
423fn local_dfx_autostart_enabled() -> bool {
425 parse_local_dfx_autostart(env::var("CANIC_AUTO_START_LOCAL_DFX").ok().as_deref())
426}
427
428fn parse_local_dfx_autostart(value: Option<&str>) -> bool {
429 !matches!(
430 value.map(str::trim).map(str::to_ascii_lowercase).as_deref(),
431 Some("0" | "false" | "no" | "off")
432 )
433}
434
435fn dfx_stop_command(dfx_root: &Path) -> Command {
437 let mut command = Command::new("dfx");
438 command.current_dir(dfx_root).arg("stop");
439 command
440}
441
442fn dfx_start_local_command(dfx_root: &Path) -> Command {
444 let mut command = Command::new("dfx");
445 command
446 .current_dir(dfx_root)
447 .args(["start", "--background", "--clean", "--system-canisters"]);
448 command
449}
450
451fn wait_for_dfx_ping(network: &str, timeout: Duration) -> Result<(), Box<dyn std::error::Error>> {
453 let start = Instant::now();
454 while start.elapsed() < timeout {
455 if dfx_ping(network)? {
456 return Ok(());
457 }
458 thread::sleep(Duration::from_millis(500));
459 }
460
461 Err(format!(
462 "dfx replica did not become ready for network '{network}' within {}s",
463 timeout.as_secs()
464 )
465 .into())
466}
467
468fn wait_for_root_ready(
470 root_canister: &str,
471 timeout_seconds: u64,
472) -> Result<(), Box<dyn std::error::Error>> {
473 let start = std::time::Instant::now();
474 let mut next_report = 0_u64;
475
476 println!("Waiting for {root_canister} to report canic_ready (timeout {timeout_seconds}s)");
477
478 loop {
479 if root_ready(root_canister)? {
480 println!(
481 "{root_canister} reported canic_ready after {}s",
482 start.elapsed().as_secs()
483 );
484 return Ok(());
485 }
486
487 if let Some(status) = root_bootstrap_status(root_canister)?
488 && let Some(last_error) = status.last_error.as_deref()
489 {
490 eprintln!(
491 "root bootstrap reported failure during phase '{}' : {}",
492 status.phase, last_error
493 );
494 eprintln!("Diagnostic: dfx canister call {root_canister} canic_bootstrap_status");
495 print_raw_call(root_canister, protocol::CANIC_BOOTSTRAP_STATUS);
496 eprintln!("Diagnostic: dfx canister call {root_canister} canic_subnet_registry");
497 print_raw_call(root_canister, "canic_subnet_registry");
498 eprintln!(
499 "Diagnostic: dfx canister call {root_canister} canic_wasm_store_bootstrap_debug"
500 );
501 print_raw_call(root_canister, "canic_wasm_store_bootstrap_debug");
502 eprintln!("Diagnostic: dfx canister call {root_canister} canic_wasm_store_overview");
503 print_raw_call(root_canister, "canic_wasm_store_overview");
504 eprintln!("Diagnostic: dfx canister call {root_canister} canic_log");
505 print_recent_root_logs(root_canister);
506 return Err(format!(
507 "root bootstrap failed during phase '{}' : {}",
508 status.phase, last_error
509 )
510 .into());
511 }
512
513 let elapsed = start.elapsed().as_secs();
514 if elapsed >= timeout_seconds {
515 eprintln!("root did not report canic_ready within {timeout_seconds}s");
516 eprintln!("Diagnostic: dfx canister call {root_canister} canic_bootstrap_status");
517 print_raw_call(root_canister, protocol::CANIC_BOOTSTRAP_STATUS);
518 eprintln!("Diagnostic: dfx canister call {root_canister} canic_subnet_registry");
519 print_raw_call(root_canister, "canic_subnet_registry");
520 eprintln!(
521 "Diagnostic: dfx canister call {root_canister} canic_wasm_store_bootstrap_debug"
522 );
523 print_raw_call(root_canister, "canic_wasm_store_bootstrap_debug");
524 eprintln!("Diagnostic: dfx canister call {root_canister} canic_wasm_store_overview");
525 print_raw_call(root_canister, "canic_wasm_store_overview");
526 eprintln!("Diagnostic: dfx canister call {root_canister} canic_log");
527 print_recent_root_logs(root_canister);
528 return Err("root did not become ready".into());
529 }
530
531 if elapsed >= next_report {
532 println!("Still waiting for {root_canister} canic_ready ({elapsed}s elapsed)");
533 if let Some(status) = root_bootstrap_status(root_canister)? {
534 match status.last_error.as_deref() {
535 Some(last_error) => println!(
536 "Current bootstrap status: phase={} ready={} error={}",
537 status.phase, status.ready, last_error
538 ),
539 None => println!(
540 "Current bootstrap status: phase={} ready={}",
541 status.phase, status.ready
542 ),
543 }
544 }
545 if let Ok(registry_json) =
546 dfx_call(root_canister, "canic_subnet_registry", None, Some("json"))
547 {
548 println!("Current subnet registry roles:");
549 println!(" {}", registry_roles(®istry_json));
550 }
551 next_report = elapsed + 5;
552 }
553
554 thread::sleep(Duration::from_secs(1));
555 }
556}
557
558fn root_ready(root_canister: &str) -> Result<bool, Box<dyn std::error::Error>> {
560 let output = dfx_call(root_canister, "canic_ready", None, Some("json"))?;
561 let data = serde_json::from_str::<Value>(&output)?;
562 Ok(parse_root_ready_value(&data))
563}
564
565fn root_bootstrap_status(
567 root_canister: &str,
568) -> Result<Option<BootstrapStatusSnapshot>, Box<dyn std::error::Error>> {
569 let output = match dfx_call(
570 root_canister,
571 protocol::CANIC_BOOTSTRAP_STATUS,
572 None,
573 Some("json"),
574 ) {
575 Ok(output) => output,
576 Err(err) => {
577 let message = err.to_string();
578 if message.contains("has no query method")
579 || message.contains("method not found")
580 || message.contains("Canister has no query method")
581 {
582 return Ok(None);
583 }
584 return Err(err);
585 }
586 };
587 let data = serde_json::from_str::<Value>(&output)?;
588 Ok(parse_bootstrap_status_value(&data))
589}
590
591fn parse_root_ready_value(data: &Value) -> bool {
593 matches!(data, Value::Bool(true)) || matches!(data.get("Ok"), Some(Value::Bool(true)))
594}
595
596fn parse_bootstrap_status_value(data: &Value) -> Option<BootstrapStatusSnapshot> {
597 serde_json::from_value::<BootstrapStatusSnapshot>(data.clone())
598 .ok()
599 .or_else(|| {
600 data.get("Ok")
601 .cloned()
602 .and_then(|ok| serde_json::from_value::<BootstrapStatusSnapshot>(ok).ok())
603 })
604}
605
606fn print_install_timing_summary(timings: &InstallTimingSummary, total: Duration) {
607 println!("Install timing summary:");
608 println!("{:<20} {:>10}", "phase", "elapsed");
609 println!("{:<20} {:>10}", "--------------------", "----------");
610 print_timing_row("create_canisters", timings.create_canisters);
611 print_timing_row("build_all", timings.build_all);
612 print_timing_row("emit_manifest", timings.emit_manifest);
613 print_timing_row("fabricate_cycles", timings.fabricate_cycles);
614 print_timing_row("install_root", timings.install_root);
615 print_timing_row("stage_release_set", timings.stage_release_set);
616 print_timing_row("resume_bootstrap", timings.resume_bootstrap);
617 print_timing_row("wait_ready", timings.wait_ready);
618 print_timing_row("total", total);
619}
620
621fn print_timing_row(label: &str, duration: Duration) {
622 println!("{label:<20} {:>9.2}s", duration.as_secs_f64());
623}
624
625fn print_install_result_summary(network: &str, fleet: &str, state_path: &Path) {
627 println!("Install result:");
628 println!("{:<14} success", "status");
629 println!("{:<14} {}", "fleet", fleet);
630 println!("{:<14} {}", "install_state", state_path.display());
631 println!("{:<14} canic list --network {}", "smoke_check", network);
632}
633
634fn print_recent_root_logs(root_canister: &str) {
636 let page_args = r"(null, null, null, record { limit = 8; offset = 0 })";
637 let Ok(logs_json) = dfx_call(root_canister, "canic_log", Some(page_args), Some("json")) else {
638 return;
639 };
640 let Ok(data) = serde_json::from_str::<Value>(&logs_json) else {
641 return;
642 };
643 let entries = data
644 .get("Ok")
645 .and_then(|ok| ok.get("entries"))
646 .and_then(Value::as_array)
647 .cloned()
648 .unwrap_or_default();
649
650 if entries.is_empty() {
651 println!(" <no runtime log entries>");
652 return;
653 }
654
655 for entry in entries.iter().rev() {
656 let level = entry.get("level").and_then(Value::as_str).unwrap_or("Info");
657 let topic = entry.get("topic").and_then(Value::as_str).unwrap_or("");
658 let message = entry
659 .get("message")
660 .and_then(Value::as_str)
661 .unwrap_or("")
662 .replace('\n', "\\n");
663 let topic_prefix = if topic.is_empty() {
664 String::new()
665 } else {
666 format!("[{topic}] ")
667 };
668 println!(" {level} {topic_prefix}{message}");
669 }
670}
671
672fn registry_roles(registry_json: &str) -> String {
674 serde_json::from_str::<Value>(registry_json)
675 .ok()
676 .and_then(|data| {
677 data.get("Ok").and_then(Value::as_array).map(|entries| {
678 entries
679 .iter()
680 .filter_map(|entry| {
681 entry
682 .get("role")
683 .and_then(Value::as_str)
684 .map(str::to_string)
685 })
686 .collect::<Vec<_>>()
687 })
688 })
689 .map_or_else(
690 || "<unavailable>".to_string(),
691 |roles| {
692 if roles.is_empty() {
693 "<empty>".to_string()
694 } else {
695 roles.join(", ")
696 }
697 },
698 )
699}
700
701fn run_command(command: &mut Command) -> Result<(), Box<dyn std::error::Error>> {
703 dfx::run_status(command).map_err(Into::into)
704}
705
706fn run_command_stdout(command: &mut Command) -> Result<String, Box<dyn std::error::Error>> {
708 dfx::run_output(command).map_err(Into::into)
709}
710
711fn run_command_allow_failure(
713 command: &mut Command,
714) -> Result<std::process::ExitStatus, Box<dyn std::error::Error>> {
715 Ok(command.status()?)
716}
717
718fn print_raw_call(root_canister: &str, method: &str) {
720 let mut command = Command::new("dfx");
721 if let Ok(root) = dfx_root() {
722 command.current_dir(root);
723 }
724 let _ = command
725 .args(["canister", "call", root_canister, method])
726 .status();
727}