From bc27e9f00a3d3921a3322383524f9f3706d9b4c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=A4ckerlin?= Date: Sun, 7 Jan 2018 01:26:05 +0100 Subject: [PATCH] new names and new script --- deploy.sh => docker-deploy | 0 docker-prune | 136 +++++++++++++++++++++++++++++++++ template.sh => docker-template | 0 update.sh => docker-update | 0 4 files changed, 136 insertions(+) rename deploy.sh => docker-deploy (100%) create mode 100755 docker-prune rename template.sh => docker-template (100%) rename update.sh => docker-update (100%) diff --git a/deploy.sh b/docker-deploy similarity index 100% rename from deploy.sh rename to docker-deploy diff --git a/docker-prune b/docker-prune new file mode 100755 index 0000000..2e59318 --- /dev/null +++ b/docker-prune @@ -0,0 +1,136 @@ +#!/bin/bash -e + +# template for bash scripts + +# internal use only +append_msg() { + if test $# -ne 0; then + echo -en ":\e[0m \e[1m$*" + fi + echo -e "\e[0m" +} + +# write a notice +notice() { + if test $# -eq 0; then + return + fi + echo -e "\e[1m$*\e[0m" 1>&3 +} + +# write error message +error() { + echo -en "\e[1;31merror" 1>&2 + append_msg $* 1>&2 +} + +# write a warning message +warning() { + echo -en "\e[1;33mwarning" 1>&2 + append_msg $* 1>&2 +} + +# write a success message +success() { + echo -en "\e[1;32msuccess" 1>&2 + append_msg $* 1>&2 +} + +# commandline parameter evaluation +filter="until=24h" +while test $# -gt 0; do + case "$1" in + (--help|-h) less < define filter (default: $filter) + --install, -i install to daily crontab + +DESCRIPTION + +Cleanup docker. Removes all docker container that are not running, +then removes all unused docker images. + +EOF + exit;; + (--filter|-f) shift; filter="$1";; + (--install|-i) sudo cp $0 /etc/cron.daily/docker-prune; exit;; + (*) error "unknow option $1, try $0 --help"; exit 1;; + esac + if test $# -eq 0; then + error "missing parameter, try $0 --help"; exit 1 + fi + shift; +done + +# run a command, print the result and abort in case of error +# option: --no-check: ignore the result, continue in case of error +run() { + check=1 + while test $# -gt 0; do + case "$1" in + (--no-check) check=0;; + (*) break;; + esac + shift; + done + echo -en "\e[1m-> running:\e[0m $* ..." + result=$($* 2>&1) + res=$? + if test $res -ne 0; then + if test $check -eq 1; then + error "failed with return code: $res" + if test -n "$result"; then + echo "$result" + fi + exit 1 + else + warning "ignored return code: $res" + fi + else + success + fi +} + +# error handler +function traperror() { + set +x + local err=($1) # error status + local line="$2" # LINENO + local linecallfunc="$3" + local command="$4" + local funcstack="$5" + for e in ${err[@]}; do + if test -n "$e" -a "$e" != "0"; then + error "line $line - command '$command' exited with status: $e (${err[@]})" + if [ "${funcstack}" != "main" -o "$linecallfunc" != "0" ]; then + echo -n " ... error at ${funcstack} " + if [ "$linecallfunc" != "" ]; then + echo -n "called at line $linecallfunc" + fi + echo + fi + exit $e + fi + done + success + exit 0 +} + +# catch errors +trap 'traperror "$? ${PIPESTATUS[@]}" $LINENO $BASH_LINENO "$BASH_COMMAND" "${FUNCNAME[@]}" "${FUNCTION}"' ERR SIGINT INT TERM EXIT + +########################################################################################## + +if test -z "$filter"; then + run docker container prune -f + run docker image prune -a -f +else + run docker container prune -f --filter "$filter" + run docker image prune -a -f --filter "$filter" +fi diff --git a/template.sh b/docker-template similarity index 100% rename from template.sh rename to docker-template diff --git a/update.sh b/docker-update similarity index 100% rename from update.sh rename to docker-update