forked from claranet/terraform-datadog-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
20_update_modules_readmes.sh
executable file
·125 lines (109 loc) · 4.34 KB
/
20_update_modules_readmes.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
source "$(dirname $0)/utils.sh"
init
echo "Update README.md for every ${REPO} modules"
# download awk script to hack terraform-docs
TERRAFORM_AWK="/tmp/terraform-docs.awk"
curl -Lso ${TERRAFORM_AWK} "https://raw.githubusercontent.com/cloudposse/build-harness/master/bin/terraform-docs.awk"
# this is the pattern from where custom information is saved to be restored
PATTERN_DOC="Related documentation"
# loop over every modules
for module in $(browse_modules "$(get_scope ${1:-})" "${REPO}-*.tf"); do
echo -e "\t- Generate README.md for module: ${module}"
cd ${module}
EXIST=0
if [ -f README.md ]; then
mv README.md README.md.bak
EXIST=1
fi
# module name from path
module_space=$(list_dirs ${module})
# module name with space as separator
module_upper=${module_space^^}
# module name with dash as separator
module_dash=${module_space//[ ]/-}
# module name with slash as separator
module_slash=${module_space//[ ]/\/}
# (re)generate README from scratch
cat <<EOF > README.md
# ${module_upper} DataDog ${REPO}
## How to use this module
\`\`\`
module "datadog-${REPO}-${module_dash}" {
source = "git::ssh://[email protected]/claranet/pt-monitoring/projects/datadog/terraform/${REPO}.git//${module_slash}?ref={revision}"
EOF
append=""
list=""
last_argument="source"
if [ "${REPO}" == "monitors" ]; then
cat <<EOF >> README.md
environment = var.environment
message = module.datadog-message-alerting.alerting-message
EOF
last_argument="message"
set +e
IFS='' read -r -d '' append <<EOF
## Purpose
Creates DataDog monitors with the following checks:
EOF
set -e
# 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
set +o pipefail
config=$(tail -n +${line} ${filename} | sed '/^}/q')
set -o pipefail
# parse monitor's name
name=$(get_name "$(echo "${config}" | grep 'name[[:space:]]*=')")
# search if monitor is 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 -q default.*true; then
name="${name} (disabled by default)"
fi
# 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
fi
# if README already exist
if [[ $EXIST -eq 1 ]]; then
# take all custom config in declaration module example after last argument and until the end of block to restore it
sed -n "/^[[:space:]]*${last_argument}[[:space:]]*=.*/,/^\}/p" README.md.bak | tail -n +2 | head -n -1 >> README.md
fi
# close block and generate the next until list of modules
cat <<EOF >> README.md
}
\`\`\`
${append}
EOF
if ! [ -z "${list}" ]; then
# write sorted list to readme appending newline to end
echo -e "$(echo -e "${list}" | sort -fdbi)\n" >> README.md
fi
# hack for terraform-docs with terraform 0.12 / HCL2 support
tmp_tf=$(mktemp -d)
awk -f ${TERRAFORM_AWK} ./*.tf > ${tmp_tf}/main.tf
# auto generate terraform docs (inputs and outputs)
terraform-docs --with-aggregate-type-defaults md table ${tmp_tf}/ >> README.md
rm -fr ${tmp_tf}
# if README does not exist
if [[ $EXIST -eq 0 ]]; then
# Simply add empty documentation section
cat <<EOF >> README.md
## ${PATTERN_DOC}
EOF
else
# else restore the custom information saved before
grep -Pzo --color=never ".*${PATTERN_DOC}(.*\n)*" README.md.bak | head -n -1 >> README.md
rm -f README.md.bak
fi
# force unix format (I don't know why for now but you never know)
dos2unix README.md 2> /dev/null
cd - >> /dev/null
done