]> Frank Brehm's Git Trees - pixelpark/puppet-tools.git/commitdiff
Adding scripts/functions.rc
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 3 Feb 2023 12:33:41 +0000 (13:33 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 3 Feb 2023 12:33:41 +0000 (13:33 +0100)
scripts/functions.rc [new file with mode: 0644]

diff --git a/scripts/functions.rc b/scripts/functions.rc
new file mode 100644 (file)
index 0000000..daf6ab2
--- /dev/null
@@ -0,0 +1,568 @@
+#!/bin/bash
+
+# Defining colors
+RED=""
+YELLOW=""
+GREEN=""
+BLUE=""
+CYAN=""
+NORMAL=""
+
+VERSION="0.3.1"
+
+# shellcheck disable=SC2034
+STD_SHORT_OPTIONS="sqdvhV"
+# shellcheck disable=SC2034
+STD_LONG_OPTIONS="simulate,quiet,debug,verbose,nocolor,help,version"
+# shellcheck disable=SC2034
+STD_USAGE_MSG=$( cat <<-EOF
+               -s|--simulate   Simulation mode - dont apply any changes.
+               -d|--debug      Debug output (bash -x).
+               -q|--quiet      Quiet execution (e.g. as a cronjob). Mutually exclusive to --verbose.
+               -v|--verbose    Set verbosity on. Mutually exclusive to --quiet.
+               --nocolor       Dont use colors on display.
+               -h|--help       Show this output and exit.
+               -V|--version    prints out version number of the script and exit
+       EOF
+    )
+
+# Standard global variables
+VERBOSE="n"
+DEBUG="n"
+QUIET="n"
+# shellcheck disable=SC2034
+DO_ASK="n"
+SIMULATE="n"
+LOGFILE=
+
+declare -a REMAINING_ARGS=()
+declare -a REMAINING_OPTS=()
+
+# shellcheck disable=SC2034
+NFS_HOMEDIR_PARENT='/mnt/nfs/home'
+
+DESCRIPTION="${DESCRIPTION:-Failing script description}"
+
+# LDAP Defaults
+LDAP_USR="cn=admin"
+LDAP_PWD_FILE="${HOME}/.private/dirsrv-prd-dpx-admin-pwd-wonl.txt"
+LDAP_BASE="o=isp"
+LDAP_HOST="ldap.pixelpark.com"
+LDAP_PORT=
+LDAP_SSL="y"
+LDAP_URL=
+
+# shellcheck disable=SC2034
+LDAP_STD_OPTS_SHORT="D:y:H:P:b:"
+# shellcheck disable=SC2034
+LDAP_STD_OPTS_LONG="bind-dn:,password-file:,ldap-host:,ldap-port:,base-dn:,no-ldap-ssl"
+
+# shellcheck disable=SC2034
+LDAP_USAGE_MSG=$( cat <<-EOF
+               -D|--bind-dn DN
+                               Use this Distinguished Name DN to bind to the LDAP directory.
+                               (Default: '${LDAP_USR}').
+               -y|--password-file FILE
+                               Use complete contents of PASSWD_FILE as the password for simple authentication
+                               (Default: '${LDAP_PWD_FILE}').
+               -H|--ldap-host HOSTNAME
+                               The hostname or IP address of the LDAP-Server (Default: '${LDAP_HOST}').
+               -P|--ldap-port PORT
+                               The port number of the LDAP-Server, if it is not the standard port.
+               -b|--base-dn SEARCH_BASE
+                               The starting point for the LDAP search (Default: '${LDAP_BASE}')
+               --no-ldap-ssl   Disable LDAPS on all LDAP actions.
+       EOF
+    )
+
+#-------------------------------------------------------------------
+detect_color() {
+
+    local safe_term="${TERM//[^[:alnum:]]/?}"
+    local match_lhs=""
+    local use_color="false"
+    local term=
+
+    if [[ -f ~/.dir_colors   ]] ; then
+        match_lhs="${match_lhs}$( grep '^TERM ' ~/.dir_colors | sed -e 's/^TERM  *//' -e 's/ .*//')"
+    fi
+    if [[ -f /etc/DIR_COLORS   ]] ; then
+        match_lhs="${match_lhs}$( grep '^TERM ' /etc/DIR_COLORS | sed -e 's/^TERM  *//' -e 's/ .*//')"
+    fi
+    if [[ -z ${match_lhs} ]] ; then
+        type -P dircolors >/dev/null && \
+        match_lhs=$(dircolors --print-database | grep '^TERM ' | sed -e 's/^TERM  *//' -e 's/ .*//')
+    fi
+    for term in ${match_lhs} ; do
+        if [[ "${safe_term}" == "${term}" || "${TERM}" == "${term}" || "${TERM}" =~ .*color ]] ; then
+            use_color="true"
+            break
+        fi
+    done
+
+    # console colors:
+    if [[ "${use_color}" = "true" ]] ; then
+        RED="\033[38;5;196m"
+        YELLOW="\033[38;5;226m"
+        GREEN="\033[38;5;46m"
+        # shellcheck disable=SC2034
+        BLUE="\033[38;5;27m"
+        CYAN="\033[38;5;14m"
+        NORMAL="\033[39m"
+    else
+        RED=""
+        YELLOW=""
+        GREEN=""
+        # shellcheck disable=SC2034
+        BLUE=""
+        CYAN=""
+        NORMAL=""
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+description() {
+    echo -e "${DESCRIPTION}"
+}
+
+#------------------------------------------------------------------------------
+eval_common_options() {
+
+    REMAINING_ARGS=()
+    REMAINING_OPTS=()
+
+    if [[ "$#" -gt 0 ]] ; then
+        while true ; do
+            case "$1" in
+                -s|--simulate)
+                    SIMULATE="y"
+                    shift
+                    ;;
+                -d|--debug)
+                    DEBUG="y"
+                    shift
+                    ;;
+                -q|--quiet)
+                    QUIET="y"
+                    shift
+                    ;;
+                -v|--verbose)
+                    VERBOSE="y"
+                    shift
+                    ;;
+                --nocolor)
+                    RED=""
+                    YELLOW=""
+                    GREEN=""
+                    # shellcheck disable=SC2034
+                    BLUE=""
+                    CYAN=""
+                    NORMAL=""
+                    shift
+                    ;;
+                -h|--help)
+                    description
+                    echo
+                    usage
+                    exit 0
+                    ;;
+                -V|--version)
+                    echo "${BASE_NAME} version: ${VERSION}"
+                    exit 0
+                    ;;
+                --) shift
+                    break
+                    ;;
+                *)  REMAINING_OPTS+=( "$1" )
+                    shift
+                    ;;
+            esac
+        done
+    fi
+
+    if [[ "${DEBUG}" = "y" ]] ; then
+        set -x
+    fi
+
+    if [[ "$#" -gt "0" ]] ; then
+        REMAINING_ARGS=("--")
+        while [[ "$#" -gt "0" ]]  ; do
+            REMAINING_ARGS+=( "$1" )
+            shift
+        done
+    fi
+
+    if [[ "${VERBOSE}" == "y" && "${QUIET}" == "y" ]] ; then
+        error "Parameters '${RED}--verbose${NORMAL}' and '${RED}--quiet${NORMAL}' are mutually exclusive."
+        echo
+        usage >&2
+        echo
+        exit 1
+    fi
+
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        echo
+        echo -e "${CYAN}Simulation mode!${NORMAL}"
+        echo "Nothing is really done."
+        echo
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+eval_ldap_options() {
+
+    REMAINING_ARGS=()
+    REMAINING_OPTS=()
+
+    if [[ "$#" -gt 0 ]] ; then
+        while true ; do
+            case "$1" in
+                -D|--bind-dn)
+                    LDAP_USR="$2"
+                    shift
+                    shift
+                    ;;
+                -y|--password-file)
+                    LDAP_PWD_FILE="$2"
+                    shift
+                    shift
+                    ;;
+                -H|--ldap-host)
+                    LDAP_HOST="$2"
+                    shift
+                    shift
+                    ;;
+                -P|--ldap-port)
+                    LDAP_PORT="$2"
+                    shift
+                    shift
+                    ;;
+                -b|--base-dn)
+                    LDAP_BASE="$2"
+                    shift
+                    shift
+                    ;;
+                --no-ldap-ssl)
+                    LDAP_SSL='n'
+                    shift
+                    ;;
+                --) shift
+                    break
+                    ;;
+                *)  REMAINING_OPTS+=( "$1" )
+                    shift
+                    ;;
+            esac
+        done
+    fi
+
+    if [[ "$#" -gt "0" ]] ; then
+        REMAINING_ARGS=("--")
+        while [[ "$#" -gt "0" ]]  ; do
+            REMAINING_ARGS+=( "$1" )
+            shift
+        done
+    fi
+
+    if [[ ! -f "${LDAP_PWD_FILE}" ]] ; then
+        error "Password file '${RED}${LDAP_PWD_FILE}'${NORMAL} not found."
+        exit 3
+    fi
+
+    if [[ ! -r "${LDAP_PWD_FILE}" ]] ; then
+        error "Password file '${RED}${LDAP_PWD_FILE}${NORMAL}' not readable."
+        exit 3
+    fi
+
+    if [[ "${LDAP_SSL}" == "n" ]] ; then
+        LDAP_URL="ldap://${LDAP_HOST}"
+        if [[ -n "${LDAP_PORT}" && "${LDAP_PORT}" != "389" ]] ; then
+            LDAP_URL+=":${LDAP_PORT}"
+        fi
+    else
+        LDAP_URL="ldaps://${LDAP_HOST}"
+        if [[ -n "${LDAP_PORT}" && "${LDAP_PORT}" != "636" ]] ; then
+            LDAP_URL+=":${LDAP_PORT}"
+        fi
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+my_date() {
+    date --rfc-3339=seconds
+}
+
+#------------------------------------------------------------------------------
+debug() {
+    if [[ "${VERBOSE}" != "y" ]] ; then
+        return 0
+    fi
+    if [[ -n "${LOGFILE}" ]] ; then
+        echo -e "[$(my_date)] [${BASE_NAME}:${CYAN}DEBUG${NORMAL}]: $@" >>"${LOGFILE}"
+    fi
+    echo -e " * [$(my_date)] [${BASE_NAME}:${CYAN}DEBUG${NORMAL}]: $*" >&2
+}
+
+#------------------------------------------------------------------------------
+info() {
+    if [[ -n "${LOGFILE}" ]] ; then
+        echo -e "[$(my_date)] [${BASE_NAME}:${GREEN}DEBUG${NORMAL}]: $@" >>"${LOGFILE}"
+    fi
+    if [[ "${QUIET}" != "y" ]] ; then
+        echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASE_NAME}:${GREEN}INFO${NORMAL}] : $*" >&2
+    fi
+}
+
+#------------------------------------------------------------------------------
+warn() {
+    if [[ -n "${LOGFILE}" ]] ; then
+        echo -e "[$(my_date)] [${BASE_NAME}:${YELLOW}DEBUG${NORMAL}]: $@" >>"${LOGFILE}"
+    fi
+    echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASE_NAME}:${YELLOW}WARN${NORMAL}] : $*" >&2
+}
+
+#------------------------------------------------------------------------------
+error() {
+    if [[ -n "${LOGFILE}" ]] ; then
+        echo -e "[$(my_date)] [${BASE_NAME}:${RED}DEBUG${NORMAL}]: $@" >>"${LOGFILE}"
+    fi
+    echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASE_NAME}:${RED}ERROR${NORMAL}]: $*" >&2
+}
+
+#------------------------------------------------------------------------------
+check_for_root() {
+    local my_id=$( id -u )
+    if [[ "${my_id}" != "0" ]] ; then
+        error "You must be ${RED}root${NORMAL} to execute this script."
+        echo >&2
+        exit 1
+    fi
+}
+
+#------------------------------------------------------------------------------
+CP() {
+    local cmd="cp"
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        cmd+=" --verbose"
+    fi
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        debug "Simulate executing: ${cmd} $*"
+        return
+    fi
+    eval ${cmd} "$@"
+}
+
+#------------------------------------------------------------------------------
+CP_force() {
+    local cmd="cp"
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        cmd+=" --verbose"
+    fi
+    debug "Executing: ${cmd} $*"
+    eval ${cmd} "$@"
+}
+
+#------------------------------------------------------------------------------
+MV() {
+    local cmd="mv"
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        cmd+=" --verbose"
+    fi
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        debug "Simulate executing: ${cmd} $*"
+        return
+    fi
+    eval ${cmd} "$@"
+}
+
+#------------------------------------------------------------------------------
+MV_force() {
+    local cmd="mv"
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        cmd+=" --verbose"
+    fi
+    debug "Executing: ${cmd} $*"
+    eval ${cmd} "$@"
+}
+
+#------------------------------------------------------------------------------
+RM() {
+
+    local cmd="rm"
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        cmd+=" --verbose"
+    fi
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        debug "Simulate executing: ${cmd} $*"
+        return
+    fi
+    eval ${cmd} "$@"
+
+}
+
+#------------------------------------------------------------------------------
+RM_force() {
+
+    local cmd="rm --force"
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        cmd+=" --verbose"
+    fi
+    debug "Executing: ${cmd} $*"
+    eval ${cmd} "$@"
+
+}
+
+#------------------------------------------------------------------------------
+MKDIR() {
+
+    local cmd="mkdir $*"
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        cmd="mkdir --verbose $*"
+    fi
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        info "Executing: ${cmd}"
+        return
+    fi
+    debug "Executing: ${cmd}"
+    eval ${cmd}
+}
+
+#------------------------------------------------------------------------------
+MKDIR_forced() {
+
+    local cmd="mkdir $*"
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        cmd="mkdir --verbose $*"
+    fi
+    debug "Executing: ${cmd}"
+    eval ${cmd}
+}
+
+#------------------------------------------------------------------------------
+RMDIR() {
+    local cmd="rmdir"
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        cmd+=" --verbose"
+    fi
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        info "Executing: ${cmd} $*"
+        return
+    fi
+    eval ${cmd} "$@"
+}
+
+#------------------------------------------------------------------------------
+CHOWN() {
+
+    local cmd="chown $*"
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        cmd="chown --verbose $*"
+    fi
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        info "Executing: ${cmd}"
+        return
+    fi
+    debug "Executing: ${cmd}"
+    eval ${cmd}
+}
+
+#------------------------------------------------------------------------------
+line() {
+
+    local lchar='-'
+    if [[ "$#" -ge 1 ]] ; then
+        lchar=$( echo "$1" | sed -e 's/^\(.\).*/\1/' )
+    fi
+
+    local count=79 
+    if [[ "$#" -ge 2 ]] ; then
+        count="$2"
+    fi
+
+    local i=0
+    local l=''
+
+    while [[ "$i" -lt "${count}" ]] ; do
+        l+="${lchar}"
+        i=$(( $i + 1 ))
+    done
+
+    if [[ -n "${LOGFILE}" ]] ; then
+        echo "${l}" >>"${LOGFILE}"
+    fi
+
+    if [[ "${QUIET}" == "y" ]] ; then
+        return
+    fi
+    echo "${l}"
+
+}
+
+#------------------------------------------------------------------------------
+dline() {
+    line '=' "$@"
+}
+
+#------------------------------------------------------------------------------
+empty_line() {
+
+    if [[ -n "${LOGFILE}" ]] ; then
+        echo >> "${LOGFILE}"
+    fi
+
+    if [[ "${QUIET}" == "y" ]] ; then
+        return
+    fi
+    echo
+
+}
+
+#------------------------------------------------------------------------------
+set_locale() {
+
+    local new_locale="$1"
+    local loc=
+    local found="n"
+
+    local oifs="${IFS}"
+    IFS="
+"
+    for loc in $( locale -a ); do
+        if [[ "${loc}" == "${new_locale}" ]] ; then
+            found="y"
+            break
+        fi
+    done
+    IFS="${oifs}"
+
+    if [[ "${found}" != "y" ]] ; then
+        error "Locale '${RED}${new_locale}${NORMAL}' not found."
+    else
+        LANG="${new_locale}"
+        LC_ALL=
+        LC_CTYPE="${new_locale}"
+        LC_NUMERIC="${new_locale}"
+        LC_TIME="${new_locale}"
+        LC_COLLATE="${new_locale}"
+        LC_MONETARY="${new_locale}"
+        LC_MESSAGES="${new_locale}"
+        # shellcheck disable=SC2034
+        LC_PAPER="${new_locale}"
+        # shellcheck disable=SC2034
+        LC_NAME="${new_locale}"
+        # shellcheck disable=SC2034
+        LC_ADDRESS="${new_locale}"
+        # shellcheck disable=SC2034
+        LC_TELEPHONE="${new_locale}"
+        # shellcheck disable=SC2034
+        LC_MEASUREMENT="${new_locale}"
+        # shellcheck disable=SC2034
+        LC_IDENTIFICATION="${new_locale}"
+    fi
+
+}
+
+# vim: filetype=sh ts=4 et list