From: Frank Brehm Date: Wed, 25 Sep 2019 11:36:10 +0000 (+0200) Subject: Adding postinstall scripts X-Git-Tag: 1.3.3~5 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=56cc5b0095926659c9e46a97eb463a8c34003ad4;p=pixelpark%2Fcreate-terraform.git Adding postinstall scripts --- diff --git a/postinstall-scripts/conf-resolver b/postinstall-scripts/conf-resolver new file mode 100755 index 0000000..666c2d6 --- /dev/null +++ b/postinstall-scripts/conf-resolver @@ -0,0 +1,403 @@ +#!/bin/bash + +set -e +set -u + +export LC_ALL="en_US.utf8" +export LANG="en_US.utf8" + +VERBOSE="n" +DEBUG="n" +QUIET='n' +DO_BACKUP="n" + +# console colors: +RED="" +YELLOW="" +GREEN="" +BLUE="" +CYAN="" +NORMAL="" + +HAS_TTY='y' +HAS_COLORS="n" + +VERSION="0.6" + +BASENAME="$(basename ${0})" +BASE_DIR="$(dirname ${0})" + +RESOLV_CONF="/etc/resolv.conf" +DOMAIN= +DATE_TPL="%Y-%m-%d_%H:%M:%S" + +declare -a SEARCH_DOMAINS=( + 'pixelpark.net' + 'pixelpark.com' +) + +declare -a NAME_SERVERS=( + '217.66.52.10' + '93.188.109.13' + '212.91.225.75' +) + +#------------------------------------------------------------------- +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 + +} +detect_color + +#------------------------------------------------------------------------------ +description() { + + local nr= + local i= + + echo "Generates a sane version of '${RESOLV_CONF}'." + echo + + printf "Included are always the search domains: " + local domain= + i=0 + nr="${#SEARCH_DOMAINS[*]}" + for domain in "${SEARCH_DOMAINS[@]}" ; do + i=$(( $i + 1 )) + printf "'${domain}'" + if [[ "${i}" != "${nr}" ]] ; then + printf ", " + fi + done + echo + + printf "As nameservers are always used: " + local ns= + i=0 + nr="${#NAME_SERVERS[*]}" + for ns in "${NAME_SERVERS[@]}" ; do + i=$(( $i + 1 )) + printf "'${ns}'" + if [[ "${i}" != "${nr}" ]] ; then + printf ", " + fi + done + echo + +} + +#------------------------------------------------------------------------------ +usage() { + cat <<-EOF + Usage: ${BASENAME} [-d|--debug] [[-v|--verbose] | [-q|--quiet]]] [-b|--backup] [-D|--domain DOMAIN] [--nocolor] + ${BASENAME} [-h|--help] + ${BASENAME} [-V|--version] + + Options: + -b|--backup Creates a backup of an existing file + of the form '${RESOLV_CONF}.${DATE_TPL}.bak'. + -D|--domain DOMAIN + Including the given domain at the first position in the search domains. + -d|--debug Debug output (bash -x). + -v|--verbose Set verbosity on. Mutually exclusive to '--quiet'. + -q|--quiet Quiet execution, only errors and warnings are shown. + --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 +} + +#------------------------------------------------------------------------------ +get_options() { + + local tmp= + local base_dir= + + set +e + tmp=$( getopt -o D:bdvqhV \ + --long domain:,backup,debug,verbose,quiet,nocolor,help,version \ + -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 + -D|--domain) + DOMAIN=$( echo "$2" | tr '[:upper:]' '[:lower:]' ) + shift + shift + ;; + -b|--backup) + DO_BACKUP="y" + shift + ;; + -d|--debug) + DEBUG="y" + shift + ;; + -v|--verbose) + VERBOSE="y" + shift + ;; + -q|--quiet) + QUIET="y" + RED="" + YELLOW="" + GREEN="" + BLUE="" + CYAN="" + NORMAL="" + HAS_COLORS="n" + 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 + ;; + --) shift + break + ;; + *) echo "Internal error!" + exit 1 + ;; + esac + done + + if [[ "${DEBUG}" = "y" ]] ; then + set -x + fi + if [[ "${VERBOSE}" == "y" && "${QUIET}" == "y" ]] ; then + error "The parameters '${RED}--verbose${NORMAL}' and '${RED}--quiet${NORMAL}' are mutually exclusive." + usage >&2 + exit 1 + 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 +} + +#------------------------------------------------------------------------------ +CP() { + local cmd="cp" + if [[ "${VERBOSE}" == "y" ]] ; then + cmd+=" --verbose" + fi + eval ${cmd} "$@" +} + +#------------------------------------------------------------------------------ +MV() { + local cmd="mv" + if [[ "${VERBOSE}" == "y" ]] ; then + cmd+=" --verbose" + fi + eval ${cmd} "$@" +} + +#------------------------------------------------------------------------------ +do_backup() { + + if [[ "${DO_BACKUP}" != "y" ]] ; then + return 0 + fi + + if [[ -f "${RESOLV_CONF}" ]] ; then + empty_line + info "Creating ${GREEN}backup of '${RESOLV_CONF}'${NORMAL} ..." + CP -p "${RESOLV_CONF}" "${RESOLV_CONF}.$( date -r ${RESOLV_CONF} +"${DATE_TPL}").bak" + fi + +} + +#------------------------------------------------------------------------------ + +generate() { + + empty_line + info "Generating a new '${GREEN}${RESOLV_CONF}${NORMAL}' ..." + + local content="# Generated by '${BASENAME}' version '${VERSION}' at: $( date --rfc-3339=seconds )" + content+=" +" + + content+="search " + + if [[ -n "${DOMAIN}" ]] ; then + if [[ "${DOMAIN}" != 'pixelpark.com' && "${DOMAIN}" != 'pixelpark.com' ]] ; then + info "Including '${GREEN}${DOMAIN}${NORMAL}' as a search domain." + content+=$( printf "${DOMAIN} " ) + else + warn "Domain '${YELLOW}${DOMAIN}${NORMAL}' is already a search domain." + fi + fi + + local domain= + local i=0 + local nr="${#SEARCH_DOMAINS[*]}" + for domain in "${SEARCH_DOMAINS[@]}" ; do + i=$(( $i + 1 )) + content+=$( printf "${domain}" ) + if [[ "${i}" != "${nr}" ]] ; then + content+=$( printf " " ) + fi + done + echo + content+=" +" + + local ns= + i=0 + for ns in "${NAME_SERVERS[@]}" ; do + content+=$( echo "nameserver ${ns}" ) + content+=" +" + done + content+=" +" + + empty_line + debug "Generated content of '${RESOLV_CONF}':" + if [[ "${VERBOSE}" == "y" ]] ; then + printf "${content}" + fi + + printf "${content}" >"${RESOLV_CONF}" + +} + + +################################################################################ +## +## Main +## +################################################################################ + +#------------------------------------------------------------------------------ +main() { + + get_options "$@" + do_backup + generate + +} + +main "$@" + +exit 0 + + + +# vim: list diff --git a/postinstall-scripts/create-motd b/postinstall-scripts/create-motd new file mode 100755 index 0000000..11ddbaf --- /dev/null +++ b/postinstall-scripts/create-motd @@ -0,0 +1,464 @@ +#!/bin/bash + +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.5" + +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 + +} +detect_color + + +#------------------------------------------------------------------------------ +get_ip() { + local iface= + + IP= + + for iface in $( ip link show | grep '^[^ ]' | tail -n +2 | awk '{print $2}' | sed -e 's/:$//' ); do + + IP=$( ip addr show dev ${iface} | grep -w inet | head -n 1 | awk '{print $2}' | sed -e 's|/.*||' ) + if [[ -n "${IP}" ]] ; then + break + fi + + done + +} + + + +NODENAME=$(hostname -s) +PURPOSE="Projekt Kunde" +DOMAIN=$(hostname --fqdn | cut -d. -f 2- ) +LOCATION="L105" +get_ip +HARDWARE=$( uname -p ) +OWNER="Publicis 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 ) +CUSTOMER="pixelpark" + +#------------------------------------------------------------------------------ +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 HIERA_CUSTOMER + The hiera customer used for Puppet, Default: '${CUSTOMER}'. + -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 +} + +#------------------------------------------------------------------------------ +get_options() { + + local tmp= + local base_dir= + local short_options="n:p:D:l:i:H:o:e:c:z:C:dvhV" + local long_options="node:,purpose:,domain:,location:,ip:,hardware:,owner:," + long_options+="email:,comments:,zone:,customer:,debug,verbose,nocolor,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) + CUSTOMER="$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 + ;; + --) 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}:${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 +} + +#------------------------------------------------------------------------------ + +generate() { + + local -a left=() + local -a right=() + local -a lines=() + + local text= + + info "Generating a new ${GREEN}/etc/motd${NORMAL} (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="" + 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: ${CUSTOMER}" + 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 ; 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: list diff --git a/postinstall-scripts/init-puppet b/postinstall-scripts/init-puppet new file mode 100755 index 0000000..a7971ee --- /dev/null +++ b/postinstall-scripts/init-puppet @@ -0,0 +1,632 @@ +#!/bin/bash + +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.9" + +BASENAME="$(basename ${0})" +BASE_DIR="$(dirname ${0})" +FQDN=$( hostname -f ) + +ENVIRONMENT= +TIER= +CUSTOMER="pixelpark" +OWNER= +PURPOSE="Projekt Kunde" +CONTACT="8x5@pixelpark.com" +OS= +RELEASE= +PKG_NAME="puppet-agent" +PUPPET_BIN="/opt/puppetlabs/bin/puppet" +PKG_INSTALLED="n" +ROLE="base_oel7" +PROJECT= + + +######################################### +# Some often used funktions + +#------------------------------------------------------------------- +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 + +} +detect_color + +#------------------------------------------------------------------------------ +my_date() { + date +'%F %T.%N %:::z' +} + +#------------------------------------------------------------------------------ +debug() { + if [[ "${VERBOSE}" != "y" ]] ; then + return 0 + fi + echo -e " * [$(my_date)] [${BASENAME}:${CYAN}DEBUG${NORMAL}]: $@" >&2 +} + +#------------------------------------------------------------------------------ +info() { + if [[ "${QUIET}" == "y" ]] ; then + return 0 + fi + echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@" >&2 +} + +#------------------------------------------------------------------------------ +warn() { + echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $@" >&2 +} + +#------------------------------------------------------------------------------ +error() { + echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2 +} + +#------------------------------------------------------------------------------ +MKDIR() { + + local cmd="mkdir -p" + if [[ "${VERBOSE}" == "y" ]] ; then + cmd="mkdir -v -p" + fi + debug "Executing: ${cmd}" "$@" + eval ${cmd} "$@" + +} + +#------------------------------------------------------------------------------ +RM() { + + local cmd="rm" + if [[ "${VERBOSE}" == "y" ]] ; then + cmd="rm -v" + fi + debug "Executing: ${cmd} $*" + eval ${cmd} "$@" + +} + +#------------------------------------------------------------------------------ +description() { + cat <<-EOF + Initialize a Puppet5 agent and start it. + + This script works only for Red Hat Linux and its derivates. + + EOF + +} + +#------------------------------------------------------------------------------ +usage() { + + cat <<-EOF + Usage: ${BASENAME} [OPTIONS ...] + ${BASENAME} [-h|--help] + ${BASENAME} [-V|--version] + + Options: + -E|--env|--environment ENVIRONMENT + The Puppet environment to use. If not given, an autodetection + by the hostname is executed. + -C|--customer|--hiera-customer HIERA_CUSTOMER + The hiera customer used for Puppet, Default: '${CUSTOMER}'. + -p|--project CUSTOMER_PROJECT + The customer related name of the project - the next level + in the Hiera structure beyond the customer name. + If not given, the Hiera customer name will be used. + -O|--owner OWNER + The owning Customer if this host. If not given, + the Hiera customer will be used. + -T|--tier TIER The tier of the production state. If not given, + the Puppet environment will be used. + -R|--role ROLE The Puppet role of the host, Default: '${ROLE}'. + -P|--purpose PURPOSE + The purpose of the host, Default: '${PURPOSE}'. + -M|--email MAIL_ADDRESS + The E-Mail-Address of the responsible Person + or Mailgroup, Default: <${CONTACT}>. + -d|--debug Debug output (bash -x). + -v|--verbose Set verbosity on. + -q|--quiet Quiet execution. Mutually exclusive to --verbose. + --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 + +} + +#------------------------------------------------------------------------------ +autodetect_env() { + + local hostname="$( hostname )" + local my_env="production" + + if [[ "${hostname}" =~ ^dev(elop(ment)?)?- ]] ; then + my_env="development" + elif [[ "${hostname}" =~ ^(tst|test|int|stage|qas)[0-9]*- ]] ; then + my_env="test" + fi + + echo "${my_env}" + +} + +#------------------------------------------------------------------------------ +get_options() { + + local tmp= + local base_dir= + local short_options="E:C:p:O:T:R:P:M:dvqhV" + local long_options="env:,environment:,customer:,hiera-customer:,project:,owner:,tier:,role:," + long_options+="purpose:,email:,debug,verbose,quiet,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 + -E|--env|--environment) + ENVIRONMENT=$( echo "$2" | tr '[:upper:]' '[:lower:]' ) + shift + shift + ;; + -C|--customer|--hiera-customer) + CUSTOMER=$( echo "$2" | tr '[:upper:]' '[:lower:]' ) + shift + shift + ;; + -p|--project) + PROJECT=$( echo "$2" | tr '[:upper:]' '[:lower:]' ) + shift + shift + ;; + -O|--owner) + OWNER="$2" + shift + shift + ;; + -T|--tier) + TIER=$( echo "$2" | tr '[:upper:]' '[:lower:]' ) + shift + shift + ;; + -R|--role) + ROLE=$( echo "$2" | tr '[:upper:]' '[:lower:]' ) + shift + shift + ;; + -P|--purpose) + PURPOSE="$2" + shift + shift + ;; + -M|--email) + CONTACT="$2" + shift + shift + ;; + -d|--debug) + DEBUG="y" + shift + ;; + -v|--verbose) + VERBOSE="y" + shift + ;; + -q|--quiet) + QUIET="y" + RED="" + YELLOW="" + GREEN="" + BLUE="" + CYAN="" + NORMAL="" + HAS_COLORS="n" + 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 + ;; + --) shift + break + ;; + *) echo "Internal error!" + exit 1 + ;; + esac + done + + if [[ "${DEBUG}" = "y" ]] ; then + set -x + fi + + if [[ "${VERBOSE}" == "y" && "${QUIET}" == "y" ]] ; then + echo "Options '${RED}--verbose${NORMAL}' and '${RED}--quiet${NORMAL}' are mutually exclusive." >&2 + echo >&2 + usage >&2 + exit 1 + fi + + if [[ -z "${ENVIRONMENT}" ]] ; then + ENVIRONMENT=$( autodetect_env ) + fi + + if [[ -z "${PROJECT}" ]] ; then + PROJECT="${CUSTOMER}" + fi + + if [[ -z "${OWNER}" ]] ; then + OWNER="${CUSTOMER}" + fi + + if [[ -z "${TIER}" ]] ; then + TIER="${ENVIRONMENT}" + fi + + if [[ -z "${ROLE}" ]] ; then + echo "Option '${RED}-R|--role${NORMAL}' may not be empty." >&2 + echo >&2 + usage >&2 + exit 1 + fi + + debug "Using following options:" + if [[ "${VERBOSE}" == "y" ]] ; then + cat <<-EOF + FQDN: ${GREEN}${FQDN}${NORMAL} + Puppet environment: ${GREEN}${ENVIRONMENT}${NORMAL} + Tier: ${GREEN}${TIER}${NORMAL} + Role: ${GREEN}${ROLE}${NORMAL} + Hiera customer: ${GREEN}${CUSTOMER}${NORMAL} + Owner: ${GREEN}${OWNER}${NORMAL} + Contact address: ${GREEN}${CONTACT}${NORMAL} + Purpose: ${GREEN}${PURPOSE}${NORMAL} + + EOF + fi + +} + +#------------------------------------------------------------------------------ +check_for_root() { + + debug "Checking for root execution ..." + if [[ "$( id -u )" != "0" ]] ; then + error "Only user '${RED}root${NORMAL}' may execute this script." + exit 1 + fi + +} + +#------------------------------------------------------------------------------ +check_os() { + + info "Checking operating system ..." + + if grep -i centos /etc/*-release >/dev/null 2>&1 ; then + OS=centos + elif grep -i oracle /etc/*-release >/dev/null 2>&1 ; then + OS=oracle + elif grep -i 'red hat' /etc/*-release >/dev/null 2>&1 ; then + OS=rhel + else + error "${RED}Operating system not supported${NORMAL} by this script." + exit 1 + fi + debug "Found OS '${OS}'." + + info "Checking OS release ..." + if [[ -f /etc/redhat-release ]] ; then + if grep --quiet -i "release 7" /etc/redhat-release ; then + RELEASE=7 + elif grep --quiet -i "release 6" /etc/redhat-release ; then + RELEASE=6 + else + error "Found invalid OS release: $(cat /etc/redhat-release)" + exit 1 + fi + else + error "File '/etc/redhat-release' not found." + exit 5 + fi + debug "Found OS Release '${RELEASE}'." + +} + +#------------------------------------------------------------------------------ +check_group_and_user() { + + local cmd= + info "Checking for existence of ${GREEN}group 'puppet'${NORMAL} ..." + if getent group puppet >/dev/null ; then + info "Group 'puppet' is already existing, GID=$(id -g puppet)." + else + info "Creating group 'puppet' ..." + cmd="groupadd -g 63000 puppet" + debug "Executing: ${cmd}" + eval ${cmd} + fi + + info "Checking for existence of u${GREEN}ser 'puppet'${NORMAL} ..." + if getent passwd puppet >/dev/null ; then + info "User 'puppet' is already existing, UID=$(id -u puppet)." + else + info "Creating user 'puppet' ..." + cmd="useradd -u 63000 -g puppet -d /var/lib/puppet -c \"Puppet configuration management\" -s /sbin/nologin puppet" + debug "Executing: ${cmd}" + eval ${cmd} + fi + +} + +#------------------------------------------------------------------------------ +check_installed_rpm() { + + info "Checking for an already install ${GREEN}package '${PKG_NAME}'${NORMAL} ..." + if rpm -qa | grep -i "${PKG_NAME}" ; then + info "Puppet package is already installed." + PKG_INSTALLED="y" + else + warn "${YELLOW}Package '${PKG_NAME}'${NORMAL} not installed. Puppet agent cannot be executed or enabled." + error "Please install package '${YELLOW}${PKG_NAME}${NORMAL}' manually and enable and start the Puppet agent." + fi + +} + +#------------------------------------------------------------------------------ +generate_puppetconf() { + + local conf_dir="/etc/puppetlabs/puppet" + local conf_file="${conf_dir}/puppet.conf" + + if [[ ! -d "${conf_dir}" ]] ; then + info "Ensuring existence of '${GREEN}${conf_dir}${NORMAL}' ..." + MKDIR "${conf_dir}" + fi + + info "Generating Puppet configuration '${GREEN}${conf_file}${NORMAL}' ..." + local conf=$( cat <<-EOF + [main] + ca_ttl = 10y + [agent] + # The file in which puppetd stores a list of the classes + # associated with the retrieved configuratiion. Can be loaded in + # the separate "puppet" executable using the "--loadclasses" + # option. + # The default value is '\$confdir/classes.txt'. + classfile = \$vardir/classes.txt + + environment = ${ENVIRONMENT} + report = true + pluginsync = true + splay = true + use_srv_records = true + srv_domain = pixelpark.info + pluginsource = puppet:///plugins + pluginfactsource = puppet:///pluginfacts + EOF + ) + + debug "Generatet content of '${conf_file}':\n${conf}" + echo "${conf}" > "${conf_file}" + +} + +#------------------------------------------------------------------------------ +generate_facts() { + + info "Generating ${GREEN}YAML files for facter${NORMAL} ..." + local facts_dir="/etc/puppetlabs/facter/facts.d" + if [[ ! -d "${facts_dir}" ]] ; then + info "Creating '${GREEN}${facts_dir}${NORMAL}' ..." + MKDIR "${facts_dir}" + fi + + local yfile="${facts_dir}/customer.yaml" + info "Generating '${GREEN}${yfile}${NORMAL}' ..." + local content=$( cat <<-EOF + --- + customer: ${CUSTOMER} + project: ${PROJECT} + EOF + ) + debug "Generatet content of '${yfile}':\n${content}" + echo "${content}" > "${yfile}" + + yfile="${facts_dir}/host.yaml" + info "Generating '${GREEN}${yfile}${NORMAL}' ..." + content=$( cat <<-EOF + --- + pp_purpose: ${PURPOSE} + pp_location: L105 + pp_owner: ${OWNER} + pp_contact: ${CONTACT} + pp_zonehost: VMWare + EOF + ) + debug "Generatet content of '${yfile}':\n${content}" + echo "${content}" > "${yfile}" + + yfile="${facts_dir}/tier.yaml" + info "Generating '${GREEN}${yfile}${NORMAL}' ..." + content=$( cat <<-EOF + --- + tier: ${TIER} + EOF + ) + debug "Generatet content of '${yfile}':\n${content}" + echo "${content}" > "${yfile}" + + yfile="${facts_dir}/role.yaml" + info "Generating '${GREEN}${yfile}${NORMAL}' ..." + content=$( cat <<-EOF + --- + role: ${ROLE} + EOF + ) + debug "Generatet content of '${yfile}':\n${content}" + echo "${content}" > "${yfile}" + + yfile="${facts_dir}/initial_install.yaml" + info "Generating '${GREEN}${yfile}${NORMAL}' ..." + content=$( cat <<-EOF + --- + initial_install: true + EOF + ) + debug "Generatet content of '${yfile}':\n${content}" + echo "${content}" > "${yfile}" + + wrong_file="${facts_dir}/host" + if [[ -r "${wrong_file}" ]] ; then + info "Removing '${GREEN}${wrong_file}${NORMAL}' ..." + RM "${wrong_file}" + fi + +} + +#------------------------------------------------------------------------------ +run_agent() { + + info "${GREEN}Running Puppet agent${NORMAL} in --noop mode ..." + + local cmd="${PUPPET_BIN} agent --test --noop || true" + + debug "Executing: ${cmd}" + eval ${cmd} + + cat <<-EOF + If you got messages about an invalid certificate, go with SSH to + ${GREEN}puppetca01.pixelpark.com${NORMAL} + and sign the certificate for '${GREEN}${FQDN}${NORMAL}'. + + EOF + +} + +################################################################################ +## +## Main +## +################################################################################ + +#------------------------------------------------------------------------------ +main() { + + get_options "$@" + check_for_root + check_os + check_group_and_user + check_installed_rpm + + generate_puppetconf + generate_facts + + run_agent + +} + +main "$@" + +exit 0 + +# vim: list diff --git a/postinstall-scripts/mk_create_motd.ksh b/postinstall-scripts/mk_create_motd.ksh new file mode 100755 index 0000000..dd0753f --- /dev/null +++ b/postinstall-scripts/mk_create_motd.ksh @@ -0,0 +1,102 @@ +#!/usr/bin/ksh +# ------------------------------------ +# Andreas Gerstenberg (Pixelpark) +# +# Berlin - 18.05.2010 +# Update - 18.05.2010 +# Update - 20.02.2012 Sebastian Kachel - make Linux confirmed +# ------------------------------------ +# Variablen, bitte ggf. anpassen +# ------------------------------------ +version="1.00" # Script Version + +nodename="$(uname -n)" +purpose="Projekt Kunde" +domain="$(hostname --fqdn | cut -d. -f2,3)" +location="L105" +network="$(getent hosts $nodename | awk '{print $1}')" +owner="Pixelpark AG" +hardware="$(prtdiag 2> /dev/null | grep "System Configuration:" | sed -e "s/^System Configuration://" | awk '{print $3" "$4" "$5" "$6}')" +check="$(prtdiag -l 2> /dev/null)" +[[ $? -gt "0" ]] && hardware="$(uname -p)" +contact="8x5@pixelpark.com" +patch="$(cat /etc/*release | head -1 | awk '{print $4}' | cut -d_ -f2 | sed -e "s/wos//")" +os="$(uname -sr | cut -d. -f1,2,3,6) $patch" +managed[0]="No Filesystem Backup by Pixelpark" +managed[1]="Managed Backup by Pixelpark" +zone="N/A" + +# ------------------------- +# Ab hier bitte nix aendern +# ------------------------- + +# Print Functions +# --------------- +help() { + print "" + print "Version : $version" + print "" + print "Option : -n " + print "Option : -p " + print "Option : -d " + print "Option : -l " + print "Option : -i " + print "Option : -h " + print "Option : -e " + print "Option : -c " + print "Option : -z " + print "" + print "Option : -b [No Managed Backup]" + print "" + print "Example : $0 -i 93.188.105.100 -p \"Deutsche Post\" -z silvanus" + print "" + exit +} + +print_motd() { + t1=12; t2=28; t3=11; t4=20; t5=59; + echo "+----------------------------------------------------------------------+" + printf "%-${t1}s%-${t2}s%-${t3}s%-${t4}s%-s\n" "| Nodename:" "$nodename" "Purpose:" "$purpose" "|" + printf "%-${t1}s%-${t2}s%-${t3}s%-${t4}s%-s\n" "| Domain:" "$domain" "Location:" "$location" "|" + printf "%-${t1}s%-${t2}s%-${t3}s%-${t4}s%-s\n" "| Network:" "$network" "Owner:" "$owner" "|" + printf "%-${t1}s%-${t2}s%-${t3}s%-${t4}s%-s\n" "| Hardware:" "$hardware" "Contact:" "$contact" "|" + printf "%-${t1}s%-${t2}s%-${t3}s%-${t4}s%-s\n" "| OS:" "$os" "Zone-Host:" "$zone" "|" + printf "%-${t1}s%-${t5}s%-s\n" "| Comments:" "$comments" "|" + echo "+----------------------------------------------------------------------+" + echo "Linux System" +} + + +# Get Functions +# ------------- +get_args() { + while getopts Vhbn:p:d:l:i:o:e:c:z: arguments ; do + case $arguments in + + V) print $version + exit 0 + ;; + + h) help + exit 0 + ;; + + b) comments=${managed[0]} ;; + n) nodename="$OPTARG" ;; + p) purpose="$OPTARG" ;; + d) domain="$OPTARG" ;; + l) location="$OPTARG" ;; + i) network="$OPTARG" ;; + o) owner="$OPTARG" ;; + e) contact="$OPTARG" ;; + c) comments="$OPTARG" ;; + z) zone="$OPTARG" ;; + + esac + done + shift `expr $OPTIND - 1` + comments=${comments:="${managed[1]}"} +} + +get_args "$@" +print_motd diff --git a/postinstall-scripts/update-all-packages b/postinstall-scripts/update-all-packages new file mode 100755 index 0000000..4e02efb --- /dev/null +++ b/postinstall-scripts/update-all-packages @@ -0,0 +1,374 @@ +#!/bin/bash + +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.4" + +BASENAME="$(basename ${0})" +BASE_DIR="$(dirname ${0})" +FQDN=$( hostname -f ) + +######################################### +# Some often used funktions + +#------------------------------------------------------------------- +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" + NORMAL="\033[39m" + CYAN="\033[38;5;36m" + 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 + +} +detect_color + +#------------------------------------------------------------------------------ +my_date() { + date --rfc-3339=seconds +} + +#------------------------------------------------------------------------------ +debug() { + if [[ "${VERBOSE}" != "y" ]] ; then + return 0 + fi + echo -e " * [$(my_date)] [${BASENAME}:${CYAN}DEBUG${NORMAL}]: $@" >&2 +} + +#------------------------------------------------------------------------------ +info() { + if [[ "${QUIET}" == "y" ]] ; then + return 0 + fi + echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@" >&2 +} + +#------------------------------------------------------------------------------ +warn() { + echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $@" >&2 +} + +#------------------------------------------------------------------------------ +error() { + echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2 +} + +#------------------------------------------------------------------------------ +description() { + cat <<-EOF + Performs a complete update of all packages. + + EOF + +} + +#------------------------------------------------------------------------------ +usage() { + + cat <<-EOF + Usage: ${BASENAME} [-d|--debug] [[-v|--verbose] | [-q|--quiet]] [--nocolor] + ${BASENAME} [-h|--help] + ${BASENAME} [-V|--version] + + Options: + -d|--debug Debug output (bash -x). + -v|--verbose Set verbosity on. + -q|--quiet Quiet execution. Mutually exclusive to --verbose. + --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 + +} + +#------------------------------------------------------------------------------ +get_options() { + + local tmp= + local short_options="dvqhV" + local long_options="debug,verbose,quiet,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 + -d|--debug) + DEBUG="y" + shift + ;; + -v|--verbose) + VERBOSE="y" + shift + ;; + -q|--quiet) + QUIET="y" + RED="" + YELLOW="" + GREEN="" + BLUE="" + CYAN="" + NORMAL="" + HAS_COLORS="n" + 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 + ;; + --) shift + break + ;; + *) echo "Internal error!" + exit 1 + ;; + esac + done + + if [[ "${DEBUG}" = "y" ]] ; then + set -x + fi + + if [[ "${VERBOSE}" == "y" && "${QUIET}" == "y" ]] ; then + echo "Options '${RED}--verbose${NORMAL}' and '${RED}--quiet${NORMAL}' are mutually exclusive." >&2 + echo >&2 + usage >&2 + exit 1 + fi + +} + +#------------------------------------------------------------------------------ +check_for_root() { + + debug "Checking for root execution ..." + if [[ "$( id -u )" != "0" ]] ; then + error "Only user '${RED}root${NORMAL}' may execute this script." + exit 1 + fi + +} + +#------------------------------------------------------------------------------ +do_el_upgrade() { + + local tool="$1" + + info "Performing dist upgrade with ${GREEN}${tool}${NORMAL} ..." + info "Tweaking '/etc/yum.conf' ..." + + local found_ip_resolv="n" + local awk_script='' + + if grep '^ip_resolve *= *4' /etc/yum.conf >/dev/null ; then + debug "Usage of IPv4 already configured" + else + + if grep '^ip_resolve[ =]' /etc/yum.conf >/dev/null ; then + found_ip_resolv="y" + fi + if [[ "${found_ip_resolv}" == "y" ]] ; then + awk_script='/^[main]/ {ok=1} +ok==1 && /^ip_resolve/ {print "ip_resolve=4"; ok=0; next} +{print $0} ' + else + awk_script='/^[main]/ {ok=1} +ok==1 {print "ip_resolve=4"; ok=0} +{print $0} ' + fi + cp -p -v /etc/yum.conf /etc/yum.conf.$( date -r /etc/yum.conf +'%Y-%m-%d_%H:%M:%S').bak + awk "${awk_script}" /etc/yum.conf >/etc/yum.conf.new + if [[ -s /etc/yum.conf.new ]] ; then + mv -v /etc/yum.conf.new /etc/yum.conf + else + rm -f -v /etc/yum.conf.new + fi + fi + + local verbose_opt="" + if [[ "${VERBOSE}" == "y" ]] ; then + verbose_opt=" --verbose" + fi + + echo + info "Cleaning ..." + local cmd="${tool}${verbose_opt} --assumeyes clean all" + debug "Executing: ${cmd}" + eval ${cmd} + + echo + info "Making cache ..." + cmd="${tool}${verbose_opt} --assumeyes makecache" + debug "Executing: ${cmd}" + eval ${cmd} + + echo + info "Doing upgrade ..." + cmd="${tool}${verbose_opt} --assumeyes upgrade" + debug "Executing: ${cmd}" + eval ${cmd} + +} + +#------------------------------------------------------------------------------ +do_deb_upgrade() { + + local tool="$1" + + info "Performing dist upgrade with ${GREEN}${tool}${NORMAL} ..." + + local quiet_opt="" + if [[ "${QUIET}" == "y" ]] ; then + quiet_opt=" --quiet" + fi + + echo + info "Updating indexes ..." + local cmd="${tool} update --yes${quiet_opt}" + debug "Executing: ${cmd}" + eval ${cmd} + + echo + info "Doing upgrade ..." + local cmd="${tool} dist-upgrade --yes${quiet_opt}" + debug "Executing: ${cmd}" + eval ${cmd} + +} + +#------------------------------------------------------------------------------ +do_distupgrade() { + + info "Performing a ${GREEN}dist upgrade${NORMAL}." + echo + + if type -p dnf >/dev/null ; then + do_el_upgrade 'dnf' + elif type -p yum >/dev/null ; then + do_el_upgrade 'yum' + elif type -p apt >/dev/null ; then + do_deb_upgrade 'apt' + elif type -p apt-get >/dev/null ; then + do_deb_upgrade 'apt-get' + else + error "The operating system is not supported." + return 0 + fi + + echo + info "Finished" + +} + + +################################################################################ +## +## Main +## +################################################################################ + +#------------------------------------------------------------------------------ +main() { + + get_options "$@" + check_for_root + do_distupgrade + +} + +main "$@" + +exit 0 + +# vim: list