summaryrefslogtreecommitdiffstats
path: root/nix-script-container-kill.sh
blob: c17f0f773a1aa85e82112f29d4da604498f46fb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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"