|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +//! APIs to help access bundles |
| 6 | +
|
| 7 | +use crate::index::SupportBundleIndex; |
| 8 | +use anyhow::Context as _; |
| 9 | +use anyhow::Result; |
| 10 | +use async_trait::async_trait; |
| 11 | +use bytes::Buf; |
| 12 | +use bytes::Bytes; |
| 13 | +use camino::Utf8Path; |
| 14 | +use camino::Utf8PathBuf; |
| 15 | +use futures::Stream; |
| 16 | +use futures::StreamExt; |
| 17 | +use omicron_uuid_kinds::GenericUuid; |
| 18 | +use omicron_uuid_kinds::SupportBundleUuid; |
| 19 | +use std::io; |
| 20 | +use std::pin::Pin; |
| 21 | +use std::task::Context; |
| 22 | +use std::task::Poll; |
| 23 | +use tokio::io::AsyncRead; |
| 24 | +use tokio::io::ReadBuf; |
| 25 | + |
| 26 | +/// An I/O source which can read to a buffer |
| 27 | +/// |
| 28 | +/// This describes access to individual files within the bundle. |
| 29 | +pub trait FileAccessor: AsyncRead + Unpin {} |
| 30 | +impl<T: AsyncRead + Unpin + ?Sized> FileAccessor for T {} |
| 31 | + |
| 32 | +pub type BoxedFileAccessor<'a> = Box<dyn FileAccessor + 'a>; |
| 33 | + |
| 34 | +/// Describes how the support bundle's data and metadata are accessed. |
| 35 | +#[async_trait] |
| 36 | +pub trait SupportBundleAccessor { |
| 37 | + /// Access the index of a support bundle |
| 38 | + async fn get_index(&self) -> Result<SupportBundleIndex>; |
| 39 | + |
| 40 | + /// Access a file within the support bundle |
| 41 | + async fn get_file<'a>( |
| 42 | + &mut self, |
| 43 | + path: &Utf8Path, |
| 44 | + ) -> Result<BoxedFileAccessor<'a>> |
| 45 | + where |
| 46 | + Self: 'a; |
| 47 | +} |
| 48 | + |
| 49 | +pub struct StreamedFile<'a> { |
| 50 | + client: &'a nexus_client::Client, |
| 51 | + id: SupportBundleUuid, |
| 52 | + path: Utf8PathBuf, |
| 53 | + stream: Option<Pin<Box<dyn Stream<Item = reqwest::Result<Bytes>> + Send>>>, |
| 54 | + buffer: Bytes, |
| 55 | +} |
| 56 | + |
| 57 | +impl<'a> StreamedFile<'a> { |
| 58 | + fn new( |
| 59 | + client: &'a nexus_client::Client, |
| 60 | + id: SupportBundleUuid, |
| 61 | + path: Utf8PathBuf, |
| 62 | + ) -> Self { |
| 63 | + Self { client, id, path, stream: None, buffer: Bytes::new() } |
| 64 | + } |
| 65 | + |
| 66 | + // NOTE: This is a distinct method from "new", because ideally some day we could |
| 67 | + // use range requests to stream out portions of the file. |
| 68 | + // |
| 69 | + // This means that we would potentially want to restart the stream with a different position. |
| 70 | + async fn start_stream(&mut self) -> Result<()> { |
| 71 | + // TODO: Add range headers, for range requests? Though this |
| 72 | + // will require adding support to Progenitor + Nexus too. |
| 73 | + let stream = self |
| 74 | + .client |
| 75 | + .support_bundle_download_file( |
| 76 | + self.id.as_untyped_uuid(), |
| 77 | + self.path.as_str(), |
| 78 | + ) |
| 79 | + .await |
| 80 | + .with_context(|| { |
| 81 | + format!( |
| 82 | + "downloading support bundle file {}: {}", |
| 83 | + self.id, self.path |
| 84 | + ) |
| 85 | + })? |
| 86 | + .into_inner_stream(); |
| 87 | + |
| 88 | + self.stream = Some(Box::pin(stream)); |
| 89 | + Ok(()) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl AsyncRead for StreamedFile<'_> { |
| 94 | + fn poll_read( |
| 95 | + mut self: Pin<&mut Self>, |
| 96 | + cx: &mut Context<'_>, |
| 97 | + buf: &mut ReadBuf<'_>, |
| 98 | + ) -> Poll<io::Result<()>> { |
| 99 | + while self.buffer.is_empty() { |
| 100 | + match futures::ready!( |
| 101 | + self.stream |
| 102 | + .as_mut() |
| 103 | + .expect("Stream must be initialized before polling") |
| 104 | + .as_mut() |
| 105 | + .poll_next(cx) |
| 106 | + ) { |
| 107 | + Some(Ok(bytes)) => { |
| 108 | + self.buffer = bytes; |
| 109 | + } |
| 110 | + Some(Err(e)) => { |
| 111 | + return Poll::Ready(Err(io::Error::new( |
| 112 | + io::ErrorKind::Other, |
| 113 | + e, |
| 114 | + ))); |
| 115 | + } |
| 116 | + None => return Poll::Ready(Ok(())), // EOF |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + let to_copy = std::cmp::min(self.buffer.len(), buf.remaining()); |
| 121 | + buf.put_slice(&self.buffer[..to_copy]); |
| 122 | + self.buffer.advance(to_copy); |
| 123 | + |
| 124 | + Poll::Ready(Ok(())) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +/// Access to a support bundle from the internal API |
| 129 | +pub struct InternalApiAccess<'a> { |
| 130 | + client: &'a nexus_client::Client, |
| 131 | + id: SupportBundleUuid, |
| 132 | +} |
| 133 | + |
| 134 | +impl<'a> InternalApiAccess<'a> { |
| 135 | + pub fn new( |
| 136 | + client: &'a nexus_client::Client, |
| 137 | + id: SupportBundleUuid, |
| 138 | + ) -> Self { |
| 139 | + Self { client, id } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// Access for: The nexus internal API |
| 144 | +#[async_trait] |
| 145 | +impl<'c> SupportBundleAccessor for InternalApiAccess<'c> { |
| 146 | + async fn get_index(&self) -> Result<SupportBundleIndex> { |
| 147 | + let stream = self |
| 148 | + .client |
| 149 | + .support_bundle_index(self.id.as_untyped_uuid()) |
| 150 | + .await |
| 151 | + .with_context(|| { |
| 152 | + format!("downloading support bundle index {}", self.id) |
| 153 | + })? |
| 154 | + .into_inner_stream(); |
| 155 | + let s = utf8_stream_to_string(stream).await?; |
| 156 | + |
| 157 | + Ok(SupportBundleIndex::new(&s)) |
| 158 | + } |
| 159 | + |
| 160 | + async fn get_file<'a>( |
| 161 | + &mut self, |
| 162 | + path: &Utf8Path, |
| 163 | + ) -> Result<BoxedFileAccessor<'a>> |
| 164 | + where |
| 165 | + 'c: 'a, |
| 166 | + { |
| 167 | + let mut file = |
| 168 | + StreamedFile::new(self.client, self.id, path.to_path_buf()); |
| 169 | + file.start_stream() |
| 170 | + .await |
| 171 | + .with_context(|| "failed to start stream in get_file")?; |
| 172 | + Ok(Box::new(file)) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +pub struct LocalFileAccess { |
| 177 | + archive: zip::read::ZipArchive<std::fs::File>, |
| 178 | +} |
| 179 | + |
| 180 | +impl LocalFileAccess { |
| 181 | + pub fn new(path: &Utf8Path) -> Result<Self> { |
| 182 | + let file = std::fs::File::open(path)?; |
| 183 | + Ok(Self { archive: zip::read::ZipArchive::new(file)? }) |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +// Access for: Local zip files |
| 188 | +#[async_trait] |
| 189 | +impl SupportBundleAccessor for LocalFileAccess { |
| 190 | + async fn get_index(&self) -> Result<SupportBundleIndex> { |
| 191 | + let names: Vec<&str> = self.archive.file_names().collect(); |
| 192 | + let all_names = names.join("\n"); |
| 193 | + Ok(SupportBundleIndex::new(&all_names)) |
| 194 | + } |
| 195 | + |
| 196 | + async fn get_file<'a>( |
| 197 | + &mut self, |
| 198 | + path: &Utf8Path, |
| 199 | + ) -> Result<BoxedFileAccessor<'a>> { |
| 200 | + let mut file = self.archive.by_name(path.as_str())?; |
| 201 | + let mut buf = Vec::new(); |
| 202 | + std::io::copy(&mut file, &mut buf)?; |
| 203 | + |
| 204 | + Ok(Box::new(AsyncZipFile { buf, copied: 0 })) |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +// We're currently buffering the entire file into memory, mostly because dealing with the lifetime |
| 209 | +// of ZipArchive and ZipFile objects is so difficult. |
| 210 | +pub struct AsyncZipFile { |
| 211 | + buf: Vec<u8>, |
| 212 | + copied: usize, |
| 213 | +} |
| 214 | + |
| 215 | +impl AsyncRead for AsyncZipFile { |
| 216 | + fn poll_read( |
| 217 | + mut self: Pin<&mut Self>, |
| 218 | + _cx: &mut Context<'_>, |
| 219 | + buf: &mut ReadBuf<'_>, |
| 220 | + ) -> Poll<io::Result<()>> { |
| 221 | + let to_copy = |
| 222 | + std::cmp::min(self.buf.len() - self.copied, buf.remaining()); |
| 223 | + if to_copy == 0 { |
| 224 | + return Poll::Ready(Ok(())); |
| 225 | + } |
| 226 | + let src = &self.buf[self.copied..]; |
| 227 | + buf.put_slice(&src[..to_copy]); |
| 228 | + self.copied += to_copy; |
| 229 | + Poll::Ready(Ok(())) |
| 230 | + } |
| 231 | +} |
| 232 | + |
| 233 | +async fn utf8_stream_to_string( |
| 234 | + mut stream: impl futures::Stream<Item = reqwest::Result<bytes::Bytes>> |
| 235 | + + std::marker::Unpin, |
| 236 | +) -> Result<String> { |
| 237 | + let mut bytes = Vec::new(); |
| 238 | + while let Some(chunk) = stream.next().await { |
| 239 | + let chunk = chunk?; |
| 240 | + bytes.extend_from_slice(&chunk); |
| 241 | + } |
| 242 | + Ok(String::from_utf8(bytes)?) |
| 243 | +} |
0 commit comments