Skip to content

MGS: Add endpoints to update and reset SP #1617

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

Merged
merged 2 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
31 changes: 31 additions & 0 deletions gateway-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ use futures::future::Fuse;
use futures::future::FusedFuture;
use futures::prelude::*;
use gateway_client::types::SpType;
use gateway_client::types::UpdateBody;
use slog::o;
use slog::Drain;
use slog::Level;
use slog::Logger;
use std::borrow::Cow;
use std::fs;
use std::net::IpAddr;
use std::net::ToSocketAddrs;
use std::path::Path;
use std::path::PathBuf;
use std::time::Duration;
use tokio::io::AsyncReadExt;
use tokio::io::AsyncWriteExt;
Expand Down Expand Up @@ -70,6 +74,13 @@ enum Command {
},
#[clap(about = "Detach any existing serial console connection")]
Detach,
#[clap(about = "Update the SP")]
Update {
#[clap(help = "Path to the new image")]
path: PathBuf,
},
#[clap(about = "Reset the SP")]
Reset,
}

fn level_from_str(s: &str) -> Result<Level> {
Expand Down Expand Up @@ -118,6 +129,20 @@ impl Client {
}
}

async fn reset(&self) -> Result<()> {
self.inner.sp_reset(self.sp_type, self.sled).await?;
Ok(())
}

async fn update(&self, path: &Path) -> Result<()> {
let image = fs::read(path)
.with_context(|| format!("failed to read {}", path.display()))?;
self.inner
.sp_update(self.sp_type, self.sled, &UpdateBody { image })
.await?;
Ok(())
}

async fn detach(&self) -> Result<()> {
self.inner
.sp_component_serial_console_detach(
Expand Down Expand Up @@ -162,6 +187,12 @@ async fn main() -> Result<()> {
Command::Detach => {
return client.detach().await;
}
Command::Update { path } => {
return client.update(&path).await;
}
Command::Reset => {
return client.reset().await;
}
};

// TODO Much of the code from this point on is derived from propolis's CLI,
Expand Down
20 changes: 20 additions & 0 deletions gateway-sp-comms/src/communicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,26 @@ impl Communicator {
Ok(sp.state().await?)
}

/// Update a given SP.
pub async fn update(
&self,
sp: SpIdentifier,
image: &[u8],
) -> Result<(), Error> {
let port = self.id_to_port(sp)?;
let sp = self.switch.sp(port).ok_or(Error::SpAddressUnknown(sp))?;
Ok(sp.update(image).await?)
}

/// Reset a given SP.
pub async fn reset(&self, sp: SpIdentifier) -> Result<(), Error> {
let port = self.id_to_port(sp)?;
let sp = self.switch.sp(port).ok_or(Error::SpAddressUnknown(sp))?;
sp.reset_prepare().await?;
sp.reset_trigger().await?;
Ok(())
}

/// Query all online SPs.
///
/// `ignition_state` should be the state returned by a (recent) call to
Expand Down
2 changes: 2 additions & 0 deletions gateway-sp-comms/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ pub enum Error {
BadIgnitionTarget(usize),
#[error("error communicating with SP: {0}")]
SpCommunicationFailed(#[from] SpCommunicationError),
#[error("updating SP failed: {0}")]
UpdateFailed(#[from] UpdateError),
#[error("serial console is already attached")]
SerialConsoleAttached,
}
Expand Down
3 changes: 2 additions & 1 deletion gateway/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ where
| SpCommsError::Timeout { .. }
| SpCommsError::BadIgnitionTarget(_)
| SpCommsError::LocalIgnitionControllerAddressUnknown
| SpCommsError::SpCommunicationFailed(_) => {
| SpCommsError::SpCommunicationFailed(_)
| SpCommsError::UpdateFailed(_) => {
HttpError::for_internal_error(err.to_string())
}
}
Expand Down
47 changes: 45 additions & 2 deletions gateway/src/http_entrypoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ async fn sp_list(
// These errors should not be possible for the request we
// made.
SpCommsError::SpDoesNotExist(_)
| SpCommsError::SerialConsoleAttached => {
| SpCommsError::SerialConsoleAttached
| SpCommsError::UpdateFailed(_) => {
unreachable!("impossible error {}", err)
}
},
Expand Down Expand Up @@ -484,7 +485,47 @@ async fn sp_component_serial_console_detach(

// TODO: how can we make this generic enough to support any update mechanism?
#[derive(Deserialize, JsonSchema)]
struct UpdateBody {}
pub struct UpdateBody {
pub image: Vec<u8>,
}

/// Update an SP
///
/// Copies a new image to the alternate bank of the SP flash.
#[endpoint {
method = POST,
path = "/sp/{type}/{slot}/update",
}]
async fn sp_update(
rqctx: Arc<RequestContext<Arc<ServerContext>>>,
path: Path<PathSp>,
body: TypedBody<UpdateBody>,
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
let comms = &rqctx.context().sp_comms;
let sp = path.into_inner().sp;
let image = body.into_inner().image;

comms.update(sp.into(), &image).await.map_err(http_err_from_comms_err)?;

Ok(HttpResponseUpdatedNoContent {})
}

/// Reset an SP
#[endpoint {
method = POST,
path = "/sp/{type}/{slot}/reset",
}]
async fn sp_reset(
rqctx: Arc<RequestContext<Arc<ServerContext>>>,
path: Path<PathSp>,
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
let comms = &rqctx.context().sp_comms;
let sp = path.into_inner().sp;

comms.reset(sp.into()).await.map_err(http_err_from_comms_err)?;

Ok(HttpResponseUpdatedNoContent {})
}

/// Update an SP component
///
Expand Down Expand Up @@ -654,6 +695,8 @@ pub fn api() -> GatewayApiDescription {
) -> Result<(), String> {
api.register(sp_list)?;
api.register(sp_get)?;
api.register(sp_update)?;
api.register(sp_reset)?;
api.register(sp_component_list)?;
api.register(sp_component_get)?;
api.register(sp_component_serial_console_attach)?;
Expand Down
104 changes: 103 additions & 1 deletion openapi/gateway.json
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,95 @@
}
}
}
},
"/sp/{type}/{slot}/reset": {
"post": {
"summary": "Reset an SP",
"operationId": "sp_reset",
"parameters": [
{
"in": "path",
"name": "slot",
"required": true,
"schema": {
"type": "integer",
"format": "uint32",
"minimum": 0
},
"style": "simple"
},
{
"in": "path",
"name": "type",
"required": true,
"schema": {
"$ref": "#/components/schemas/SpType"
},
"style": "simple"
}
],
"responses": {
"204": {
"description": "resource updated"
},
"4XX": {
"$ref": "#/components/responses/Error"
},
"5XX": {
"$ref": "#/components/responses/Error"
}
}
}
},
"/sp/{type}/{slot}/update": {
"post": {
"summary": "Update an SP",
"description": "Copies a new image to the alternate bank of the SP flash.",
"operationId": "sp_update",
"parameters": [
{
"in": "path",
"name": "slot",
"required": true,
"schema": {
"type": "integer",
"format": "uint32",
"minimum": 0
},
"style": "simple"
},
{
"in": "path",
"name": "type",
"required": true,
"schema": {
"$ref": "#/components/schemas/SpType"
},
"style": "simple"
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/UpdateBody"
}
}
},
"required": true
},
"responses": {
"204": {
"description": "resource updated"
},
"4XX": {
"$ref": "#/components/responses/Error"
},
"5XX": {
"$ref": "#/components/responses/Error"
}
}
}
}
},
"components": {
Expand Down Expand Up @@ -937,7 +1026,20 @@
]
},
"UpdateBody": {
"type": "object"
"type": "object",
"properties": {
"image": {
"type": "array",
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0
}
}
},
"required": [
"image"
]
}
}
}
Expand Down