set -e
set -u
-export LC_ALL="en_US.utf8"
-export LANG="en_US.utf8"
+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
-VERBOSE="n"
-DEBUG="n"
-QUIET='n'
-
-# console colors:
-RED=""
-YELLOW=""
-GREEN=""
-BLUE=""
-CYAN=""
-NORMAL=""
-
-HAS_TTY='y'
-HAS_COLORS="n"
-
-VERSION="1.5"
-
-BASENAME="$(basename ${0})"
-BASE_DIR="$(dirname ${0})"
FQDN=$( hostname -f )
ENVIRONMENT=
PUPPET_SERVER="puppetmaster.pixelpark.com"
PUPPET_CA_SERVER="puppetca01.pixelpark.com"
-#########################################
-# 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
+DESCRIPTION=$( cat <<-EOF
Initialize a Puppet 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]
+ Usage: ${BASE_NAME} [OPTIONS ...]
+ ${BASE_NAME} [-h|--help]
+ ${BASE_NAME} [-V|--version]
Options:
-E|--env|--environment ENVIRONMENT
--ca-server HOSTADDRESS
The hostname or IP address of the Puppet CA server to use
in the puppet configuration. Default: '${PUPPET_CA_SERVER}'.
- -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
+ echo "${STD_USAGE_MSG}"
}
#------------------------------------------------------------------------------
get_options() {
local tmp=
- local base_dir=
- local short_options="E:C:p:O:T:R:IP:M:l:z:dvqhV"
+ local short_options="E:C:p:O:T:R:IP:M:l:z:${STD_SHORT_OPTIONS}"
local long_options="env:,environment:,customer:,hiera-customer:,project:,owner:,tier:,role:,"
long_options+="initial-install,purpose:,server:,ca-server:,email:,location:,zone:,"
- long_options+="debug,verbose,quiet,help,version"
+ long_options+="${STD_LONG_OPTIONS}"
set +e
tmp=$( getopt -o "${short_options}" --long "${long_options}" -n "${BASENAME}" -- "$@" )
# Note the quotes around `$TEMP': they are essential!
eval set -- "${tmp}"
+ if [[ "${DEBUG}" == 'y' ]] ; then
+ declare -p REMAINING_OPTS
+ declare -p REMAINING_ARGS
+ fi
+
+ local len="${#REMAINING_OPTS[*]}"
+ local i="0"
+ local j=
+ local arg=
+ while [[ "$i" -lt "${len}" ]] ; do
- local p=
+ arg="${REMAINING_OPTS[$i]}"
- while true ; do
case "$1" in
-E|--env|--environment)
- ENVIRONMENT=$( echo "$2" | tr '[:upper:]' '[:lower:]' )
- shift
- shift
+ j=$(( $i + 1 ))
+ ENVIRONMENT=$( echo "${REMAINING_OPTS[$j]}" | tr '[:upper:]' '[:lower:]' )
+ i=$(( $i + 2 ))
;;
-C|--customer|--hiera-customer)
- CUSTOMER=$( echo "$2" | tr '[:upper:]' '[:lower:]' )
- shift
- shift
+ j=$(( $i + 1 ))
+ CUSTOMER=$( echo "${REMAINING_OPTS[$j]}" | tr '[:upper:]' '[:lower:]' )
+ i=$(( $i + 2 ))
;;
-p|--project)
- PROJECT=$( echo "$2" | tr '[:upper:]' '[:lower:]' )
- shift
- shift
+ j=$(( $i + 1 ))
+ PROJECT=$( echo "${REMAINING_OPTS[$j]}" | tr '[:upper:]' '[:lower:]' )
+ i=$(( $i + 2 ))
;;
-O|--owner)
- OWNER="$2"
- shift
- shift
+ j=$(( $i + 1 ))
+ OWNER="${REMAINING_OPTS[$j]}"
+ i=$(( $i + 2 ))
;;
-T|--tier)
- TIER=$( echo "$2" | tr '[:upper:]' '[:lower:]' )
- shift
- shift
+ j=$(( $i + 1 ))
+ TIER=$( echo "${REMAINING_OPTS[$j]}" | tr '[:upper:]' '[:lower:]' )
+ i=$(( $i + 2 ))
;;
-R|--role)
- ROLE=$( echo "$2" | tr '[:upper:]' '[:lower:]' )
- shift
- shift
+ j=$(( $i + 1 ))
+ ROLE=$( echo "${REMAINING_OPTS[$j]}" | tr '[:upper:]' '[:lower:]' )
+ i=$(( $i + 2 ))
;;
-I|--initial-install)
INITIAL_INSTALL="y"
- shift
+ i=$(( $i + 1 ))
;;
-P|--purpose)
- PURPOSE="$2"
- shift
- shift
+ j=$(( $i + 1 ))
+ PURPOSE="${REMAINING_OPTS[$j]}"
+ i=$(( $i + 2 ))
;;
-M|--email)
- CONTACT="$2"
- shift
- shift
+ j=$(( $i + 1 ))
+ CONTACT="${REMAINING_OPTS[$j]}"
+ i=$(( $i + 2 ))
;;
-l|--location)
- LOCATION="$2"
- shift
- shift
+ j=$(( $i + 1 ))
+ LOCATION="${REMAINING_OPTS[$j]}"
+ i=$(( $i + 2 ))
;;
-z|--zone)
- ZONE="$2"
- shift
- shift
+ j=$(( $i + 1 ))
+ ZONE="${REMAINING_OPTS[$j]}"
+ i=$(( $i + 2 ))
;;
--server)
- PUPPET_SERVER="$2"
- shift
- shift
+ j=$(( $i + 1 ))
+ PUPPET_SERVER="${REMAINING_OPTS[$j]}"
+ i=$(( $i + 2 ))
;;
--ca-server)
- PUPPET_CA_SERVER="$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
+ j=$(( $i + 1 ))
+ PUPPET_CA_SERVER="${REMAINING_OPTS[$j]}"
+ i=$(( $i + 2 ))
;;
- --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!"
+ *) echo -e "Internal error - option '${RED}${arg}${NORMAL} was wrong!"
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
}
-#------------------------------------------------------------------------------
-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
-
-}
-
#------------------------------------------------------------------------------
empty_repo_file() {
info "Checking OS release ..."
if [[ -f /etc/redhat-release ]] ; then
- if grep --quiet --extended-regexp --ignore-case "release 8(\.|[ ]*$)" /etc/redhat-release ; then
+ if grep --quiet --perl-regexp --ignore-case "release 9\." /etc/redhat-release ; then
+ RELEASE=9
+ elif grep --quiet --perl-regexp --ignore-case 'release 8(\.|\s*$)' /etc/redhat-release ; then
RELEASE=8
elif grep --quiet --ignore-case "release 7\." /etc/redhat-release ; then
RELEASE=7
get_options "$@"
check_for_root
+
+ empty_line
+ info "Initializing ${CYAN}Puppet agent${NORMAL} ..."
+ empty_line
+
check_os
check_group_and_user
check_installed_rpm