]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Fixing postinstall-scripts/init-puppet for using functions.rc and for RHEL 9
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 18 Oct 2023 14:03:02 +0000 (16:03 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 18 Oct 2023 14:03:02 +0000 (16:03 +0200)
example/.gitignore [new file with mode: 0644]
postinstall-scripts/init-puppet
postinstall-scripts/register-rhel

diff --git a/example/.gitignore b/example/.gitignore
new file mode 100644 (file)
index 0000000..7ed4bdf
--- /dev/null
@@ -0,0 +1,2 @@
+*repo03*
+*repo01*
index 5b71e7f9f2e417a1db7836dc0a7c2646c0ff3327..883d9e294b44b6529540314b3866bccc336ee3cd 100755 (executable)
@@ -3,28 +3,18 @@
 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="1.5"
-
-BASENAME="$(basename ${0})"
-BASE_DIR="$(dirname ${0})"
 FQDN=$( hostname -f )
 
 ENVIRONMENT=
@@ -47,137 +37,24 @@ INITIAL_INSTALL="n"
 PUPPET_SERVER="puppetmaster.pixelpark.com"
 PUPPET_CA_SERVER="puppetca01.pixelpark.com"
 
-#########################################
-# 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"
-        CYAN="\033[38;5;36m"
-        NORMAL="\033[39m"
-        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 +'%F %T.%N %:::z'
-}
-
-#------------------------------------------------------------------------------
-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
-}
-
-#------------------------------------------------------------------------------
-MKDIR() {
-
-    local cmd="mkdir -p"
-    if [[ "${VERBOSE}" == "y" ]] ; then
-        cmd="mkdir -v -p"
-    fi
-    debug "Executing: ${cmd}" "$@"
-    eval ${cmd} "$@"
-
-}
-
-#------------------------------------------------------------------------------
-RM() {
-
-    local cmd="rm"
-    if [[ "${VERBOSE}" == "y" ]] ; then
-        cmd="rm -v"
-    fi
-    debug "Executing: ${cmd} $*"
-    eval ${cmd} "$@"
-
-}
-
-#------------------------------------------------------------------------------
-description() {
-    cat <<-EOF
+DESCRIPTION=$( cat <<-EOF
        Initialize a Puppet agent and start it.
 
        This script works only for Red Hat Linux and its derivates.
 
        EOF
-
-}
+)
 
 #------------------------------------------------------------------------------
 usage() {
 
     cat <<-EOF
-       Usage: ${BASENAME} [OPTIONS ...]
-              ${BASENAME} [-h|--help]
-              ${BASENAME} [-V|--version]
+       Usage: ${BASE_NAME} [OPTIONS ...]
+              ${BASE_NAME} [-h|--help]
+              ${BASE_NAME} [-V|--version]
 
            Options:
                -E|--env|--environment ENVIRONMENT
@@ -214,15 +91,9 @@ usage() {
                --ca-server HOSTADDRESS
                                The hostname or IP address of the Puppet CA server to use
                                in the puppet configuration. Default: '${PUPPET_CA_SERVER}'.
-               -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}"
 }
 
 #------------------------------------------------------------------------------
@@ -245,11 +116,10 @@ autodetect_env() {
 get_options() {
 
     local tmp=
-    local base_dir=
-    local short_options="E:C:p:O:T:R:IP:M:l:z:dvqhV"
+    local short_options="E:C:p:O:T:R:IP:M:l:z:${STD_SHORT_OPTIONS}"
     local long_options="env:,environment:,customer:,hiera-customer:,project:,owner:,tier:,role:,"
     long_options+="initial-install,purpose:,server:,ca-server:,email:,location:,zone:,"
-    long_options+="debug,verbose,quiet,help,version"
+    long_options+="${STD_LONG_OPTIONS}"
 
     set +e
     tmp=$( getopt -o "${short_options}" --long "${long_options}" -n "${BASENAME}" -- "$@" )
@@ -262,134 +132,90 @@ get_options() {
 
     # Note the quotes around `$TEMP': they are essential!
     eval set -- "${tmp}"
+    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
 
-    local p=
+        arg="${REMAINING_OPTS[$i]}"
 
-    while true ; do
         case "$1" in
             -E|--env|--environment)
-                ENVIRONMENT=$( echo "$2" | tr '[:upper:]' '[:lower:]' )
-                shift
-                shift
+                j=$(( $i + 1 ))
+                ENVIRONMENT=$( echo "${REMAINING_OPTS[$j]}" | tr '[:upper:]' '[:lower:]' )
+                i=$(( $i + 2 ))
                 ;;
             -C|--customer|--hiera-customer)
-                CUSTOMER=$( echo "$2" | tr '[:upper:]' '[:lower:]' )
-                shift
-                shift
+                j=$(( $i + 1 ))
+                CUSTOMER=$( echo "${REMAINING_OPTS[$j]}" | tr '[:upper:]' '[:lower:]' )
+                i=$(( $i + 2 ))
                 ;;
             -p|--project)
-                PROJECT=$( echo "$2" | tr '[:upper:]' '[:lower:]' )
-                shift
-                shift
+                j=$(( $i + 1 ))
+                PROJECT=$( echo "${REMAINING_OPTS[$j]}" | tr '[:upper:]' '[:lower:]' )
+                i=$(( $i + 2 ))
                 ;;
             -O|--owner)
-                OWNER="$2"
-                shift
-                shift
+                j=$(( $i + 1 ))
+                OWNER="${REMAINING_OPTS[$j]}"
+                i=$(( $i + 2 ))
                 ;;
             -T|--tier)
-                TIER=$( echo "$2" | tr '[:upper:]' '[:lower:]' )
-                shift
-                shift
+                j=$(( $i + 1 ))
+                TIER=$( echo "${REMAINING_OPTS[$j]}" | tr '[:upper:]' '[:lower:]' )
+                i=$(( $i + 2 ))
                 ;;
             -R|--role)
-                ROLE=$( echo "$2" | tr '[:upper:]' '[:lower:]' )
-                shift
-                shift
+                j=$(( $i + 1 ))
+                ROLE=$( echo "${REMAINING_OPTS[$j]}" | tr '[:upper:]' '[:lower:]' )
+                i=$(( $i + 2 ))
                 ;;
             -I|--initial-install)
                 INITIAL_INSTALL="y"
-                shift
+                i=$(( $i + 1 ))
                 ;;
             -P|--purpose)
-                PURPOSE="$2"
-                shift
-                shift
+                j=$(( $i + 1 ))
+                PURPOSE="${REMAINING_OPTS[$j]}"
+                i=$(( $i + 2 ))
                 ;;
             -M|--email)
-                CONTACT="$2"
-                shift
-                shift
+                j=$(( $i + 1 ))
+                CONTACT="${REMAINING_OPTS[$j]}"
+                i=$(( $i + 2 ))
                 ;;
             -l|--location)
-                LOCATION="$2"
-                shift
-                shift
+                j=$(( $i + 1 ))
+                LOCATION="${REMAINING_OPTS[$j]}"
+                i=$(( $i + 2 ))
                 ;;
             -z|--zone)
-                ZONE="$2"
-                shift
-                shift
+                j=$(( $i + 1 ))
+                ZONE="${REMAINING_OPTS[$j]}"
+                i=$(( $i + 2 ))
                 ;;
             --server)
-                PUPPET_SERVER="$2"
-                shift
-                shift
+                j=$(( $i + 1 ))
+                PUPPET_SERVER="${REMAINING_OPTS[$j]}"
+                i=$(( $i + 2 ))
                 ;;
             --ca-server)
-                PUPPET_CA_SERVER="$2"
-                shift
-                shift
-                ;;
-            -d|--debug)
-                DEBUG="y"
-                shift
-                ;;
-            -v|--verbose)
-                VERBOSE="y"
-                shift
-                ;;
-            -q|--quiet)
-                QUIET="y"
-                RED=""
-                YELLOW=""
-                GREEN=""
-                BLUE=""
-                CYAN=""
-                NORMAL=""
-                HAS_COLORS="n"
-                shift
+                j=$(( $i + 1 ))
+                PUPPET_CA_SERVER="${REMAINING_OPTS[$j]}"
+                i=$(( $i + 2 ))
                 ;;
-            --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!"
+            *)  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
-
     if [[ -z "${ENVIRONMENT}" ]] ; then
         ENVIRONMENT=$( autodetect_env )
     fi
@@ -442,17 +268,6 @@ get_options() {
 
 }
 
-#------------------------------------------------------------------------------
-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
-
-}
-
 #------------------------------------------------------------------------------
 empty_repo_file() {
 
@@ -572,7 +387,9 @@ check_os() {
 
     info "Checking OS release ..."
     if [[ -f /etc/redhat-release ]] ; then
-        if grep --quiet --extended-regexp --ignore-case "release 8(\.|[        ]*$)" /etc/redhat-release ; then
+        if grep --quiet --perl-regexp --ignore-case "release 9\." /etc/redhat-release ; then
+            RELEASE=9
+        elif grep --quiet --perl-regexp --ignore-case 'release 8(\.|\s*$)' /etc/redhat-release ; then
             RELEASE=8
         elif grep --quiet --ignore-case "release 7\." /etc/redhat-release ; then
             RELEASE=7
@@ -787,6 +604,11 @@ main() {
 
     get_options "$@"
     check_for_root
+
+    empty_line
+    info "Initializing ${CYAN}Puppet agent${NORMAL} ..."
+    empty_line
+
     check_os
     check_group_and_user
     check_installed_rpm
index 2204e14b8d8ee1269ec9e5265896c6d16fe18bbc..63a18742f5c7253bf4bc5bab470d6346639ee83f 100755 (executable)
@@ -200,6 +200,11 @@ main() {
 
     get_options "$@"
     set_locale "en_US.utf8"
+
+    empty_line
+    info "Trying to ${CYAN}register this host at Red Hat subscription management${NORMAL} ..."
+    empty_line
+
     check_for_root
     register_rhel
     mangle_repos