From 036ce643b90ad675199cd364ee30eaf96d78935a Mon Sep 17 00:00:00 2001 From: Quentin Manfroi Date: Mon, 19 Aug 2019 10:09:21 +0200 Subject: [PATCH] MON-494 use terraform-config-inspect to parse terraform modules --- scripts/00_requirements.sh | 4 +-- scripts/10_update_output.sh | 8 +++--- scripts/20_update_global_readme.sh | 2 +- scripts/20_update_modules_readmes.sh | 37 +++++++++++++++------------- scripts/30_update_module.sh | 2 +- scripts/90_best_practices.sh | 2 +- scripts/99_terraform.sh | 1 + 7 files changed, 31 insertions(+), 25 deletions(-) diff --git a/scripts/00_requirements.sh b/scripts/00_requirements.sh index 984e950..77138f6 100755 --- a/scripts/00_requirements.sh +++ b/scripts/00_requirements.sh @@ -36,7 +36,7 @@ function check_version() { req_ver="0.6.0" cur_ver=$(terraform-docs --version) else - return + return 0 fi if ! verlte $req_ver $cur_ver; then echo "This requires at least version ${req_ver} of $1, please upgrade (current version is ${cur_ver})" @@ -44,7 +44,7 @@ function check_version() { fi } -for cmd in terraform terraform-docs; do +for cmd in terraform terraform-docs terraform-config-inspect jq; do echo -e "\t- Check command \"$cmd\" exists and in right version" check_command $cmd check_version $cmd diff --git a/scripts/10_update_output.sh b/scripts/10_update_output.sh index 282d81a..97f5127 100755 --- a/scripts/10_update_output.sh +++ b/scripts/10_update_output.sh @@ -4,14 +4,16 @@ source "$(dirname $0)/utils.sh" init echo "Generate terraform outputs.tf files for every monitors modules" -# loop over every monitors set +# loop over every modules for module in $(browse_modules "$(get_scope ${1:-})" 'monitors-*.tf'); do echo -e "\t- Generate outputs.tf for module: ${module}" cd ${module} # empty outputs > outputs.tf - # loop over monitors for each set - for monitor in $(cat monitors-*.tf | grep 'resource "datadog_monitor"' | awk '{print $3}' | tr -d '"' ); do + # gather a information line splitted with "|" for every monitor + for row in $(terraform-config-inspect --json | jq -c -r '.managed_resources | map([.name] | join("|")) | join("\n")'); do + # split line for each info one variable + IFS='|' read monitor type < <(echo $row) # create output block for current monitor cat >> outputs.tf <> README.md -# loop over all ready monitors sets on the repo +# loop over path of modules tree for path in $(find -mindepth 1 -type d ! -path '*/.*' ! -path './scripts*' -print | sort -fdbi); do # split path in directories directories=($(list_dirs $path)) diff --git a/scripts/20_update_modules_readmes.sh b/scripts/20_update_modules_readmes.sh index 6e33e06..6332322 100755 --- a/scripts/20_update_modules_readmes.sh +++ b/scripts/20_update_modules_readmes.sh @@ -10,9 +10,9 @@ curl -Lso ${TERRAFORM_AWK} "https://raw.githubusercontent.com/cloudposse/build-h # this is the pattern from where custom information is saved to be restored PATTERN_DOC="Related documentation" -# loop over every monitors set readme +# loop over every modules for module in $(browse_modules "$(get_scope ${1:-})" 'monitors-*.tf'); do - echo -e "\t- Generate outputs.tf for module: ${module}" + echo -e "\t- Generate README.md for module: ${module}" cd ${module} EXIST=0 if [ -f README.md ]; then @@ -59,27 +59,30 @@ EOF Creates DataDog monitors with the following checks: EOF - SAVEIFS=$IFS - # allow looping over strings which contains spaces - IFS=$(echo -en "\n\b") - # loop over each monitor in the set - for match in $(cat monitors-*.tf | grep -E ^[[:space:]]+name[[:space:]]+= | sort -fdbi); do - ## TODO rewrite this (and other things) using: - ## terraform-config-inspect --json| jq -C - ## awk '1;/^\}/{exit}' monitors-ingress.tf # with line numer of each resource + list="" + # gather a information line splitted with "|" for every monitor + for row in $(terraform-config-inspect --json | jq -c -r '.managed_resources | map([.pos.filename, .pos.line] | join("|")) | join("\n")' | sort -fdbi); do + # split line for each info one variable + IFS='|' read filename line < <(echo $row) + # gather all config HCL code for current monitor + config=$(tail -n +${line} ${filename} | sed '/^}/q') # parse monitor's name - name=$(get_name "${match}") + name=$(get_name "$(echo "${config}" | grep 'name[[:space:]]*=')") # search if monitor is enabled - [[ "$(cat monitors-*.tf | grep -B1 "$name" | grep -q enabled)" =~ ^[[:space:]]*count[[:space:]]*=[[:space:]]*var\.([a-z0-9_]*_enabled) ]] && + [[ "$(echo "${config}" | grep 'count[[:space:]]*=')" =~ ^[[: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 + if ! grep -A4 "${BASH_REMATCH[1]}" inputs.tf | grep -q default.*true; then name="${name} (disabled by default)" fi - # monitor name element to the list and replace "could reach" pattern to "forecast" for better naming - echo "- ${name/could reach/forecast}" >> README.md + # append new line to list if not empty + if ! [ -z "${list}" ]; then + list="${list}\n" + fi + # append name to list and improve forecast naming + list="${list}- ${name/could reach/forecast}" done - IFS=$SAVEIFS - echo >> README.md + # write sorted list to readme appending newline to end + echo -e "$(echo -e "${list}" | sort -fdbi)\n" >> README.md # hack for terraform-docs with terraform 0.12 / HCL2 support tmp_tf=$(mktemp -d) awk -f ${TERRAFORM_AWK} ./*.tf > ${tmp_tf}/main.tf diff --git a/scripts/30_update_module.sh b/scripts/30_update_module.sh index 135a73c..67f1d62 100755 --- a/scripts/30_update_module.sh +++ b/scripts/30_update_module.sh @@ -5,7 +5,7 @@ init echo "Generate outputs.tf files when does not exist for every monitors modules" root=$(basename ${PWD}) -# loop over every monitors set +# loop over every modules for module in $(browse_modules "$(get_scope ${1:-})" 'monitors-*.tf'); do cd ${module} # get name of the monitors set directory diff --git a/scripts/90_best_practices.sh b/scripts/90_best_practices.sh index f82befb..1527b34 100755 --- a/scripts/90_best_practices.sh +++ b/scripts/90_best_practices.sh @@ -5,7 +5,7 @@ init echo "Check best practices respect" echo -e "\t- Check only one notify_no_data set to true per module" -# loop over every monitors set +# loop over every modules for module in $(browse_modules "$(get_scope ${1:-})" 'monitors-*.tf'); do # check if there is more than 1 notify_no_data parameter set to true per set of monitors if [[ $(cat ${module}/monitors-*.tf | grep -c notify_no_data.*true) -gt 1 ]]; then diff --git a/scripts/99_terraform.sh b/scripts/99_terraform.sh index fc3999d..9fbed0f 100755 --- a/scripts/99_terraform.sh +++ b/scripts/99_terraform.sh @@ -12,6 +12,7 @@ trap 'err $LINENO' ERR TERM EXIT INT provider_version=$(grep ^[[:space:]]*version[[:space:]]= README.md | awk '{print $3}') +# loop over every modules for module in $(browse_modules "$(get_scope ${1:-})" 'inputs.tf'); do echo -e "\t- Terraform validate on module: ${module}" cat < ${module}/tmp.tf