Skip to content

Commit

Permalink
apply-live: Invoke systemctl daemon-reload after unit files change
Browse files Browse the repository at this point in the history
After a package undergoes a version upgrade, one that modifies a
systemd unit in /usr/etc or /usr/lib, the daemon is reloaded and the
services that require to be restarted are displayed.
  • Loading branch information
RishabhSaini authored and cgwalters committed Sep 29, 2022
1 parent 88e5904 commit 72334b9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
38 changes: 36 additions & 2 deletions rust/src/builtins/apply_live.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use anyhow::{anyhow, Result};
use clap::Parser;
use glib::Variant;
use ostree_ext::{gio, glib, ostree, prelude::*};
use std::process::Command;

#[derive(Debug, Parser)]
#[clap(name = "apply-live")]
Expand Down Expand Up @@ -94,9 +95,42 @@ pub(crate) fn applylive_finish(sysroot: &crate::ffi::OstreeSysroot) -> CxxResult
if pkgdiff.n_removed() == 0 && pkgdiff.n_modified() == 0 {
crate::ffi::output_message("Successfully updated running filesystem tree.");
} else {
let lib_diff = ostree_ext::diff::diff(
repo,
&booted_commit,
&live_state.commit.as_str(),
Some("/usr/lib/systemd/system"),
)?;

let etc_diff = ostree_ext::diff::diff(
repo,
&booted_commit,
&live_state.commit.as_str(),
Some("/usr/etc/systemd/system"),
)?;

if !lib_diff.changed_files.is_empty()
|| !etc_diff.changed_files.is_empty()
|| !lib_diff.added_files.is_empty()
|| !etc_diff.added_files.is_empty()
{
let output = Command::new("/usr/bin/systemctl")
.arg("daemon-reload")
.status()
.expect("Failed to reload systemd manager configuration");
assert!(output.success());
}
let changed: Vec<String> = lib_diff
.changed_files
.union(&etc_diff.changed_files)
.filter(|s| s.contains(".service"))
.cloned()
.collect();
crate::ffi::output_message(
"Successfully updated running filesystem tree; some services may need to be restarted.",
);
"Successfully updated running filesystem tree; Following services may need to be restarted:");
for service in changed {
crate::ffi::output_message(&format!("{}", service.strip_prefix('/').unwrap()));
}
}
Ok(())
}
30 changes: 28 additions & 2 deletions tests/kolainst/destructive/apply-live
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,24 @@ set -euo pipefail

set -x

libtest_prepare_fully_offline
libtest_enable_repover 0
cd $(mktemp -d)

case "${AUTOPKGTEST_REBOOT_MARK:-}" in
"")

libtest_prepare_fully_offline
libtest_enable_repover 0

# Verify we're offline
rpm-ostree upgrade --unchanged-exit-77 || rc=$?
assert_streq "${rc}" 77

/tmp/autopkgtest-reboot "1"
;;
"1")

rpm-ostree cleanup -pr

if rpm -q foo 2>/dev/null; then
fatal "found foo"
fi
Expand Down Expand Up @@ -141,6 +148,24 @@ assert_not_file_has_content_literal status.txt 'LiveDiff:'

echo "ok livefs reset"

# testing apply-live when changes made to systemd unit while upgrade
rpm-ostree cleanup -pr
rpm-ostree install pkgsystemd
/tmp/autopkgtest-reboot "2"
;;
"2")
pkgsystemd > pkg_version.txt
assert_file_has_content_literal pkg_version.txt '1.0-1'
rm -f pkg_version.txt
sed -i -e 's,rpm-repos/0,rpm-repos/1,' /etc/yum.repos.d/libtest.repo
rpm-ostree upgrade
rpm-ostree apply-live --allow-replacement | tee out_ap.txt
pkgsystemd > pkg_version.txt
assert_file_has_content_literal pkg_version.txt '2.0-1'
rm -f pkg_version.txt
assert_file_has_content out_ap.txt 'Successfully updated running filesystem tree; Following services may need to be restarted:'
assert_file_has_content out_ap.txt 'pkgsystemd.service'

# Validate that we can generate a local ostree commit
# that adds content, but doesn't change any packages -
# i.e. there's no package diff. This is a bit of a corner
Expand All @@ -158,4 +183,5 @@ assert_file_has_content out.txt mytestdata
echo "ok local ref without package changes"
;;
*) echo "unexpected mark: ${AUTOPKGTEST_REBOOT_MARK}"; exit 1;;

esac
19 changes: 19 additions & 0 deletions tests/kolainst/kolainst-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ build_rpm testpkg-post-infinite-loop \
post "echo entering testpkg-post-infinite-loop 1>&2; while true; do sleep 1h; done"
build_rpm testpkg-touch-run \
post "touch /{run,tmp,var/tmp}/should-not-persist"

# Will be useful in checking difference in package version while doing apply-live
build_rpm pkgsystemd \
version 1.0 \
release 1 \
build 'echo -e "[Unit]\nDescription=Testinglive apply\n\n[Service]\nExecStart=/bin/bash echo done\n\n[Install]\nWantedBy=multi-user.target" > %{name}.service' \
install "mkdir -p %{buildroot}/usr/lib/systemd/system
install %{name}.service %{buildroot}/usr/lib/systemd/system" \
files "/usr/lib/systemd/system/%{name}.service"

# Test module
build_rpm foomodular
build_module foomodular \
Expand Down Expand Up @@ -90,6 +100,15 @@ mv ${test_tmpdir}/yumrepo/* ${test_tmpdir}/rpm-repos/${repover}
repover=1
mkdir ${test_tmpdir}/rpm-repos/${repover}
build_rpm zincati version 99.99 release 4

#different package version to test apply-live
build_rpm pkgsystemd \
version 2.0 \
release 1 \
build 'echo -e "[Unit]\nDescription=Testinglive apply\n\n[Service]\nExecStart=/bin/bash echo upgradeDone\n\n[Install]\nWantedBy=multi-user.target" > %{name}.service' \
install "mkdir -p %{buildroot}/usr/lib/systemd/system
install %{name}.service %{buildroot}/usr/lib/systemd/system" \
files "/usr/lib/systemd/system/%{name}.service"
mv ${test_tmpdir}/yumrepo/* ${test_tmpdir}/rpm-repos/${repover}

# Create an empty repo when we want to test inability to find a package
Expand Down

0 comments on commit 72334b9

Please sign in to comment.