|
| 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 | +//! Support for inventory checks via wicketd. |
| 6 | +
|
| 7 | +use crate::cli::CommandOutput; |
| 8 | +use crate::wicketd::create_wicketd_client; |
| 9 | +use anyhow::Context; |
| 10 | +use anyhow::Result; |
| 11 | +use clap::{Subcommand, ValueEnum}; |
| 12 | +use owo_colors::OwoColorize; |
| 13 | +use sled_hardware_types::Baseboard; |
| 14 | +use slog::Logger; |
| 15 | +use std::fmt; |
| 16 | +use std::net::SocketAddrV6; |
| 17 | +use std::time::Duration; |
| 18 | +use wicket_common::rack_setup::BootstrapSledDescription; |
| 19 | + |
| 20 | +const WICKETD_TIMEOUT: Duration = Duration::from_secs(5); |
| 21 | + |
| 22 | +#[derive(Debug, Subcommand)] |
| 23 | +pub(crate) enum InventoryArgs { |
| 24 | + /// List state of all bootstrap sleds, as configured with rack-setup |
| 25 | + ConfiguredBootstrapSleds { |
| 26 | + /// Select output format |
| 27 | + #[clap(long, default_value_t = OutputFormat::Table)] |
| 28 | + format: OutputFormat, |
| 29 | + }, |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Debug, ValueEnum, Clone)] |
| 33 | +pub enum OutputFormat { |
| 34 | + /// Print output as operator-readable table |
| 35 | + Table, |
| 36 | + |
| 37 | + /// Print output as json |
| 38 | + Json, |
| 39 | +} |
| 40 | + |
| 41 | +impl fmt::Display for OutputFormat { |
| 42 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 43 | + match self { |
| 44 | + OutputFormat::Table => write!(f, "table"), |
| 45 | + OutputFormat::Json => write!(f, "json"), |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl InventoryArgs { |
| 51 | + pub(crate) async fn exec( |
| 52 | + self, |
| 53 | + log: Logger, |
| 54 | + wicketd_addr: SocketAddrV6, |
| 55 | + mut output: CommandOutput<'_>, |
| 56 | + ) -> Result<()> { |
| 57 | + let client = create_wicketd_client(&log, wicketd_addr, WICKETD_TIMEOUT); |
| 58 | + |
| 59 | + match self { |
| 60 | + InventoryArgs::ConfiguredBootstrapSleds { format } => { |
| 61 | + // We don't use the /bootstrap-sleds endpoint, because that |
| 62 | + // gets all sleds visible on the bootstrap network. We want |
| 63 | + // something subtly different here. |
| 64 | + // - We want the status of only sleds we've configured wicket |
| 65 | + // to use for setup. /bootstrap-sleds will give us sleds |
| 66 | + // we don't want |
| 67 | + // - We want the status even if they aren't visible on the |
| 68 | + // bootstrap network yet. |
| 69 | + // |
| 70 | + // In other words, we want the sled information displayed at the |
| 71 | + // bottom of the rack setup screen in the TUI, and we get it the |
| 72 | + // same way it does. |
| 73 | + let conf = client |
| 74 | + .get_rss_config() |
| 75 | + .await |
| 76 | + .context("failed to get rss config")?; |
| 77 | + |
| 78 | + let bootstrap_sleds = &conf.insensitive.bootstrap_sleds; |
| 79 | + match format { |
| 80 | + OutputFormat::Json => { |
| 81 | + let json_str = |
| 82 | + serde_json::to_string_pretty(bootstrap_sleds) |
| 83 | + .context("serializing sled data failed")?; |
| 84 | + writeln!(output.stdout, "{}", json_str) |
| 85 | + .expect("writing to stdout failed"); |
| 86 | + } |
| 87 | + OutputFormat::Table => { |
| 88 | + for sled in bootstrap_sleds { |
| 89 | + print_bootstrap_sled_data(sled, &mut output); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + Ok(()) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +fn print_bootstrap_sled_data( |
| 101 | + desc: &BootstrapSledDescription, |
| 102 | + output: &mut CommandOutput<'_>, |
| 103 | +) { |
| 104 | + let slot = desc.id.slot; |
| 105 | + |
| 106 | + let identifier = match &desc.baseboard { |
| 107 | + Baseboard::Gimlet { identifier, .. } => identifier.clone(), |
| 108 | + Baseboard::Pc { identifier, .. } => identifier.clone(), |
| 109 | + Baseboard::Unknown => "unknown".to_string(), |
| 110 | + }; |
| 111 | + |
| 112 | + let address = desc.bootstrap_ip; |
| 113 | + |
| 114 | + // Create status indicators |
| 115 | + let status = match address { |
| 116 | + None => format!("{}", '⚠'.red()), |
| 117 | + Some(_) => format!("{}", '✔'.green()), |
| 118 | + }; |
| 119 | + |
| 120 | + let addr_fmt = match address { |
| 121 | + None => "(not available)".to_string(), |
| 122 | + Some(addr) => format!("{}", addr), |
| 123 | + }; |
| 124 | + |
| 125 | + // Print out this entry. We say "Cubby" rather than "Slot" here purely |
| 126 | + // because the TUI also says "Cubby". |
| 127 | + writeln!( |
| 128 | + output.stdout, |
| 129 | + "{status} Cubby {:02}\t{identifier}\t{addr_fmt}", |
| 130 | + slot |
| 131 | + ) |
| 132 | + .expect("writing to stdout failed"); |
| 133 | +} |
0 commit comments