forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrift-report.sh
More file actions
92 lines (79 loc) · 2.6 KB
/
Copy pathdrift-report.sh
File metadata and controls
92 lines (79 loc) · 2.6 KB
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
#!/usr/bin/env bash
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "${REPO_ROOT}"
REPORT_DIR="${REPORT_DIR:-infrastructure/ci/reports}"
OUTPUT_FORMAT="${OUTPUT_FORMAT:-text}" # text | json | markdown
log() { echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] $*"; }
latest_report() {
ls -t "${REPORT_DIR}"/drift-*.json 2>/dev/null | head -1
}
render_text() {
local report="$1"
local ts tf_count k8s_count
ts="$(jq -r '.timestamp' "${report}")"
tf_count="$(jq '.terraform_drift | length' "${report}")"
k8s_count="$(jq '.kubernetes_drift | length' "${report}")"
echo "========================================"
echo " GistPin Infrastructure Drift Report"
echo " Generated: ${ts}"
echo "========================================"
echo ""
echo "Terraform Drift (${tf_count} resource(s)):"
if (( tf_count > 0 )); then
jq -r '.terraform_drift[] | " [\(.action | join(","))] \(.resource) — \(.reason)"' "${report}"
else
echo " None"
fi
echo ""
echo "Kubernetes Drift (${k8s_count} resource(s)):"
if (( k8s_count > 0 )); then
jq -r '.kubernetes_drift[] | " \(.kind)/\(.name) in \(.namespace) [\(.manifest)]"' "${report}"
else
echo " None"
fi
echo ""
echo "Total drifted resources: $(( tf_count + k8s_count ))"
}
render_markdown() {
local report="$1"
local ts tf_count k8s_count
ts="$(jq -r '.timestamp' "${report}")"
tf_count="$(jq '.terraform_drift | length' "${report}")"
k8s_count="$(jq '.kubernetes_drift | length' "${report}")"
echo "# GistPin Infrastructure Drift Report"
echo ""
echo "**Generated:** ${ts}"
echo ""
echo "## Terraform Drift (${tf_count})"
if (( tf_count > 0 )); then
echo "| Resource | Action | Reason |"
echo "|---|---|---|"
jq -r '.terraform_drift[] | "| \(.resource) | \(.action | join(",")) | \(.reason) |"' "${report}"
else
echo "_No drift detected._"
fi
echo ""
echo "## Kubernetes Drift (${k8s_count})"
if (( k8s_count > 0 )); then
echo "| Kind | Name | Namespace | Manifest |"
echo "|---|---|---|---|"
jq -r '.kubernetes_drift[] | "| \(.kind) | \(.name) | \(.namespace) | \(.manifest) |"' "${report}"
else
echo "_No drift detected._"
fi
}
main() {
local report="${1:-$(latest_report)}"
if [[ -z "${report}" || ! -f "${report}" ]]; then
log "No drift report found in ${REPORT_DIR}. Run detect-drift.sh first."
exit 1
fi
log "Rendering report: ${report} (format: ${OUTPUT_FORMAT})"
case "${OUTPUT_FORMAT}" in
json) cat "${report}" ;;
markdown) render_markdown "${report}" ;;
*) render_text "${report}" ;;
esac
}
main "$@"