|
| 1 | +//! `vz build-mgmt` -- build entity lifecycle management commands. |
| 2 | +//! |
| 3 | +//! Provides `list`, `inspect`, and `cancel` subcommands backed by the |
| 4 | +//! `vz-stack` state store for build persistence. |
| 5 | +
|
| 6 | +#![allow(clippy::print_stdout)] |
| 7 | + |
| 8 | +use std::path::PathBuf; |
| 9 | + |
| 10 | +use anyhow::{Context, bail}; |
| 11 | +use clap::{Args, Subcommand}; |
| 12 | +use vz_runtime_contract::BuildState; |
| 13 | +use vz_stack::StateStore; |
| 14 | + |
| 15 | +/// Manage asynchronous build operations. |
| 16 | +#[derive(Args, Debug)] |
| 17 | +pub struct BuildMgmtArgs { |
| 18 | + #[command(subcommand)] |
| 19 | + pub action: BuildMgmtCommand, |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Subcommand, Debug)] |
| 23 | +pub enum BuildMgmtCommand { |
| 24 | + /// List all builds. |
| 25 | + List(BuildMgmtListArgs), |
| 26 | + |
| 27 | + /// Show detailed build information. |
| 28 | + Inspect(BuildMgmtInspectArgs), |
| 29 | + |
| 30 | + /// Cancel a running or queued build. |
| 31 | + Cancel(BuildMgmtCancelArgs), |
| 32 | +} |
| 33 | + |
| 34 | +/// Arguments for `vz build-mgmt list`. |
| 35 | +#[derive(Args, Debug)] |
| 36 | +pub struct BuildMgmtListArgs { |
| 37 | + /// Path to the state database. |
| 38 | + #[arg(long, default_value = "stack-state.db")] |
| 39 | + state_db: PathBuf, |
| 40 | + |
| 41 | + /// Filter by sandbox identifier. |
| 42 | + #[arg(long)] |
| 43 | + sandbox_id: Option<String>, |
| 44 | + |
| 45 | + /// Output as JSON. |
| 46 | + #[arg(long)] |
| 47 | + json: bool, |
| 48 | +} |
| 49 | + |
| 50 | +/// Arguments for `vz build-mgmt inspect`. |
| 51 | +#[derive(Args, Debug)] |
| 52 | +pub struct BuildMgmtInspectArgs { |
| 53 | + /// Build identifier. |
| 54 | + pub build_id: String, |
| 55 | + |
| 56 | + /// Path to the state database. |
| 57 | + #[arg(long, default_value = "stack-state.db")] |
| 58 | + state_db: PathBuf, |
| 59 | +} |
| 60 | + |
| 61 | +/// Arguments for `vz build-mgmt cancel`. |
| 62 | +#[derive(Args, Debug)] |
| 63 | +pub struct BuildMgmtCancelArgs { |
| 64 | + /// Build identifier. |
| 65 | + pub build_id: String, |
| 66 | + |
| 67 | + /// Path to the state database. |
| 68 | + #[arg(long, default_value = "stack-state.db")] |
| 69 | + state_db: PathBuf, |
| 70 | +} |
| 71 | + |
| 72 | +/// Run the build management subcommand. |
| 73 | +pub async fn run(args: BuildMgmtArgs) -> anyhow::Result<()> { |
| 74 | + match args.action { |
| 75 | + BuildMgmtCommand::List(list_args) => cmd_list(list_args), |
| 76 | + BuildMgmtCommand::Inspect(inspect_args) => cmd_inspect(inspect_args), |
| 77 | + BuildMgmtCommand::Cancel(cancel_args) => cmd_cancel(cancel_args), |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +fn cmd_list(args: BuildMgmtListArgs) -> anyhow::Result<()> { |
| 82 | + let store = StateStore::open(&args.state_db).context("failed to open state store")?; |
| 83 | + |
| 84 | + let builds = if let Some(ref sandbox_id) = args.sandbox_id { |
| 85 | + store |
| 86 | + .list_builds_for_sandbox(sandbox_id) |
| 87 | + .context("failed to list builds for sandbox")? |
| 88 | + } else { |
| 89 | + store.list_builds().context("failed to list builds")? |
| 90 | + }; |
| 91 | + |
| 92 | + if args.json { |
| 93 | + let json = serde_json::to_string_pretty(&builds).context("failed to serialize builds")?; |
| 94 | + println!("{json}"); |
| 95 | + return Ok(()); |
| 96 | + } |
| 97 | + |
| 98 | + if builds.is_empty() { |
| 99 | + println!("No builds found."); |
| 100 | + return Ok(()); |
| 101 | + } |
| 102 | + |
| 103 | + println!( |
| 104 | + "{:<40} {:<20} {:<12} {:<20} {:<12}", |
| 105 | + "BUILD ID", "SANDBOX ID", "STATE", "CONTEXT", "DIGEST" |
| 106 | + ); |
| 107 | + for build in &builds { |
| 108 | + let state = serde_json::to_string(&build.state) |
| 109 | + .unwrap_or_default() |
| 110 | + .trim_matches('"') |
| 111 | + .to_string(); |
| 112 | + let context_display = if build.build_spec.context.len() > 18 { |
| 113 | + format!("{}...", &build.build_spec.context[..15]) |
| 114 | + } else { |
| 115 | + build.build_spec.context.clone() |
| 116 | + }; |
| 117 | + let digest = build.result_digest.as_deref().unwrap_or("-"); |
| 118 | + let digest_display = if digest.len() > 10 { |
| 119 | + format!("{}...", &digest[..7]) |
| 120 | + } else { |
| 121 | + digest.to_string() |
| 122 | + }; |
| 123 | + println!( |
| 124 | + "{:<40} {:<20} {:<12} {:<20} {:<12}", |
| 125 | + build.build_id, build.sandbox_id, state, context_display, digest_display |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + Ok(()) |
| 130 | +} |
| 131 | + |
| 132 | +fn cmd_inspect(args: BuildMgmtInspectArgs) -> anyhow::Result<()> { |
| 133 | + let store = StateStore::open(&args.state_db).context("failed to open state store")?; |
| 134 | + let build = store |
| 135 | + .load_build(&args.build_id) |
| 136 | + .context("failed to load build")?; |
| 137 | + |
| 138 | + match build { |
| 139 | + Some(b) => { |
| 140 | + let json = serde_json::to_string_pretty(&b).context("failed to serialize build")?; |
| 141 | + println!("{json}"); |
| 142 | + } |
| 143 | + None => bail!("build {} not found", args.build_id), |
| 144 | + } |
| 145 | + |
| 146 | + Ok(()) |
| 147 | +} |
| 148 | + |
| 149 | +fn cmd_cancel(args: BuildMgmtCancelArgs) -> anyhow::Result<()> { |
| 150 | + let store = StateStore::open(&args.state_db).context("failed to open state store")?; |
| 151 | + let mut build = store |
| 152 | + .load_build(&args.build_id) |
| 153 | + .context("failed to load build")? |
| 154 | + .ok_or_else(|| anyhow::anyhow!("build {} not found", args.build_id))?; |
| 155 | + |
| 156 | + if build.state.is_terminal() { |
| 157 | + println!("Build {} is already in terminal state.", args.build_id); |
| 158 | + return Ok(()); |
| 159 | + } |
| 160 | + |
| 161 | + let now = std::time::SystemTime::now() |
| 162 | + .duration_since(std::time::UNIX_EPOCH) |
| 163 | + .map(|d| d.as_secs()) |
| 164 | + .unwrap_or(0); |
| 165 | + |
| 166 | + build.ended_at = Some(now); |
| 167 | + |
| 168 | + build |
| 169 | + .transition_to(BuildState::Canceled) |
| 170 | + .map_err(|e| anyhow::anyhow!("{e}"))?; |
| 171 | + |
| 172 | + store.save_build(&build).context("failed to save build")?; |
| 173 | + |
| 174 | + println!("Build {} canceled.", args.build_id); |
| 175 | + |
| 176 | + Ok(()) |
| 177 | +} |
0 commit comments