canic-cli 0.110.49

Operator CLI for Canic fleet setup, builds, evidence, catalog, backup, and restore workflows
Documentation
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
407
408
409
410
411
//! Review supplied infrastructure and publish its receipted physical identity for later import.

#[cfg(test)]
mod tests;

use crate::{
    cli::{
        clap::{
            parse_matches, render_usage, required_string, string_option, string_option_or_else,
            value_arg,
        },
        defaults::default_icp,
        globals::{internal_environment_arg, internal_icp_arg},
        help::print_help_or_version,
    },
    fleet::{
        FleetCommandError, identity_arg, now_nanoseconds, parse_digest, quote_review_argument,
        resolve_from_root,
    },
    version_text,
};
use canic_core::ids::ReleaseBuildId;
use canic_host::{
    durable_io::read_regular_bytes,
    fleet_ensure::{
        FleetEnsureReport, FleetGenerateRequest, IcpEnsurePlatform,
        dto::infrastructure_bootstrap::InfrastructureBootstrapReviewRequest,
        generate_infrastructure_bootstrap,
        model::infrastructure_bootstrap::BootstrapCoordinatorSelection,
        workflow::infrastructure_bootstrap,
    },
    icp::IcpCli,
    icp_config::resolve_current_canic_icp_root,
};
use clap::{ArgAction, ArgMatches, Command};
use std::{ffi::OsString, path::PathBuf};

pub(super) fn command() -> Command {
    let mut command = Command::new("bootstrap").bin_name("canic fleet bootstrap")
        .about("Review or apply explicit supplied infrastructure before Root-local pool import")
        .disable_help_flag(true)
        .arg(value_arg("fleet").required(true).help("Fleet identity"))
        .arg(value_arg("apply").long("apply").value_name("PLAN_SHA256").value_parser(parse_digest)
            .help("Resume exact retained initialization and estate publication"))
        .arg(value_arg("recover").long("recover").value_name("PLAN_SHA256").value_parser(parse_digest)
            .conflicts_with("apply").help("Review registration budget and funding after infrastructure effects complete"))
        .arg(value_arg("approve-recovery").long("approve-recovery").value_name("REVIEW_SHA256").value_parser(parse_digest)
            .requires("recover").help("Approve the exact registration recovery review and resume the retained operation"))
        .arg(value_arg("coordinator").long("coordinator").value_parser(["create", "initialize", "ready"])
            .required_unless_present_any(["apply", "recover"]).conflicts_with_all(["apply", "recover"])
            .help("Explicitly create a Coordinator, wipe a supplied ID, or verify current genesis authority"))
        .arg(value_arg("release-build").long("release-build").value_name("RELEASE_BUILD_ID")
            .value_parser(clap::value_parser!(ReleaseBuildId)).required_unless_present_any(["apply", "recover"]).conflicts_with_all(["apply", "recover"])
            .help("Finalized current release build"))
        .arg(value_arg("source").long("source").value_name("PATH").conflicts_with_all(["apply", "recover"])
            .help("Fleet policy; defaults to deployments/<fleet>.toml"))
        .arg(value_arg("seed").long("seed").value_name("PATH").conflicts_with_all(["apply", "recover"])
            .help("Explicit identity seed; defaults to deployments/<fleet>.estate.toml"))
        .arg(value_arg("json").long("json").num_args(0).action(ArgAction::SetTrue)
            .help("Print the complete reviewed plan and exact next command as JSON"))
        .arg(internal_environment_arg()).arg(internal_icp_arg()).arg(identity_arg());
    for (name, help) in [
        (
            "app-config",
            "Canonical App canic.toml for current typed authority",
        ),
        (
            "declarations",
            "Original supplied-ID custody and destructive disposition TOML",
        ),
    ] {
        command = command.arg(
            value_arg(name)
                .long(name)
                .value_name("PATH")
                .required_unless_present_any(["apply", "recover"])
                .conflicts_with_all(["apply", "recover"])
                .help(help),
        );
    }
    command.after_help("Examples:\n  canic --environment staging fleet bootstrap staging --app-config canic.toml --release-build <sha256> --coordinator initialize --declarations deployments/bootstrap.toml\n  canic --environment staging fleet bootstrap staging --apply <plan-sha256>\n\nReview retains bounded management observations. Apply clears reviewed infrastructure code/state, preserves supplied IDs and publishes the estate seed. Import held capacity through fleet import before generating ordinary workload authority. Seed coordinator = \"create\" is accepted only with explicit --coordinator create.")
}

pub(super) fn run(args: Vec<OsString>) -> Result<(), FleetCommandError> {
    if print_help_or_version(&args, || render_usage(command), version_text()) {
        return Ok(());
    }
    let matches = parse_matches(command(), args)
        .map_err(|error| FleetCommandError::Usage(error.to_string()))?;
    execute(&matches)
}

fn execute(matches: &ArgMatches) -> Result<(), FleetCommandError> {
    let environment = string_option(matches, "environment")
        .ok_or_else(|| FleetCommandError::Usage("fleet bootstrap requires --environment".into()))?;
    let fleet = required_string(matches, "fleet");
    let executable = string_option_or_else(matches, "icp", default_icp);
    let identity = string_option(matches, "identity");
    let workspace = resolve_current_canic_icp_root()?;
    if let Some(digest) = string_option(matches, "recover") {
        return recover(
            matches,
            &workspace,
            &environment,
            &fleet,
            &digest,
            &executable,
            identity.as_deref(),
        );
    }
    let (plan, completed) = if let Some(digest) = string_option(matches, "apply") {
        let plan = infrastructure_bootstrap::retained(&workspace, &environment, &fleet, &digest)
            .map_err(|error| FleetCommandError::InfrastructureBootstrap(Box::new(error)))?;
        let desired = plan
            .reviewed_desired
            .as_ref()
            .expect("verified bootstrap desired")
            .desired()
            .clone();
        let mut platform = IcpEnsurePlatform::new(desired, &executable, &workspace)
            .with_identity(identity.as_deref());
        let published = infrastructure_bootstrap::apply(
            &workspace,
            &environment,
            &fleet,
            &digest,
            &mut platform,
        )?;
        (published.plan, published.completed)
    } else {
        (
            review(
                matches,
                &workspace,
                &environment,
                &fleet,
                &executable,
                identity.as_deref(),
            )?,
            false,
        )
    };
    let mut next = format!(
        "canic --environment {} --icp {} fleet bootstrap {}",
        quote_review_argument(&environment),
        quote_review_argument(&executable),
        quote_review_argument(&fleet)
    );
    if let Some(identity) = &identity {
        next.push_str(" --identity ");
        next.push_str(&quote_review_argument(identity));
    }
    next.push_str(" --apply ");
    next.push_str(&plan.plan_sha256);
    render(plan, completed, matches, &next)
}

fn recover(
    matches: &ArgMatches,
    workspace: &std::path::Path,
    environment: &str,
    fleet: &str,
    digest: &str,
    executable: &str,
    identity: Option<&str>,
) -> Result<(), FleetCommandError> {
    let plan = infrastructure_bootstrap::retained(workspace, environment, fleet, digest)
        .map_err(|error| FleetCommandError::InfrastructureBootstrap(Box::new(error)))?;
    let desired = plan
        .reviewed_desired
        .as_ref()
        .expect("verified bootstrap desired")
        .desired()
        .clone();
    let mut platform =
        IcpEnsurePlatform::new(desired, executable, workspace).with_identity(identity);
    let mut command = format!(
        "canic --environment {} --icp {} fleet bootstrap {} --recover {}",
        quote_review_argument(environment),
        quote_review_argument(executable),
        quote_review_argument(fleet),
        digest
    );
    if let Some(identity) = identity {
        command.push_str(" --identity ");
        command.push_str(&quote_review_argument(identity));
    }
    if let Some(approved) = string_option(matches, "approve-recovery") {
        infrastructure_bootstrap::registration_recovery::approve(
            workspace,
            environment,
            fleet,
            digest,
            &approved,
            &mut platform,
        )?;
        let published =
            infrastructure_bootstrap::apply(workspace, environment, fleet, digest, &mut platform)?;
        command.push_str(" --approve-recovery ");
        command.push_str(&approved);
        return render(published.plan, published.completed, matches, &command);
    }
    let review = infrastructure_bootstrap::registration_recovery::review(
        workspace,
        environment,
        fleet,
        digest,
        now_nanoseconds()?,
        &mut platform,
    )?;
    command.push_str(" --approve-recovery ");
    command.push_str(&review.review_sha256);
    if matches.get_flag("json") {
        let mut args =
            super::automation::command(environment, executable, identity, "bootstrap", fleet);
        args.extend([
            "--recover".into(),
            digest.into(),
            "--approve-recovery".into(),
            review.review_sha256.clone(),
        ]);
        println!(
            "{}",
            serde_json::to_string_pretty(&serde_json::json!({
                "schema_version": 1, "stage": "infrastructure_registration_recovery", "completed": false,
                "registration_recovery": canic_host::fleet_ensure::registration_recovery_json_value(&review)?, "apply_command": command,
                "automation": super::automation::bootstrap(&plan, false, Some(review.review_sha256.clone()), false, args),
            }))?
        );
    } else {
        println!("retained_plan_sha256: {}", review.plan_sha256);
        println!("recovery_review_sha256: {}", review.review_sha256);
        println!("registration_actions: {}", review.protocol_actions.len());
        println!(
            "registration_execution_burn_cycles: {}",
            review.successor_burn_cycles
        );
        println!(
            "recovery_execution_burn_cycles: {}",
            review.recovery_burn_cycles
        );
        println!(
            "maximum_execution_burn_cycles: {}",
            review.maximum_execution_burn_cycles
        );
        println!(
            "additional_funding_cycles: {}",
            review.additional_funding_cycles
        );
        println!(
            "additional_ledger_fee_cycles: {}",
            review.additional_ledger_fee_cycles
        );
        for action in &review.funding_actions {
            println!("funding: {}", serde_json::to_string(action)?);
        }
        println!("apply: {command}");
    }
    Ok(())
}

fn render(
    plan: canic_host::fleet_ensure::model::FleetEnsurePlan,
    completed: bool,
    matches: &ArgMatches,
    next: &str,
) -> Result<(), FleetCommandError> {
    let report = FleetEnsureReport {
        plan,
        terminal: completed,
        effects_applied: 0,
        funding_review: None,
        actual_conservation: None,
    };
    let plan = &report.plan;
    if matches.get_flag("json") {
        let executable = string_option_or_else(matches, "icp", default_icp);
        let identity = string_option(matches, "identity");
        let mut args = super::automation::command(
            &plan.environment,
            &executable,
            identity.as_deref(),
            "bootstrap",
            &plan.fleet,
        );
        let recovery = string_option(matches, "approve-recovery");
        if let Some(review) = &recovery {
            args.extend([
                "--recover".into(),
                plan.plan_sha256.clone(),
                "--approve-recovery".into(),
                review.clone(),
            ]);
        } else {
            args.extend(["--apply".into(), plan.plan_sha256.clone()]);
        }
        println!(
            "{}",
            serde_json::to_string_pretty(&serde_json::json!({
                "schema_version": 1, "stage": "infrastructure_bootstrap", "completed": completed,
                "plan": plan, "apply_command": next,
                "continuation_forecast": canic_host::fleet_ensure::policy::continuation_forecast::forecast(&report),
                "automation": super::automation::bootstrap(plan, completed, recovery.clone(), matches.get_one::<String>("apply").is_some() || recovery.is_some(), args),
            }))?
        );
    } else {
        let source = plan
            .infrastructure_bootstrap
            .as_ref()
            .expect("verified bootstrap source");
        println!("coordinator_selection: {:?}", source.coordinator);
        for (name, original) in &source.sources {
            println!(
                "supplied: {name}; id={}; subnet={}; controllers={:?}; module={:?}; native_cycles={}; reserved_cycles={}",
                original.sample.binding.canister_id,
                original.sample.binding.subnet,
                original.sample.binding.controllers,
                original
                    .sample
                    .binding
                    .module_sha256
                    .map(canic_core::cdk::utils::hash::hex_bytes),
                original.sample.cycles,
                original.sample.reserved_cycles
            );
        }
        for target in &plan.canisters {
            for action in &target.actions {
                println!("effect: {}", serde_json::to_string(action)?);
            }
        }
        super::render_report(&report, false)?;
        println!("apply: {next}");
        if completed {
            println!(
                "Infrastructure setup and estate publication complete. Next: review each Root's held set with fleet import, then fleet generate and fleet ensure."
            );
        }
    }
    Ok(())
}

fn review(
    matches: &ArgMatches,
    workspace: &std::path::Path,
    environment: &str,
    fleet: &str,
    executable: &str,
    identity: Option<&str>,
) -> Result<canic_host::fleet_ensure::model::FleetEnsurePlan, FleetCommandError> {
    let selection = match required_string(matches, "coordinator").as_str() {
        "create" => BootstrapCoordinatorSelection::Create,
        "initialize" => BootstrapCoordinatorSelection::Initialize,
        "ready" => BootstrapCoordinatorSelection::Ready,
        _ => unreachable!("Clap validates Coordinator selection"),
    };
    let source = string_option(matches, "source").map_or_else(
        || PathBuf::from(format!("deployments/{fleet}.toml")),
        PathBuf::from,
    );
    let seed = string_option(matches, "seed").map_or_else(
        || PathBuf::from(format!("deployments/{fleet}.estate.toml")),
        PathBuf::from,
    );
    let desired = generate_infrastructure_bootstrap(
        &FleetGenerateRequest {
            catalog_progress: None,
            app_config: &resolve_from_root(
                workspace,
                &PathBuf::from(required_string(matches, "app-config")),
            ),
            environment,
            fleet,
            icp_executable: executable,
            signing_identity: identity,
            release_build_id: *matches
                .get_one::<ReleaseBuildId>("release-build")
                .expect("required release build"),
            root: workspace,
            seed: &resolve_from_root(workspace, &seed),
            source: &resolve_from_root(workspace, &source),
        },
        selection,
        None,
    )?;
    let declaration_path = resolve_from_root(
        workspace,
        &PathBuf::from(required_string(matches, "declarations")),
    );
    let declarations = String::from_utf8(read_regular_bytes(&declaration_path, 256 * 1024)?)
        .map_err(|_| {
            FleetCommandError::Usage("bootstrap declarations must be UTF-8 TOML".into())
        })?;
    let icp = IcpCli::new(executable, Some(environment.into()))
        .with_identity(identity)
        .with_cwd(workspace);
    let mut platform =
        IcpEnsurePlatform::new(desired.clone(), executable, workspace).with_identity(identity);
    Ok(infrastructure_bootstrap::review(
        &InfrastructureBootstrapReviewRequest {
            workspace,
            desired: &desired,
            coordinator: selection,
            declarations_toml: &declarations,
            seed: &resolve_from_root(workspace, &seed),
            planned_at_time: now_nanoseconds()?,
        },
        &mut platform,
        &icp,
    )?)
}