Skip to content

Commit

Permalink
cli: Add container import --write-ref option
Browse files Browse the repository at this point in the history
This will be needed for coreos/fedora-coreos-tracker#828
so coreos-assembler can find the commit it imported.
  • Loading branch information
cgwalters committed May 27, 2021
1 parent 5da9b91 commit 64428ce
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ enum ContainerOpts {

/// Image reference, e.g. registry:quay.io/exampleos/exampleos:latest
imgref: String,

/// Create an ostree ref pointing to the imported commit
#[structopt(long)]
write_ref: Option<String>,
},

/// Print information about an exported ostree-container image.
Expand Down Expand Up @@ -139,7 +143,7 @@ fn tar_export(opts: &ExportOpts) -> Result<()> {
}

/// Import a container image with an encapsulated ostree commit.
async fn container_import(repo: &str, imgref: &str) -> Result<()> {
async fn container_import(repo: &str, imgref: &str, write_ref: Option<&str>) -> Result<()> {
let repo = &ostree::Repo::open_at(libc::AT_FDCWD, repo, gio::NONE_CANCELLABLE)?;
let imgref = imgref.try_into()?;
let (tx_progress, rx_progress) = tokio::sync::watch::channel(Default::default());
Expand All @@ -153,19 +157,32 @@ async fn container_import(repo: &str, imgref: &str) -> Result<()> {
let import = crate::container::import(repo, &imgref, Some(tx_progress));
tokio::pin!(import);
tokio::pin!(rx_progress);
loop {
let import = loop {
tokio::select! {
_ = rx_progress.changed() => {
let n = rx_progress.borrow().processed_bytes;
pb.set_message(&format!("Processed: {}", indicatif::HumanBytes(n)));
}
import = &mut import => {
pb.finish();
println!("Imported: {}", import?.ostree_commit);
return Ok(())
break import?;
}
}
};

if let Some(write_ref) = write_ref {
repo.set_ref_immediate(
None,
write_ref,
Some(import.ostree_commit.as_str()),
gio::NONE_CANCELLABLE,
)?;
println!("Imported: {} => {}", write_ref, import.ostree_commit.as_str());
} else {
println!("Imported: {}", import.ostree_commit);
}

Ok(())
}

/// Export a container image with an encapsulated ostree commit.
Expand Down Expand Up @@ -216,8 +233,8 @@ where
Opt::Tar(TarOpts::Import(ref opt)) => tar_import(opt).await,
Opt::Tar(TarOpts::Export(ref opt)) => tar_export(opt),
Opt::Container(ContainerOpts::Info { imgref }) => container_info(imgref.as_str()).await,
Opt::Container(ContainerOpts::Import { repo, imgref }) => {
container_import(&repo, &imgref).await
Opt::Container(ContainerOpts::Import { repo, imgref, write_ref }) => {
container_import(&repo, &imgref, write_ref.as_deref()).await
}
Opt::Container(ContainerOpts::Export { repo, rev, imgref }) => {
container_export(&repo, &rev, &imgref).await
Expand Down

0 comments on commit 64428ce

Please sign in to comment.