Skip to content

Commit

Permalink
Merge pull request #17130 from srp/nixos-container-kill
Browse files Browse the repository at this point in the history
nixos-container: add 'kill' command, 'destroy' to use 'kill'
  • Loading branch information
edolstra authored Jul 22, 2016
2 parents ad941c1 + 60c6c7b commit 390b49a
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions pkgs/tools/virtualization/nixos-container/nixos-container.pl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ sub showHelp {
nixos-container destroy <container-name>
nixos-container start <container-name>
nixos-container stop <container-name>
nixos-container kill <container-name> [--signal <signal-specifier>]
nixos-container status <container-name>
nixos-container update <container-name> [--config <string>]
nixos-container login <container-name>
Expand All @@ -37,13 +38,15 @@ sub showHelp {
my $ensureUniqueName = 0;
my $autoStart = 0;
my $extraConfig;
my $signal;

GetOptions(
"help" => sub { showHelp() },
"ensure-unique-name" => \$ensureUniqueName,
"auto-start" => \$autoStart,
"system-path=s" => \$systemPath,
"config=s" => \$extraConfig
"config=s" => \$extraConfig,
"signal=s" => \$signal
) or exit 1;

my $action = $ARGV[0] or die "$0: no action specified\n";
Expand Down Expand Up @@ -186,6 +189,14 @@ sub isContainerRunning {
return $status =~ /ActiveState=active/;
}

sub killContainer {
my @args = ();
push(@args, ("--signal", $signal)) if ($signal ne "");

system("machinectl", "kill", $containerName, @args) == 0
or die "$0: failed to kill container\n";
}

sub stopContainer {
system("systemctl", "stop", "container\@$containerName") == 0
or die "$0: failed to stop container\n";
Expand Down Expand Up @@ -228,7 +239,8 @@ sub safeRemoveTree {
die "$0: cannot destroy declarative container (remove it from your configuration.nix instead)\n"
unless POSIX::access($confFile, &POSIX::W_OK);

stopContainer if isContainerRunning;
$signal = "SIGKILL";
killContainer if (isContainerRunning);

safeRemoveTree($profileDir) if -e $profileDir;
safeRemoveTree($gcRootsDir) if -e $gcRootsDir;
Expand All @@ -245,6 +257,10 @@ sub safeRemoveTree {
stopContainer;
}

elsif ($action eq "kill") {
killContainer;
}

elsif ($action eq "status") {
print isContainerRunning() ? "up" : "down", "\n";
}
Expand Down

0 comments on commit 390b49a

Please sign in to comment.