]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Starting with bin/get-k8s-master-configs
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 28 May 2019 11:49:08 +0000 (13:49 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 28 May 2019 11:49:08 +0000 (13:49 +0200)
bin/get-k8s-master-configs [new file with mode: 0755]

diff --git a/bin/get-k8s-master-configs b/bin/get-k8s-master-configs
new file mode 100755 (executable)
index 0000000..6bfc3e4
--- /dev/null
@@ -0,0 +1,237 @@
+#!/usr/bin/env bash
+
+export LC_ALL=C
+export LANG=C
+
+VERBOSE="n"
+DEBUG="n"
+QUIET='n'
+
+VERSION="0.1"
+
+# console colors:
+RED=""
+YELLOW=""
+GREEN=""
+BLUE=""
+NORMAL=""
+
+HAS_TTY='y'
+
+BASENAME="$(basename ${0})"
+BASE_DIR="$(dirname ${0})"
+
+REL_K8S_CFGDIR='.kube'
+REL_K8S_CFGFILE='config.yaml'
+
+#-------------------------------------------------------------------
+detect_color() {
+
+    local safe_term="${TERM//[^[:alnum:]]/?}"
+    local match_lhs=""
+    local use_color="false"
+    [[ -f ~/.dir_colors   ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
+    [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
+    [[ -z ${match_lhs}    ]] \
+        && type -P dircolors >/dev/null \
+        && match_lhs=$(dircolors --print-database)
+    [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color="true"
+
+    # console colors:
+    if [ "${use_color}" = "true" ] ; then
+        RED="\033[38;5;196m"
+        YELLOW="\033[38;5;226m"
+        GREEN="\033[38;5;46m"
+        BLUE="\033[38;5;27m"
+        NORMAL="\033[39m"
+    else
+        RED=""
+        YELLOW=""
+        GREEN=""
+        BLUE=""
+        NORMAL=""
+    fi
+
+    local my_tty=$(tty)
+    if [[ "${my_tty}" =~ 'not a tty' ]] ; then
+        my_tty='-'
+    fi
+
+    if [[ "${my_tty}" = '-' || "${safe_term}" = "dump" ]] ; then
+        HAS_TTY='n'
+    fi
+
+}
+detect_color
+
+#------------------------------------------------------------------------------
+description() {
+    echo -e $( cat <<-EOF
+               Gets the current root Kubernetes configuration files of both live
+               and stage Kubernetes of Sparkasse.
+
+               Only the user '${GREEN}root${NORMAL}' may execute this script.
+
+               EOF
+    )
+}
+
+#------------------------------------------------------------------------------
+usage() {
+    cat <<-EOF
+       Usage: ${BASENAME} [-d|--debug] [[-v|--verbose] | [-q|--quiet]]] [--nocolor]
+              ${BASENAME} [-h|--help]
+              ${BASENAME} [-V|--version]
+
+           Options:
+               -d|--debug      Debug output (bash -x).
+               -v|--verbose    Set verbosity on. Mutually exclusive to '--quiet'.
+               -q|--quiet      Quiet execution, only errors and warnings are shown.
+               --nocolor       Don't use colors on display.
+               -h|--help       Show this output and exit.
+               -V|--version    prints out version number of the script and exit
+       EOF
+}
+
+
+#------------------------------------------------------------------------------
+get_options() {
+
+    local tmp=
+    local base_dir=
+
+    set +e
+    tmp=$( getopt -o dvqhV \
+                    --long debug,verbose,quiet,nocolor,help,version \
+                    -n "${BASENAME}" -- "$@" )
+    if [[ $? != 0 ]] ; then
+        echo "" >&2
+        usage >&2
+        exit 1
+    fi
+    set -e
+
+    # Note the quotes around `$TEMP': they are essential!
+    eval set -- "${tmp}"
+
+    local p=
+
+    while true ; do
+        case "$1" in
+            -d|--debug)
+                DEBUG="y"
+                shift
+                ;;
+            -v|--verbose)
+                VERBOSE="y"
+                shift
+                ;;
+            -q|--quiet)
+                QUIET="y"
+                RED=""
+                YELLOW=""
+                GREEN=""
+                BLUE=""
+                NORMAL=""
+                shift
+                ;;
+            --nocolor)
+                RED=""
+                YELLOW=""
+                GREEN=""
+                BLUE=""
+                NORMAL=""
+                shift
+                ;;
+            -h|--help)
+                description
+                echo
+                usage
+                exit 0
+                ;;
+            -V|--version)
+                echo "${BASENAME} version: ${VERSION}"
+                exit 0
+                ;;
+            --) shift
+                break
+                ;;
+            *)  echo "Internal error!"
+                exit 1
+                ;;
+        esac
+    done
+
+    if [[ "${DEBUG}" = "y" ]] ; then
+        set -x
+    fi
+    if [[ "${VERBOSE}" == "y" && "${QUIET}" == "y" ]] ; then
+        error "The parameters '${RED}${VERBOSE}${NORMAL}' and '${RED}${VERBOSE}${NORMAL}' are mutually exclusive."
+        usage >&2
+        exit 1
+    fi
+
+    local cur_user_id=$( id -u )
+    if [[ "${cur_user_id}" != "0" ]] ; then
+        error "Wrong user '${RED}$( id -u -n )${NORMAL}'."
+        echo >&2
+        description >&2
+        echo
+        usage >&2
+        exit 1
+    fi
+
+}
+
+#########################################
+# Some often used funktions
+
+#------------------------------------------------------------------------------
+my_date() {
+    date +'%F %T.%N %:::z'
+}
+
+#------------------------------------------------------------------------------
+debug() {
+    if [[ "${VERBOSE}" != "y" ]] ; then
+        return 0
+    fi
+    echo -e " * [$(my_date)] [${BASENAME}:DEBUG]: $@"
+}
+
+#------------------------------------------------------------------------------
+info() {
+    if [[ "${QUIET}" == "y" ]] ; then
+        return
+    fi
+    echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@"
+}
+
+#------------------------------------------------------------------------------
+warn() {
+    echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $@" >&2
+}
+
+#------------------------------------------------------------------------------
+error() {
+    echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2
+}
+
+################################################################################
+##
+## Main
+##
+################################################################################
+
+#------------------------------------------------------------------------------
+main() {
+
+    get_options "$@"
+
+}
+
+main "$@"
+
+exit 0
+
+# vim: ts=4 et list