]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Refactoring postinstall-scripts/update-all-packages to use functions.rc.
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 19 Oct 2023 12:56:32 +0000 (14:56 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 19 Oct 2023 12:56:32 +0000 (14:56 +0200)
postinstall-scripts/functions.rc
postinstall-scripts/update-all-packages

index c52b9b79d56d4797e81bb13c5fbf7c853964666c..1cbe308d582a0c4eaa88762a864043f9b0c71e76 100644 (file)
@@ -258,12 +258,17 @@ exec_cmd() {
 CP() {
 
     local -a cmd=( "cp" )
+    local token=
 
     if [[ "${VERBOSE}" == "y" ]] ; then
         cmd+=( "--verbose" )
     fi
 
-    exec_cmd $cmd
+    for token in "$@"; do
+        cmd+=( "${token}" )
+    done
+
+    exec_cmd "${cmd[@]}"
 
 }
 
index 1b643d8014827f6ef1604b6e3c626e07f656d089..002552790f53898fc692c91b0cf580f06aa326b9 100755 (executable)
 set -e
 set -u
 
-export LC_ALL="en_US.utf8"
-export LANG="en_US.utf8"
+BASE_NAME="$( basename ${0} )"
+MY_REAL_NAME=$( readlink -f $0 )
+BIN_DIR=$( dirname "${MY_REAL_NAME}" )
+BASE_DIR=$( dirname "${BIN_DIR}" )
+
+if [[ -f "${BIN_DIR}/functions.rc" ]] ; then
+    . "${BIN_DIR}/functions.rc"
+else
+    echo "Bash resource file '${BIN_DIR}/functions.rc' not found" >&2
+    exit 5
+fi
 
-VERBOSE="n"
-DEBUG="n"
-QUIET='n'
-
-# console colors:
-RED=""
-YELLOW=""
-GREEN=""
-BLUE=""
-CYAN=""
-NORMAL=""
-
-HAS_TTY='y'
-HAS_COLORS="n"
-
-VERSION="0.6"
-
-BASENAME="$(basename ${0})"
-BASE_DIR="$(dirname ${0})"
 FQDN=$( hostname -f )
 
-#########################################
-# Some often used funktions
-#-------------------------------------------------------------------
-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}$( cat ~/.dir_colors | grep '^TERM ' | sed -e 's/^TERM  *//' -e 's/ .*//')"
-    fi
-    if [[ -f /etc/DIR_COLORS   ]] ; then
-        match_lhs="${match_lhs}$( cat /etc/DIR_COLORS | grep '^TERM ' | 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} ]] ; 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"
-        BLUE="\033[38;5;27m"
-        NORMAL="\033[39m"
-        CYAN="\033[38;5;36m"
-        HAS_COLORS="y"
-    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
-
-}
 detect_color
 
 #------------------------------------------------------------------------------
-my_date() {
-    date --rfc-3339=seconds
-}
-
-#------------------------------------------------------------------------------
-debug() {
-    if [[ "${VERBOSE}" != "y" ]] ; then
-        return 0
-    fi
-    echo -e " * [$(my_date)] [${BASENAME}:${CYAN}DEBUG${NORMAL}]: $@" >&2
-}
-
-#------------------------------------------------------------------------------
-info() {
-    if [[ "${QUIET}" == "y" ]] ; then
-        return 0
-    fi
-    echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@" >&2
-}
-
-#------------------------------------------------------------------------------
-warn() {
-    echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $@" >&2
-}
-
-#------------------------------------------------------------------------------
-error() {
-    echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2
-}
-
-#------------------------------------------------------------------------------
-description() {
-    cat <<-EOF
+DESCRIPTION=$( cat <<-EOF
        Performs a complete update of all packages.
 
        EOF
-
-}
+)
 
 #------------------------------------------------------------------------------
 usage() {
 
     cat <<-EOF
-       Usage: ${BASENAME} [-d|--debug] [[-v|--verbose] | [-q|--quiet]] [--nocolor]
-              ${BASENAME} [-h|--help]
-              ${BASENAME} [-V|--version]
+       Usage: ${BASE_NAME} ${STD_USAGE}
+              ${BASE_NAME} [-h|--help]
+              ${BASE_NAME} [-V|--version]
 
            Options:
-               -d|--debug      Debug output (bash -x).
-               -v|--verbose    Set verbosity on.
-               -q|--quiet      Quiet execution. Mutually exclusive to --verbose.
-               --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
 
+       echo "${STD_USAGE_MSG}"
 }
 
 #------------------------------------------------------------------------------
 get_options() {
 
     local tmp=
-    local short_options="dvqhV"
-    local long_options="debug,verbose,quiet,help,version"
+    local short_options="${STD_SHORT_OPTIONS}"
+    local long_options="${STD_LONG_OPTIONS}"
 
     set +e
-    tmp=$( getopt -o "${short_options}" --long "${long_options}" -n "${BASENAME}" -- "$@" )
+    tmp=$( getopt -o "${short_options}" --long "${long_options}" -n "${BASE_NAME}" -- "$@" )
     if [[ $? != 0 ]] ; then
         echo "" >&2
         usage >&2
@@ -163,81 +58,27 @@ get_options() {
 
     # Note the quotes around `$TEMP': they are essential!
     eval set -- "${tmp}"
+    eval_common_options "$@"
+    if [[ "${DEBUG}" == 'y' ]] ; then
+        declare -p REMAINING_OPTS
+        declare -p REMAINING_ARGS
+    fi
 
-    local p=
+    local len="${#REMAINING_OPTS[*]}"
+    local i="0"
+    local j=
+    local arg=
+    while [[ "$i" -lt "${len}" ]] ; do
 
-    while true ; do
-        case "$1" in
-            -d|--debug)
-                DEBUG="y"
-                shift
-                ;;
-            -v|--verbose)
-                VERBOSE="y"
-                shift
-                ;;
-            -q|--quiet)
-                QUIET="y"
-                RED=""
-                YELLOW=""
-                GREEN=""
-                BLUE=""
-                CYAN=""
-                NORMAL=""
-                HAS_COLORS="n"
-                shift
-                ;;
-            --nocolor)
-                RED=""
-                YELLOW=""
-                GREEN=""
-                BLUE=""
-                CYAN=""
-                NORMAL=""
-                HAS_COLORS="n"
-                shift
-                ;;
-            -h|--help)
-                description
-                echo
-                usage
-                exit 0
-                ;;
-            -V|--version)
-                echo "${BASENAME} version: ${VERSION}"
-                exit 0
-                ;;
-            --) shift
-                break
-                ;;
-            *)  echo "Internal error!"
+        arg="${REMAINING_OPTS[$i]}"
+
+        case "${arg}" in
+            *)  echo -e "Internal error - option '${RED}${arg}${NORMAL} was wrong!"
                 exit 1
                 ;;
         esac
     done
 
-    if [[ "${DEBUG}" = "y" ]] ; then
-        set -x
-    fi
-
-    if [[ "${VERBOSE}" == "y" && "${QUIET}" == "y" ]] ; then
-        echo "Options '${RED}--verbose${NORMAL}' and '${RED}--quiet${NORMAL}' are mutually exclusive." >&2
-        echo >&2
-        usage >&2
-        exit 1
-    fi
-
-}
-
-#------------------------------------------------------------------------------
-check_for_root() {
-
-    debug "Checking for root execution ..."
-    if [[ "$( id -u )" != "0" ]] ; then
-        error "Only user '${RED}root${NORMAL}' may execute this script."
-        exit 1
-    fi
-
 }
 
 #------------------------------------------------------------------------------
@@ -252,11 +93,6 @@ empty_repo_file() {
     local bak_repo="${old_repo_dir}/${stem}.${i}.repo"
     debug "Checking '${CYAN}${orig_repo}${NORMAL}' ..."
 
-    local verbose_opt=""
-    if [[ "${VERBOSE}" == "y" ]] ; then
-         verbose_opt=" --verbose"
-    fi
-
     if [[ -f "${orig_repo}" && -s "${orig_repo}" ]] ; then
         info "Repo file '${YELLOW}${orig_repo}${NORMAL}' exists and has a size greater than zero."
         while [[ -e "${bak_repo}" ]] ; do
@@ -265,14 +101,10 @@ empty_repo_file() {
         done
 
         info "Copying '${YELLOW}${orig_repo}${NORMAL}' to '${GREEN}${old_repo_dir}${NORMAL}' ..."
-        cmd="cp -p ${verbose_opt} \"${orig_repo}\" \"${bak_repo}\""
-        debug "Executing: ${cmd}"
-        eval ${cmd}
+        CP -p "${orig_repo}" "${bak_repo}"
 
         info "Truncating '${YELLOW}${orig_repo}${NORMAL}' ..."
-        cmd="cp /dev/null \"${orig_repo}\""
-        debug "Executing: ${cmd}"
-        eval ${cmd}
+        CP /dev/null "${orig_repo}"
 
     else
         return 1
@@ -333,12 +165,17 @@ ok==1 && /^ip_resolve/ {print "ip_resolve=4"; ok=0; next}
 ok==1 {print "ip_resolve=4"; ok=0}
 {print $0} '
         fi
-        cp -p -v /etc/yum.conf /etc/yum.conf.$( date -r /etc/yum.conf +'%Y-%m-%d_%H:%M:%S').bak
-        awk "${awk_script}" /etc/yum.conf >/etc/yum.conf.new
+        CP -p /etc/yum.conf /etc/yum.conf.$( date -r /etc/yum.conf +'%Y-%m-%d_%H:%M:%S').bak
+        if [[ "${SIMULATE}" == "y" ]] ; then
+            info "Simulating: awk \"${awk_script}\" /etc/yum.conf >/etc/yum.conf.new"
+        else
+            debug "Executing: awk \"${awk_script}\" /etc/yum.conf >/etc/yum.conf.new"
+            awk "${awk_script}" /etc/yum.conf >/etc/yum.conf.new
+        fi
         if [[ -s /etc/yum.conf.new ]] ; then
-            mv -v /etc/yum.conf.new /etc/yum.conf
+            MV /etc/yum.conf.new /etc/yum.conf
         else
-            rm -f -v /etc/yum.conf.new
+            RM -f /etc/yum.conf.new
         fi
     fi
 
@@ -349,61 +186,25 @@ ok==1 {print "ip_resolve=4"; ok=0}
 
     echo
     info "Cleaning ..."
-    local cmd="${tool}${verbose_opt} --assumeyes clean all"
-    debug "Executing: ${cmd}"
-    eval ${cmd}
+    exec_cmd ${tool} --assumeyes clean all
 
     echo
     info "Making cache ..."
-    cmd="${tool}${verbose_opt} --assumeyes makecache"
-    debug "Executing: ${cmd}"
-    eval ${cmd}
+    exec_cmd ${tool} --assumeyes makecache
 
     echo
     info "Doing upgrade ..."
-    cmd="${tool}${verbose_opt} --assumeyes upgrade"
-    debug "Executing: ${cmd}"
-    eval ${cmd}
+    exec_cmd ${tool} --assumeyes upgrade
 
     empty_oracle_repo_files || true
 
     echo
     info "Cleaning again ..."
-    local cmd="${tool}${verbose_opt} --assumeyes clean all"
-    debug "Executing: ${cmd}"
-    eval ${cmd}
+    exec_cmd ${tool} --assumeyes clean all
 
     echo
     info "Making cache again ..."
-    cmd="${tool}${verbose_opt} --assumeyes makecache"
-    debug "Executing: ${cmd}"
-    eval ${cmd}
-
-}
-
-#------------------------------------------------------------------------------
-do_deb_upgrade() {
-
-    local tool="$1"
-
-    info "Performing dist upgrade with ${GREEN}${tool}${NORMAL} ..."
-
-    local quiet_opt=""
-    if [[ "${QUIET}" == "y" ]] ; then
-         quiet_opt=" --quiet"
-    fi
-
-    echo
-    info "Updating indexes ..."
-    local cmd="${tool} update --yes${quiet_opt}"
-    debug "Executing: ${cmd}"
-    eval ${cmd}
-
-    echo
-    info "Doing upgrade ..."
-    local cmd="${tool} dist-upgrade --yes${quiet_opt}"
-    debug "Executing: ${cmd}"
-    eval ${cmd}
+    exec_cmd ${tool} --assumeyes makecache
 
 }
 
@@ -417,10 +218,6 @@ do_distupgrade() {
         do_el_upgrade 'dnf'
     elif type -p yum >/dev/null ; then
         do_el_upgrade 'yum'
-    elif type -p apt >/dev/null ; then
-        do_deb_upgrade 'apt'
-    elif type -p apt-get >/dev/null ; then
-        do_deb_upgrade 'apt-get'
     else
         error "The operating system is not supported."
         return 0
@@ -431,7 +228,6 @@ do_distupgrade() {
 
 }
 
-
 ################################################################################
 ##
 ## Main
@@ -442,12 +238,13 @@ do_distupgrade() {
 main() {
 
     get_options "$@"
-    check_for_root
+    set_locale "en_US.utf8"
 
-    echo
+    empty_line
     info "Trying to ${CYAN}updating all packages${NORMAL} ..."
-    echo
+    empty_line
 
+    check_for_root
     do_distupgrade
 
 }