]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Adding first version of scripts/get-rhel-iso-images
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 11 Aug 2023 14:13:21 +0000 (16:13 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 11 Aug 2023 14:13:21 +0000 (16:13 +0200)
scripts/get-rhel-iso-images [new file with mode: 0755]

diff --git a/scripts/get-rhel-iso-images b/scripts/get-rhel-iso-images
new file mode 100755 (executable)
index 0000000..642f577
--- /dev/null
@@ -0,0 +1,269 @@
+#!/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 "${BIN_DIR}/functions.rc" ]] ; then
+    . "${BIN_DIR}/functions.rc"
+else
+    echo "Bash resource file '${BIN_DIR}/functions.rc' not found" >&2
+    exit 5
+fi
+
+CONFIG_RC="/etc/pixelpark/${BASE_NAME}.rc"
+
+detect_color
+
+LOG_DIR="/var/log/${BASE_NAME}"
+
+DESCRIPTION=$( cat <<-EOF
+       Get the boot and DVD ISO image for RHEL from RedHat.
+
+       EOF
+)
+
+OFFLINE_TOKEN=
+ACCESS_TOKEN=
+
+ACCESS_TOKEN_URL='https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token'
+IMAGE_URL='https://api.access.redhat.com/management/v1/images/'
+
+CHECKSUM_BOOT_ISO="087f5387f28e6edf1f770ec7eb41e56823b6c3c4d4c3ef09b3b61f59440c229c"
+CHECKSUM_DVD_ISO="a18bf014e2cb5b6b9cee3ea09ccfd7bc2a84e68e09487bb119a98aa0e3563ac2"
+
+ISO_FILE_DIR='/var/lib/cobbler/iso-images'
+
+#------------------------------------------------------------------------------
+usage() {
+    cat <<-EOF
+       Usage: ${BASE_NAME} [-s|--simulate] [-d|--debug] [[-q|--quiet]|[-v|--verbose]] [--nocolor]
+              ${BASE_NAME} [-h|--help]
+              ${BASE_NAME} [-V|--version]
+
+           Common Options:
+       EOF
+
+    echo "${STD_USAGE_MSG}"
+
+}
+
+#------------------------------------------------------------------------------
+get_options() {
+
+    local tmp=
+
+    set +e
+    tmp=$( getopt -o "${STD_SHORT_OPTIONS}" \
+                    --long "${STD_LONG_OPTIONS}" \
+                    -n "${BASE_NAME}" -- "$@" )
+    # shellcheck disable=SC2181
+    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
+
+    # debug "Remaining arguments: ${CYAN}${#REMAINING_ARGS[@]}${NORMAL}"
+
+    if [[ "${#REMAINING_OPTS[@]}" -gt 0 ]] ; then
+        error "Unknown options: ${REMAINING_OPTS[*]}"
+        echo >&2
+        usage >&2
+        exit 2
+    fi
+
+    if [[ "${#REMAINING_ARGS[@]}" != "0" ]] ; then
+        local -a args=()
+        local arg=
+        local not_first=""
+        for arg in "${REMAINING_ARGS[@]}" ; do
+            if [[ "${arg}" != '--'  && "${not_first}" ]] ; then
+                args+=( "${arg}" )
+            fi
+            not_first='y'
+        done
+        error "Invalid arguments given: ${RED}${args[*]}${NORMAL}"
+        echo >&2
+        usage >&2
+        exit 1
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+check_preferences() {
+
+    info "Checking preferences ..."
+    local all_ok="y"
+
+    check_for_root
+
+    if [[ -f "${CONFIG_RC}" ]] ; then
+        debug "Sourcing '${CYAN}${CONFIG_RC}${NORMAL}' ..."
+        . "${CONFIG_RC}"
+    fi
+
+    local -a tools=('rsync' 'jq' 'curl' 'diff' 'losetup' 'mount')
+    local tool=
+
+    for tool in "${tools[@]}" ; do
+        debug "Checking for '${CYAN}${tool}${NORMAL}' ..."
+        if type -p ${tool} >/dev/null ; then
+            :
+        else
+            all_ok=""
+            error "Did not found '${RED}${tool}${NORMAL}'. Maybe not installed?"
+        fi
+    done
+
+    if [[ -z "${OFFLINE_TOKEN}" ]] ; then
+        error "No offline token given in '${CONFIG_RC}'."
+        all_ok=""
+    fi
+
+    if [[ ! -d "${ISO_FILE_DIR}" ]] ; then
+        error "Directory '${RED}${ISO_FILE_DIR}${NORMAL}' for received ISO images not found."
+        all_ok=""
+    fi
+
+    if [[ -z "${all_ok}" ]] ; then
+        exit 5
+    fi
+
+    if [[ ! -d "${LOG_DIR}" ]] ; then
+        info "Creating '${GREEN}${LOG_DIR}${NORMAL}' ..."
+        MKDIR_forced "${LOG_DIR}"
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+get_access_token() {
+
+    empty_line
+    line
+    info "Getting access token ..."
+
+    local cmd="curl -s '${ACCESS_TOKEN_URL}' -d grant_type=refresh_token -d client_id=rhsm-api "
+    cmd+="-d 'refresh_token=${OFFLINE_TOKEN}'"
+
+    local cmd_show="curl -s '${ACCESS_TOKEN_URL}' -d grant_type=refresh_token -d client_id=rhsm-api "
+    cmd_show+="-d 'refresh_token=********'"
+
+    debug "Executing: ${cmd_show}"
+
+    ACCESS_TOKEN_JS_RAW=$( eval ${cmd} )
+    ACCESS_TOKEN=$( echo "${ACCESS_TOKEN_JS_RAW}" | jq -r '.access_token' )
+    debug "Got access_token: ${ACCESS_TOKEN}"
+
+    if [[ -z "${ACCESS_TOKEN}" || "${ACCESS_TOKEN}" =~ ^null ]] ; then
+        local out=$( echo "${ACCESS_TOKEN_JS_RAW}" | jq -r '.' )
+        error "Got no access token: ${out}"
+        exit 6
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+get_iso_image() {
+
+    local checksum="$1"
+    local image_desc="$2"
+
+    empty_line
+    line
+    info "Getting ${CYAN}ISO ${image_desc} image${NORMAL} ..."
+
+    local image_url="${IMAGE_URL}${checksum}/download"
+    debug "Get image from URL: '${image_url}'."
+
+    local cmd="curl -s -H \"Authorization: Bearer ${ACCESS_TOKEN}\" \"${image_url}\""
+    local image_info=$( eval ${cmd} )
+    local image_info_out=$( echo "${image_info}" | jq . )
+
+    debug "Got image info: ${image_info_out}"
+
+    local filename=$(echo "${image_info}" | jq -r .body.filename)
+    info "Got filename: '${CYAN}${filename}${NORMAL}'"
+    local download_url=$(echo "${image_info}" | jq -r .body.href)
+    debug "Got Download-URL: ${download_url}"
+
+    if [[ -z "${filename}" || "${filename}" =~ ^null || -z "${download_url}" || "${download_url}" =~ ^null ]] ; then
+        error "Error getting image information for RHEL ${image_desc} ISO image: ${image_info_out}"
+        exit 7
+    fi
+
+    local target_file="${ISO_FILE_DIR}/${filename}"
+    local target_file_tmp="${ISO_FILE_DIR}/.~${filename}.tmp"
+    debug "Download target: '${CYAN}${target_file}${NORMAL}'."
+    info "Starting download of '${CYAN}${filename}${NORMAL}' ..."
+
+    local cmd="curl -s \"${download_url}\" -o \"${target_file_tmp}\""
+
+    if [[ "${SIMULATE}" == 'y' ]] ; then
+        info "Simulate executing: ${cmd}"
+    else
+        debug "Executing: ${cmd}"
+        eval ${cmd}
+
+        if [[ ! -s "${target_file_tmp}" ]] ; then
+            error "The downloaded ISO image '${target_file_tmp}' has zero size."
+            RM "${target_file_tmp}"
+            exit 8
+        fi
+    fi
+    info "Finished download of '${CYAN}${filename}${NORMAL}'."
+
+    if [[ -f "${target_file}" ]] ; then
+        debug "Comparing existing and downloaded file ..."
+        if [[ -f "${target_file_tmp}" ]] ; then
+            if diff --brief "${target_file}" "${target_file_tmp}" ; then
+                info "Files are identic, removing new file."
+                RM "${target_file_tmp}"
+            fi
+        fi
+    fi
+    if [[ -f "${target_file_tmp}" || "${SIMULATE}" == 'y' ]] ; then
+        debug "Renaming downloaded image to '${CYAN}${target_file}${NORMAL}' ..."
+        MV "${target_file_tmp}" "${target_file}"
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+main() {
+
+    get_options "$@"
+
+    check_preferences
+    get_access_token
+
+    get_iso_image "${CHECKSUM_BOOT_ISO}" "Boot"
+    get_iso_image "${CHECKSUM_DVD_ISO}" "DVD"
+
+    LOGFILE=
+    empty_line
+    info "Finished all."
+
+}
+
+main "$@"
+exit 0
+
+# vim: et list