Skip to content

Commit

Permalink
Merge branch 'main' into diesel-less-generic
Browse files Browse the repository at this point in the history
  • Loading branch information
smklein committed Sep 27, 2023
2 parents da14e80 + 0a5b91a commit f594c97
Show file tree
Hide file tree
Showing 46 changed files with 341 additions and 214 deletions.
47 changes: 36 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ ipnetwork = { version = "0.20", features = ["schemars"] }
itertools = "0.11.0"
key-manager = { path = "key-manager" }
lazy_static = "1.4.0"
libc = "0.2.147"
libc = "0.2.148"
linear-map = "1.2.0"
macaddr = { version = "1.0.1", features = ["serde_std"] }
mime_guess = "2.0.4"
Expand Down Expand Up @@ -352,7 +352,7 @@ tokio-postgres = { version = "0.7", features = [ "with-chrono-0_4", "with-uuid-1
tokio-stream = "0.1.14"
tokio-tungstenite = "0.18"
tokio-util = "0.7.8"
toml = "0.7.6"
toml = "0.7.8"
toml_edit = "0.19.15"
topological-sort = "0.2.2"
tough = { version = "0.12", features = [ "http" ] }
Expand Down
52 changes: 52 additions & 0 deletions dev-tools/omdb/src/bin/omdb/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ enum DbCommands {
Services(ServicesArgs),
/// Print information about sleds
Sleds,
/// Print information about customer instances
Instances,
}

#[derive(Debug, Args)]
Expand Down Expand Up @@ -246,6 +248,9 @@ impl DbArgs {
DbCommands::Sleds => {
cmd_db_sleds(&opctx, &datastore, self.fetch_limit).await
}
DbCommands::Instances => {
cmd_db_instances(&datastore, self.fetch_limit).await
}
}
}
}
Expand Down Expand Up @@ -682,6 +687,53 @@ async fn cmd_db_sleds(
Ok(())
}

#[derive(Tabled)]
#[tabled(rename_all = "SCREAMING_SNAKE_CASE")]
struct CustomerInstanceRow {
id: Uuid,
state: String,
propolis_id: Uuid,
sled_id: Uuid,
}

impl From<Instance> for CustomerInstanceRow {
fn from(i: Instance) -> Self {
CustomerInstanceRow {
id: i.id(),
state: format!("{:?}", i.runtime_state.state.0),
propolis_id: i.runtime_state.propolis_id,
sled_id: i.runtime_state.sled_id,
}
}
}

/// Run `omdb db instances`: list data about customer VMs.
async fn cmd_db_instances(
datastore: &DataStore,
limit: NonZeroU32,
) -> Result<(), anyhow::Error> {
use db::schema::instance::dsl;
let instances = dsl::instance
.limit(i64::from(u32::from(limit)))
.select(Instance::as_select())
.load_async(datastore.pool_for_tests().await?)
.await
.context("loading instances")?;

let ctx = || "listing instances".to_string();
check_limit(&instances, limit, ctx);

let rows = instances.into_iter().map(|i| CustomerInstanceRow::from(i));
let table = tabled::Table::new(rows)
.with(tabled::settings::Style::empty())
.with(tabled::settings::Padding::new(0, 1, 0, 0))
.to_string();

println!("{}", table);

Ok(())
}

// DNS

/// Run `omdb db dns show`.
Expand Down
22 changes: 12 additions & 10 deletions dev-tools/omdb/tests/usage_errors.out
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ Query the control plane database (CockroachDB)
Usage: omdb db [OPTIONS] <COMMAND>

Commands:
disks Print information about disks
dns Print information about internal and external DNS
services Print information about control plane services
sleds Print information about sleds
help Print this message or the help of the given subcommand(s)
disks Print information about disks
dns Print information about internal and external DNS
services Print information about control plane services
sleds Print information about sleds
instances Print information about customer instances
help Print this message or the help of the given subcommand(s)

Options:
--db-url <DB_URL> URL of the database SQL interface [env: OMDB_DB_URL=]
Expand All @@ -106,11 +107,12 @@ Query the control plane database (CockroachDB)
Usage: omdb db [OPTIONS] <COMMAND>

Commands:
disks Print information about disks
dns Print information about internal and external DNS
services Print information about control plane services
sleds Print information about sleds
help Print this message or the help of the given subcommand(s)
disks Print information about disks
dns Print information about internal and external DNS
services Print information about control plane services
sleds Print information about sleds
instances Print information about customer instances
help Print this message or the help of the given subcommand(s)

Options:
--db-url <DB_URL> URL of the database SQL interface [env: OMDB_DB_URL=]
Expand Down
4 changes: 2 additions & 2 deletions dev-tools/xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ license = "MPL-2.0"
[dependencies]
anyhow.workspace = true
camino.workspace = true
cargo_toml = "0.15"
cargo_metadata = "0.17"
cargo_toml = "0.16"
cargo_metadata = "0.18"
clap.workspace = true
5 changes: 2 additions & 3 deletions gateway-test-utils/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,12 @@ pub async fn test_setup_with_config(
}

// Start gateway server
let rack_id = Uuid::parse_str(RACK_UUID).unwrap();
let rack_id = Some(Uuid::parse_str(RACK_UUID).unwrap());

let args = MgsArguments { id: Uuid::new_v4(), addresses };
let args = MgsArguments { id: Uuid::new_v4(), addresses, rack_id };
let server = omicron_gateway::Server::start(
server_config.clone(),
args,
rack_id,
log.clone(),
)
.await
Expand Down
41 changes: 34 additions & 7 deletions gateway/src/bin/mgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ enum Args {
struct ConfigProperties {
id: Uuid,
addresses: Vec<SocketAddrV6>,
rack_id: Option<Uuid>,
}

#[tokio::main]
Expand Down Expand Up @@ -91,15 +92,17 @@ async fn do_run() -> Result<(), CmdError> {
))
})?;

let (id, addresses) = if id_and_address_from_smf {
let (id, addresses, rack_id) = if id_and_address_from_smf {
let config = read_smf_config()?;
(config.id, config.addresses)
(config.id, config.addresses, config.rack_id)
} else {
// Clap ensures these are present if `id_and_address_from_smf`
// is false, so we can safely unwrap.
(id.unwrap(), vec![address.unwrap()])
// Does it matter if `rack_id` is always `None` in this case?
let rack_id = None;
// Clap ensures the first two fields are present if
// `id_and_address_from_smf` is false, so we can safely unwrap.
(id.unwrap(), vec![address.unwrap()], rack_id)
};
let args = MgsArguments { id, addresses };
let args = MgsArguments { id, addresses, rack_id };
let mut server =
start_server(config, args).await.map_err(CmdError::Failure)?;

Expand All @@ -114,6 +117,7 @@ async fn do_run() -> Result<(), CmdError> {
.to_string()
));
}
server.set_rack_id(new_config.rack_id);
server
.adjust_dropshot_addresses(&new_config.addresses)
.await
Expand Down Expand Up @@ -148,6 +152,9 @@ fn read_smf_config() -> Result<ConfigProperties, CmdError> {
// Name of the property within CONFIG_PG for our server addresses.
const PROP_ADDR: &str = "address";

// Name of the property within CONFIG_PG for our rack ID.
const PROP_RACK_ID: &str = "rack_id";

// This function is pretty boilerplate-y; we can reduce it by using this
// error type to help us construct a `CmdError::Failure(_)` string. It
// assumes (for the purposes of error messages) any property being fetched
Expand Down Expand Up @@ -210,6 +217,26 @@ fn read_smf_config() -> Result<ConfigProperties, CmdError> {
))
})?;

let prop_rack_id = config
.get_property(PROP_RACK_ID)
.map_err(|err| Error::GetProperty { prop: PROP_RACK_ID, err })?
.ok_or_else(|| Error::MissingProperty { prop: PROP_RACK_ID })?
.value()
.map_err(|err| Error::GetValue { prop: PROP_RACK_ID, err })?
.ok_or(Error::MissingValue { prop: PROP_RACK_ID })?
.as_string()
.map_err(|err| Error::ValueAsString { prop: PROP_RACK_ID, err })?;

let rack_id = if prop_rack_id.as_str() == "unknown" {
None
} else {
Some(Uuid::try_parse(&prop_rack_id).map_err(|err| {
CmdError::Failure(format!(
"failed to parse `{CONFIG_PG}/{PROP_RACK_ID}` ({prop_rack_id:?}) as a UUID: {err}"
))
})?)
};

let prop_addr = config
.get_property(PROP_ADDR)
.map_err(|err| Error::GetProperty { prop: PROP_ADDR, err })?
Expand All @@ -236,7 +263,7 @@ fn read_smf_config() -> Result<ConfigProperties, CmdError> {
"no addresses specified by `{CONFIG_PG}/{PROP_ADDR}`"
)))
} else {
Ok(ConfigProperties { id: prop_id, addresses })
Ok(ConfigProperties { id: prop_id, addresses, rack_id })
}
}

Expand Down
14 changes: 13 additions & 1 deletion gateway/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,41 @@ use crate::error::StartupError;
use crate::management_switch::ManagementSwitch;
use crate::management_switch::SwitchConfig;
use gateway_sp_comms::InMemoryHostPhase2Provider;
use slog::Logger;
use slog::{info, Logger};
use std::sync::Arc;
use std::sync::OnceLock;
use uuid::Uuid;

/// Shared state used by API request handlers
pub struct ServerContext {
pub mgmt_switch: ManagementSwitch,
pub host_phase2_provider: Arc<InMemoryHostPhase2Provider>,
pub rack_id: OnceLock<Uuid>,
pub log: Logger,
}

impl ServerContext {
pub async fn new(
host_phase2_provider: Arc<InMemoryHostPhase2Provider>,
switch_config: SwitchConfig,
rack_id_config: Option<Uuid>,
log: &Logger,
) -> Result<Arc<Self>, StartupError> {
let mgmt_switch =
ManagementSwitch::new(switch_config, &host_phase2_provider, log)
.await?;

let rack_id = if let Some(id) = rack_id_config {
info!(log, "Setting rack_id"; "rack_id" => %id);
OnceLock::from(id)
} else {
OnceLock::new()
};

Ok(Arc::new(ServerContext {
mgmt_switch,
host_phase2_provider,
rack_id,
log: log.clone(),
}))
}
Expand Down
Loading

0 comments on commit f594c97

Please sign in to comment.