forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate-state.sh
More file actions
60 lines (50 loc) 路 1.7 KB
/
Copy pathvalidate-state.sh
File metadata and controls
60 lines (50 loc) 路 1.7 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
#!/usr/bin/env bash
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "${REPO_ROOT}"
TERRAFORM_DIR="${TERRAFORM_DIR:-infrastructure/terraform}"
REPORT_DIR="${REPORT_DIR:-infrastructure/ci/reports}"
log() { echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] $*"; }
error() { log "ERROR: $*"; }
mkdir -p "${REPORT_DIR}"
REPORT_FILE="${REPORT_DIR}/state-validation-$(date -u +%Y%m%d-%H%M%S).json"
check_state_file() {
local state_file="${TERRAFORM_DIR}/terraform.tfstate"
if [[ ! -f "${state_file}" ]]; then
log "No local state file found, checking remote state..."
terraform -chdir="${TERRAFORM_DIR}" state pull > /dev/null 2>&1 || true
fi
if terraform -chdir="${TERRAFORM_DIR}" state list > /dev/null 2>&1; then
log "State file is accessible."
else
error "State file is not accessible."
fi
}
check_state_version() {
log "Checking Terraform state version..."
local version
version=$(terraform -chdir="${TERRAFORM_DIR}" state pull 2>/dev/null | jq -r '.terraform_version // "unknown"' 2>/dev/null || echo "unknown")
log "State created with Terraform version: ${version}"
}
check_resource_count() {
log "Counting managed resources..."
local count
count=$(terraform -chdir="${TERRAFORM_DIR}" state list 2>/dev/null | wc -l)
log "Total resources in state: ${count}"
}
generate_report() {
jq -n \
--arg timestamp "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
--arg exit_code "${EXIT_CODE:-0}" \
'{timestamp: $timestamp, valid: ($exit_code == "0")}' \
> "${REPORT_FILE}"
log "Validation report written to ${REPORT_FILE}"
}
main() {
check_state_file
check_state_version
check_resource_count
generate_report
log "State validation completed."
}
main "$@"