From 58abe14a53096d08dd3ecc744f7f9d62fc9030ee Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 17 Jun 2020 16:25:50 +0200 Subject: [PATCH] Adding bin/create-motd.sh --- bin/create-motd.sh | 434 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 434 insertions(+) create mode 100755 bin/create-motd.sh diff --git a/bin/create-motd.sh b/bin/create-motd.sh new file mode 100755 index 0000000..d398457 --- /dev/null +++ b/bin/create-motd.sh @@ -0,0 +1,434 @@ +#!/bin/bash +# +# Author: Frank Brehm +# + +# +# Generating /etc/motd like: +# +# +----------------------------------------------------------------+ +# | Nodename: cobbler Purpose: Cobbler Install Server | +# | Domain: pixelpark.com Location: L105 | +# | Network: 192.168.88.8 Owner: Pixelpark GmbH | +# | Hardware: vmware (x86_64) Contact: 8x5@pixelpark.com | +# | OS: CentOS 7.5.1804 Zone-Host: VMWare | +# | Puppet Env: test Customer: pixelpark | +# | Puppet Role: default Project: cobbler | +# +----------------------------------------------------------------+ +# + +set -e +set -u + +export LC_ALL="en_US.utf8" +export LANG="en_US.utf8" + +VERBOSE="n" +DEBUG="n" +QUIET='n' + +VERSION="0.9.9" + +BASENAME="$(basename ${0})" +BASE_DIR="$(dirname ${0})" +IP= + +#------------------------------------------------------------------------------ +get_ip() { + local iface= + + IP= + + for iface in $( ip -oneline link show | grep '^[^ ]' | tail -n +2 | awk '{print $2}' | sed -e 's/:$//' ); do + + IP=$( ip -oneline addr show dev eth0 | \ + grep -P -o '\s+inet\s+(\S+)' | \ + head -n 1 | \ + sed -e 's/[ ]*inet[ ][ ]*//' -e 's|/.*||' ) + if [[ -n "${IP}" ]] ; then + break + fi + + done + +} + +NODENAME=$(hostname -s) +PURPOSE="VMWare Template" +DOMAIN=$(hostname --fqdn | cut -d. -f 2- ) +LOCATION="L105" +get_ip +HARDWARE=$( uname -p ) +OWNER="Digitas Pixelpark GmbH" +CONTACT="8x5@pixelpark.com" +COMMENTS= +ZONE="N/A" +OS_NAME=$( cat /etc/os-release | grep '^PRETTY_NAME' | awk -F= '{print $2}' | sed -e 's/^"//' -e 's/"$//' ) +KERNEL=$( uname -r | cut -d. -f1,2,3 ) +PUPPET_CUSTOMER="pixelpark" +PUPPET_PROJECT="" +PUPPET_ENV="${system_status:-development}" +PUPPET_ROLE="default" + +#------------------------------------------------------------------------------ +description() { + cat <<-EOF + Prints out an initial version of a MOTD. + EOF + +} + +#------------------------------------------------------------------------------ +usage() { + cat <<-EOF + Usage: ${BASENAME} [-d|--debug] [-v|--verbose] [--nocolor] [] + ${BASENAME} [-h|--help] + ${BASENAME} [-V|--version] + + Options: + -n|--node NODENAME + The nodename in the MOTD, Default: '${NODENAME}'. + -p|--purpose PURPOSE + The purpose in the MOTD, Default: '${PURPOSE}'. + -D|--domain DOMAIN + The domain the MOTD, Default: '${DOMAIN}'. + -l|--location LOCATION + The location in the MOTD, Default: '${LOCATION}'. + -i|--ip IP The IP address in the MOTD, Default: '${IP}'. + -H|--hardware HARDWARE + The type of hardware, Default: '${HARDWARE}'. + -o|--owner OWNER + The owning Customer if this host, Default: '${OWNER}'. + -e|--email EMAIL_ADDRESS + The E-Mail-Address of the responsible Person + or Mailgroup, Default: <${CONTACT}>. + -c|--comments COMMENTS + Some additional comments to this machine. + -z|--zone ZONE_HOST + The containing zone host of this zone, VM or container, Default: '${ZONE}'. + -C|--customer PUPPET_CUSTOMER + The hiera customer used for Puppet, Default: '${PUPPET_CUSTOMER}'. + -P|--project PUPPET_PROJECT + The hiera project below the customer used for Puppet, Default: '${PUPPET_PROJECT}'. + -E|--env|--environment ENVIRONMENT + The Puppet Environment, Default: '${PUPPET_ENV}'. + -R|--role ROLE The Puppet Role, Default: '${PUPPET_ROLE}'. + -d|--debug Debug output (bash -x). + -v|--verbose Set verbosity on. + -h|--help Show this output and exit. + -V|--version prints out version number of the script and exit + EOF +} + +#------------------------------------------------------------------------------ +get_options() { + + local tmp= + local base_dir= + local short_options="n:p:D:l:i:H:o:e:c:z:C:P:E:R:dvhV" + local long_options="node:,purpose:,domain:,location:,ip:,hardware:,owner:," + long_options+="email:,comments:,zone:,customer:,project:,env:,environment:,role;," + long_options+="debug,verbose,help,version" + + set +e + tmp=$( getopt -o "${short_options}" --long "${long_options}" -n "${BASENAME}" -- "$@" ) + if [[ $? != 0 ]] ; then + echo "" >&2 + usage >&2 + exit 1 + fi + set -e + + # Note the quotes around `$TEMP': they are essential! + eval set -- "${tmp}" + + local p= + + while true ; do + case "$1" in + -n|--node) + NODENAME="$2" + shift + shift + ;; + -p|--purpose) + PURPOSE="$2" + shift + shift + ;; + -D|--domain) + DOMAIN="$2" + shift + shift + ;; + -l|--location) + LOCATION="$2" + shift + shift + ;; + -i|--ip) + IP="$2" + shift + shift + ;; + -H|--hardware) + HARDWARE="$2" + shift + shift + ;; + -o|--owner) + OWNER="$2" + shift + shift + ;; + -e|--email) + CONTACT="$2" + shift + shift + ;; + -c|--comments) + COMMENTS="$2" + shift + shift + ;; + -z|--zone) + ZONE="$2" + shift + shift + ;; + -C|--customer) + PUPPET_CUSTOMER="$2" + shift + shift + ;; + -P|--project) + PUPPET_PROJECT="$2" + shift + shift + ;; + -E|--env|--environment) + PUPPET_ENV="$2" + shift + shift + ;; + -R|--role) + PUPPET_ROLE="$2" + shift + shift + ;; + -d|--debug) + DEBUG="y" + shift + ;; + -v|--verbose) + VERBOSE="y" + shift + ;; + -h|--help) + description + echo + usage + exit 0 + ;; + -V|--version) + echo "${BASENAME} version: ${VERSION}" + exit 0 + ;; + --) shift + break + ;; + *) echo "Internal error!" + 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}:DEBUG]: $@" +} + +#------------------------------------------------------------------------------ +info() { + if [[ "${QUIET}" == "y" ]] ; then + return 0 + fi + echo -e " * [$(my_date)] [${BASENAME}:INFO] : $@" +} + +#------------------------------------------------------------------------------ +warn() { + echo -e " * [$(my_date)] [${BASENAME}:WARN] : $@" >&2 +} + +#------------------------------------------------------------------------------ +error() { + echo -e " * [$(my_date)] [${BASENAME}:ERROR]: $@" >&2 +} + +#------------------------------------------------------------------------------ +empty_line() { + if [[ "${QUIET}" == "y" ]] ; then + return 0 + fi + echo +} + +#------------------------------------------------------------------------------ + +generate() { + + local -a left=() + local -a right=() + local -a lines=() + + local text= + + info "Generating a new /etc/motd (Message of the day) ..." >&2 + + # Texte linke Spalte + text="Nodename: ${NODENAME}" + left+=("${text}") + + text="Domain: ${DOMAIN}" + left+=("${text}") + + text="Network: ${IP}" + left+=("${text}") + + text="Hardware: ${HARDWARE}" + left+=("${text}") + + text="OS: ${OS_NAME} (${KERNEL})" + left+=("${text}") + + text="Puppet Env: ${PUPPET_ENV}" + left+=("${text}") + + text="Puppet Role: ${PUPPET_ROLE}" + left+=("${text}") + + # Texte rechte Spalte + text="Purpose: ${PURPOSE}" + right+=("${text}") + + text="Location: ${LOCATION}" + right+=("${text}") + + text="Owner: ${OWNER}" + right+=("${text}") + + text="Contact: ${CONTACT}" + right+=("${text}") + + text="Zone-Host: ${ZONE}" + right+=("${text}") + + text="Customer: ${PUPPET_CUSTOMER}" + right+=("${text}") + + text="Project: ${PUPPET_PROJECT}" + right+=("${text}") + + local length= + local max_left="1" + local max_right="1" + local max="1" + + for text in "${left[@]}" ; do + length=$( printf "${text}" | wc -c ) + if [[ "${length}" -gt "${max_left}" ]] ; then + max_left="${length}" + fi + done + if [[ "${VERBOSE}" == "y" ]] ; then + echo "Max. length left: ${max_left}" >&2 + fi + + for text in "${right[@]}" ; do + length=$( printf "${text}" | wc -c ) + if [[ "${length}" -gt "${max_right}" ]] ; then + max_right="${length}" + fi + done + if [[ "${VERBOSE}" == "y" ]] ; then + echo "Max. length right: ${max_right}" >&2 + fi + + local i= + for i in 0 1 2 3 4 5 6 ; do + text=$( printf "%-*s %-*s" "${max_left}" "${left[${i}]}" \ + "${max_right}" "${right[${i}]}" ) + lines+=("${text}") + length=$( printf "${text}" | wc -c ) + if [[ "${length}" -gt "${max}" ]] ; then + max="${length}" + fi + done + if [[ -n "${COMMENTS}" ]] ; then + text="Comments: ${COMMENTS}" + lines+=("${text}") + length=$( printf "${text}" | wc -c ) + if [[ "${length}" -gt "${max}" ]] ; then + max="${length}" + fi + fi + if [[ "${VERBOSE}" == "y" ]] ; then + echo "Max. line length: ${max}" >&2 + fi + + local ruler="+" + local i=0 + local ruler_length=$(( ${max} + 2 )) + while [[ "${i}" -lt "${ruler_length}" ]] ; do + i=$(( $i + 1 )) + ruler+="-" + done + ruler+="+" + + echo "${ruler}" + for text in "${lines[@]}" ; do + printf "| %-*s |\n" ${max} "${text}" + done + echo "${ruler}" + echo + +} + +################################################################################ +## +## Main +## +################################################################################ + +#------------------------------------------------------------------------------ +main() { + + get_options "$@" + generate + +} + +main "$@" + +exit 0 + + +# vim: ts=4 et list -- 2.39.5