summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2015-09-12 20:03:07 +0200
committerMatthias Beyer <mail@beyermatthias.de>2015-09-12 20:03:07 +0200
commitba04a4b03ffef129f85e24e28ca403f6086e4a92 (patch)
tree7b32cef1ec5d96ab7f24e47a7f49549cf7c3cc06
parent4701a8aa55b1923a3b49bc6f892338208f7f68fa (diff)
parent9d42d87e9f8bea1cbe7939ba94c3b594aeb425d1 (diff)
Merge pull request #74 from matthiasbeyer/add-container-helpers
Add container helpers
-rw-r--r--example.rc8
-rwxr-xr-xnix-script-container-kill.sh78
-rwxr-xr-xnix-script-container-setup.sh85
-rwxr-xr-xnix-script-container-stats.sh78
-rwxr-xr-xnix-script-container.sh50
-rw-r--r--nix-utils.sh10
6 files changed, 309 insertions, 0 deletions
diff --git a/example.rc b/example.rc
index 03776b7..272312f 100644
--- a/example.rc
+++ b/example.rc
@@ -34,3 +34,11 @@ RC_SWITCH_DEFAULT_TAG_FLAGS="-a"
#
RC_SWITCH_DEFAULT_TAG_FLAGS_NIXPKGS=""
+#
+# Container configuration templates can be put in single files and put in a
+# directory which should be configured here.
+#
+# The file names must end with .template.nix
+#
+RC_CONTAINER_CONF_TEMPLATE_DIR=~/.nixos-scripts-container-conf-templates/
+
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"
+
diff --git a/nix-script-container-setup.sh b/nix-script-container-setup.sh
new file mode 100755
index 0000000..7c0a6d9
--- /dev/null
+++ b/nix-script-container-setup.sh
@@ -0,0 +1,85 @@
+#!/usr/bin/env
+
+source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh
+
+usage() {
+ cat <<EOS >&2
+ $(help_synopsis "container" "setup [-h] -n <container name> [-e] [-d <dir>] [-t <template>]")
+
+ -n <name> | Container name
+ -e | Do not edit the configuration.nix of the container
+ -t <template> | Use this configuration template
+ -d <dir> | Use this configuration template dir, default: $RC_CONTAINER_CONF_TEMPLATE_DIR
+ -h | Show this help and exit
+
+$(help_end)
+EOS
+}
+
+DO_EDIT=0 # 0 == true
+TEMPLATE=
+TEMPLATE_DIR=$RC_CONTAINER_CONF_TEMPLATE_DIR
+
+while getopts "n:et:d:h" OPTION
+do
+ case $OPTION in
+ n)
+ NAME=$OPTARG
+ dbg "NAME = $NAME"
+ ;;
+
+ e)
+ DO_EDIT=1 # 1 == false
+ dbg "DO_NOT_EDIT = $DO_NOT_EDIT"
+ ;;
+
+ t)
+ TEMPLATE=$OPTARG
+ dbg "TEMPLATE = $TEMPLATE"
+ ;;
+
+ d)
+ TEMPLATE_DIR=$OPTARG
+ dbg "TEMPLATE_DIR = $TEMPLATE_DIR"
+ ;;
+
+ h)
+ usage
+ exit 0
+ ;;
+ *)
+ ;;
+ esac
+done
+
+[[ -z "$NAME" ]] && stderr "No container name given" && exit 1
+[[ -z "$TEMPLATE_DIR" && ! -z "$TEMPLATE" ]] && \
+ stderr "No container config template dir path given" && exit 1
+
+stdout "Creating container '$NAME'"
+explain sudo nixos-container create $NAME
+stdout "Creating container '$NAME': done"
+
+if [[ ! -z "$TEMPLATE_DIR" && ! -z "$TEMPLATE" ]]
+then
+ stdout "Looking for template in template dir"
+
+ __template__="${TEMPLATE_DIR}/${TEMPLATE}.template.nix"
+ if [[ -e "$__template__" ]]
+ then
+ explain sudo cp $__template__ $(container_conf_path $NAME)
+ else
+ stderr "'$__template__' does not exist. Exiting"
+ exit 1
+ fi
+else
+ stderr "No template dir, no template specified."
+ stderr "Won't change configuration.nix automatically"
+fi
+
+[[ $DO_EDIT ]] && explain sudo $EDITOR $(container_conf_path $NAME)
+
+stdout "Starting container '$NAME'"
+explain sudo nixos-container start $NAME
+stdout "Starting container '$NAME': done"
+
diff --git a/nix-script-container-stats.sh b/nix-script-container-stats.sh
new file mode 100755
index 0000000..3754110
--- /dev/null
+++ b/nix-script-container-stats.sh
@@ -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"
+
diff --git a/nix-script-container.sh b/nix-script-container.sh
new file mode 100755
index 0000000..14e6241
--- /dev/null
+++ b/nix-script-container.sh
@@ -0,0 +1,50 @@
+#!/usr/bin/env bash
+
+# Level-2 wrapper to be able to
+#
+# nix-script container <command>
+
+source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh
+
+export VERBOSE
+
+usage() {
+ cat <<EOS >&2
+ $(help_synopsis "${BASH_SOURCE[0]}" "[-h] [-l] <command>")
+
+ -l List all available commands
+ -h Show this help and exit
+
+$(help_end "container")
+EOS
+}
+
+while getopts "hl" OPTION
+do
+ case $OPTION in
+ h)
+ usage
+ exit 0
+ ;;
+
+ l)
+ caller_util_list_subcommands_for "nix-script-container"
+ exit 0
+ ;;
+ *)
+ ;;
+ esac
+done
+
+[[ -z "$1" ]] && stderr "No command given" && usage && exit 1
+
+SCRIPT=$(caller_util_get_script "nix-script-container" "$1")
+[[ -z "$SCRIPT" ]] && exit 1
+stdout "SCRIPT = $SCRIPT"
+
+stdout "Parsing args for '$1'"
+SCRIPT_ARGS=$(echo $* | sed -r "s/(.*)$1(.*)/\2/")
+
+stdout "Calling: '$SCRIPT $SCRIPT_ARGS'"
+RC_CONFIG=$RC_CONFIG RC_NIXPKGS=$RC_NIXPKGS exec bash $SCRIPT $SCRIPT_ARGS
+
diff --git a/nix-utils.sh b/nix-utils.sh
index 4028004..a289018 100644
--- a/nix-utils.sh
+++ b/nix-utils.sh
@@ -168,3 +168,13 @@ caller_util_get_script() {
echo "$SCRIPT"
}
+
+#
+# Container helper functions
+#
+
+# get the configuration.nix path for the container by name
+container_conf_path() {
+ echo /var/lib/containers/$1/etc/nixos/configuration.nix
+}
+