Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add internal API and omdb command to expunge a sled #5234

Merged
merged 12 commits into from
Mar 13, 2024
26 changes: 19 additions & 7 deletions dev-tools/omdb/src/bin/omdb/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,28 @@ struct Omdb {
command: OmdbCommands,
}

impl Omdb {
fn check_allow_destructive(&self) -> anyhow::Result<()> {
anyhow::ensure!(
self.allow_destructive,
"This command is potentially destructive. \
mod check_allow_destructive {
/// Zero-size type that potentially-destructive functions can accept to
/// ensure `Omdb::check_allow_destructive` has been called.
// This is tucked away inside a module to prevent it from being constructed
// by anything other than `Omdb::check_allow_destructive`.
pub(crate) struct DestructiveOperationToken(());
jgallagher marked this conversation as resolved.
Show resolved Hide resolved

impl super::Omdb {
pub(crate) fn check_allow_destructive(
&self,
) -> anyhow::Result<DestructiveOperationToken> {
anyhow::ensure!(
self.allow_destructive,
"This command is potentially destructive. \
Pass the `-w` / `--destructive` flag to allow it."
);
Ok(())
);
Ok(DestructiveOperationToken(()))
}
}
}

impl Omdb {
async fn dns_lookup_all(
&self,
log: slog::Logger,
Expand Down
35 changes: 22 additions & 13 deletions dev-tools/omdb/src/bin/omdb/nexus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

//! omdb commands that query or update specific Nexus instances

use crate::check_allow_destructive::DestructiveOperationToken;
use crate::db::DbUrlOptions;
use crate::Omdb;
use anyhow::bail;
Expand Down Expand Up @@ -234,8 +235,8 @@ impl NexusArgs {
NexusCommands::Blueprints(BlueprintsArgs {
command: BlueprintsCommands::Delete(args),
}) => {
omdb.check_allow_destructive()?;
cmd_nexus_blueprints_delete(&client, args).await
let token = omdb.check_allow_destructive()?;
cmd_nexus_blueprints_delete(&client, args, token).await
}
NexusCommands::Blueprints(BlueprintsArgs {
command:
Expand All @@ -249,21 +250,23 @@ impl NexusArgs {
command: BlueprintTargetCommands::Set(args),
}),
}) => {
omdb.check_allow_destructive()?;
cmd_nexus_blueprints_target_set(&client, args).await
let token = omdb.check_allow_destructive()?;
cmd_nexus_blueprints_target_set(&client, args, token).await
}
NexusCommands::Blueprints(BlueprintsArgs {
command: BlueprintsCommands::Regenerate,
}) => {
omdb.check_allow_destructive()?;
cmd_nexus_blueprints_regenerate(&client).await
let token = omdb.check_allow_destructive()?;
cmd_nexus_blueprints_regenerate(&client, token).await
}
NexusCommands::Blueprints(BlueprintsArgs {
command: BlueprintsCommands::GenerateFromCollection(args),
}) => {
omdb.check_allow_destructive()?;
cmd_nexus_blueprints_generate_from_collection(&client, args)
.await
let token = omdb.check_allow_destructive()?;
cmd_nexus_blueprints_generate_from_collection(
&client, args, token,
)
.await
}

NexusCommands::Sleds(SledsArgs {
Expand All @@ -272,14 +275,14 @@ impl NexusArgs {
NexusCommands::Sleds(SledsArgs {
command: SledsCommands::Add(args),
}) => {
omdb.check_allow_destructive()?;
cmd_nexus_sled_add(&client, args).await
let token = omdb.check_allow_destructive()?;
cmd_nexus_sled_add(&client, args, token).await
}
NexusCommands::Sleds(SledsArgs {
command: SledsCommands::Expunge(args),
}) => {
omdb.check_allow_destructive()?;
cmd_nexus_sled_expunge(&client, args, omdb, log).await
let token = omdb.check_allow_destructive()?;
cmd_nexus_sled_expunge(&client, args, omdb, log, token).await
}
}
}
Expand Down Expand Up @@ -937,6 +940,7 @@ async fn cmd_nexus_blueprints_diff(
async fn cmd_nexus_blueprints_delete(
client: &nexus_client::Client,
args: &BlueprintIdArgs,
_destruction_token: DestructiveOperationToken,
) -> Result<(), anyhow::Error> {
let _ = client
.blueprint_delete(&args.blueprint_id)
Expand All @@ -962,6 +966,7 @@ async fn cmd_nexus_blueprints_target_show(
async fn cmd_nexus_blueprints_target_set(
client: &nexus_client::Client,
args: &BlueprintTargetSetArgs,
_destruction_token: DestructiveOperationToken,
) -> Result<(), anyhow::Error> {
let enabled = match args.enabled {
BlueprintTargetSetEnabled::Enabled => true,
Expand Down Expand Up @@ -997,6 +1002,7 @@ async fn cmd_nexus_blueprints_target_set(
async fn cmd_nexus_blueprints_generate_from_collection(
client: &nexus_client::Client,
args: &CollectionIdArgs,
_destruction_token: DestructiveOperationToken,
) -> Result<(), anyhow::Error> {
let blueprint = client
.blueprint_generate_from_collection(
Expand All @@ -1012,6 +1018,7 @@ async fn cmd_nexus_blueprints_generate_from_collection(

async fn cmd_nexus_blueprints_regenerate(
client: &nexus_client::Client,
_destruction_token: DestructiveOperationToken,
) -> Result<(), anyhow::Error> {
let blueprint =
client.blueprint_regenerate().await.context("generating blueprint")?;
Expand Down Expand Up @@ -1065,6 +1072,7 @@ async fn cmd_nexus_sleds_list_uninitialized(
async fn cmd_nexus_sled_add(
client: &nexus_client::Client,
args: &SledAddArgs,
_destruction_token: DestructiveOperationToken,
) -> Result<(), anyhow::Error> {
client
.sled_add(&UninitializedSledId {
Expand All @@ -1083,6 +1091,7 @@ async fn cmd_nexus_sled_expunge(
args: &SledExpungeArgs,
omdb: &Omdb,
log: &slog::Logger,
_destruction_token: DestructiveOperationToken,
) -> Result<(), anyhow::Error> {
// This is an extremely dangerous and irreversible operation. We put a
// couple of safeguards in place to ensure this cannot be called without
Expand Down