mirror of
https://codeberg.org/Toasterson/solstice-ci.git
synced 2026-04-10 13:20:41 +00:00
Add local file:// source support for orchestrator image preparation
This commit enhances image handling in the orchestrator by adding support for `file://` sources. It introduces logic to handle both local file copying and decompression options, complementing the existing `http(s)://` download functionality.
This commit is contained in:
parent
9597bbf64d
commit
c1380b1095
2 changed files with 68 additions and 37 deletions
|
|
@ -100,8 +100,9 @@ fn default_example_path() -> PathBuf {
|
|||
PathBuf::from("examples/orchestrator-image-map.yaml")
|
||||
}
|
||||
|
||||
/// Ensure images referenced in config exist at local_path. If missing, download
|
||||
/// from `source` and optionally decompress according to `decompress`.
|
||||
/// Ensure images referenced in config exist at local_path. If missing, fetch
|
||||
/// from `source` (supports http(s):// and file://) and optionally decompress
|
||||
/// according to `decompress`.
|
||||
pub async fn ensure_images(cfg: &OrchestratorConfig) -> Result<()> {
|
||||
for (label, image) in cfg.images.iter() {
|
||||
if image.local_path.exists() {
|
||||
|
|
@ -111,46 +112,75 @@ pub async fn ensure_images(cfg: &OrchestratorConfig) -> Result<()> {
|
|||
if let Some(parent) = image.local_path.parent() {
|
||||
tokio::fs::create_dir_all(parent).await.into_diagnostic()?;
|
||||
}
|
||||
// Download to temporary file
|
||||
let tmp_path = image.local_path.with_extension("part");
|
||||
tracing::info!(label = %label, url = %image.source, local = ?image.local_path, "downloading base image");
|
||||
let resp = reqwest::get(&image.source).await.into_diagnostic()?;
|
||||
let status = resp.status();
|
||||
if !status.is_success() {
|
||||
miette::bail!(
|
||||
"failed to download {url}: {status}",
|
||||
url = image.source,
|
||||
status = status
|
||||
);
|
||||
}
|
||||
let bytes = resp.bytes().await.into_diagnostic()?;
|
||||
tokio::fs::write(&tmp_path, &bytes)
|
||||
.await
|
||||
.into_diagnostic()?;
|
||||
|
||||
// Decompress or move into place
|
||||
match image.decompress.unwrap_or(Decompress::None) {
|
||||
Decompress::None => {
|
||||
tokio::fs::rename(&tmp_path, &image.local_path)
|
||||
let source = image.source.as_str();
|
||||
let is_file = source.starts_with("file://");
|
||||
let tmp_path = image.local_path.with_extension("part");
|
||||
|
||||
if is_file {
|
||||
// Local file source: copy or decompress from local path
|
||||
let src_path = PathBuf::from(&source[7..]); // naive parse; paths should be absolute
|
||||
tracing::info!(label = %label, src = ?src_path, local = ?image.local_path, "preparing base image from file:// source");
|
||||
|
||||
match image.decompress.unwrap_or(Decompress::None) {
|
||||
Decompress::None => {
|
||||
// Copy to temporary then atomically move into place
|
||||
tokio::fs::copy(&src_path, &tmp_path).await.into_diagnostic()?;
|
||||
tokio::fs::rename(&tmp_path, &image.local_path).await.into_diagnostic()?;
|
||||
}
|
||||
Decompress::Zstd => {
|
||||
let src = src_path.clone();
|
||||
let tmp_out = tmp_path.clone();
|
||||
task::spawn_blocking(move || -> miette::Result<()> {
|
||||
let infile = fs::File::open(&src).into_diagnostic()?;
|
||||
let mut decoder = zstd::stream::read::Decoder::new(infile).into_diagnostic()?;
|
||||
let mut outfile = fs::File::create(&tmp_out).into_diagnostic()?;
|
||||
std::io::copy(&mut decoder, &mut outfile).into_diagnostic()?;
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
.into_diagnostic()?;
|
||||
.into_diagnostic()??;
|
||||
tokio::fs::rename(&tmp_path, &image.local_path).await.into_diagnostic()?;
|
||||
}
|
||||
}
|
||||
Decompress::Zstd => {
|
||||
let src = tmp_path.clone();
|
||||
let dst = image.local_path.clone();
|
||||
task::spawn_blocking(move || -> miette::Result<()> {
|
||||
let infile = fs::File::open(&src).into_diagnostic()?;
|
||||
let mut decoder = zstd::stream::read::Decoder::new(infile).into_diagnostic()?;
|
||||
let mut outfile = fs::File::create(&dst).into_diagnostic()?;
|
||||
std::io::copy(&mut decoder, &mut outfile).into_diagnostic()?;
|
||||
// remove compressed temp
|
||||
std::fs::remove_file(&src).ok();
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
.into_diagnostic()??;
|
||||
} else {
|
||||
// Remote URL (HTTP/HTTPS): download to temporary file first
|
||||
tracing::info!(label = %label, url = %image.source, local = ?image.local_path, "downloading base image");
|
||||
let resp = reqwest::get(&image.source).await.into_diagnostic()?;
|
||||
let status = resp.status();
|
||||
if !status.is_success() {
|
||||
miette::bail!(
|
||||
"failed to download {url}: {status}",
|
||||
url = image.source,
|
||||
status = status
|
||||
);
|
||||
}
|
||||
let bytes = resp.bytes().await.into_diagnostic()?;
|
||||
tokio::fs::write(&tmp_path, &bytes).await.into_diagnostic()?;
|
||||
|
||||
// Decompress or move into place
|
||||
match image.decompress.unwrap_or(Decompress::None) {
|
||||
Decompress::None => {
|
||||
tokio::fs::rename(&tmp_path, &image.local_path).await.into_diagnostic()?;
|
||||
}
|
||||
Decompress::Zstd => {
|
||||
let src = tmp_path.clone();
|
||||
let dst = image.local_path.clone();
|
||||
task::spawn_blocking(move || -> miette::Result<()> {
|
||||
let infile = fs::File::open(&src).into_diagnostic()?;
|
||||
let mut decoder = zstd::stream::read::Decoder::new(infile).into_diagnostic()?;
|
||||
let mut outfile = fs::File::create(&dst).into_diagnostic()?;
|
||||
std::io::copy(&mut decoder, &mut outfile).into_diagnostic()?;
|
||||
// remove compressed temp
|
||||
std::fs::remove_file(&src).ok();
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
.into_diagnostic()??;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tracing::info!(label = %label, local = ?image.local_path, "image ready");
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ images:
|
|||
openindiana-hipster:
|
||||
# All images are backend-agnostic and must support NoCloud. Backends are chosen by host.
|
||||
source: https://dlc.openindiana.org/isos/hipster/20250402/OI-hipster-cloudimage.img.zstd
|
||||
#source: file:///home/toasty/ws/illumos/installer/cloudimage-ttya-openindiana-hipster.raw.zst
|
||||
# Local path (raw .img) target after download/decompression. Adjust per host.
|
||||
local_path: /var/lib/solstice/images/openindiana-hipster.img
|
||||
decompress: zstd # if omitted, assumed already uncompressed raw or qcow2
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue