run_policy.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env bash
  2. # To generate script:
  3. # ~/.local/bin/gengetoptions embed run_policy.sh.template > run_policy.sh
  4. # @getoptions
  5. parser_definition() {
  6. setup REST help:usage -- "Usage: run_policy [options]... [arguments]" ''
  7. msg -- 'Options:'
  8. flag FLAG_D -d --debug init:=0 -- "displays debug information"
  9. flag DRY_RUN -n --dry-run init:=0 -- "run a dry run"
  10. param POLICY -p --policy base:"default" -- "Policy to run (default: 'base')"
  11. disp :usage -h --help
  12. }
  13. # @gengetoptions parser -i parser_definition parse
  14. # Generated by getoptions (BEGIN)
  15. # URL: https://github.com/ko1nksm/getoptions (v3.3.0)
  16. FLAG_D='0'
  17. DRY_RUN='0'
  18. POLICY=''
  19. REST=''
  20. parse() {
  21. OPTIND=$(($#+1))
  22. while OPTARG= && [ $# -gt 0 ]; do
  23. case $1 in
  24. --?*=*) OPTARG=$1; shift
  25. eval 'set -- "${OPTARG%%\=*}" "${OPTARG#*\=}"' ${1+'"$@"'}
  26. ;;
  27. --no-*|--without-*) unset OPTARG ;;
  28. -[p]?*) OPTARG=$1; shift
  29. eval 'set -- "${OPTARG%"${OPTARG#??}"}" "${OPTARG#??}"' ${1+'"$@"'}
  30. ;;
  31. -[dnh]?*) OPTARG=$1; shift
  32. eval 'set -- "${OPTARG%"${OPTARG#??}"}" -"${OPTARG#??}"' ${1+'"$@"'}
  33. OPTARG= ;;
  34. esac
  35. case $1 in
  36. '-d'|'--debug')
  37. [ "${OPTARG:-}" ] && OPTARG=${OPTARG#*\=} && set "noarg" "$1" && break
  38. eval '[ ${OPTARG+x} ] &&:' && OPTARG='1' || OPTARG=''
  39. FLAG_D="$OPTARG"
  40. ;;
  41. '-n'|'--dry-run')
  42. [ "${OPTARG:-}" ] && OPTARG=${OPTARG#*\=} && set "noarg" "$1" && break
  43. eval '[ ${OPTARG+x} ] &&:' && OPTARG='1' || OPTARG=''
  44. DRY_RUN="$OPTARG"
  45. ;;
  46. '-p'|'--policy')
  47. [ $# -le 1 ] && set "required" "$1" && break
  48. OPTARG=$2
  49. POLICY="$OPTARG"
  50. shift ;;
  51. '-h'|'--help')
  52. usage
  53. exit 0 ;;
  54. --)
  55. shift
  56. while [ $# -gt 0 ]; do
  57. REST="${REST} \"\${$(($OPTIND-$#))}\""
  58. shift
  59. done
  60. break ;;
  61. [-]?*) set "unknown" "$1"; break ;;
  62. *)
  63. REST="${REST} \"\${$(($OPTIND-$#))}\""
  64. esac
  65. shift
  66. done
  67. [ $# -eq 0 ] && { OPTIND=1; unset OPTARG; return 0; }
  68. case $1 in
  69. unknown) set "Unrecognized option: $2" "$@" ;;
  70. noarg) set "Does not allow an argument: $2" "$@" ;;
  71. required) set "Requires an argument: $2" "$@" ;;
  72. pattern:*) set "Does not match the pattern (${1#*:}): $2" "$@" ;;
  73. notcmd) set "Not a command: $2" "$@" ;;
  74. *) set "Validation error ($1): $2" "$@"
  75. esac
  76. echo "$1" >&2
  77. exit 1
  78. }
  79. usage() {
  80. cat<<'GETOPTIONSHERE'
  81. Usage: run_policy [options]... [arguments]
  82. Options:
  83. -d, --debug displays debug information
  84. -n, --dry-run run a dry run
  85. -p, --policy POLICY Policy to run (default: 'base')
  86. -h, --help
  87. GETOPTIONSHERE
  88. }
  89. # Generated by getoptions (END)
  90. # @end
  91. parse "$@"
  92. eval "set -- ${REST}"
  93. set -e
  94. # Ref:
  95. # https://medium.com/@emachnic/using-policyfiles-with-chef-client-local-mode-4f47477b24db
  96. ./scripts/export_policy.sh ${POLICY}
  97. echo -e "\n\e[4m\e[33mRunning:\e[0m \e[33m\e[1mchef-client -z\e[39m\e[0m"
  98. pushd policy-export > /dev/null
  99. if [ ${DRY_RUN} -eq 1 ]; then
  100. OPT="--why-run"
  101. fi
  102. if [ ${FLAG_D} -eq 1 ]; then
  103. OPT="${OPT} --log_level debug"
  104. fi
  105. bundle exec chef-client --chef-license 'accept' -z ${OPT}
  106. bundle exec chef-client --chef-license 'accept' --config-option 'client_d_dir=../client.d' -z ${OPT}
  107. popd > /dev/null
  108. ./scripts/end_run.sh