Skip to content

Commit f7036d5

Browse files
committed
Move pull code into deploy
Prep for a podman backend; also we should thin out the logic in `cli.rs`. Signed-off-by: Colin Walters <[email protected]>
1 parent e626cad commit f7036d5

File tree

2 files changed

+56
-57
lines changed

2 files changed

+56
-57
lines changed

lib/src/cli.rs

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ use camino::Utf8PathBuf;
77
use clap::Parser;
88
use fn_error_context::context;
99
use ostree::gio;
10-
use ostree_container::store::LayeredImageState;
1110
use ostree_container::store::PrepareResult;
12-
use ostree_container::OstreeImageReference;
1311
use ostree_ext::container as ostree_container;
1412
use ostree_ext::container::SignatureSource;
1513
use ostree_ext::keyfileext::KeyFileExt;
@@ -214,57 +212,6 @@ pub(crate) async fn get_locked_sysroot() -> Result<ostree_ext::sysroot::SysrootL
214212
Ok(sysroot)
215213
}
216214

217-
/// Wrapper for pulling a container image, wiring up status output.
218-
async fn new_importer(
219-
repo: &ostree::Repo,
220-
imgref: &ostree_container::OstreeImageReference,
221-
) -> Result<ostree_container::store::ImageImporter> {
222-
let config = Default::default();
223-
let mut imp = ostree_container::store::ImageImporter::new(repo, imgref, config).await?;
224-
imp.require_bootable();
225-
Ok(imp)
226-
}
227-
228-
/// Wrapper for pulling a container image, wiring up status output.
229-
#[context("Pulling")]
230-
async fn pull(
231-
repo: &ostree::Repo,
232-
imgref: &ImageReference,
233-
quiet: bool,
234-
) -> Result<Box<LayeredImageState>> {
235-
let imgref = &OstreeImageReference::from(imgref.clone());
236-
let mut imp = new_importer(repo, imgref).await?;
237-
let prep = match imp.prepare().await? {
238-
PrepareResult::AlreadyPresent(c) => {
239-
println!("No changes in {} => {}", imgref, c.manifest_digest);
240-
return Ok(c);
241-
}
242-
PrepareResult::Ready(p) => p,
243-
};
244-
if let Some(warning) = prep.deprecated_warning() {
245-
ostree_ext::cli::print_deprecated_warning(warning).await;
246-
}
247-
ostree_ext::cli::print_layer_status(&prep);
248-
let printer = (!quiet).then(|| {
249-
let layer_progress = imp.request_progress();
250-
let layer_byte_progress = imp.request_layer_progress();
251-
tokio::task::spawn(async move {
252-
ostree_ext::cli::handle_layer_progress_print(layer_progress, layer_byte_progress).await
253-
})
254-
});
255-
let import = imp.import(prep).await;
256-
if let Some(printer) = printer {
257-
let _ = printer.await;
258-
}
259-
let import = import?;
260-
if let Some(msg) =
261-
ostree_container::store::image_filtered_content_warning(repo, &imgref.imgref)?
262-
{
263-
eprintln!("{msg}")
264-
}
265-
Ok(import)
266-
}
267-
268215
#[context("Querying root privilege")]
269216
pub(crate) fn require_root() -> Result<()> {
270217
let uid = rustix::process::getuid();
@@ -327,7 +274,7 @@ async fn upgrade(opts: UpgradeOpts) -> Result<()> {
327274
let mut changed = false;
328275
if opts.check {
329276
let imgref = imgref.clone().into();
330-
let mut imp = new_importer(repo, &imgref).await?;
277+
let mut imp = crate::deploy::new_importer(repo, &imgref).await?;
331278
match imp.prepare().await? {
332279
PrepareResult::AlreadyPresent(_) => {
333280
println!("No changes in: {}", imgref);
@@ -347,7 +294,7 @@ async fn upgrade(opts: UpgradeOpts) -> Result<()> {
347294
}
348295
}
349296
} else {
350-
let fetched = pull(&sysroot.repo(), imgref, opts.quiet).await?;
297+
let fetched = crate::deploy::pull(&sysroot.repo(), imgref, opts.quiet).await?;
351298
let staged_digest = staged_image.as_ref().map(|s| s.image_digest.as_str());
352299
let fetched_digest = fetched.manifest_digest.as_str();
353300
tracing::debug!("staged: {staged_digest:?}");
@@ -424,7 +371,7 @@ async fn switch(opts: SwitchOpts) -> Result<()> {
424371
}
425372
let new_spec = RequiredHostSpec::from_spec(&new_spec)?;
426373

427-
let fetched = pull(repo, &target, opts.quiet).await?;
374+
let fetched = crate::deploy::pull(repo, &target, opts.quiet).await?;
428375

429376
if !opts.retain {
430377
// By default, we prune the previous ostree ref so it will go away after later upgrades
@@ -467,7 +414,7 @@ async fn edit(opts: EditOpts) -> Result<()> {
467414
return Ok(());
468415
}
469416
let new_spec = RequiredHostSpec::from_spec(&new_host.spec)?;
470-
let fetched = pull(repo, new_spec.image, opts.quiet).await?;
417+
let fetched = crate::deploy::pull(repo, new_spec.image, opts.quiet).await?;
471418

472419
// TODO gc old layers here
473420

lib/src/deploy.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use ostree::{gio, glib};
99
use ostree_container::store::LayeredImageState;
1010
use ostree_container::OstreeImageReference;
1111
use ostree_ext::container as ostree_container;
12+
use ostree_ext::container::store::PrepareResult;
1213
use ostree_ext::ostree;
1314
use ostree_ext::ostree::Deployment;
1415
use ostree_ext::sysroot::SysrootLock;
@@ -39,6 +40,57 @@ impl<'a> RequiredHostSpec<'a> {
3940
}
4041
}
4142

43+
/// Wrapper for pulling a container image, wiring up status output.
44+
pub(crate) async fn new_importer(
45+
repo: &ostree::Repo,
46+
imgref: &ostree_container::OstreeImageReference,
47+
) -> Result<ostree_container::store::ImageImporter> {
48+
let config = Default::default();
49+
let mut imp = ostree_container::store::ImageImporter::new(repo, imgref, config).await?;
50+
imp.require_bootable();
51+
Ok(imp)
52+
}
53+
54+
/// Wrapper for pulling a container image, wiring up status output.
55+
#[context("Pulling")]
56+
pub(crate) async fn pull(
57+
repo: &ostree::Repo,
58+
imgref: &ImageReference,
59+
quiet: bool,
60+
) -> Result<Box<LayeredImageState>> {
61+
let imgref = &OstreeImageReference::from(imgref.clone());
62+
let mut imp = new_importer(repo, imgref).await?;
63+
let prep = match imp.prepare().await? {
64+
PrepareResult::AlreadyPresent(c) => {
65+
println!("No changes in {} => {}", imgref, c.manifest_digest);
66+
return Ok(c);
67+
}
68+
PrepareResult::Ready(p) => p,
69+
};
70+
if let Some(warning) = prep.deprecated_warning() {
71+
ostree_ext::cli::print_deprecated_warning(warning).await;
72+
}
73+
ostree_ext::cli::print_layer_status(&prep);
74+
let printer = (!quiet).then(|| {
75+
let layer_progress = imp.request_progress();
76+
let layer_byte_progress = imp.request_layer_progress();
77+
tokio::task::spawn(async move {
78+
ostree_ext::cli::handle_layer_progress_print(layer_progress, layer_byte_progress).await
79+
})
80+
});
81+
let import = imp.import(prep).await;
82+
if let Some(printer) = printer {
83+
let _ = printer.await;
84+
}
85+
let import = import?;
86+
if let Some(msg) =
87+
ostree_container::store::image_filtered_content_warning(repo, &imgref.imgref)?
88+
{
89+
eprintln!("{msg}")
90+
}
91+
Ok(import)
92+
}
93+
4294
pub(crate) async fn cleanup(sysroot: &SysrootLock) -> Result<()> {
4395
let repo = sysroot.repo();
4496
let sysroot = sysroot.sysroot.clone();

0 commit comments

Comments
 (0)