]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Adding bin/nfs-clean-homedirs lib/functions.rc; removing bin/.gitkeep lib/.gitkeep
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 18 Mar 2019 12:21:12 +0000 (13:21 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 18 Mar 2019 12:21:12 +0000 (13:21 +0100)
bin/.gitkeep [deleted file]
bin/nfs-clean-homedirs [new file with mode: 0755]
lib/.gitkeep [deleted file]
lib/functions.rc [new file with mode: 0644]

diff --git a/bin/.gitkeep b/bin/.gitkeep
deleted file mode 100644 (file)
index e69de29..0000000
diff --git a/bin/nfs-clean-homedirs b/bin/nfs-clean-homedirs
new file mode 100755 (executable)
index 0000000..c33f92b
--- /dev/null
@@ -0,0 +1,109 @@
+#!/bin/bash
+
+set -e
+set -u
+
+BASE_NAME="$( basename ${0} )"
+MY_REAL_NAME=$( readlink -f $0 )
+BIN_DIR=$( dirname "${MY_REAL_NAME}" )
+BASE_DIR=$( dirname "${BIN_DIR}" )
+LIB_DIR="${BASE_DIR}/lib"
+CONF_DIR="${BASE_DIR}/etc"
+
+if [[ -f "${LIB_DIR}/functions.rc" ]] ; then
+    . "${LIB_DIR}/functions.rc"
+else
+    echo "Bash resource file '${LIB_DIR}/functions.rc' not found" >&2
+    exit 5
+fi
+
+if [[ -f "${CONF_DIR}/pp-nfs.rc" ]] ; then
+    . "${CONF_DIR}/pp-nfs.rc"
+fi
+
+NFS_HOMEDIRS="${NFS_HOMEDIR_PARENT}"
+
+detect_color
+
+DESCRIPTION=$( cat <<-EOF
+       Removes orphaned NFS home directories under ${CYAN}${NFS_HOMEDIR_PARENT}${NORMAL}
+       and archives them under ${CYAN}${NFS_HOMEDIR_PARENT}/_old_homes${NORMAL}.
+       EOF
+)
+
+#------------------------------------------------------------------------------
+usage() {
+    cat <<-EOF
+       Usage: ${BASE_NAME} [-d] [-v] [--nocolor] [NFS_HOMEDIR_PARENT]
+              ${BASE_NAME} [-h|--help]
+              ${BASE_NAME} [-V|--version]
+
+           Optional Parameter:
+               NFS_HOMEDIR_PARENT: The parent directory of the NFS home directories.
+                                   Defaults to: '${NFS_HOMEDIR_PARENT}'.
+
+           Options:
+       EOF
+
+    echo "${STD_USAGE_MSG}"
+
+}
+
+#------------------------------------------------------------------------------
+get_options() {
+
+    local tmp=
+    local base_dir=
+
+    set +e
+    tmp=$( getopt -o ${STD_SHORT_OPTIONS} \
+                    --long start:,${STD_LONG_OPTIONS} \
+                    -n "${BASE_NAME}" -- "$@" )
+    if [[ $? != 0 ]] ; then
+        echo "" >&2
+        usage >&2
+        exit 1
+    fi
+    set -e
+
+    # Note the quotes around `$TEMP': they are essential!
+    eval set -- "${tmp}"
+    eval_common_options "$@"
+    #if [[ "${VERBOSE}" == 'y' ]] ; then
+    #    declare -p REMAINING_ARGS
+    #fi
+
+    local num_args="${#REMAINING_ARGS[@]}"
+
+    if [[ "${num_args}" != "0" ]] ; then
+        if [[ "${num_args}" -gt "2" ]] ; then
+            error "Invalid number of arguments."
+            echo >&2
+            usage >&2
+            exit 1
+        fi
+        NFS_HOMEDIRS="${REMAINING_ARGS[1]}"
+    fi
+
+    if [[ ! -d "${NFS_HOMEDIRS}" ]] ; then
+        error "Parent of NFS home directories '${RED}${NFS_HOMEDIRS}${NORMAL}' not found."
+        echo >&2
+        usage >&2
+        exit 2
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+main() {
+
+    get_options "$@"
+
+}
+
+main "$@"
+
+
+exit 0
+
+# vim: et ts=4 list
diff --git a/lib/.gitkeep b/lib/.gitkeep
deleted file mode 100644 (file)
index e69de29..0000000
diff --git a/lib/functions.rc b/lib/functions.rc
new file mode 100644 (file)
index 0000000..f604878
--- /dev/null
@@ -0,0 +1,170 @@
+#!/bin/bash
+
+HAS_TTY='y'
+
+# Defining colors
+RED=""
+YELLOW=""
+GREEN=""
+BLUE=""
+CYAN=""
+NORMAL=""
+
+VERSION="0.1.0"
+
+STD_SHORT_OPTIONS="dvhV"
+STD_LONG_OPTIONS="debug,verbose,nocolor,help,version"
+STD_USAGE_MSG=$( cat <<-EOF
+               -d|--debug      Debug output (bash -x).
+               -v|--verbose    Set verbosity on.
+               --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"
+DO_ASK="n"
+SIMULATE="n"
+
+declare -a REMAINING_ARGS=()
+
+NFS_HOMEDIR_PARENT='/mnt/nfs/home'
+
+DESCRIPTION="${DESCRIPTION:-Failing script description}"
+
+#-------------------------------------------------------------------
+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"
+        CYAN="\033[38;5;14m"
+        NORMAL="\033[39m"
+    else
+        RED=""
+        YELLOW=""
+        GREEN=""
+        BLUE=""
+        CYAN=""
+        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
+
+}
+
+#------------------------------------------------------------------------------
+description() {
+    echo -e "${DESCRIPTION}"
+}
+
+#------------------------------------------------------------------------------
+eval_common_options() {
+
+    REMAINING_ARGS=()
+
+    while true ; do
+        case "$1" in
+            -d|--debug)
+                DEBUG="y"
+                shift
+                ;;
+            -v|--verbose)
+                VERBOSE="y"
+                shift
+                ;;
+            --nocolor)
+                RED=""
+                YELLOW=""
+                GREEN=""
+                BLUE=""
+                CYAN=""
+                NORMAL=""
+                shift
+                ;;
+            -h|--help)
+                description
+                echo
+                usage
+                exit 0
+                ;;
+            -V|--version)
+                echo "${BASE_NAME} version: ${VERSION}"
+                exit 0
+                ;;
+            --) shift
+                break
+                ;;
+            *)  REMAINING_ARGS+=($1)
+                shift
+                ;;
+        esac
+    done
+
+    if [[ "${DEBUG}" = "y" ]] ; then
+        set -x
+    fi
+
+    if [[ "$#" -gt "0" ]] ; then
+        REMAINING_ARGS+="--"
+        while [[ "$#" -gt "0" ]]  ; do
+            REMAINING_ARGS+=($1)
+            shift
+        done
+    fi
+        
+
+}
+
+#------------------------------------------------------------------------------
+my_date() {
+    date +'%F %T.%N %:::z'
+}
+
+#------------------------------------------------------------------------------
+debug() {
+    if [[ "${VERBOSE}" != "y" ]] ; then
+        return 0
+    fi
+    echo -e " * [$(my_date)] [${BASE_NAME}:DEBUG]: $@" >&2
+}
+
+#------------------------------------------------------------------------------
+info() {
+    echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASE_NAME}:${GREEN}INFO${NORMAL}] : $@" >&2
+}
+
+#------------------------------------------------------------------------------
+warn() {
+    echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASE_NAME}:${YELLOW}WARN${NORMAL}] : $@" >&2
+}
+
+#------------------------------------------------------------------------------
+error() {
+    echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASE_NAME}:${RED}ERROR${NORMAL}]: $@" >&2
+}
+
+# vim: filetype=sh ts=4 et