From aa6fd9f8a25fbea1f73869ddcc02fd0aea1f4e91 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 3 Aug 2012 13:47:59 -0400 Subject: [PATCH] switch-to-configuration: Handle unit template instances --- .../activation/switch-to-configuration.pl | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/modules/system/activation/switch-to-configuration.pl b/modules/system/activation/switch-to-configuration.pl index e017098a4111..4f6a2a14cd15 100644 --- a/modules/system/activation/switch-to-configuration.pl +++ b/modules/system/activation/switch-to-configuration.pl @@ -64,19 +64,19 @@ system("@systemd@/bin/systemctl", "reset-failed"); # 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. @@ -86,6 +86,7 @@ while (my ($unit, $state) = each %{$active}) { } } +print STDERR "stopping the following units: ", join(", ", sort(@unitsToStop)), "\n"; system("@systemd@/bin/systemctl", "stop", @unitsToStop) if scalar @unitsToStop > 0; # FIXME: ignore errors? @@ -110,9 +111,10 @@ system("@systemd@/bin/systemctl", "start", "default.target") == 0 or $res = 4; # 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); } @@ -126,7 +128,7 @@ while (my ($unit, $state) = each %{$active}) { 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");