Skip to content
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
10 changes: 5 additions & 5 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions nexus/src/internal_api/http_entrypoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use super::params::{
};
use dropshot::endpoint;
use dropshot::ApiDescription;
use dropshot::FreeformBody;
use dropshot::HttpError;
use dropshot::HttpResponseOk;
use dropshot::HttpResponseUpdatedNoContent;
use dropshot::Path;
use dropshot::RequestContext;
use dropshot::TypedBody;
use http::{Response, StatusCode};
use hyper::Body;
use omicron_common::api::internal::nexus::DiskRuntimeState;
use omicron_common::api::internal::nexus::InstanceRuntimeState;
Expand Down Expand Up @@ -299,13 +299,13 @@ async fn cpapi_metrics_collect(
async fn cpapi_artifact_download(
request_context: Arc<RequestContext<Arc<ServerContext>>>,
path_params: Path<UpdateArtifact>,
) -> Result<Response<Body>, HttpError> {
) -> Result<HttpResponseOk<FreeformBody>, HttpError> {
let context = request_context.context();
let nexus = &context.nexus;
let opctx = OpContext::for_internal_api(&request_context).await;
// TODO: return 404 if the error we get here says that the record isn't found
let body =
nexus.download_artifact(&opctx, path_params.into_inner()).await?;

Ok(Response::builder().status(StatusCode::OK).body(body.into())?)
Ok(HttpResponseOk(Body::from(body).into()))
}
8 changes: 7 additions & 1 deletion openapi/nexus-internal.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@
}
],
"responses": {
"default": {
"200": {
"description": "",
"content": {
"*/*": {
"schema": {}
}
}
},
"4XX": {
"$ref": "#/components/responses/Error"
},
"5XX": {
"$ref": "#/components/responses/Error"
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions sled-agent/src/mocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ use nexus_client::types::{
InstanceRuntimeState, SledAgentStartupInfo, UpdateArtifactKind,
ZpoolPutRequest, ZpoolPutResponse,
};
use reqwest::Response;
use slog::Logger;
use uuid::Uuid;

type Result<T> = std::result::Result<
T,
progenitor::progenitor_client::ResponseValue<T>,
progenitor::progenitor_client::Error<nexus_client::types::Error>,
>;

Expand Down Expand Up @@ -44,7 +43,7 @@ mock! {
kind: UpdateArtifactKind,
name: &str,
version: i64,
) -> Result<Response>;
) -> Result<progenitor::progenitor_client::ByteStream>;
pub async fn zpool_put(
&self,
sled_id: &Uuid,
Expand Down
87 changes: 49 additions & 38 deletions sled-agent/src/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
//! Management of per-sled updates

use crate::nexus::NexusClient;
use futures::{TryFutureExt, TryStreamExt};
use omicron_common::api::internal::nexus::{
UpdateArtifact, UpdateArtifactKind,
};
use std::path::PathBuf;
use tokio::io::AsyncWriteExt;

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand All @@ -19,11 +21,8 @@ pub enum Error {
err: std::io::Error,
},

#[error("Failed to contact nexus: {0}")]
Nexus(anyhow::Error),

#[error("Failed to read response from Nexus: {0}")]
Response(reqwest::Error),
#[error("Failed request to Nexus: {0}")]
Response(nexus_client::Error<nexus_client::types::Error>),
}

pub async fn download_artifact(
Expand All @@ -48,34 +47,41 @@ pub async fn download_artifact(

// Fetch the artifact and write to the file in its entirety,
// replacing it if it exists.
// TODO: Would love to stream this instead.
// ALSO TODO: This is, for the moment, using the endpoint directly
// instead of using the client method to work around issues in
// dropshot/progenitor for getting raw response bodies.

let response = nexus
.client()
.get(format!(
"{}/artifacts/{}/{}/{}",
nexus.baseurl(),
artifact.kind,
artifact.name,
artifact.version
))
.send()
.cpapi_artifact_download(
nexus_client::types::UpdateArtifactKind::Zone,
&artifact.name,
artifact.version,
)
.await
.map_err(Error::Response)?;
let contents =
response.bytes().await.map_err(|e| Error::Response(e))?;
tokio::fs::write(&tmp_path, contents).await.map_err(|err| {
Error::Io {
message: format!(
"Downloading artifact to temporary path: {tmp_path:?}"
),
err,
}
})?;

// Write the file to its final path.
let mut file =
tokio::fs::File::create(&tmp_path).await.map_err(|err| {
Error::Io {
message: format!(
"create {}",
tmp_path.to_string_lossy()
),
err,
}
})?;
let mut stream = response.into_inner_stream();
while let Some(chunk) = stream
.try_next()
.await
.map_err(|e| Error::Response(e.into()))?
{
file.write_all(&chunk)
.map_err(|err| Error::Io {
message: "write_all".to_string(),
err,
})
.await?;
}

// Move the file to its final path.
let destination = directory.join(artifact.name);
tokio::fs::rename(&tmp_path, &destination).await.map_err(
|err| Error::Io {
Expand All @@ -94,13 +100,13 @@ pub async fn download_artifact(
mod test {
use super::*;
use crate::mocks::MockNexusClient;
use http::{Response, StatusCode};
use bytes::Bytes;
use http::StatusCode;
use progenitor::progenitor_client::{ByteStream, ResponseValue};
use reqwest::{header::HeaderMap, Result};

#[tokio::test]
#[serial_test::serial]
// TODO this is hard to mock out when not using the generated client
// methods :( but the logic is covered in the updates integration test
#[ignore]
async fn test_write_artifact_to_filesystem() {
// The (completely fabricated) artifact we'd like to download.
let expected_name = "test_artifact";
Expand All @@ -122,11 +128,16 @@ mod test {
assert_eq!(name, "test_artifact");
assert_eq!(version, 3);
assert_eq!(kind.to_string(), "zone");
let response = Response::builder()
.status(StatusCode::OK)
.body(expected_contents)
.unwrap();
Ok(response.into())
let response = ByteStream::new(Box::pin(
futures::stream::once(futures::future::ready(Result::Ok(
Bytes::from(expected_contents),
))),
));
Ok(ResponseValue::new(
response,
StatusCode::OK,
HeaderMap::default(),
))
},
);

Expand Down