diff --git a/dev-tools/omdb/src/bin/omdb/db.rs b/dev-tools/omdb/src/bin/omdb/db.rs index 22d553fbdcc..ad04ee254bc 100644 --- a/dev-tools/omdb/src/bin/omdb/db.rs +++ b/dev-tools/omdb/src/bin/omdb/db.rs @@ -190,6 +190,7 @@ mod db_metadata; mod ereport; mod saga; mod sitrep; +mod target_release; mod user_data_export; mod whatis; @@ -405,6 +406,8 @@ enum DbCommands { Sleds(SledsArgs), /// Show instances grouped by the sled they are running on SledInstances(SledInstancesArgs), + /// Print the current target release and the update date + TargetRelease(target_release::TargetReleaseArgs), /// Print information about customer instances. Instance(InstanceArgs), /// Alias to `omdb instance list`. @@ -1453,6 +1456,9 @@ impl DbArgs { ) .await } + DbCommands::TargetRelease(args) => { + target_release::cmd_db_target_release(&opctx, &datastore, &fetch_opts, args).await + } DbCommands::Instance(InstanceArgs { command: InstanceCommands::List(args), }) => { diff --git a/dev-tools/omdb/src/bin/omdb/db/target_release.rs b/dev-tools/omdb/src/bin/omdb/db/target_release.rs new file mode 100644 index 00000000000..c64f56400e1 --- /dev/null +++ b/dev-tools/omdb/src/bin/omdb/db/target_release.rs @@ -0,0 +1,97 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at https://mozilla.org/MPL/2.0/. + +//! `omdb db target-release` subcommand +//! +//! Shows the current target release and date when update was requested. +//! When `--all` option is used, lists all target release records. + +use crate::db::DbFetchOptions; +use crate::db::check_limit; +use crate::helpers::datetime_rfc3339_concise; +use anyhow::Context; +use async_bb8_diesel::AsyncRunQueryDsl; +use clap::Args; +use diesel::ExpressionMethods; +use diesel::QueryDsl; +use diesel::SelectableHelper; +use nexus_db_model::TargetRelease; +use nexus_db_model::TargetReleaseSource; +use nexus_db_queries::context::OpContext; +use nexus_db_queries::db::DataStore; +use nexus_db_schema::schema::target_release::dsl; +use tabled::Tabled; + +#[derive(Debug, Args, Clone)] +pub(super) struct TargetReleaseArgs { + /// List the most recent target releases, sorted from oldest to newest + #[clap(long)] + all: bool, +} + +#[derive(Tabled)] +#[tabled(rename_all = "SCREAMING_SNAKE_CASE")] +struct TargetReleaseRow { + target_release: String, + time_requested: String, +} + +pub(super) async fn cmd_db_target_release( + opctx: &OpContext, + datastore: &DataStore, + fetch_opts: &DbFetchOptions, + args: &TargetReleaseArgs, +) -> Result<(), anyhow::Error> { + let releases = if args.all { + let limit = fetch_opts.fetch_limit; + let conn = datastore.pool_connection_for_tests().await?; + let mut releases: Vec = dsl::target_release + .select(TargetRelease::as_select()) + .order_by(dsl::generation.desc()) + .limit(i64::from(u32::from(limit))) + .load_async(&*conn) + .await + .context("listing target releases")?; + check_limit(&releases, limit, || { + String::from("listing target releases") + }); + releases.reverse(); + releases + } else { + vec![ + datastore + .target_release_get_current(opctx) + .await + .context("fetching current target release")?, + ] + }; + + let mut rows = Vec::with_capacity(releases.len()); + for release in releases { + let target_release = match release + .release_source() + .context("interpreting target release source")? + { + TargetReleaseSource::Unspecified => "".to_string(), + TargetReleaseSource::SystemVersion(tuf_repo_id) => datastore + .tuf_repo_get_version(opctx, &tuf_repo_id) + .await + .with_context(|| format!("fetching TUF repo {tuf_repo_id}"))? + .to_string(), + }; + rows.push(TargetReleaseRow { + target_release, + time_requested: datetime_rfc3339_concise(&release.time_requested), + }); + } + + let table = tabled::Table::new(rows) + .with(tabled::settings::Style::empty()) + .with(tabled::settings::Padding::new(1, 1, 0, 0)) + .to_string(); + + println!("{}", table); + + Ok(()) +} diff --git a/dev-tools/omdb/tests/usage_errors.out b/dev-tools/omdb/tests/usage_errors.out index 3795a6be563..00b2ec9b52f 100644 --- a/dev-tools/omdb/tests/usage_errors.out +++ b/dev-tools/omdb/tests/usage_errors.out @@ -142,6 +142,7 @@ Commands: sitreps Show the current history of fault management situation reports sleds Print information about sleds sled-instances Show instances grouped by the sled they are running on + target-release Print the current target release and the update date instance Print information about customer instances instances Alias to `omdb instance list` network Print information about the network @@ -210,6 +211,7 @@ Commands: sitreps Show the current history of fault management situation reports sleds Print information about sleds sled-instances Show instances grouped by the sled they are running on + target-release Print the current target release and the update date instance Print information about customer instances instances Alias to `omdb instance list` network Print information about the network