set -e
set -u
-VERBOSE="n"
-DEBUG="n"
-
-VERSION="0.2"
-
-# console colors:
-RED=""
-YELLOW=""
-GREEN=""
-BLUE=""
-NORMAL=""
-
-HAS_TTY='y'
-
-BASENAME="$(basename ${0})"
-BASE_DIR="$(dirname ${0})"
+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
declare -a MODULES=()
PUPPET_ENV='*'
ENV_DIR="/etc/puppetlabs/code/environments"
-#-------------------------------------------------------------------
-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}$( grep '^TERM ' ~/.dir_colors | sed -e 's/^TERM *//' -e 's/ .*//')"
- fi
- if [[ -f /etc/DIR_COLORS ]] ; then
- match_lhs="${match_lhs}$( grep '^TERM ' /etc/DIR_COLORS | 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}" || "${TERM}" =~ .*color ]] ; 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"
- # shellcheck disable=SC2034
- BLUE="\033[38;5;27m"
- CYAN="\033[38;5;14m"
- NORMAL="\033[39m"
- else
- RED=""
- YELLOW=""
- GREEN=""
- # shellcheck disable=SC2034
- BLUE=""
- CYAN=""
- NORMAL=""
- fi
-
-}
-detect_color
+DESCRIPTION=$( cat <<-EOF
+ Retrieving version number of given Puppet module(s) in all environments.
-#------------------------------------------------------------------------------
-description() {
- echo -e $( cat <<-EOF
- Retrieving version number of given Puppet module(s) in all environments.
-
- EOF
- )
-}
+ EOF
+)
#------------------------------------------------------------------------------
usage() {
cat <<-EOF
- Usage: ${BASENAME} [OPTIONS] [-E <ENVIRONMENT>] <MODULE> [<MODULE ...]
- ${BASENAME} [-h|--help]
- ${BASENAME} [-V|--version]
+ Usage: ${BASE_NAME} [Common Options] [-E <ENVIRONMENT>] [-D <PUPPET_ENV_DIR>] <MODULE> [<MODULE ...]
+ ${BASE_NAME} [-h|--help]
+ ${BASE_NAME} [-V|--version]
+
+ Mandatory Parameter(s):
+ MODULE: The short name of the module(s) (without vendor), for which the
+ current used version should be shown.
Options:
-E|--env|--environment ENVIRONMENT
The Puppet environment, in wich to search for the module version.
If not given, '*' (all) environments are searched.
- -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
+ -D|--dir PUPPET_ENV_DIR
+ The directory, where the Puppet r10k environments are checked out
+ (Default: '${ENV_DIR}').
EOF
+
+ echo
+ echo " Common Options:"
+ echo "${STD_USAGE_MSG}"
+
}
#------------------------------------------------------------------------------
local tmp=
local base_dir=
+ local short_options="E:D:${STD_SHORT_OPTIONS}"
+ local long_options="env:,environment:,dir:,${STD_LONG_OPTIONS}"
+
set +e
- tmp=$( getopt -o E:dvhV \
- --long env:,environment:,debug,verbose,nocolor,help,version \
- -n "${BASENAME}" -- "$@" )
+ tmp=$( getopt -o "${short_options}" --long "${long_options}" -n "${BASE_NAME}" -- "$@" )
if [[ $? != 0 ]] ; then
echo "" >&2
usage >&2
# 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 len="${#REMAINING_OPTS[*]}"
+ local i="0"
+ local j=
+ local arg=
+ while [[ "$i" -lt "${len}" ]] ; do
- local p=
+ arg="${REMAINING_OPTS[$i]}"
- while true ; do
- # echo "Evaluating option '$1' ..."
- case "$1" in
+ case "${arg}" in
-E|--env|--environment)
- PUPPET_ENV="$2"
- shift
- shift
- ;;
- -d|--debug)
- DEBUG="y"
- shift
- ;;
- -v|--verbose)
- VERBOSE="y"
- shift
- ;;
- --nocolor)
- RED=""
- YELLOW=""
- GREEN=""
- BLUE=""
- NORMAL=""
- shift
+ j=$(( $i + 1 ))
+ PUPPET_ENV="${REMAINING_OPTS[$j]}"
+ i=$(( $i + 2 ))
;;
- -h|--help)
- description
- usage
- exit 0
+ -D|--dir)
+ j=$(( $i + 1 ))
+ ENV_DIR="${REMAINING_OPTS[$j]}"
+ i=$(( $i + 2 ))
;;
- -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 [[ ! -d "${ENV_DIR}" ]] ; then
- error "Puppet environments directory '${RED}${ENV_DIR}${NORMAL}' does not exists."
- exit 5
+ if [[ "${#REMAINING_OPTS[@]}" -gt 0 ]] ; then
+ error "Unknown options: ${REMAINING_OPTS[*]}"
+ echo >&2
+ usage >&2
+ exit 2
fi
- if [[ "$#" -lt 1 ]] ; then
+ if [[ "${#REMAINING_ARGS[@]}" == "0" ]] ; then
error "No module name to search for the version given."
- echo
+ echo >&2
usage >&2
exit 1
- else
- local mod
- for mod in "$@" ; do
- MODULES+=( "${mod}" )
- done
fi
-}
-
-#########################################
-# Some often used funktions
-
-#------------------------------------------------------------------------------
-my_date() {
- date +'%F %T %:::z'
-}
-
-#------------------------------------------------------------------------------
-debug() {
- if [[ "${VERBOSE}" != "y" ]] ; then
- return 0
+ if [[ ! -d "${ENV_DIR}" ]] ; then
+ error "Puppet environments directory '${RED}${ENV_DIR}${NORMAL}' does not exists."
+ exit 5
fi
- echo -e " * [$(my_date)] [${BASENAME}:DEBUG]: $@" >&2
-}
-#------------------------------------------------------------------------------
-info() {
- echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@" >&2
-}
+ local mod
+ for mod in "${REMAINING_ARGS[@]}" ; do
+ MODULES+=( "${mod}" )
+ done
-#------------------------------------------------------------------------------
-warn() {
- echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $@" >&2
-}
+ if [[ "${DEBUG}" == 'y' ]] ; then
+ declare -p MODULES
+ fi
-#------------------------------------------------------------------------------
-error() {
- echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2
}
#------------------------------------------------------------------------------
max_len_env=$(( max_len_env + 1 ))
for md_file in "${md_files[@]}" ; do
- env=$( echo "${md_file}" | sed -e "s|${ENV_DIR}/||" -e 's|/modules/.*/metadata.json||' )
- ver=$( grep --no-filename '"version"' "${md_file}" | sed -e 's/"version"//' -e 's/,//g' -e 's/^[ ]*:[ ]*//g' -e 's/"//g' )
+ env=$( echo "${md_file}" | \
+ sed -e "s|${ENV_DIR}/||" -e 's|/modules/.*/metadata.json||' )
+ ver=$( grep --no-filename '"version"' "${md_file}" | \
+ sed -e 's/"version"//' -e 's/,//g' -e 's/^[ ]*:[ ]*//g' -e 's/"//g' )
printf "%-${max_len_env}s ${CYAN}${ver}${NORMAL}\n" "${env}:"
done