Skip to content

Commit

Permalink
switch-to-configuration: Handle unit template instances
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Aug 3, 2012
1 parent bd3c9fe commit aa6fd9f
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions modules/system/activation/switch-to-configuration.pl
Expand Up @@ -64,19 +64,19 @@ sub getActiveUnits {

# Stop all services that no longer exist or have changed in the new
# configuration.
# FIXME: handle template units (e.g. getty@.service).
my @unitsToStop;
my $active = getActiveUnits;
while (my ($unit, $state) = each %{$active}) {
my $state = $active->{$unit};
my $curUnitFile = "/etc/systemd/system/$unit";
my $baseUnit = $unit;
# Recognise template instances.
$baseUnit = "$1\@.$2" if $unit =~ /^(.*)@[^\.]*\.(.*)$/;
my $curUnitFile = "/etc/systemd/system/$baseUnit";
if (-e $curUnitFile && ($state->{state} eq "active" || $state->{state} eq "activating")) {
my $newUnitFile = "@out@/etc/systemd/system/$unit";
my $newUnitFile = "@out@/etc/systemd/system/$baseUnit";
if (! -e $newUnitFile) {
print STDERR "stopping obsolete unit ‘$unit’...\n";
push @unitsToStop, $unit;
} elsif (abs_path($curUnitFile) ne abs_path($newUnitFile)) {
print STDERR "stopping changed unit ‘$unit’...\n";
# Record that this unit needs to be started below. We
# write this to a file to ensure that the service gets
# restarted if we're interrupted.
Expand All @@ -86,6 +86,7 @@ sub getActiveUnits {
}
}

print STDERR "stopping the following units: ", join(", ", sort(@unitsToStop)), "\n";
system("@systemd@/bin/systemctl", "stop", @unitsToStop)
if scalar @unitsToStop > 0; # FIXME: ignore errors?

Expand All @@ -110,9 +111,10 @@ sub getActiveUnits {
# manually started).
my @stopped = split '\n', read_file($restartListFile, err_mode => 'quiet') // "";
if (scalar @stopped > 0) {
print STDERR "restarting the following units: ", join(" ", @stopped), "\n";
my %unique = map { $_, 1 } @stopped;
system("@systemd@/bin/systemctl", "start", keys(%unique)) == 0 or $res = 4;
my @unique = sort(keys(%unique));
print STDERR "restarting the following units: ", join(", ", @unique), "\n";
system("@systemd@/bin/systemctl", "start", @unique) == 0 or $res = 4;
unlink($restartListFile);
}

Expand All @@ -126,7 +128,7 @@ sub getActiveUnits {
push @failed, $unit if $state->{state} eq "failed";
}
if (scalar @failed > 0) {
print STDERR "warning: the following units failed: ", join(", ", @failed), "\n";
print STDERR "warning: the following units failed: ", join(", ", sort(@failed)), "\n";
foreach my $unit (@failed) {
print STDERR "\n";
system("@systemd@/bin/systemctl status '$unit' >&2");
Expand Down

0 comments on commit aa6fd9f

Please sign in to comment.