Skip to content

Commit

Permalink
Add container helper for listing container stats
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasbeyer committed Sep 12, 2015
1 parent 273f4bc commit 9d42d87
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions nix-script-container-stats.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env

source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh

usage() {
cat <<EOS >&2
$(help_synopsis "container" "stats [-h] [-t]")
-t | Do not print table header
-h | Show this help and exit
$(help_end "container")
EOS
}

HEADER=1

while getopts "th" OPTION
do
case $OPTION in
t)
HEADER=0
dbg "HEADER = $HEADER"
;;

h)
usage
exit 1
;;
esac
done

stdout "Listing containers"
containers=$(sudo nixos-container list)

stdout "Building table layout"
longestname=$(echo $containers | awk '{print length}' | sort -nr | head -n 1)

__FMT_IP__=" | %-15s"
__FMT_STAT__=" | %-6s"
__FMT_HKEY__=" | %-15s"

__FORMAT__="%-${longestname}s${__FMT_IP__}${__FMT_STAT__}${__FMT_HKEY__}\n"

stdout "Ready building table layout"

stdout "Starting table"
repeat_char() {
local str=$1
local num=$2
local v=$(printf "%-${num}s" "$str")
echo "${v// /$str}"
}

[[ $HEADER -eq 1 ]] && \
printf "$__FORMAT__" Name IP Status Host-Key && \
printf "$__FORMAT__" "$(repeat_char "-" $longestname)"\
"$(repeat_char "-" 15)"\
"$(repeat_char "-" 6)"\
"$(repeat_char "-" 15)"

for name in $containers
do
stdout "Calling nixos-container with"
stdout "'show-ip'"
stdout "'status'"
stdout "'show-host-key'"
stdout "in sudo now..."

ip="$(sudo nixos-container show-ip $name)"
stat="$(sudo nixos-container status $name)"
hkey="$(sudo nixos-container show-host-key $name)"

printf "$__FORMAT__" "$name" "$ip" "$stat" "$hkey"
done

stdout "Ready printing table"

0 comments on commit 9d42d87

Please sign in to comment.