#!/usr/bin/env bash # To generate script: # ~/.local/bin/gengetoptions embed run_policy.sh.template > run_policy.sh # @getoptions parser_definition() { setup REST help:usage -- "Usage: run_policy [options]... [arguments]" '' msg -- 'Options:' flag FLAG_D -d --debug init:=0 -- "displays debug information" flag DRY_RUN -n --dry-run init:=0 -- "run a dry run" param POLICY -p --policy base:"default" -- "Policy to run (default: 'base')" disp :usage -h --help } # @gengetoptions parser -i parser_definition parse # Generated by getoptions (BEGIN) # URL: https://github.com/ko1nksm/getoptions (v3.3.0) FLAG_D='0' DRY_RUN='0' POLICY='' REST='' parse() { OPTIND=$(($#+1)) while OPTARG= && [ $# -gt 0 ]; do case $1 in --?*=*) OPTARG=$1; shift eval 'set -- "${OPTARG%%\=*}" "${OPTARG#*\=}"' ${1+'"$@"'} ;; --no-*|--without-*) unset OPTARG ;; -[p]?*) OPTARG=$1; shift eval 'set -- "${OPTARG%"${OPTARG#??}"}" "${OPTARG#??}"' ${1+'"$@"'} ;; -[dnh]?*) OPTARG=$1; shift eval 'set -- "${OPTARG%"${OPTARG#??}"}" -"${OPTARG#??}"' ${1+'"$@"'} OPTARG= ;; esac case $1 in '-d'|'--debug') [ "${OPTARG:-}" ] && OPTARG=${OPTARG#*\=} && set "noarg" "$1" && break eval '[ ${OPTARG+x} ] &&:' && OPTARG='1' || OPTARG='' FLAG_D="$OPTARG" ;; '-n'|'--dry-run') [ "${OPTARG:-}" ] && OPTARG=${OPTARG#*\=} && set "noarg" "$1" && break eval '[ ${OPTARG+x} ] &&:' && OPTARG='1' || OPTARG='' DRY_RUN="$OPTARG" ;; '-p'|'--policy') [ $# -le 1 ] && set "required" "$1" && break OPTARG=$2 POLICY="$OPTARG" shift ;; '-h'|'--help') usage exit 0 ;; --) shift while [ $# -gt 0 ]; do REST="${REST} \"\${$(($OPTIND-$#))}\"" shift done break ;; [-]?*) set "unknown" "$1"; break ;; *) REST="${REST} \"\${$(($OPTIND-$#))}\"" esac shift done [ $# -eq 0 ] && { OPTIND=1; unset OPTARG; return 0; } case $1 in unknown) set "Unrecognized option: $2" "$@" ;; noarg) set "Does not allow an argument: $2" "$@" ;; required) set "Requires an argument: $2" "$@" ;; pattern:*) set "Does not match the pattern (${1#*:}): $2" "$@" ;; notcmd) set "Not a command: $2" "$@" ;; *) set "Validation error ($1): $2" "$@" esac echo "$1" >&2 exit 1 } usage() { cat<<'GETOPTIONSHERE' Usage: run_policy [options]... [arguments] Options: -d, --debug displays debug information -n, --dry-run run a dry run -p, --policy POLICY Policy to run (default: 'base') -h, --help GETOPTIONSHERE } # Generated by getoptions (END) # @end parse "$@" eval "set -- ${REST}" set -e # Ref: # https://medium.com/@emachnic/using-policyfiles-with-chef-client-local-mode-4f47477b24db ./scripts/export_policy.sh ${POLICY} echo -e "\n\e[4m\e[33mRunning:\e[0m \e[33m\e[1mchef-client -z\e[39m\e[0m" pushd policy-export > /dev/null if [ ${DRY_RUN} -eq 1 ]; then OPT="--why-run" fi if [ ${FLAG_D} -eq 1 ]; then OPT="${OPT} --log_level debug" fi bundle exec chef-client --chef-license 'accept' --config-option 'client_d_dir=../client.d' -z ${OPT} popd > /dev/null ./scripts/end_run.sh