From 261b9977f7bf68d3f9a843cba304af0ca80ce035 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 6 Jul 2022 17:21:28 +0200 Subject: [PATCH] Finishing first attempt of scripts/update-cobbler-distros --- scripts/functions.rc | 118 +++++++++++++++++++++++++++++++-- scripts/update-cobbler-distros | 67 ++++++++++++++++++- 2 files changed, 179 insertions(+), 6 deletions(-) diff --git a/scripts/functions.rc b/scripts/functions.rc index 2238282..c34613e 100644 --- a/scripts/functions.rc +++ b/scripts/functions.rc @@ -11,14 +11,15 @@ NORMAL="" VERSION="0.2.1" # shellcheck disable=SC2034 -STD_SHORT_OPTIONS="sdvhV" +STD_SHORT_OPTIONS="sqdvhV" # shellcheck disable=SC2034 -STD_LONG_OPTIONS="simulate,debug,verbose,nocolor,help,version" +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). - -v|--verbose Set verbosity on. + -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 @@ -28,9 +29,11 @@ STD_USAGE_MSG=$( cat <<-EOF # 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=() @@ -88,7 +91,7 @@ detect_color() { 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 + if [[ "${safe_term}" == "${term}" || "${TERM}" == "${term}" || "${TERM}" =~ .*color ]] ; then use_color="true" break fi @@ -137,6 +140,10 @@ eval_common_options() { DEBUG="y" shift ;; + -q|--quiet) + QUIET="y" + shift + ;; -v|--verbose) VERBOSE="y" shift @@ -183,6 +190,14 @@ eval_common_options() { 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 echo echo -e "${CYAN}Simulation mode!${NORMAL}" @@ -271,7 +286,9 @@ debug() { #------------------------------------------------------------------------------ info() { - echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASE_NAME}:${GREEN}INFO${NORMAL}] : $*" >&2 + if [[ "${QUIET}" != "y" ]] ; then + echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASE_NAME}:${GREEN}INFO${NORMAL}] : $*" >&2 + fi } #------------------------------------------------------------------------------ @@ -284,6 +301,16 @@ error() { echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASE_NAME}:${RED}ERROR${NORMAL}]: $*" >&2 } +#------------------------------------------------------------------------------ +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 +} + #------------------------------------------------------------------------------ RM() { @@ -299,6 +326,87 @@ RM() { } +#------------------------------------------------------------------------------ +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} +} + +#------------------------------------------------------------------------------ +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() { diff --git a/scripts/update-cobbler-distros b/scripts/update-cobbler-distros index 3f5bf6c..d23317a 100755 --- a/scripts/update-cobbler-distros +++ b/scripts/update-cobbler-distros @@ -19,9 +19,26 @@ fi detect_color +IMAGE_ROOT="/var/www/cobbler/distro_mirror" + +DIR_OWNER="apache" +DIR_GROUP="apache" + declare -a DISTRO_IDS=( 'almalinux-8' 'centos-stream-8' 'centos-stream-9' ) declare -a DISTRO_ID_UPDATE=() +declare -A SOURCE=() +declare -A TARGET=() + +SOURCE['almalinux-8']="rsync://ftp.fau.de/almalinux/8/BaseOS/x86_64/os/" +TARGET['almalinux-8']="AlmaLinux-8" + +SOURCE['centos-stream-8']="rsync://ftp.fau.de/centos/8-stream/BaseOS/x86_64/os/" +TARGET['centos-stream-8']="CentOS-Stream-8" + +SOURCE['centos-stream-9']="rsync://mirror1.hs-esslingen.de/centos-stream/9-stream/BaseOS/x86_64/os/" +TARGET['centos-stream-9']="CentOS-Stream-9" + DESCRIPTION=$( cat <<-EOF Updates the boot environments of cobbler boot environments. @@ -124,6 +141,52 @@ get_options() { exit 1 fi +} + +#------------------------------------------------------------------------------ +update_distro() { + + distro_id="$1" + + local src_url="${SOURCE[${distro_id}]}" + if [[ -z "${src_url}" ]] ; then + error "Cobbler distribution '${RED}${distro_id}${NORMAL}' has no source URL defined." + exit 8 + fi + + local target_dir_rel="${TARGET[${distro_id}]}" + if [[ -z "${target_dir_rel}" ]] ; then + error "Cobbler distribution '${RED}${distro_id}${NORMAL}' has no target directory defined." + exit 8 + fi + local target_dir="${IMAGE_ROOT}/${target_dir_rel}" + + info "Syncing from '${CYAN}${src_url}${NORMAL}' ==> '${CYAN}${target_dir}${NORMAL}' ..." + + local cmd="rsync --recursive --links --perms --times --specials --hard-links --compress" + if [[ "${VERBOSE}" == "y" ]] ; then + cmd+=" --verbose" + elif [[ "${QUIET}" == "y" ]] ; then + cmd+=" --quiet" + fi + cmd+=" --compress --stats --delete --delete-after --delete-excluded --delay-updates" + if [[ -n "${LOGFILE}" ]] ; then + cmd+=" --log-file \"${LOGFILE}\"" + fi + if [[ "${SIMULATE}" == "y" ]] ; then + cmd+=" --dry-run" + fi + cmd+=" \"${src_url}\" \"${target_dir}/\"" + debug "Executing: ${cmd}" + eval ${cmd} + + # info "Chowning '${CYAN}${target_dir}${NORMAL}' to '${CYAN}${DIR_OWNER}:${DIR_GROUP}${CYAN}' ..." + # CHOWN --recursive "${DIR_OWNER}:${DIR_GROUP}" "${target_dir}" + + return 0 + + + } #------------------------------------------------------------------------------ @@ -134,8 +197,10 @@ main() { get_options "$@" for distro_id in "${DISTRO_ID_UPDATE[@]}" ; do - echo + empty_line + line info "Updating Cobbler distribution '${GREEN}${distro_id}${NORMAL}' ..." + update_distro "${distro_id}" done } -- 2.39.5