]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Rewrite bin/get-centos8-streams-boot for my normal framing
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 8 Jun 2021 07:53:24 +0000 (09:53 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 8 Jun 2021 07:53:24 +0000 (09:53 +0200)
bin/get-centos8-streams-boot

index ee9e6858217fd741f695f197bb45ddc9006e880e..8f94da3abce49e041e024819c3ab46c25eb20c29 100755 (executable)
@@ -8,9 +8,9 @@ DEBUG="n"
 QUIET='n'
 SIMULATE="n"
 
-VERSION="0.2.0"
+VERSION="0.2.1"
 
-BASENAME="$(basename ${0})"
+BASE_NAME="$(basename ${0})"
 BASE_DIR="$(dirname ${0})"
 
 declare -a MIRROR_URLS=(
@@ -40,6 +40,10 @@ HAS_MOUNTED="n"
 MOUNTPOINT="/var/tmp/centos8-stream-${ARCH}"
 IMAGE_ROOT="/var/www/cobbler/ks_mirror"
 IMAGE_DIR="${IMAGE_ROOT}/CentOS-8-Stream-${ARCH}"
+LAST_TSTAMP="0"
+LOGFILE=
+LOG_ROOTDIR="/var/log"
+LOG_BASENAME="update-centos8-streams-boot.log"
 
 HAS_TTY='y'
 
@@ -115,9 +119,9 @@ detect_color() {
 #------------------------------------------------------------------------------
 usage() {
     cat <<-EOF
-       Usage: ${BASENAME} [-s|--simulate] [-d|--debug] [[-v|--verbose] | [-q|--quiet]] [--nocolor]
-              ${BASENAME} [-h|--help]
-              ${BASENAME} [-V|--version]
+       Usage: ${BASE_NAME} [-s|--simulate] [-d|--debug] [[-v|--verbose] | [-q|--quiet]] [--nocolor]
+              ${BASE_NAME} [-h|--help]
+              ${BASE_NAME} [-V|--version]
 
            Options:
                -s|--simulate   Simulation mode - dont apply any changes.
@@ -139,7 +143,7 @@ get_options() {
     local long_options="simulate,debug,verbose,quiet,help,version"
 
     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
@@ -223,8 +227,189 @@ get_options() {
 
 }
 
-LAST_TSTAMP="0"
+#------------------------------------------------------------------------------
+my_date() {
+    date --rfc-3339=seconds
+}
+
+#------------------------------------------------------------------------------
+debug() {
+    if [[ "${VERBOSE}" != "y" ]] ; then
+        return 0
+    fi
+    local ts=$(my_date)
+    if [[ -n "${LOGFILE}" ]] ; then
+        echo -e "[${ts}] [${BASE_NAME}:${CYAN}DEBUG${NORMAL}]: $@" >>"${LOGFILE}"
+    fi
+    echo -e " * [${ts}] [${BASE_NAME}:${CYAN}DEBUG${NORMAL}]: $@" >&2
+}
+
+#------------------------------------------------------------------------------
+info() {
+    local ts=$(my_date)
+    if [[ -n "${LOGFILE}" ]] ; then
+        echo -e "[${ts}] [${BASE_NAME}:${GREEN}INFO${NORMAL}] : $@" >>"${LOGFILE}"
+    fi
+    if [[ "${QUIET}" != "y" ]] ; then
+        echo -e " ${GREEN}*${NORMAL} [${ts}] [${BASE_NAME}:${GREEN}INFO${NORMAL}] : $@" >&2
+    fi
+}
+
+#------------------------------------------------------------------------------
+warn() {
+    local ts=$(my_date)
+    if [[ -n "${LOGFILE}" ]] ; then
+        echo -e "[${ts}] [${BASE_NAME}:${YELLOW}WARN${NORMAL}] : $@" >>"${LOGFILE}"
+    fi
+    echo -e " ${YELLOW}*${NORMAL} [${ts}] [${BASE_NAME}:${YELLOW}WARN${NORMAL}] : $@" >&2
+}
+
+#------------------------------------------------------------------------------
+error() {
+    local ts=$(my_date)
+    if [[ -n "${LOGFILE}" ]] ; then
+        echo -e "[${ts}] [${BASE_NAME}:${RED}ERROR${NORMAL}]: $@" >>"${LOGFILE}"
+    fi
+    echo -e " ${RED}*${NORMAL} [${ts}] [${BASE_NAME}:${RED}ERROR${NORMAL}]: $@" >&2
+}
+
+#------------------------------------------------------------------------------
+RM() {
+
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        debug "Simulated removing of: $*"
+        return
+    fi
+    if [[ "${VERBOSE}" != "y" ]] ; then
+        rm "$@"
+    else
+        rm --verbose "$@"
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+purge() {
+
+    if [[ "${VERBOSE}" != "y" ]] ; then
+        rm "$@"
+    else
+        rm --verbose "$@"
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+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}"
+        LC_PAPER="${new_locale}"
+        LC_NAME="${new_locale}"
+        LC_ADDRESS="${new_locale}"
+        LC_TELEPHONE="${new_locale}"
+        LC_MEASUREMENT="${new_locale}"
+        LC_IDENTIFICATION="${new_locale}"
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+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
+
+}
+
+#------------------------------------------------------------------------------
+check_for_root() {
+    local my_id=$( id -u )
+    if [[ "${my_id}" != "0" ]] ; then
+        error "You must be ${RED}root${NORMAL} to execute this script."
+        echo >&2
+        exit 1
+    fi
+}
+
+#------------------------------------------------------------------------------
+check_preferences() {
+
+    info "Checking preferences ..."
+    local all_ok="y"
+
+    check_for_root
+
+}
+
+#------------------------------------------------------------------------------
 create_urls() {
 
     local omit_url="${1:-}"
@@ -252,6 +437,7 @@ create_urls() {
     return 0
 }
 
+#------------------------------------------------------------------------------
 perform_mirror() {
 
     local mirror="$1"
@@ -259,7 +445,7 @@ perform_mirror() {
 
     local line=$( curl -s -L "${checksum_url}" | grep -v '#' | grep -- '-boot.iso' )
     if [[ -z "${line}" ]] ; then
-        echo "Image not found."
+        info "Image not found on '${CYAN}${checksum_url}${NORMAL}'."
         return 1
     fi
     # echo "Line: ${line}"
@@ -271,38 +457,74 @@ perform_mirror() {
     local tstamp=$( echo "${base_name}" | sed -e 's/CentOS-Stream-8-x86_64-\([0-9][0-9]*\)-.*/\1/i' )
 
     if [[ "${tstamp}" -le "${LAST_TSTAMP}" ]] ; then
-        echo "Timestamp '${tstamp}' is too old."
+        info "Timestamp '${CYAN}${tstamp}${NORMAL}' of mirror '${CYAN}${mirror}${NORMAL}'is too old."
         return 1
     fi
 
-    echo "$( cat <<-EOF
+    local out="$( cat <<-EOF
        Performing:
            Image:     ${image_url}
            Timestamp: ${tstamp}
            Check:     ${check_method}sum -> '${checksum}'
        EOF
     )"
-    echo
+    info "${out}"
+    empty_line
 
     return 0
 
 }
 
-create_urls
-while [[ "${#URLS[*]}" -ge 1 ]] ; do
+#------------------------------------------------------------------------------
+do_update() {
+
+    local len=
+    local index=
+    local base_url=
 
-    LEN="${#URLS[*]}"
-    INDEX=$(( ${RANDOM} % ${LEN} ))
-    BASE_URL="${URLS[${INDEX}]}"
-    echo "Trying '${BASE_URL}' ..."
-    echo
-    if perform_mirror "${BASE_URL}" ; then
-        exit 0
-    fi
-    sleep 2
-    create_urls "${BASE_URL}"
+    create_urls
+
+    while [[ "${#URLS[*]}" -ge 1 ]] ; do
+
+        len="${#URLS[*]}"
+        index=$(( ${RANDOM} % ${len} ))
+        base_url="${URLS[${index}]}"
+        info "Trying '${CYAN}${base_url}${NORMAL}' ..."
+        empty_line
+        if perform_mirror "${base_url}" ; then
+            return 0
+        fi
+        sleep 1
+        create_urls "${base_url}"
+
+    done
+
+}
+
+#------------------------------------------------------------------------------
+main() {
+
+    detect_color
+
+    get_options "$@"
+    umask 0022
+
+    check_preferences
+    LOGFILE="${LOG_ROOTDIR}/${LOG_BASENAME}"
+
+    empty_line
+    dline
+    info "Starting update of boot environment of '${GREEN}CentOS-8 Stream${NORMAL}'."
+    empty_line
+
+    do_update
+
+    info "Finished."
+}
+
+main "$@"
 
-done
+exit 0
 
 
 # vim: ts=4 et list