]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Adding possibility to give additional opptions for executing puppet agent in scripts...
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 21 Nov 2022 14:11:56 +0000 (15:11 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 21 Nov 2022 14:11:56 +0000 (15:11 +0100)
scripts/exec-puppet-agent

index b310664f0a89ad60e76c4c2a64217e2519fa9131..d03d8d603826de20f9461a8f686a7f963a8827a1 100755 (executable)
@@ -22,6 +22,9 @@ else
     exit 5
 fi
 
+declare -a ADD_PUPPET_AGENT_ARGS=()
+TIMINGS="n"
+
 detect_color
 
 DESCRIPTION=$( cat <<-EOF
@@ -33,10 +36,13 @@ DESCRIPTION=$( cat <<-EOF
 #------------------------------------------------------------------------------
 usage() {
     cat <<-EOF
-       Usage: ${BASE_NAME} [Common Options]
+       Usage: ${BASE_NAME} [Common Options] [-t|--timing] [-- ADDITIIONAL_OPTION_FOR_PUPPET ...]
               ${BASE_NAME} [-h|--help]
               ${BASE_NAME} [-V|--version]
 
+           Special Options:
+               -t|--timing     Display statistics about real, user and system time on execution of puppet agent.
+
            Common Options:
        ${STD_USAGE_MSG}
        EOF
@@ -48,9 +54,12 @@ get_options() {
 
     local tmp=
     local base_dir=
+    local arg=
 
     set +e
-    tmp=$( getopt -o ${STD_SHORT_OPTIONS} --long ${STD_LONG_OPTIONS} -n "${BASE_NAME}" -- "$@" )
+    tmp=$( getopt -o ${STD_SHORT_OPTIONS}t \
+                  --long ${STD_LONG_OPTIONS},timing \
+                  -n "${BASE_NAME}" -- "$@" )
     if [[ $? != 0 ]] ; then
         echo "" >&2
         usage >&2
@@ -66,18 +75,33 @@ get_options() {
         declare -p REMAINING_ARGS
     fi
 
-    if [[ "${#REMAINING_OPTS[@]}" -gt 0 ]] ; then
-        error "Unknown options: ${REMAINING_OPTS[*]}"
-        echo >&2
-        usage >&2
-        exit 2
-    fi
+    local len="${#REMAINING_OPTS[*]}"
+    local i="0"
+    while [[ "$i" -lt "${len}" ]] ; do
+
+        arg="${REMAINING_OPTS[$i]}"
+
+        case "${arg}" in
+            -t|--timing)
+                TIMINGS="y"
+                i=$(( $i + 1 ))
+                ;;
+            *)  echo -e "Internal error - option '${RED}${arg}${NORMAL} was wrong!"
+                exit 1
+                ;;
+        esac
+
+    done
 
     if [[ "${#REMAINING_ARGS[@]}" != "0" ]] ; then
-        error "Invalig arguments given: ${REMAINING_ARGS[*]}"
-        echo >&2
-        usage >&2
-        exit 2
+        i="0"
+        for arg in "${REMAINING_ARGS[@]}" ; do
+            if [[ "${i}" == "0" && "${arg}" == '--' ]] ; then
+                continue
+            fi
+            i=$(( $i + 1 ))
+            ADD_PUPPET_AGENT_ARGS+=( "${arg}" )
+        done
     fi
 
     if [[ $(id -u -n) != "root" ]] ; then
@@ -110,10 +134,17 @@ exec_puppet_agent() {
         i=$(( $1 + 1 ))
     done
 
-    local cmd="${PUPPET_BIN} agent --test"
+    local cmd=""
+    if [[ "${TIMINGS}" == "y" ]] ; then
+        cmd="time "
+    fi
+    cmd+="${PUPPET_BIN} agent --test"
     if [[ "${SIMULATE}" == "y" ]] ; then
         cmd+=" --noop"
     fi
+    if [[ "${#ADD_PUPPET_AGENT_ARGS[@]}" != "0" ]] ; then
+        cmd+=" ${ADD_PUPPET_AGENT_ARGS[@]}"
+    fi
     debug "Executing: ${cmd}"
     set +e
     eval ${cmd}