-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·171 lines (148 loc) · 6.97 KB
/
setup.sh
File metadata and controls
executable file
·171 lines (148 loc) · 6.97 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash
# Exit on error
set -e
# Define color codes and symbols
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
CHECK_MARK="${GREEN}✓${NC}"
X_MARK="${RED}✗${NC}"
SPINNER="⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
# Function to execute command and show status with spinner
show_spinner() {
local message=$1
shift # Remove message from arguments
local start_time=$(date +%s)
local i=0
local spin_len=${#SPINNER}
local message_width=60 # Fixed width for message area
# Create a temporary file for command output
local temp_output=$(mktemp)
# Start the command in background, redirecting output to temp file
"$@" > "$temp_output" 2>&1 &
local pid=$!
# Show spinner while command is running
while ps -p $pid > /dev/null; do
local elapsed=$(( $(date +%s) - start_time ))
local spinner=${SPINNER:i++%spin_len:1}
# Clear the entire line first
echo -en "\r$(tput el)"
# Print with fixed message width and status
printf "\r%-*s [%s] %ds" "$message_width" "$message" "$(echo -e "${YELLOW}${spinner}${NC}")" "$elapsed"
sleep 0.12 # Slightly slower animation for better visibility
done
# Wait for command to complete and get exit code
wait $pid
local exit_code=$?
local elapsed=$(( $(date +%s) - start_time ))
# Show final status
echo -en "\r$(tput el)" # Clear the entire line
if [ $exit_code -eq 0 ]; then
printf "\r%-*s [%s] %ds\n" "$message_width" "$message" "$(echo -e "$CHECK_MARK")" "$elapsed"
else
printf "\r%-*s [%s] %ds\n" "$message_width" "$message" "$(echo -e "$X_MARK")" "$elapsed"
# If command failed, show the output
if [ -s "$temp_output" ]; then
echo -e "${RED}Command output:${NC}"
cat "$temp_output"
fi
fi
# Clean up temp file
rm -f "$temp_output"
return $exit_code
}
# Function to execute command and show status
run_step() {
local message=$1
shift # Remove message from arguments
show_spinner "$message" "$@"
}
# Function to print section header
print_section() {
echo -e "\n${BLUE}=== $1 ===${NC}"
}
# Function to check prerequisites
check_prerequisites() {
local required_commands=("kubectl" "helm" "kind" "kubectl-krew")
for cmd in "${required_commands[@]}"; do
run_step "[Prerequisites] Checking $cmd installation" command -v "$cmd"
done
}
# Check prerequisites
print_section "Prerequisites Check"
check_prerequisites
# Infrastructure and Storage Setup
print_section "Infrastructure and Storage Setup"
run_step "[Kind] Resetting previous environment" kind delete cluster --name pg-demo || true
run_step "[Kind] Creating cluster" kind create cluster --name pg-demo --config manifests/config/kind-config.yaml
run_step "[Kind] Waiting for nodes" kubectl wait --for=condition=ready nodes --all --timeout=60s
run_step "[Kind] Creating pg-demo namespace" kubectl apply -f manifests/config/namespaces/postgres-demo-namespace.yaml
run_step "[Storage] Configuring storage class" kubectl apply -f manifests/config/storage/postgres-storage-class.yaml
# Security and Certificates Setup
print_section "Security and Certificates Setup"
run_step "[Cert-Manager] Installing cert-manager" kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.13.3/cert-manager.yaml
run_step "[Cert-Manager] Waiting for deployment" kubectl wait --for=condition=ready pod -l app.kubernetes.io/instance=cert-manager -n cert-manager --timeout=60s
run_step "[CNPG] Deploying certificates" kubectl apply -f manifests/certificates/ca-certificates.yaml
run_step "[CNPG] Waiting for certificates" kubectl wait --for=condition=ready certificate -n pg-demo --all --timeout=60s
run_step "[CNPG] Creating admin credentials job" kubectl apply -f manifests/config/jobs/postgres-generate-admin-secret-job.yaml
run_step "[CNPG] Waiting for admin credentials job" kubectl wait --for=condition=complete job/postgres-generate-admin-secret-job -n pg-demo --timeout=60s
# PostgreSQL Setup and Validation
print_section "PostgreSQL Setup and Validation"
run_step "[CNPG] Adding Helm repository" helm repo add cloudnative-pg https://cloudnative-pg.github.io/charts && helm repo update >/dev/null 2>&1
run_step "[CNPG] Installing operator" helm install cloudnative-pg cloudnative-pg/cloudnative-pg --namespace pg-demo
run_step "[CNPG] Waiting for operator deployment" kubectl wait --for=condition=ready pod -l app.kubernetes.io/name=cloudnative-pg -n pg-demo --timeout=60s
run_step "[CNPG] Deploying primary cluster" kubectl apply -f manifests/clusters/postgres-primary.yaml
run_step "[CNPG] Waiting for primary cluster" kubectl wait --for=condition=ready cluster/postgres-primary -n pg-demo --timeout=60s
run_step "[CNPG] Deploying replica cluster" kubectl apply -f manifests/clusters/postgres-replica.yaml
run_step "[CNPG] Waiting for replica cluster" kubectl wait --for=condition=ready cluster/postgres-replica -n pg-demo --timeout=60s
# Cluster Health Validation
print_section "Cluster Health Validation"
run_step "[Health] Validating primary cluster status" bash -c '
output=$(kubectl cnpg status postgres-primary -n pg-demo)
echo "$output"
if ! echo "$output" | grep -q "Cluster in healthy state"; then
echo "Primary cluster is not healthy"
exit 1
fi
if ! echo "$output" | grep -q "Primary.*OK"; then
echo "Primary cluster is not in primary role"
exit 1
fi
if ! echo "$output" | grep -q "Ready instances:.*1"; then
echo "Primary cluster does not have ready instances"
exit 1
fi
'
run_step "[Health] Validating replica cluster status" bash -c '
output=$(kubectl cnpg status postgres-replica -n pg-demo)
echo "$output"
if ! echo "$output" | grep -q "Cluster in healthy state"; then
echo "Replica cluster is not healthy"
exit 1
fi
if ! echo "$output" | grep -q "Source cluster:.*postgres-primary"; then
echo "Replica is not replicating from the correct primary"
exit 1
fi
if ! echo "$output" | grep -q "Ready instances:.*1"; then
echo "Replica cluster does not have ready instances"
exit 1
fi
'
# Replication Tests Setup
print_section "Replication Tests Setup"
run_step "[Replication] Waiting for replication to initialize" kubectl wait --for=condition=ready cluster/postgres-replica -n pg-demo --timeout=300s
# Create ConfigMap for replication tests
run_step "[Replication] Creating test ConfigMap" bash -c '
cd manifests/config/tests/replication-test && \
kubectl create configmap sql-replication-test \
--from-file=replication-test-replica.sql \
--from-file=replication-test-primary.sql \
-n pg-demo \
--dry-run=client -o yaml | kubectl apply -f -'
# Run replication tests
print_section "Replication Tests"
run_step "[Replication] Running replication tests" bash manifests/config/tests/replication-test/run-replication-tests.sh
echo -e "\n${GREEN}✓ PostgreSQL setup completed successfully!${NC}"