forked from containers/bootc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
168 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use anyhow::{anyhow, Result}; | ||
use clap::ValueEnum; | ||
|
||
use ostree::glib; | ||
use ostree_ext::container::OstreeImageReference; | ||
use ostree_ext::keyfileext::KeyFileExt; | ||
use ostree_ext::ostree; | ||
use ostree_ext::sysroot::SysrootLock; | ||
|
||
use crate::spec::ImageStatus; | ||
|
||
mod ostree_container; | ||
|
||
#[derive(Default)] | ||
pub(crate) struct CachedImageStatus { | ||
pub image: Option<ImageStatus>, | ||
pub cached_update: Option<ImageStatus>, | ||
} | ||
|
||
pub(crate) trait Backend { | ||
fn backend(&self) -> Result<Box<dyn BackendImpl>>; | ||
} | ||
|
||
pub(crate) trait BackendImpl { | ||
fn imagestatus( | ||
&self, | ||
sysroot: &SysrootLock, | ||
deployment: &ostree::Deployment, | ||
image: OstreeImageReference, | ||
) -> Result<CachedImageStatus>; | ||
} | ||
|
||
impl Backend for crate::spec::Backend { | ||
fn backend<'a>(&self) -> Result<Box<dyn BackendImpl>> { | ||
match self { | ||
crate::spec::Backend::OstreeContainer => { | ||
Ok(Box::new(ostree_container::OstreeContainerBackend)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl Backend for ostree::Deployment { | ||
fn backend<'a>(&self) -> Result<Box<dyn BackendImpl>> { | ||
if let Some(origin) = self.origin().as_ref() { | ||
origin.backend() | ||
} else { | ||
Err(anyhow!("Deployment has no origin")) | ||
} | ||
} | ||
} | ||
|
||
impl Backend for &glib::KeyFile { | ||
fn backend(&self) -> Result<Box<dyn BackendImpl>> { | ||
let backend = self | ||
.optional_string("bootc", "backend")? | ||
.map(|v| crate::spec::Backend::from_str(&v, true)) | ||
.transpose() | ||
.map_err(anyhow::Error::msg)? | ||
.unwrap_or_default(); | ||
backend.backend() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use anyhow::{Context, Result}; | ||
|
||
use ostree_ext::container as ostree_container; | ||
use ostree_ext::oci_spec; | ||
use ostree_ext::oci_spec::image::ImageConfiguration; | ||
use ostree_ext::ostree; | ||
use ostree_ext::sysroot::SysrootLock; | ||
|
||
use super::CachedImageStatus; | ||
use crate::spec::{ImageReference, ImageStatus}; | ||
|
||
pub(super) struct OstreeContainerBackend; | ||
|
||
impl super::BackendImpl for OstreeContainerBackend { | ||
fn imagestatus( | ||
&self, | ||
sysroot: &SysrootLock, | ||
deployment: &ostree::Deployment, | ||
image: ostree_container::OstreeImageReference, | ||
) -> Result<CachedImageStatus> { | ||
let repo = &sysroot.repo(); | ||
let image = ImageReference::from(image); | ||
let csum = deployment.csum(); | ||
let imgstate = ostree_container::store::query_image_commit(repo, &csum)?; | ||
let cached = imgstate.cached_update.map(|cached| { | ||
create_imagestatus(image.clone(), &cached.manifest_digest, &cached.config) | ||
}); | ||
let imagestatus = | ||
create_imagestatus(image, &imgstate.manifest_digest, &imgstate.configuration); | ||
|
||
Ok(CachedImageStatus { | ||
image: Some(imagestatus), | ||
cached_update: cached, | ||
}) | ||
} | ||
} | ||
|
||
/// Convert between a subset of ostree-ext metadata and the exposed spec API. | ||
fn create_imagestatus( | ||
image: ImageReference, | ||
manifest_digest: &str, | ||
config: &ImageConfiguration, | ||
) -> ImageStatus { | ||
let labels = labels_of_config(config); | ||
let timestamp = labels | ||
.and_then(|l| { | ||
l.get(oci_spec::image::ANNOTATION_CREATED) | ||
.map(|s| s.as_str()) | ||
}) | ||
.and_then(try_deserialize_timestamp); | ||
|
||
let version = ostree_container::version_for_config(config).map(ToOwned::to_owned); | ||
ImageStatus { | ||
image, | ||
version, | ||
timestamp, | ||
image_digest: manifest_digest.to_owned(), | ||
} | ||
} | ||
|
||
fn labels_of_config( | ||
config: &oci_spec::image::ImageConfiguration, | ||
) -> Option<&std::collections::HashMap<String, String>> { | ||
config.config().as_ref().and_then(|c| c.labels().as_ref()) | ||
} | ||
|
||
fn try_deserialize_timestamp(t: &str) -> Option<chrono::DateTime<chrono::Utc>> { | ||
match chrono::DateTime::parse_from_rfc3339(t).context("Parsing timestamp") { | ||
Ok(t) => Some(t.into()), | ||
Err(e) => { | ||
tracing::warn!("Invalid timestamp in image: {:#}", e); | ||
None | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters