diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2015-09-08 10:38:32 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2015-09-08 10:38:32 +0200 |
commit | 273f4bc17d4ef8db9a1af0440ee3476c08518ecd (patch) | |
tree | ce4f3f67fa35b0fbc2fa409c379e6558196a77b8 | |
parent | fdaca994716a03cf05de625a0f7085bf783af41a (diff) |
Add command for killing containers
-rwxr-xr-x | nix-script-container-kill.sh | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/nix-script-container-kill.sh b/nix-script-container-kill.sh new file mode 100755 index 0000000..c17f0f7 --- /dev/null +++ b/nix-script-container-kill.sh @@ -0,0 +1,78 @@ +#!/usr/bin/env + +source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh + +usage() { + cat <<EOS >&2 + $(help_synopsis "container" "kill [-h] [-n <container name>] [-d] [-- <name...>]") + + -n <name> | Container name (only one) + -d | Destroy the containers, too + -h | Show this help and exit + + You can pass a single name with -n <name> or pass several names after two + dashes. Everything after the dashes will be treated as container name. If + there is an invalid name, the operation is aborted. + +$(help_end) +EOS +} + +NAMES="" +[[ $(echo $* | grep "\-\-") ]] && NAMES=$(echo $* | sed -r 's,(.*)\-\-(.*),\2,') + +while getopts "n:d:h" OPTION +do + case $OPTION in + n) + [[ ! -z "$NAMES" ]] && \ + stderr "Names given. No single name allowed" && exit 1 + + NAMES=$OPTARG + dbg "NAMES = $NAMES" + ;; + + d) + DESTROY=1 + dbg "DESTROY = $DESTROY" + ;; + + h) + usage + exit 1 + ;; + esac +done + +containers=$(sudo nixos-container list) + +for name in $NAMES +do + if [[ $(echo $containers | grep $name) ]] + then + dbg "Found container: $name" + else + stderr "'$name' is not a container, aborting operation" + exit 1 + fi +done + +stdout "Shutting down containers..." +for name in $NAMES +do + stdout "Shutting down container '$name'" + explain sudo nixos-container stop $name +done +stdout "Done with shutting down containers" + +stdout "Destroying containers..." +if [[ $DESTROY -eq 1 ]] +then + for name in $NAMES + do + stdout "Destroying container '$name'" + explain sudo nixos-container destroy $name + done +fi +stdout "Done with destroying containers" + |