-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add subscription-manager fact generation #977
Changes from all commits
608d681
93079bb
caecb64
0b09728
e5ed45b
898bff0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
% bootc-status-updated.path(8) | ||
|
||
# NAME | ||
|
||
bootc-status-updated.path | ||
|
||
# DESCRIPTION | ||
|
||
This unit watches the `bootc` root directory (/ostree/bootc) for | ||
modification, and triggers the companion `bootc-status-updated.target` | ||
systemd unit. | ||
|
||
The `bootc` program updates the mtime on its root directory when the | ||
contents of `bootc status` changes as a result of an | ||
update/upgrade/edit/switch/rollback operation. | ||
|
||
# SEE ALSO | ||
|
||
**bootc**(1), **bootc-status-updated.target**(8) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
% bootc-status-updated.target(8) | ||
|
||
# NAME | ||
|
||
bootc-status-updated.target | ||
|
||
# DESCRIPTION | ||
|
||
This unit is triggered by the companion `bootc-status-updated.path` | ||
systemd unit. This target is intended to enable users to add custom | ||
services to trigger as a result of `bootc status` changing. | ||
|
||
Add the following to your unit configuration to active it when `bootc | ||
status` changes: | ||
|
||
``` | ||
[Install] | ||
WantedBy=bootc-status-updated.target | ||
``` | ||
|
||
# SEE ALSO | ||
|
||
**bootc**(1), **bootc-status-updated.path**(8) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -747,6 +747,9 @@ pub(crate) async fn rollback(sysroot: &Storage) -> Result<()> { | |
} else { | ||
println!("Next boot: rollback deployment"); | ||
} | ||
|
||
sysroot.update_mtime()?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated to this particular changed line but I think we could test this in one of the integration tests pretty easily. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking about punting this to a followup. Unless I'm blind, it doesn't look like in integration we currently test any of upgrade/edit/switch/rollback so that's a whole other thing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yeah I was just looking at the stuff under |
||
|
||
Ok(()) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,3 +42,6 @@ mod install; | |
mod kernel; | ||
#[cfg(feature = "install")] | ||
pub(crate) mod mount; | ||
|
||
#[cfg(feature = "rhsm")] | ||
mod rhsm; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
//! Integration with Red Hat Subscription Manager | ||
|
||
use anyhow::Result; | ||
use cap_std::fs::{Dir, OpenOptions}; | ||
use cap_std_ext::cap_std; | ||
use fn_error_context::context; | ||
use serde::Serialize; | ||
|
||
const FACTS_PATH: &str = "etc/rhsm/facts/bootc.json"; | ||
|
||
#[derive(Serialize, PartialEq, Eq, Debug, Default)] | ||
struct RhsmFacts { | ||
#[serde(rename = "bootc.booted.image")] | ||
booted_image: String, | ||
#[serde(rename = "bootc.booted.version")] | ||
booted_version: String, | ||
#[serde(rename = "bootc.booted.digest")] | ||
booted_digest: String, | ||
#[serde(rename = "bootc.staged.image")] | ||
staged_image: String, | ||
#[serde(rename = "bootc.staged.version")] | ||
staged_version: String, | ||
#[serde(rename = "bootc.staged.digest")] | ||
staged_digest: String, | ||
#[serde(rename = "bootc.rollback.image")] | ||
rollback_image: String, | ||
#[serde(rename = "bootc.rollback.version")] | ||
rollback_version: String, | ||
#[serde(rename = "bootc.rollback.digest")] | ||
rollback_digest: String, | ||
#[serde(rename = "bootc.available.image")] | ||
available_image: String, | ||
#[serde(rename = "bootc.available.version")] | ||
available_version: String, | ||
#[serde(rename = "bootc.available.digest")] | ||
available_digest: String, | ||
} | ||
|
||
/// Return the image reference, version and digest as owned strings. | ||
/// A missing version is serialized as the empty string. | ||
fn status_to_strings(imagestatus: &crate::spec::ImageStatus) -> (String, String, String) { | ||
let image = imagestatus.image.image.clone(); | ||
let version = imagestatus.version.as_ref().cloned().unwrap_or_default(); | ||
let digest = imagestatus.image_digest.clone(); | ||
(image, version, digest) | ||
} | ||
|
||
impl From<crate::spec::HostStatus> for RhsmFacts { | ||
jeckersb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fn from(hoststatus: crate::spec::HostStatus) -> Self { | ||
let (booted_image, booted_version, booted_digest) = hoststatus | ||
.booted | ||
.as_ref() | ||
cgwalters marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.and_then(|boot_entry| boot_entry.image.as_ref().map(status_to_strings)) | ||
.unwrap_or_default(); | ||
|
||
let (staged_image, staged_version, staged_digest) = hoststatus | ||
.staged | ||
.as_ref() | ||
.and_then(|boot_entry| boot_entry.image.as_ref().map(status_to_strings)) | ||
.unwrap_or_default(); | ||
|
||
let (rollback_image, rollback_version, rollback_digest) = hoststatus | ||
.rollback | ||
.as_ref() | ||
.and_then(|boot_entry| boot_entry.image.as_ref().map(status_to_strings)) | ||
.unwrap_or_default(); | ||
|
||
let (available_image, available_version, available_digest) = hoststatus | ||
.booted | ||
.as_ref() | ||
.and_then(|boot_entry| boot_entry.cached_update.as_ref().map(status_to_strings)) | ||
.unwrap_or_default(); | ||
|
||
Self { | ||
booted_image, | ||
booted_version, | ||
booted_digest, | ||
staged_image, | ||
staged_version, | ||
staged_digest, | ||
rollback_image, | ||
rollback_version, | ||
rollback_digest, | ||
available_image, | ||
available_version, | ||
available_digest, | ||
} | ||
} | ||
} | ||
|
||
/// Publish facts for subscription-manager consumption | ||
#[context("Publishing facts")] | ||
pub(crate) async fn publish_facts(root: &Dir) -> Result<()> { | ||
let sysroot = super::cli::get_storage().await?; | ||
let booted_deployment = sysroot.booted_deployment(); | ||
let (_deployments, host) = crate::status::get_status(&sysroot, booted_deployment.as_ref())?; | ||
|
||
let facts = RhsmFacts::from(host.status); | ||
let mut bootc_facts_file = root.open_with( | ||
FACTS_PATH, | ||
OpenOptions::new().write(true).create(true).truncate(true), | ||
)?; | ||
serde_json::to_writer_pretty(&mut bootc_facts_file, &facts)?; | ||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
use crate::spec::Host; | ||
|
||
#[test] | ||
fn test_rhsm_facts_from_host() { | ||
let host: Host = serde_yaml::from_str(include_str!("fixtures/spec-staged-booted.yaml")) | ||
.expect("No spec found"); | ||
let facts = RhsmFacts::from(host.status); | ||
|
||
assert_eq!( | ||
facts, | ||
RhsmFacts { | ||
booted_image: "quay.io/example/someimage:latest".into(), | ||
booted_version: "nightly".into(), | ||
booted_digest: | ||
"sha256:736b359467c9437c1ac915acaae952aad854e07eb4a16a94999a48af08c83c34".into(), | ||
staged_image: "quay.io/example/someimage:latest".into(), | ||
staged_version: "nightly".into(), | ||
staged_digest: | ||
"sha256:16dc2b6256b4ff0d2ec18d2dbfb06d117904010c8cf9732cdb022818cf7a7566".into(), | ||
..Default::default() | ||
} | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[Unit] | ||
Description=Publish bootc facts to Red Hat Subscription Manager | ||
Documentation=man:bootc(8) | ||
ConditionPathExists=/etc/rhsm/facts | ||
|
||
[Service] | ||
Type=oneshot | ||
ExecStart=/usr/bin/bootc internals publish-rhsm-facts | ||
|
||
[Install] | ||
WantedBy=bootc-status-updated.target |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[Unit] | ||
Description=Monitor bootc for status changes | ||
Documentation=man:bootc-status-updated.path(8) | ||
|
||
[Path] | ||
PathChanged=/ostree/bootc | ||
Unit=bootc-status-updated.target | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK as is, but we will also need to do a corresponding change to the Fedora spec file to turn it on (bcond that is default off in Fedora, on in RHEL?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR for rawhide