Fix reload for services without reload functionality.

This commit is contained in:
r-vdp 2023-06-29 17:10:39 +02:00
parent 9ddb6340d9
commit 1454525760
No known key found for this signature in database
4 changed files with 63 additions and 18 deletions

View file

@ -1,4 +1,4 @@
{ lib, pkgs, ... }: { { lib, ... }: {
config = { config = {
nixpkgs.hostPlatform = "x86_64-linux"; nixpkgs.hostPlatform = "x86_64-linux";
@ -62,7 +62,6 @@
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;
ExecReload = "${lib.getBin pkgs.coreutils}/bin/true";
}; };
wantedBy = [ "system-manager.target" ]; wantedBy = [ "system-manager.target" ];
requiredBy = lib.mkIf (ix > 5) [ "service-0.service" ]; requiredBy = lib.mkIf (ix > 5) [ "service-0.service" ];

View file

@ -89,7 +89,7 @@ pub fn activate(
wait_for_jobs( wait_for_jobs(
&service_manager, &service_manager,
&job_monitor, &job_monitor,
reload_units(&service_manager, convert_services(&services_to_reload)) reload_or_restart_units(&service_manager, convert_services(&services_to_reload))
+ start_units(&service_manager, ["system-manager.target"]), + start_units(&service_manager, ["system-manager.target"]),
&timeout, &timeout,
) )
@ -229,12 +229,15 @@ where
for_each_unit(|s| service_manager.stop_unit(s), units.as_ref(), "stopping") for_each_unit(|s| service_manager.stop_unit(s), units.as_ref(), "stopping")
} }
fn reload_units<'a, U>(service_manager: &systemd::ServiceManager, units: U) -> HashSet<JobId> fn reload_or_restart_units<'a, U>(
service_manager: &systemd::ServiceManager,
units: U,
) -> HashSet<JobId>
where where
U: AsRef<[&'a str]>, U: AsRef<[&'a str]>,
{ {
for_each_unit( for_each_unit(
|s| service_manager.reload_unit(s), |s| service_manager.reload_or_restart_unit(s),
units.as_ref(), units.as_ref(),
"reloading", "reloading",
) )

View file

@ -235,9 +235,13 @@ impl ServiceManager {
Ok(true) Ok(true)
} }
pub fn reload_unit(&self, unit_name: &str) -> Result<Job, Error> { pub fn reload_or_restart_unit(&self, unit_name: &str) -> Result<Job, Error> {
Ok(Job { Ok(Job {
path: OrgFreedesktopSystemd1Manager::reload_unit(&self.proxy, unit_name, "replace")?, path: OrgFreedesktopSystemd1Manager::reload_or_restart_unit(
&self.proxy,
unit_name,
"replace",
)?,
}) })
} }

View file

@ -6,8 +6,44 @@
let let
forEachUbuntuImage = lib.flip lib.mapAttrs' system-manager.lib.images.ubuntu.${system}; forEachUbuntuImage = lib.flip lib.mapAttrs' system-manager.lib.images.ubuntu.${system};
# To test reload and restart, we include two services, one that can be reloaded
# and one that cannot.
# The id parameter is a string that can be used to force reloading the services
# between two configs by changing their contents.
testModule = id: { lib, pkgs, ... }: {
systemd.services = {
has-reload = {
enable = true;
description = "service-reload";
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecReload = ''
${lib.getBin pkgs.coreutils}/bin/true
'';
};
wantedBy = [ "system-manager.target" ];
script = ''
echo "I can be reloaded (id: ${id})"
'';
};
has-no-reload = {
enable = true;
description = "service-no-reload";
serviceConfig.Type = "simple";
wantedBy = [ "system-manager.target" ];
script = ''
while true; do
echo "I cannot be reloaded (id: ${id})"
done
'';
};
};
};
newConfig = system-manager.lib.makeSystemConfig { newConfig = system-manager.lib.makeSystemConfig {
modules = [ modules = [
(testModule "new")
({ lib, pkgs, ... }: { ({ lib, pkgs, ... }: {
config = { config = {
nixpkgs.hostPlatform = system; nixpkgs.hostPlatform = system;
@ -22,7 +58,8 @@ let
}; };
}; };
systemd.services.new-service = { systemd.services = {
new-service = {
enable = true; enable = true;
description = "new-service"; description = "new-service";
serviceConfig = { serviceConfig = {
@ -36,6 +73,7 @@ let
''; '';
}; };
}; };
};
}) })
]; ];
}; };
@ -55,6 +93,7 @@ forEachUbuntuImage
nodes = { nodes = {
node1 = { config, ... }: { node1 = { config, ... }: {
modules = [ modules = [
(testModule "old")
../../../examples/example.nix ../../../examples/example.nix
]; ];