forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate-env-configs.sh
More file actions
144 lines (127 loc) 路 4.39 KB
/
Copy pathvalidate-env-configs.sh
File metadata and controls
144 lines (127 loc) 路 4.39 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env bash
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "${REPO_ROOT}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPORT_DIR="${REPORT_DIR:-infrastructure/ci/reports}"
BLOCK_DEPLOY="${BLOCK_DEPLOY:-false}"
EXIT_CODE=0
ENVIRONMENTS=("dev" "staging" "production")
log() { echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] $*"; }
error() { log "ERROR: $*"; EXIT_CODE=1; }
mkdir -p "${REPORT_DIR}"
REPORT_FILE="${REPORT_DIR}/config-validation-$(date -u +%Y%m%d-%H%M%S).json"
validate_terraform_vars() {
log "Validating Terraform variable consistency across environments..."
local tf_dir="infrastructure/terraform"
local all_vars=()
local env_vars=()
for env in "${ENVIRONMENTS[@]}"; do
local var_file="${tf_dir}/${env}.tfvars"
if [[ ! -f "${var_file}" ]]; then
error "Missing terraform vars file: ${var_file}"
continue
fi
while IFS='=' read -r key _; do
key="$(echo "${key}" | xargs)"
[[ -z "${key}" ]] && continue
env_vars+=("${key}")
done < <(grep -v '^\s*#' "${var_file}" | grep '=' || true)
done
for env in "${ENVIRONMENTS[@]}"; do
local var_file="${tf_dir}/${env}.tfvars"
[[ ! -f "${var_file}" ]] && continue
while IFS='=' read -r key value; do
key="$(echo "${key}" | xargs)"
value="$(echo "${value}" | xargs)"
[[ -z "${key}" ]] && continue
all_vars+=("${key}=${value}")
done < <(grep -v '^\s*#' "${var_file}" | grep '=' || true)
done
}
validate_k8s_configmaps() {
log "Validating Kubernetes ConfigMap consistency..."
local configmaps_dir="infrastructure/k8s/configmaps"
local keys_found=()
for env in "${ENVIRONMENTS[@]}"; do
local cm_file="${configmaps_dir}/${env}-configmap.yaml"
if [[ ! -f "${cm_file}" ]]; then
error "Missing ConfigMap: ${cm_file}"
continue
fi
while IFS=': ' read -r key value; do
key="$(echo "${key}" | xargs)"
[[ -z "${key}" ]] && continue
keys_found+=("${env}:${key}=${value}")
done < <(grep -A500 'data:' "${cm_file}" | grep -E '^\s+[a-zA-Z_]' || true)
done
for env in "${ENVIRONMENTS[@]}"; do
local cm_file="${configmaps_dir}/${env}-configmap.yaml"
[[ ! -f "${cm_file}" ]] && continue
local defined_keys
defined_keys=$(grep -A500 'data:' "${cm_file}" | grep -Eo '^\s+[a-zA-Z_][a-zA-Z0-9_]*' | tr -d ' ' || true)
while IFS= read -r key; do
[[ -z "${key}" ]] && continue
for other_env in "${ENVIRONMENTS[@]}"; do
[[ "${other_env}" == "${env}" ]] && continue
local other_file="${configmaps_dir}/${other_env}-configmap.yaml"
if [[ -f "${other_file}" ]]; then
if ! grep -q "${key}:" "${other_file}"; then
error "ConfigMap key '${key}' exists in ${env} but missing in ${other_env}"
fi
fi
done
done <<< "${defined_keys}"
done
}
validate_env_files() {
log "Validating .env files..."
for env in "${ENVIRONMENTS[@]}"; do
local env_file=".env.${env}"
local example_file=".env.${env}.example"
if [[ ! -f "${env_file}" ]]; then
error "Missing environment file: ${env_file}"
continue
fi
if [[ ! -f "${example_file}" ]]; then
log "WARNING: No example file for ${env}, skipping cross-check"
continue
fi
local example_vars
example_vars=$(grep -v '^\s*#' "${example_file}" | grep -Eo '^[A-Z_][A-Z0-9_]*' || true)
while IFS= read -r var; do
[[ -z "${var}" ]] && continue
if ! grep -q "${var}=" "${env_file}"; then
error "Variable '${var}' defined in ${example_file} but missing in ${env_file}"
fi
done <<< "${example_vars}"
done
}
generate_report() {
if [[ "${EXIT_CODE}" -eq 0 ]]; then
log "All environment configurations are consistent."
fi
jq -n \
--arg timestamp "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
--arg exit_code "${EXIT_CODE}" \
--arg block_deploy "${BLOCK_DEPLOY}" \
'{timestamp: $timestamp, valid: ($exit_code == "0"), block_deploy: $block_deploy}' \
> "${REPORT_FILE}"
log "Report written to ${REPORT_FILE}"
}
main() {
validate_terraform_vars
validate_k8s_configmaps
validate_env_files
generate_report
if [[ "${EXIT_CODE}" -ne 0 ]]; then
log "Configuration validation FAILED."
if [[ "${BLOCK_DEPLOY}" == "true" ]]; then
log "BLOCKING deploy due to configuration inconsistencies."
fi
else
log "Configuration validation PASSED."
fi
exit "${EXIT_CODE}"
}
main "$@"