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

[omdb] add basic support for activating background tasks #5615

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dev-tools/omdb/src/bin/omdb/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ struct Omdb {
#[arg(env = "OMDB_DNS_SERVER", long)]
dns_server: Option<SocketAddr>,

/// allow potentially-destructive subcommands
#[arg(short = 'w', long = "destructive")]
/// Allow potentially-destructive subcommands.
#[arg(short = 'w', long = "destructive", global = true)]
allow_destructive: bool,

#[command(subcommand)]
Expand Down
36 changes: 36 additions & 0 deletions dev-tools/omdb/src/bin/omdb/nexus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use futures::future::try_join;
use futures::TryStreamExt;
use nexus_client::types::ActivationReason;
use nexus_client::types::BackgroundTask;
use nexus_client::types::BackgroundTasksActivateRequest;
use nexus_client::types::CurrentStatus;
use nexus_client::types::LastResult;
use nexus_client::types::SledSelector;
Expand Down Expand Up @@ -77,6 +78,15 @@ enum BackgroundTasksCommands {
List,
/// Print human-readable summary of the status of each background task
Show,
/// Activate one or more background tasks
Activate(BackgroundTasksActivateArgs),
}

#[derive(Debug, Args)]
struct BackgroundTasksActivateArgs {
/// Name of the background tasks to activate
#[clap(value_name = "TASK_NAME", required = true)]
tasks: Vec<String>,
}

#[derive(Debug, Args)]
Expand Down Expand Up @@ -298,6 +308,12 @@ impl NexusArgs {
NexusCommands::BackgroundTasks(BackgroundTasksArgs {
command: BackgroundTasksCommands::Show,
}) => cmd_nexus_background_tasks_show(&client).await,
NexusCommands::BackgroundTasks(BackgroundTasksArgs {
command: BackgroundTasksCommands::Activate(args),
}) => {
let token = omdb.check_allow_destructive()?;
cmd_nexus_background_tasks_activate(&client, args, token).await
}

NexusCommands::Blueprints(BlueprintsArgs {
command: BlueprintsCommands::List,
Expand Down Expand Up @@ -465,6 +481,26 @@ async fn cmd_nexus_background_tasks_show(
Ok(())
}

/// Runs `omdb nexus background-tasks activate`
async fn cmd_nexus_background_tasks_activate(
client: &nexus_client::Client,
args: &BackgroundTasksActivateArgs,
sunshowers marked this conversation as resolved.
Show resolved Hide resolved
// This isn't quite "destructive" in the sense that of it being potentially
// dangerous, but it does modify the system rather than being a read-only
// view on it.
_destruction_token: DestructiveOperationToken,
) -> Result<(), anyhow::Error> {
let body =
BackgroundTasksActivateRequest { bgtask_names: args.tasks.clone() };
client
.bgtask_activate(&body)
.await
.context("error activating background tasks")?;

eprintln!("activated background tasks: {}", args.tasks.join(", "));
Ok(())
}

fn print_task(bgtask: &BackgroundTask) {
println!("task: {:?}", bgtask.name);
println!(
Expand Down
9 changes: 9 additions & 0 deletions dev-tools/omdb/tests/successes.out
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,15 @@ warning: unknown background task: "switch_port_config_manager" (don't know how t
stderr:
note: using Nexus URL http://127.0.0.1:REDACTED_PORT/
=============================================
EXECUTING COMMAND: omdb ["nexus", "background-tasks", "activate", "inventory_collection", "--destructive"]
termination: Exited(0)
---------------------------------------------
stdout:
---------------------------------------------
stderr:
note: using Nexus URL http://127.0.0.1:REDACTED_PORT/
activated background tasks: inventory_collection
=============================================
EXECUTING COMMAND: omdb ["nexus", "blueprints", "list"]
termination: Exited(0)
---------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions dev-tools/omdb/tests/test_all_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ async fn test_omdb_usage_errors() {
&["mgs"],
&["nexus"],
&["nexus", "background-tasks"],
&["nexus", "blueprints"],
&["nexus", "sleds"],
&["sled-agent"],
&["sled-agent", "zones"],
&["sled-agent", "zpools"],
Expand Down Expand Up @@ -93,6 +95,13 @@ async fn test_omdb_success_cases(cptestctx: &ControlPlaneTestContext) {
&["mgs", "inventory"],
&["nexus", "background-tasks", "doc"],
&["nexus", "background-tasks", "show"],
&[
"nexus",
"background-tasks",
"activate",
"inventory_collection",
"--destructive",
],
&["nexus", "blueprints", "list"],
&["nexus", "blueprints", "show", &initial_blueprint_id],
&["nexus", "blueprints", "show", "current-target"],
Expand Down
97 changes: 77 additions & 20 deletions dev-tools/omdb/tests/usage_errors.out
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Commands:
Options:
--log-level <LOG_LEVEL> log level filter [env: LOG_LEVEL=] [default: warn]
--dns-server <DNS_SERVER> [env: OMDB_DNS_SERVER=]
-w, --destructive allow potentially-destructive subcommands
-w, --destructive Allow potentially-destructive subcommands
-h, --help Print help (see more with '--help')
=============================================
EXECUTING COMMAND: omdb ["--help"]
Expand Down Expand Up @@ -54,7 +54,7 @@ Options:
[env: OMDB_DNS_SERVER=]

-w, --destructive
allow potentially-destructive subcommands
Allow potentially-destructive subcommands

-h, --help
Print help (see a summary with '-h')
Expand Down Expand Up @@ -114,6 +114,7 @@ Options:
OMDB_FETCH_LIMIT=] [default: 500]
--include-deleted whether to include soft-deleted records when enumerating objects
that can be soft-deleted
-w, --destructive Allow potentially-destructive subcommands
-h, --help Print help
=============================================
EXECUTING COMMAND: omdb ["db", "--help"]
Expand Down Expand Up @@ -143,6 +144,7 @@ Options:
OMDB_FETCH_LIMIT=] [default: 500]
--include-deleted whether to include soft-deleted records when enumerating objects
that can be soft-deleted
-w, --destructive Allow potentially-destructive subcommands
-h, --help Print help
---------------------------------------------
stderr:
Expand All @@ -155,7 +157,7 @@ stdout:
stderr:
Print information about disks

Usage: omdb db disks <COMMAND>
Usage: omdb db disks [OPTIONS] <COMMAND>

Commands:
info Get info for a specific disk
Expand All @@ -164,7 +166,8 @@ Commands:
help Print this message or the help of the given subcommand(s)

Options:
-h, --help Print help
-w, --destructive Allow potentially-destructive subcommands
-h, --help Print help
=============================================
EXECUTING COMMAND: omdb ["db", "dns"]
termination: Exited(2)
Expand All @@ -174,7 +177,7 @@ stdout:
stderr:
Print information about internal and external DNS

Usage: omdb db dns <COMMAND>
Usage: omdb db dns [OPTIONS] <COMMAND>

Commands:
show Summarize current version of all DNS zones
Expand All @@ -183,7 +186,8 @@ Commands:
help Print this message or the help of the given subcommand(s)

Options:
-h, --help Print help
-w, --destructive Allow potentially-destructive subcommands
-h, --help Print help
=============================================
EXECUTING COMMAND: omdb ["db", "dns", "diff"]
termination: Exited(2)
Expand Down Expand Up @@ -221,15 +225,16 @@ stdout:
stderr:
Print information about snapshots

Usage: omdb db snapshots <COMMAND>
Usage: omdb db snapshots [OPTIONS] <COMMAND>

Commands:
info Get info for a specific snapshot
list Summarize current snapshots
help Print this message or the help of the given subcommand(s)

Options:
-h, --help Print help
-w, --destructive Allow potentially-destructive subcommands
-h, --help Print help
=============================================
EXECUTING COMMAND: omdb ["db", "network"]
termination: Exited(2)
Expand All @@ -247,8 +252,9 @@ Commands:
help Print this message or the help of the given subcommand(s)

Options:
--verbose Print out raw data structures from the data store
-h, --help Print help
--verbose Print out raw data structures from the data store
-w, --destructive Allow potentially-destructive subcommands
-h, --help Print help
=============================================
EXECUTING COMMAND: omdb ["mgs"]
termination: Exited(2)
Expand All @@ -268,6 +274,7 @@ Commands:

Options:
--mgs-url <MGS_URL> URL of an MGS instance to query [env: OMDB_MGS_URL=]
-w, --destructive Allow potentially-destructive subcommands
-h, --help Print help
=============================================
EXECUTING COMMAND: omdb ["nexus"]
Expand All @@ -289,6 +296,7 @@ Commands:
Options:
--nexus-internal-url <NEXUS_INTERNAL_URL> URL of the Nexus internal API [env:
OMDB_NEXUS_URL=]
-w, --destructive Allow potentially-destructive subcommands
-h, --help Print help
=============================================
EXECUTING COMMAND: omdb ["nexus", "background-tasks"]
Expand All @@ -299,16 +307,62 @@ stdout:
stderr:
print information about background tasks

Usage: omdb nexus background-tasks <COMMAND>
Usage: omdb nexus background-tasks [OPTIONS] <COMMAND>

Commands:
doc Show documentation about background tasks
list Print a summary of the status of all background tasks
show Print human-readable summary of the status of each background task
help Print this message or the help of the given subcommand(s)
doc Show documentation about background tasks
list Print a summary of the status of all background tasks
show Print human-readable summary of the status of each background task
activate Activate one or more background tasks
help Print this message or the help of the given subcommand(s)

Options:
-w, --destructive Allow potentially-destructive subcommands
-h, --help Print help
=============================================
EXECUTING COMMAND: omdb ["nexus", "blueprints"]
termination: Exited(2)
---------------------------------------------
stdout:
---------------------------------------------
stderr:
interact with blueprints

Usage: omdb nexus blueprints [OPTIONS] <COMMAND>

Commands:
list List all blueprints
show Show a blueprint
diff Diff two blueprints
delete Delete a blueprint
target Interact with the current target blueprint
regenerate Generate a new blueprint
import Import a blueprint
help Print this message or the help of the given subcommand(s)

Options:
-w, --destructive Allow potentially-destructive subcommands
-h, --help Print help
=============================================
EXECUTING COMMAND: omdb ["nexus", "sleds"]
termination: Exited(2)
---------------------------------------------
stdout:
---------------------------------------------
stderr:
interact with sleds

Usage: omdb nexus sleds [OPTIONS] <COMMAND>

Commands:
list-uninitialized List all uninitialized sleds
add Add an uninitialized sled
expunge Expunge a sled (DANGEROUS)
help Print this message or the help of the given subcommand(s)

Options:
-h, --help Print help
-w, --destructive Allow potentially-destructive subcommands
-h, --help Print help
=============================================
EXECUTING COMMAND: omdb ["sled-agent"]
termination: Exited(2)
Expand All @@ -328,6 +382,7 @@ Commands:

Options:
--sled-agent-url <SLED_AGENT_URL> URL of the Sled internal API [env: OMDB_SLED_AGENT_URL=]
-w, --destructive Allow potentially-destructive subcommands
-h, --help Print help
=============================================
EXECUTING COMMAND: omdb ["sled-agent", "zones"]
Expand All @@ -338,14 +393,15 @@ stdout:
stderr:
print information about zones

Usage: omdb sled-agent zones <COMMAND>
Usage: omdb sled-agent zones [OPTIONS] <COMMAND>

Commands:
list Print list of all running control plane zones
help Print this message or the help of the given subcommand(s)

Options:
-h, --help Print help
-w, --destructive Allow potentially-destructive subcommands
-h, --help Print help
=============================================
EXECUTING COMMAND: omdb ["sled-agent", "zpools"]
termination: Exited(2)
Expand All @@ -355,12 +411,13 @@ stdout:
stderr:
print information about zpools

Usage: omdb sled-agent zpools <COMMAND>
Usage: omdb sled-agent zpools [OPTIONS] <COMMAND>

Commands:
list Print list of all zpools managed by the sled agent
help Print this message or the help of the given subcommand(s)

Options:
-h, --help Print help
-w, --destructive Allow potentially-destructive subcommands
-h, --help Print help
=============================================
35 changes: 35 additions & 0 deletions nexus/src/app/background/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use omicron_common::api::external::LookupResult;
use omicron_common::api::external::LookupType;
use omicron_common::api::external::ResourceType;
use std::collections::BTreeMap;
use std::collections::BTreeSet;

impl Nexus {
pub(crate) async fn bgtasks_list(
Expand Down Expand Up @@ -53,4 +54,38 @@ impl Nexus {
let period = driver.task_period(task);
Ok(BackgroundTask::new(task.name(), description, period, status))
}

pub(crate) async fn bgtask_activate(
&self,
opctx: &OpContext,
mut names: BTreeSet<String>,
) -> Result<(), Error> {
opctx.authorize(authz::Action::Modify, &authz::FLEET).await?;
let driver = &self.background_tasks.driver;

// Ensure all task names are valid by removing them from the set of
// names as we find them.
let tasks_to_activate: Vec<_> =
driver.tasks().filter(|t| names.remove(t.name())).collect();

// If any names weren't recognized, return an error.
if !names.is_empty() {
let mut names_str = "background tasks: ".to_owned();
for (i, name) in names.iter().enumerate() {
names_str.push_str(name);
if i < names.len() - 1 {
names_str.push_str(", ");
}
}

return Err(LookupType::ByOther(names_str)
.into_not_found(ResourceType::BackgroundTask));
}

for task in tasks_to_activate {
driver.activate(task);
}

Ok(())
}
}
Loading
Loading