Skip to content

Commit 64428ce

Browse files
committed
cli: Add container import --write-ref option
This will be needed for coreos/fedora-coreos-tracker#828 so coreos-assembler can find the commit it imported.
1 parent 5da9b91 commit 64428ce

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

lib/src/cli.rs

+23-6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ enum ContainerOpts {
6565

6666
/// Image reference, e.g. registry:quay.io/exampleos/exampleos:latest
6767
imgref: String,
68+
69+
/// Create an ostree ref pointing to the imported commit
70+
#[structopt(long)]
71+
write_ref: Option<String>,
6872
},
6973

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

141145
/// Import a container image with an encapsulated ostree commit.
142-
async fn container_import(repo: &str, imgref: &str) -> Result<()> {
146+
async fn container_import(repo: &str, imgref: &str, write_ref: Option<&str>) -> Result<()> {
143147
let repo = &ostree::Repo::open_at(libc::AT_FDCWD, repo, gio::NONE_CANCELLABLE)?;
144148
let imgref = imgref.try_into()?;
145149
let (tx_progress, rx_progress) = tokio::sync::watch::channel(Default::default());
@@ -153,19 +157,32 @@ async fn container_import(repo: &str, imgref: &str) -> Result<()> {
153157
let import = crate::container::import(repo, &imgref, Some(tx_progress));
154158
tokio::pin!(import);
155159
tokio::pin!(rx_progress);
156-
loop {
160+
let import = loop {
157161
tokio::select! {
158162
_ = rx_progress.changed() => {
159163
let n = rx_progress.borrow().processed_bytes;
160164
pb.set_message(&format!("Processed: {}", indicatif::HumanBytes(n)));
161165
}
162166
import = &mut import => {
163167
pb.finish();
164-
println!("Imported: {}", import?.ostree_commit);
165-
return Ok(())
168+
break import?;
166169
}
167170
}
171+
};
172+
173+
if let Some(write_ref) = write_ref {
174+
repo.set_ref_immediate(
175+
None,
176+
write_ref,
177+
Some(import.ostree_commit.as_str()),
178+
gio::NONE_CANCELLABLE,
179+
)?;
180+
println!("Imported: {} => {}", write_ref, import.ostree_commit.as_str());
181+
} else {
182+
println!("Imported: {}", import.ostree_commit);
168183
}
184+
185+
Ok(())
169186
}
170187

171188
/// Export a container image with an encapsulated ostree commit.
@@ -216,8 +233,8 @@ where
216233
Opt::Tar(TarOpts::Import(ref opt)) => tar_import(opt).await,
217234
Opt::Tar(TarOpts::Export(ref opt)) => tar_export(opt),
218235
Opt::Container(ContainerOpts::Info { imgref }) => container_info(imgref.as_str()).await,
219-
Opt::Container(ContainerOpts::Import { repo, imgref }) => {
220-
container_import(&repo, &imgref).await
236+
Opt::Container(ContainerOpts::Import { repo, imgref, write_ref }) => {
237+
container_import(&repo, &imgref, write_ref.as_deref()).await
221238
}
222239
Opt::Container(ContainerOpts::Export { repo, rev, imgref }) => {
223240
container_export(&repo, &rev, &imgref).await

0 commit comments

Comments
 (0)