]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Adding postinstall-scripts/unregister-rhel for unregistring from RHEL on destroying...
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 20 Oct 2023 12:04:49 +0000 (14:04 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 20 Oct 2023 12:04:49 +0000 (14:04 +0200)
postinstall-scripts/unregister-rhel [new file with mode: 0755]

diff --git a/postinstall-scripts/unregister-rhel b/postinstall-scripts/unregister-rhel
new file mode 100755 (executable)
index 0000000..a44cb1c
--- /dev/null
@@ -0,0 +1,149 @@
+#!/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=
+RHSM_PWD_FILE="${BIN_DIR}/rhsm-user-passwd"
+SM_BIN='subscription-manager'
+
+detect_color
+
+#------------------------------------------------------------------------------
+DESCRIPTION=$( cat <<-EOF
+       Unregisters the current host from the Subscription management of RedHat.
+
+       EOF
+)
+
+#------------------------------------------------------------------------------
+usage() {
+    cat <<-EOF
+       Usage: ${BASE_NAME} ${STD_USAGE}
+              ${BASE_NAME} [-h|--help]
+              ${BASE_NAME} [-V|--version]
+
+           Options:
+       EOF
+
+       echo "${STD_USAGE_MSG}"
+}
+
+#------------------------------------------------------------------------------
+get_options() {
+
+    local tmp=
+    local short_options="${STD_SHORT_OPTIONS}"
+    local long_options="${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
+            *)  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
+
+}
+
+#------------------------------------------------------------------------------
+unregister_rhel() {
+
+    local subscr_active=$( "${SM_BIN}" list --consumed --no-progress-messages | \
+                           grep -i '^Active:' | \
+                           sed -e 's/Active:[  ]*//i'  | \
+                           tr '[:upper:]' '[:lower:]' )
+
+    if [[ "${subscr_active}" == 'true' ]] ; then
+
+        local -a cmd=( "${SM_BIN}" 'unregister' )
+        cmd+=( '--no-progress-messages' )
+
+        exec_cmd "${cmd[@]}"
+
+    else
+        info "Current host is already unregistered."
+    fi
+
+}
+
+################################################################################
+##
+## Main
+##
+################################################################################
+
+#------------------------------------------------------------------------------
+main() {
+
+    get_options "$@"
+    set_locale "en_US.utf8"
+
+    empty_line
+    info "Trying to ${CYAN}unregister this host from Red Hat subscription management${NORMAL} ..."
+    empty_line
+
+    check_for_root
+    unregister_rhel
+
+}
+
+main "$@"
+
+exit 0
+
+# vim: list