|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +//! omdb commands that query a crucible-pantry |
| 6 | +
|
| 7 | +use anyhow::Context; |
| 8 | +use anyhow::bail; |
| 9 | +use clap::Args; |
| 10 | +use clap::Subcommand; |
| 11 | +use crucible_pantry_client::Client; |
| 12 | +use crucible_pantry_client::types::VolumeStatus; |
| 13 | +use tabled::Tabled; |
| 14 | +use uuid::Uuid; |
| 15 | + |
| 16 | +use crate::Omdb; |
| 17 | +use crate::helpers::CONNECTION_OPTIONS_HEADING; |
| 18 | + |
| 19 | +/// Arguments to the "omdb crucible-pantry" subcommand |
| 20 | +#[derive(Debug, Args)] |
| 21 | +pub struct CruciblePantryArgs { |
| 22 | + /// URL of the crucible pantry internal API |
| 23 | + #[clap( |
| 24 | + long, |
| 25 | + env = "OMDB_CRUCIBLE_PANTRY_URL", |
| 26 | + global = true, |
| 27 | + help_heading = CONNECTION_OPTIONS_HEADING, |
| 28 | + )] |
| 29 | + crucible_pantry_url: Option<String>, |
| 30 | + |
| 31 | + #[command(subcommand)] |
| 32 | + command: CruciblePantryCommands, |
| 33 | +} |
| 34 | + |
| 35 | +/// Subcommands for the "omdb crucible-pantry" subcommand |
| 36 | +#[derive(Debug, Subcommand)] |
| 37 | +enum CruciblePantryCommands { |
| 38 | + /// Print information about a pantry jobs finished status |
| 39 | + IsFinished(FinishedArgs), |
| 40 | + /// Print information about the pantry |
| 41 | + Status, |
| 42 | + /// Print information about a specific volume |
| 43 | + Volume(VolumeArgs), |
| 44 | +} |
| 45 | + |
| 46 | +#[derive(Debug, Args, Clone)] |
| 47 | +struct FinishedArgs { |
| 48 | + /// The Job ID |
| 49 | + id: String, |
| 50 | +} |
| 51 | + |
| 52 | +#[derive(Debug, Args, Clone)] |
| 53 | +struct VolumeArgs { |
| 54 | + /// The UUID of the volume |
| 55 | + uuid: Uuid, |
| 56 | +} |
| 57 | + |
| 58 | +impl CruciblePantryArgs { |
| 59 | + /// Run a `omdb crucible-pantry` subcommand. |
| 60 | + pub(crate) async fn run_cmd( |
| 61 | + &self, |
| 62 | + _omdb: &Omdb, |
| 63 | + ) -> Result<(), anyhow::Error> { |
| 64 | + // The crucible pantry URL is required, but can come from the |
| 65 | + // environment, in which case it won't be on the command line. |
| 66 | + let Some(crucible_pantry_url) = &self.crucible_pantry_url else { |
| 67 | + bail!( |
| 68 | + "crucible pantry URL must be specified with \ |
| 69 | + --crucible-pantry-url or by setting the environment variable \ |
| 70 | + OMDB_CRUCIBLE_PANTRY_URL" |
| 71 | + ); |
| 72 | + }; |
| 73 | + let client = Client::new(crucible_pantry_url); |
| 74 | + |
| 75 | + match &self.command { |
| 76 | + CruciblePantryCommands::IsFinished(id) => { |
| 77 | + cmd_is_finished(&client, id).await |
| 78 | + } |
| 79 | + CruciblePantryCommands::Status => cmd_pantry_status(&client).await, |
| 80 | + CruciblePantryCommands::Volume(uuid) => { |
| 81 | + cmd_volume_info(&client, uuid).await |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/// Runs `omdb crucible-pantry volume` |
| 88 | +async fn cmd_is_finished( |
| 89 | + client: &crucible_pantry_client::Client, |
| 90 | + args: &FinishedArgs, |
| 91 | +) -> Result<(), anyhow::Error> { |
| 92 | + let is_finished = |
| 93 | + client.is_job_finished(&args.id).await.context("checking jobs")?; |
| 94 | + println!("Job: {} reports is_finished: {:?}", args.id, is_finished); |
| 95 | + Ok(()) |
| 96 | +} |
| 97 | + |
| 98 | +/// Runs `omdb crucible-pantry volume` |
| 99 | +async fn cmd_volume_info( |
| 100 | + client: &crucible_pantry_client::Client, |
| 101 | + args: &VolumeArgs, |
| 102 | +) -> Result<(), anyhow::Error> { |
| 103 | + let volume = args.uuid.to_string(); |
| 104 | + let VolumeStatus { active, num_job_handles, seen_active } = |
| 105 | + *client.volume_status(&volume).await.context("listing volumes")?; |
| 106 | + |
| 107 | + println!(" active: {}", active); |
| 108 | + println!(" num_job_handles: {}", num_job_handles); |
| 109 | + println!(" seen_active: {}", seen_active); |
| 110 | + Ok(()) |
| 111 | +} |
| 112 | + |
| 113 | +#[derive(Tabled)] |
| 114 | +#[tabled(rename_all = "SCREAMING_SNAKE_CASE")] |
| 115 | +struct VolumeInfo { |
| 116 | + volume_id: String, |
| 117 | + active: String, |
| 118 | + num_job_handles: String, |
| 119 | + seen_active: String, |
| 120 | +} |
| 121 | + |
| 122 | +/// Runs `omdb crucible-pantry status` |
| 123 | +async fn cmd_pantry_status( |
| 124 | + client: &crucible_pantry_client::Client, |
| 125 | +) -> Result<(), anyhow::Error> { |
| 126 | + let status = client.pantry_status().await.context("listing volumes")?; |
| 127 | + |
| 128 | + println!("num_job_handles {:?}", status.num_job_handles); |
| 129 | + println!("Volumes found: {}", status.volumes.len()); |
| 130 | + |
| 131 | + let mut rows = Vec::new(); |
| 132 | + for v in &status.volumes { |
| 133 | + // let vs = client.volume_status(&v).await.context("listing volumes")?; |
| 134 | + match client.volume_status(&v).await.context("listing volumes") { |
| 135 | + Ok(vs) => { |
| 136 | + rows.push(VolumeInfo { |
| 137 | + volume_id: v.clone().to_string(), |
| 138 | + active: vs.active.clone().to_string(), |
| 139 | + num_job_handles: vs.num_job_handles.clone().to_string(), |
| 140 | + seen_active: vs.seen_active.clone().to_string(), |
| 141 | + }); |
| 142 | + } |
| 143 | + Err(e) => { |
| 144 | + println!("Failed to get info for volume {v}: {e}"); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + let table = tabled::Table::new(rows) |
| 149 | + .with(tabled::settings::Style::empty()) |
| 150 | + .with(tabled::settings::Padding::new(0, 1, 0, 0)) |
| 151 | + .to_string(); |
| 152 | + |
| 153 | + println!("{}", table); |
| 154 | + |
| 155 | + Ok(()) |
| 156 | +} |
0 commit comments