From: Frank Brehm Date: Thu, 19 Oct 2023 09:50:58 +0000 (+0200) Subject: Refactoring postinstall-scripts/create-motd to use functions.rc. X-Git-Tag: 1.8.0^2~30 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=ec1c25b6f8bb0cce470708f22af778569ed92e09;p=pixelpark%2Fcreate-terraform.git Refactoring postinstall-scripts/create-motd to use functions.rc. --- diff --git a/postinstall-scripts/create-motd b/postinstall-scripts/create-motd index 5334854..de758c6 100755 --- a/postinstall-scripts/create-motd +++ b/postinstall-scripts/create-motd @@ -3,82 +3,18 @@ set -e set -u -export LC_ALL="en_US.utf8" -export LANG="en_US.utf8" - -VERBOSE="n" -DEBUG="n" -QUIET='n' - -# console colors: -RED="" -YELLOW="" -GREEN="" -BLUE="" -CYAN="" -NORMAL="" - -HAS_TTY='y' -HAS_COLORS="n" - -VERSION="0.7" - -BASENAME="$(basename ${0})" -BASE_DIR="$(dirname ${0})" - -#------------------------------------------------------------------- -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 +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 -} detect_color @@ -99,8 +35,6 @@ get_ip() { } - - NODENAME=$(hostname -s) PURPOSE="Customer project" FQDN=$( hostname --fqdn ) @@ -127,19 +61,18 @@ PUPPET_TIER="production" #------------------------------------------------------------------------------ -description() { - cat <<-EOF +DESCRIPTION=$( cat <<-EOF Prints out an initial version of a MOTD. - EOF -} + EOF +) #------------------------------------------------------------------------------ usage() { cat <<-EOF - Usage: ${BASENAME} [-d|--debug] [-v|--verbose] [--nocolor] [] - ${BASENAME} [-h|--help] - ${BASENAME} [-V|--version] + Usage: ${BASE_NAME} [Options ...] + ${BASE_NAME} [-h|--help] + ${BASE_NAME} [-V|--version] Options: -n|--node NODENAME @@ -170,26 +103,22 @@ usage() { The Puppet Environment, Default: '${PUPPET_ENV}'. -R|--role ROLE The Puppet Role, Default: '${PUPPET_ROLE}'. -T|--tier TIER The Puppet Tier, Default: '${PUPPET_TIER}'. - -d|--debug Debug output (bash -x). - -v|--verbose Set verbosity on. - --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}" } #------------------------------------------------------------------------------ get_options() { local tmp= - local base_dir= - local short_options="n:p:D:l:i:H:o:e:c:z:C:P:E:R:T:dvhV" - local long_options="node:,purpose:,domain:,location:,ip:,hardware:,owner:," - long_options+="email:,comments:,zone:,customer:,project:,env:,environment:,role:," - long_options+="tier:,debug,verbose,nocolor,help,version" + local short_options="n:p:D:l:i:H:o:e:c:z:C:P:E:R:T:${STD_SHORT_OPTIONS}" + local long_options="node:,purpose:,domain:,location:,ip:,hardware:,owner:,email:," + long_options+="comments:,zone:,customer:,project:,env:,environment:,role:,tier:," + long_options+="${STD_LONG_OPTIONS}" 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 @@ -199,213 +128,102 @@ get_options() { # 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 p= + local len="${#REMAINING_OPTS[*]}" + local i="0" + local j= + local arg= + while [[ "$i" -lt "${len}" ]] ; do - while true ; do - case "$1" in + arg="${REMAINING_OPTS[$i]}" + + case "${arg}" in -n|--node) - NODENAME="$2" - shift - shift + j=$(( $i + 1 )) + NODENAME="${REMAINING_OPTS[$j]}" + i=$(( $i + 2 )) ;; -p|--purpose) - PURPOSE="$2" - shift - shift + j=$(( $i + 1 )) + PURPOSE="${REMAINING_OPTS[$j]}" + i=$(( $i + 2 )) ;; -D|--domain) - DOMAIN="$2" - shift - shift + j=$(( $i + 1 )) + DOMAIN="${REMAINING_OPTS[$j]}" + i=$(( $i + 2 )) ;; -l|--location) - LOCATION="$2" - shift - shift + j=$(( $i + 1 )) + LOCATION="${REMAINING_OPTS[$j]}" + i=$(( $i + 2 )) ;; -i|--ip) - IP="$2" - shift - shift + j=$(( $i + 1 )) + IP="${REMAINING_OPTS[$j]}" + i=$(( $i + 2 )) ;; -H|--hardware) - HARDWARE="$2" - shift - shift + j=$(( $i + 1 )) + HARDWARE="${REMAINING_OPTS[$j]}" + i=$(( $i + 2 )) ;; -o|--owner) - OWNER="$2" - shift - shift + j=$(( $i + 1 )) + OWNER="${REMAINING_OPTS[$j]}" + i=$(( $i + 2 )) ;; -e|--email) - CONTACT="$2" - shift - shift + j=$(( $i + 1 )) + CONTACT="${REMAINING_OPTS[$j]}" + i=$(( $i + 2 )) ;; -c|--comments) - COMMENTS="$2" - shift - shift + j=$(( $i + 1 )) + COMMENTS="${REMAINING_OPTS[$j]}" + i=$(( $i + 2 )) ;; -z|--zone) - ZONE="$2" - shift - shift + j=$(( $i + 1 )) + ZONE="${REMAINING_OPTS[$j]}" + i=$(( $i + 2 )) ;; -C|--customer) - PUPPET_CUSTOMER="$2" - shift - shift + j=$(( $i + 1 )) + PUPPET_CUSTOMER="${REMAINING_OPTS[$j]}" + i=$(( $i + 2 )) ;; -P|--project) - PUPPET_PROJECT="$2" - shift - shift + j=$(( $i + 1 )) + PUPPET_PROJECT="${REMAINING_OPTS[$j]}" + i=$(( $i + 2 )) ;; -E|--env|--environment) - PUPPET_ENV="$2" - shift - shift + j=$(( $i + 1 )) + PUPPET_ENV="${REMAINING_OPTS[$j]}" + i=$(( $i + 2 )) ;; -R|--role) - PUPPET_ROLE="$2" - shift - shift + j=$(( $i + 1 )) + PUPPET_ROLE="${REMAINING_OPTS[$j]}" + i=$(( $i + 2 )) ;; -T|--tier) - PUPPET_TIER="$2" - shift - shift - ;; - -d|--debug) - DEBUG="y" - shift - ;; - -v|--verbose) - VERBOSE="y" - shift - ;; - --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 + j=$(( $i + 1 )) + PUPPET_TIER="${REMAINING_OPTS[$j]}" + i=$(( $i + 2 )) ;; - --) 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 - -} - -######################################### -# Some often used funktions - -#------------------------------------------------------------------------------ -my_date() { - date --rfc-3339=seconds -} - -#------------------------------------------------------------------------------ -debug() { - if [[ "${VERBOSE}" != "y" ]] ; then - return 0 - fi - echo -e " * [$(my_date)] [${BASENAME}:${CYAN}DEBUG${NORMAL}]: $@" -} - -#------------------------------------------------------------------------------ -info() { - if [[ "${QUIET}" == "y" ]] ; then - return 0 - fi - echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@" -} - -#------------------------------------------------------------------------------ -warn() { - echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $@" >&2 -} - -#------------------------------------------------------------------------------ -error() { - echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2 -} - -#------------------------------------------------------------------------------ -empty_line() { - if [[ "${QUIET}" == "y" ]] ; then - return 0 - fi - echo -} -#------------------------------------------------------------------------------ -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}" - # shellcheck disable=SC2034 - LC_PAPER="${new_locale}" - # shellcheck disable=SC2034 - LC_NAME="${new_locale}" - # shellcheck disable=SC2034 - LC_ADDRESS="${new_locale}" - # shellcheck disable=SC2034 - LC_TELEPHONE="${new_locale}" - # shellcheck disable=SC2034 - LC_MEASUREMENT="${new_locale}" - # shellcheck disable=SC2034 - LC_IDENTIFICATION="${new_locale}" - fi - } #------------------------------------------------------------------------------ @@ -584,10 +402,11 @@ generate() { main() { get_options "$@" + set_locale "en_US.utf8" - echo + empty_line >&2 info "Trying to ${CYAN}generate /etc/motd${NORMAL} ..." - echo + empty_line >&2 generate diff --git a/postinstall-scripts/register-rhel b/postinstall-scripts/register-rhel index 63a1874..a34cccf 100755 --- a/postinstall-scripts/register-rhel +++ b/postinstall-scripts/register-rhel @@ -73,7 +73,7 @@ get_options() { declare -p REMAINING_ARGS fi - local len="${#REMAINING_OPTS[*]}" + local len="${#REMAINING_OPTS[*]}" local i="0" local j= local arg=