]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Adding postinstall-scripts/functions.rc and postinstall-scripts/register-rhel
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 16 Oct 2023 09:15:52 +0000 (11:15 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 16 Oct 2023 09:15:52 +0000 (11:15 +0200)
postinstall-scripts/functions.rc [new file with mode: 0644]
postinstall-scripts/register-rhel [new file with mode: 0755]

diff --git a/postinstall-scripts/functions.rc b/postinstall-scripts/functions.rc
new file mode 100644 (file)
index 0000000..3141c19
--- /dev/null
@@ -0,0 +1,480 @@
+#!/bin/bash
+
+# Defining colors
+RED=""
+YELLOW=""
+GREEN=""
+BLUE=""
+CYAN=""
+NORMAL=""
+
+VERSION="1.7.2"
+
+# 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
+    )
+STD_USAGE="[-d] [[-v] | [-q]]] [-s] [--nocolor]"
+
+# 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=()
+
+DESCRIPTION="${DESCRIPTION:-Failing script description}"
+
+#-------------------------------------------------------------------
+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
+        local msg=$( cat <<-EOF
+
+                           ${CYAN}###############################${NORMAL}
+                           ${CYAN}#${NORMAL}       ${YELLOW}Simulation mode${NORMAL}       ${CYAN}#${NORMAL}
+                           ${CYAN}#${NORMAL} Nothing will be really done ${CYAN}#${NORMAL}
+                           ${CYAN}###############################${NORMAL}
+
+                       EOF
+        )
+        echo -e "${msg}"
+    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() {
+
+    debug "Checking for execution as root ..."
+    local my_id=$( id -u )
+    if [[ "${my_id}" != "0" ]] ; then
+        empty_line
+        error "You must be ${RED}root${NORMAL} to execute this script."
+        empty_line
+        if [[ "${SIMULATE}" != "y" ]] ; then
+            exit 5
+        fi
+    fi
+}
+
+#------------------------------------------------------------------------------
+exec_cmd() {
+
+    local msg="Executing"
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        msg="Simulating"
+    fi
+    if [[ "${SIMULATE}" == "y" || "${VERBOSE}" == "y" ]] ; then
+        echo "${msg}: $*"
+    fi
+    if [[ "${SIMULATE}" != "y" ]] ; then
+        eval "$@"
+    fi
+}
+
+#------------------------------------------------------------------------------
+CP() {
+
+    local -a cmd=( "cp" )
+
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        cmd+=( "--verbose" )
+    fi
+
+    exec_cmd $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
diff --git a/postinstall-scripts/register-rhel b/postinstall-scripts/register-rhel
new file mode 100755 (executable)
index 0000000..d49c1f8
--- /dev/null
@@ -0,0 +1,145 @@
+#!/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}" )
+
+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
+
+export PATH='/sbin:/bin:/usr/sbin:/usr/bin'
+
+RHSM_USER='dpx-subscriber'
+RHSM_PWD=
+SM_BIN='subscription-manager'
+
+detect_color
+
+#------------------------------------------------------------------------------
+DESCRIPTION=$( cat <<-EOF
+       Registers the current host at the Subscription management of RedHat.
+
+       EOF
+)
+
+#------------------------------------------------------------------------------
+usage() {
+    cat <<-EOF
+       Usage: ${BASE_NAME} [-U USER ] [-P PASSWORD ] ${STD_USAGE}
+              ${BASE_NAME} [-h|--help]
+              ${BASE_NAME} [-V|--version]
+
+           Options:
+               -U|--user USER  Gives the username for the account which is registering the system.
+               -P|--password PASSWORD
+                               Gives the user account password.
+       EOF
+
+       echo "${STD_USAGE_MSG}"
+}
+
+#------------------------------------------------------------------------------
+get_options() {
+
+    local tmp=
+    local short_options="U:P:${STD_SHORT_OPTIONS}"
+    local long_options="user:,password:,${STD_LONG_OPTIONS}"
+
+    set +e
+    tmp=$( getopt -o "${short_options}" --long "${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 [[ "${DEBUG}" == 'y' ]] ; then
+        declare -p REMAINING_OPTS
+        declare -p REMAINING_ARGS
+    fi
+
+       local len="${#REMAINING_OPTS[*]}"
+    local i="0"
+    local j=
+    local arg=
+    while [[ "$i" -lt "${len}" ]] ; do
+
+        arg="${REMAINING_OPTS[$i]}"
+
+        case "${arg}" in
+            -U|--user)
+                j=$(( $i + 1 ))
+                RHSM_USER=$( echo "${REMAINING_OPTS[$j]}" | tr '[:upper:]' '[:lower:]' | sed -e 's/^[  ]*//' -e 's/[   ]*$//' )
+                i=$(( $i + 2 ))
+                ;;
+            -P|--password)
+                j=$(( $i + 1 ))
+                RHSM_PWD="${REMAINING_OPTS[$j]}"
+                i=$(( $i + 2 ))
+                ;;
+            *)  echo -e "Internal error - option '${RED}${arg}${NORMAL} was wrong!"
+                exit 1
+                ;;
+        esac
+    done
+
+    if [[ "${#REMAINING_ARGS[@]}" != "0" ]] ; then
+        error "Invalid arguments given."
+        echo >&2
+        usage >&2
+        exit 1
+    fi
+
+    local sm_bin=$( type -p ${SM_BIN} )
+    if [[ -n "${sm_bin}" ]] ; then
+        debug "Found ${SM_BIN} at '${CYAN}${sm_bin}${NORMAL}'."
+    else
+        error "Binary '${CYAN}${SM_BIN}${NORMAL}' not found. Maybe ${RED}not a RedHat Enterprize Linux${NORMAL} system?"
+        exit 2
+    fi
+
+    if [[ -z "${RHSM_USER}" ]] ; then
+        error "Invalid user for registring at RedHat subscription management given."
+        exit 1
+    fi
+
+    if [[ -z "${RHSM_PWD}" ]] ; then
+        error "No password of the user for registring at RedHat subscription management given."
+        exit 1
+    fi
+
+}
+
+################################################################################
+##
+## Main
+##
+################################################################################
+
+#------------------------------------------------------------------------------
+main() {
+
+    get_options "$@"
+    check_for_root
+    # register_rhel
+    # mangle_repos
+
+}
+
+main "$@"
+
+exit 0
+
+# vim: list