diff --git a/scripts/00_requirements.sh b/scripts/00_requirements.sh index 9889c2d..f65fa22 100755 --- a/scripts/00_requirements.sh +++ b/scripts/00_requirements.sh @@ -1,8 +1,7 @@ #!/bin/bash -set -u source "$(dirname $0)/utils.sh" -goto_root +init function check_command() { local cmd="$1" diff --git a/scripts/01_update_output.sh b/scripts/01_update_output.sh index 593e7a1..354e77a 100755 --- a/scripts/01_update_output.sh +++ b/scripts/01_update_output.sh @@ -1,11 +1,10 @@ #!/bin/bash -set -xueo pipefail source "$(dirname $0)/utils.sh" -goto_root +init # loop over every monitors set -for path in $(find "$(get_scope ${1-})" -name 'monitors-*.tf' -print | sort -fdbi); do +for path in $(find "$(get_scope ${1:-})" -name 'monitors-*.tf' -print | sort -fdbi); do cd $(dirname $path) # empty outputs > outputs.tf diff --git a/scripts/02_update_readme.sh b/scripts/02_update_readme.sh index dff0358..fab7f9e 100755 --- a/scripts/02_update_readme.sh +++ b/scripts/02_update_readme.sh @@ -1,12 +1,11 @@ #!/bin/bash -set -xueo pipefail source "$(dirname $0)/utils.sh" -goto_root +init # download awk script to hack terraform-docs TERRAFORM_AWK="/tmp/terraform-docs.awk" -curl -Lo ${TERRAFORM_AWK} "https://raw.githubusercontent.com/cloudposse/build-harness/master/bin/terraform-docs.awk" +curl -Lso ${TERRAFORM_AWK} "https://raw.githubusercontent.com/cloudposse/build-harness/master/bin/terraform-docs.awk" ## root README generator # only keep current README from begining to "Monitors summary" section (delete monitors list) @@ -36,7 +35,7 @@ done PATTERN_DOC="Related documentation" # loop over every monitors set readme -for path in $(find "$(get_scope ${1-})" -name 'monitors-*.tf' -print | sort -fdbi); do +for path in $(find "$(get_scope ${1:-})" -name 'monitors-*.tf' -print | sort -fdbi); do cd $(dirname $path) EXIST=0 if [ -f README.md ]; then @@ -94,7 +93,7 @@ EOF # parse monitor's name name=$(get_name "${match}") # search if monitor is enabled - [[ "$(grep -B1 "$name" $(basename ${path}) | grep enabled)" =~ ^[[:space:]]*count[[:space:]]*=[[:space:]]*var\.([a-z0-9_]*_enabled) ]] && + [[ "$(grep -B1 "$name" $(basename ${path}) | grep -q enabled)" =~ ^[[:space:]]*count[[:space:]]*=[[:space:]]*var\.([a-z0-9_]*_enabled) ]] && # add "disabled by default" mention if not enabled if ! grep -A4 "${BASH_REMATCH[1]}" inputs.tf | grep default.*true; then name="${name} (disabled by default)" @@ -123,6 +122,6 @@ EOF rm README.md.bak fi # force unix format (I don't know why for now but you never know) - dos2unix README.md + dos2unix README.md 2> /dev/null cd - >> /dev/null done diff --git a/scripts/03_update_module.sh b/scripts/03_update_module.sh index 0c8e05b..5b3e259 100755 --- a/scripts/03_update_module.sh +++ b/scripts/03_update_module.sh @@ -1,15 +1,11 @@ #!/bin/bash -set -xueo pipefail source "$(dirname $0)/utils.sh" -goto_root - +init root=$(basename ${PWD}) -echo "$(get_scope ${1-})" -exit # loop over every monitors set -for path in $(find "$(get_scope ${1-})" -name 'monitors-*.tf' -print | sort -fdbi); do +for path in $(find "$(get_scope ${1:-})" -name 'monitors-*.tf' -print | sort -fdbi); do cd $(dirname $path) # get name of the monitors set directory resource="$(basename $(dirname $path))" diff --git a/scripts/90_best_practices.sh b/scripts/90_best_practices.sh index 4f41514..8811a64 100755 --- a/scripts/90_best_practices.sh +++ b/scripts/90_best_practices.sh @@ -1,11 +1,10 @@ #!/bin/bash -set -xueo pipefail source "$(dirname $0)/utils.sh" -goto_root +init # loop over every monitors set -for path in $(find "$(get_scope ${1-})" -name 'monitors-*.tf' -print | sort -fdbi); do +for path in $(find "$(get_scope ${1:-})" -name 'monitors-*.tf' -print | sort -fdbi); do # check if there is more than 1 notify_no_data parameter set to true per set of monitors if [[ $(grep -c notify_no_data.*true $path) -gt 1 ]]; then echo "More than one notify_no_data set to true on $path" diff --git a/scripts/99_terraform.sh b/scripts/99_terraform.sh index 16e5d37..22417ca 100755 --- a/scripts/99_terraform.sh +++ b/scripts/99_terraform.sh @@ -1,12 +1,11 @@ #!/bin/bash -set -xueo pipefail source "$(dirname $0)/utils.sh" -goto_root +init provider_version=$(grep ^[[:space:]]*version[[:space:]]= README.md | awk '{print $3}') -for path in $(find "$(get_scope ${1-})" -name 'inputs.tf' -print); do +for path in $(find "$(get_scope ${1:-})" -name 'inputs.tf' -print); do dir=$(dirname ${path}) cat < ${dir}/tmp.tf provider "datadog" { diff --git a/scripts/auto_update.sh b/scripts/auto_update.sh index 572bb6b..3371f04 100755 --- a/scripts/auto_update.sh +++ b/scripts/auto_update.sh @@ -1,13 +1,8 @@ #!/bin/bash -set -xueo pipefail - -# MON-478 fix sort order behavior on case -export LC_COLLATE=C source "$(dirname $0)/utils.sh" - -cd $(dirname $0) +init scripts for script in [0-9][0-9]_*.sh; do - ./${script} "$(get_scope ${1-})" + ./${script} "$(get_scope ${1:-})" done diff --git a/scripts/utils.sh b/scripts/utils.sh index 084e8f3..ef17a22 100755 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -1,6 +1,6 @@ #!/bin/bash -function goto_root { +function goto_root() { script_dir=$(dirname $0) if [[ "$script_dir" == "." ]]; then cd .. @@ -9,7 +9,20 @@ function goto_root { fi } -function get_scope { +function init() { + set -euo pipefail + if [[ ${GITLAB_CI:-} == "true" ]]; then + set -x + fi + # MON-478 fix sort order behavior on case + export LC_COLLATE=C + goto_root + if ! [ -z ${1:-} ]; then + cd "$1" + fi +} + +function get_scope() { TO_PARSE="./" if [ ! -z ${1+x} ] && [ $1 != "." ]; then TO_PARSE="$1" @@ -20,11 +33,11 @@ function get_scope { echo $TO_PARSE } -function list_dirs { +function list_dirs() { echo ${1} | awk -F '/' '{$1=""; print $0}' | cut -c 2- } -function get_name { +function get_name() { regex='^[[:space:]]+name[[:space:]]+=[[:space:]]+"\$.*\[.*\][[:space:]]+(.*)"$' if [[ "${1}" =~ ${regex} ]]; then name="${BASH_REMATCH[1]}"