forked from redhat-appstudio/managed-gitops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-dev-env.sh
executable file
·403 lines (352 loc) · 13.7 KB
/
create-dev-env.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/env bash
# Checks if a binary is present on the local system
POSTGRESQL_DATABASE="${POSTGRESQL_DATABASE:=postgres}"
exit_if_binary_not_installed() {
for binary in "$@"; do
command -v "$binary" >/dev/null 2>&1 || {
echo >&2 "Script requires '$binary' command-line utility to be installed on your local machine. Aborting..."
exit 1
}
done
}
# Checks if a command is successful within a timeframe
wait-until() {
command="${1}"
timeout="${2:-120}"
i=1
until eval "${command}"; do
echo "$i try ..."
((i++))
if [ "${i}" -gt "${timeout}" ]; then
echo "postgres server never replied back, aborting due to ${timeout}s timeout!"
exit 1
fi
sleep 1
done
}
# Installs 'db-schema.sql' into the PostgreSQL running in Kubernetes cluster
# With 'kube' is tests if port-forwarding is working and gives you the commands to do it manually
if [ "$1" = "kube" ]; then
exit_if_binary_not_installed "kubectl" "psql"
# Get the secret
counter=0
until kubectl -n gitops get secrets | grep gitops-postgresql-staging
do
((counter++))
sleep 1
if [ "$counter" -gt 30 ]; then
echo " --> Error: Cannot find gitops-postgresql-staging secret."
exit 1
fi
done
echo " * Postgres secret has been created."
# Wait until postgres pod is running
echo " * Wait until Postgres pod is running"
counter=0
until kubectl -n gitops get pods | grep postgres | grep '1/1' | grep 'Running' &> /dev/null
do
((counter++))
sleep 1
if [ "$counter" -gt 150 ]; then
echo " --> Error: PostgreSQL pod cannot start. Quitting ..."
echo ""
echo "Namespace events:"
kubectl get events -n gitops
exit 1
fi
done
echo " * Postgres Pod is running."
# With the new migration logic, this block should no longer be required: remove the commented out
# section once we confirmed it is no longer needed.
# Checks if 5432 is occupied
if lsof -i:5432 | grep LISTEN; then
echo " --> Error: Your local port TCP 5432 is already in use. Quit port-forward."
exit 1
fi
echo " * Start port-fowarding PostgreSQL to localhost:5432 ..."
# Port forward the PostgreSQL service locally, so we can access it
kubectl port-forward --namespace gitops svc/gitops-postgresql-staging 5432:5432 &
KUBE_PID=$!
# Checks if 5432 is occupied
counter=0
until lsof -i:5432 | grep LISTEN
do
sleep 1
if [ "$counter" -gt 10 ]; then
echo ".. retry $counter ..."
echo " --> Error: Port-forwarding takes too long. Quiting ..."
if ! kill $KUBE_PID; then
echo " --> Error: Cannot kill the background port-forward, do it yourself."
fi
exit 1
fi
done
echo " * Port-Forwarding worked"
# Decode the password from the secret
POSTGRES_PASSWORD=$(kubectl get -n gitops secret gitops-postgresql-staging -o jsonpath="{.data.postgresql-password}" | base64 --decode)
# Call the migration binary to migrate the database to the latest version
make db-migrate
# Stop port-forwarding
if ! kill $KUBE_PID; then
echo " --> Error: Cannot kill the background port-forward, do it yourself."
exit 1
else
echo " * Port-forwarding has been successfully stopped"
fi
if lsof -i:5432 | grep LISTEN; then
echo " * Port forwarding is still active for some reason. Investigate further ..."
fi
# Exit now, do not continue with the rest of the bash script
echo
echo " ------------------------------------------------"
echo "| To access the database outside of the cluster |"
echo " ------------------------------------------------"
echo " - Run: kubectl port-forward --namespace gitops svc/gitops-postgresql-staging 5432:5432 &"
echo " - Credentials: HOST=127.0.0.1:5432 USERNAME=postgres PASSWORD=$POSTGRES_PASSWORD DB=$POSTGRESQL_DATABASE"
echo " - Access Example: psql postgresql://postgres:[email protected]:5432/$POSTGRESQL_DATABASE -c \"select now()\""
echo
echo " - To run Backend & ClusterAgent Operators locally: export DB_PASS=$POSTGRES_PASSWORD && goreman start"
echo " -------------------------------------------------"
echo
exit 0
fi
# With 'kube-auto' it will try to do it automatically (if it fails, you can still do it manually)
if [ "$1" = "kube-auto" ]; then
exit_if_binary_not_installed "kubectl" "psql"
# Get the secret
counter=0
until kubectl -n gitops get secrets | grep gitops-postgresql-staging
do
((counter++))
sleep 1
if [ "$counter" -gt 30 ]; then
echo " --> Error: Cannot find gitops-postgresql-staging secret."
exit 1
fi
done
echo " * Postgres secret has been created."
# Wait until postgres pod is running
echo " * Wait until Postgres pod is running"
counter=0
until kubectl -n gitops get pods | grep postgres | grep '1/1' | grep 'Running' &> /dev/null
do
((counter++))
sleep 1
if [ "$counter" -gt 150 ]; then
echo " --> Error: PostgreSQL pod cannot start. Quitting ..."
echo ""
echo "Namespace events:"
kubectl get events -n gitops
exit 1
fi
done
echo " * Postgres Pod is running."
# With the new migration logic, this block should no longer be required: remove the commented out
# section once we confirmed it is no longer needed.
# Checks if 5432 is occupied
if lsof -i:5432 | grep LISTEN; then
echo " --> Error: Your local port TCP 5432 is already in use. Quit port-forward."
exit 1
fi
echo " * Start port-fowarding PostgreSQL to localhost:5432 ..."
# Port forward the PostgreSQL service locally, so we can access it
kubectl port-forward --namespace gitops svc/gitops-postgresql-staging 5432:5432 &>/dev/null &
KUBE_PID=$!
# Checks if 5432 is occupied
counter=0
until lsof -i:5432 | grep LISTEN
do
sleep 1
if [ "$counter" -gt 10 ]; then
echo ".. retry $counter ..."
echo " --> Error: Port-forwarding takes too long. Quiting ..."
if ! kill $KUBE_PID; then
echo " --> Error: Cannot kill the background port-forward, do it yourself."
fi
exit 1
fi
done
echo " * Port-Forwarding worked"
# Decode the password from the secret
POSTGRES_PASSWORD=$(kubectl get -n gitops secret gitops-postgresql-staging -o jsonpath="{.data.postgresql-password}" | base64 --decode)
# Call the migration binary to migrate the database to the latest version
make db-migrate
# Do not stop port-forwarding
echo "Port-forwarding is active. You can stop it with 'kill $KUBE_PID'"
echo "Or you can find the process with typing: 'sudo lsof -i:5432'"
# Set up pg_stat_statements for postgresql metrics
echo " * Setting up pg_stat_statements for PostgreSQL metrics"
# This statement creates the postgresql.auto.conf file and is located in the same folder as the postgresql.conf file
# It can located via the command SHOW config_file;
./psql.sh -c 'ALTER SYSTEM SET shared_preload_libraries TO pg_stat_statements;'
# This reloads the config
./psql.sh -c 'select pg_reload_conf();'
# This creates the pg_stat_statements extension if it doesn't already exist
./psql.sh -c 'CREATE EXTENSION IF NOT EXISTS pg_stat_statements;'
# The above requires us to restart the server/service. For our dev env, this should be 'ok' to do.
echo " * Need to restart the Postgres pod and wait until Postgres pod is running"
# This command forces a delete and recreate of the postgres pod. Set the timeout to be 5s for now because the code
# that follows will wait for the pod to be recreated. That way we see the dots (...) to indicate that you have to wait.
kubectl get pod gitops-postgresql-staging-postgresql-0 -n gitops -o yaml | kubectl replace --timeout=5s --force -f - &> /dev/null
# Now, wait until postgres pod is running
echo " * Wait until Postgres pod is running and ready"
counter=0
until kubectl -n gitops get pods | grep postgres | grep '1/1' | grep 'Running' &> /dev/null
do
((counter++))
sleep 1
echo -n "."
if [ "$counter" -gt 150 ]; then
echo " --> Error: PostgreSQL pod cannot start. Quitting ..."
echo ""
echo "Namespace events:"
kubectl get events -n gitops
exit 1
fi
done
echo
echo " * Postgres Pod is running."
echo " * Finished setting up pg_stat_statements for PostgreSQL metrics"
echo " * Restarting port forwarding"
# The port forward process needs to be terminated and restarted since we restarted the PostgreSQL service, so
# kill the original process. If we don't kill it, the server will eventually kill it.
# Stop port-forwarding
if ! kill $KUBE_PID &>/dev/null; then
echo " Error: Cannot kill the port-forward process, kill the process yourself and rerun the command"
exit 1
else
wait $KUBE_PID 2>/dev/null
echo " * Previous port-forwarding has been successfully stopped"
fi
echo " * Creating a new port-forward process"
# Port forward the PostgreSQL service locally again, so we can access it
kubectl port-forward --namespace gitops svc/gitops-postgresql-staging 5432:5432 &>/dev/null &
PORT_FORWARD_PID=$!
# Checks if 5432 is occupied
counter=0
until lsof -i:5432 | grep LISTEN
do
sleep 1
if [ "$counter" -gt 10 ]; then
echo "Timed out waiting for port-forward process to start. Rerun port-forward command."
exit 1
fi
done
echo " * Port-Forwarding worked"
echo " * Port-forwarding is active. You can stop it with 'kill $PORT_FORWARD_PID'"
echo " * Or you can find the process with typing: 'sudo lsof -i:5432'"
exit 0
fi
# Binary requirements
exit_if_binary_not_installed "mktemp" "docker"
# Exit if docker is not running (or cannot interact with it)
if ! docker info >/dev/null 2>&1; then
echo "Error: docker is not running"
exit 1
fi
SCRIPTPATH="$(
cd -- "$(dirname "$0")" >/dev/null 2>&1 || exit
pwd -P
)"
# Variables
NETWORK="gitops-net"
POSTGRES_CONTAINER="managed-gitops-postgres"
PGADMIN_CONTAINER="managed-gitops-pgadmin"
RETRIES=30 # in seconds
POSTGRES_DATA_DIR=$(mktemp -d -t postgres-XXXXXXXXXX) # Map the docker data directory into a temporary directory
POSTGRES_SERVER_IS_UP="docker exec --user postgres -e PGPASSWORD=gitops -i \"$POSTGRES_CONTAINER\" \"psql\" -h localhost -d postgres -U postgres -p 5432 -c \"select 1\" | grep '1 row' >/dev/null 2>&1"
# Create docker network if one doesn't exist yet
echo "* Creating docker network '$NETWORK'"
ID=$(docker network ls --filter "name=$NETWORK" -q 2>/dev/null)
if [ "$(docker network inspect "$ID" -f '{{.Name}}')" == "$NETWORK" ]; then
echo " Skip creation: '$NETWORK' has already been created."
else
docker network create "$NETWORK"
fi
# TEST IT
if ! docker network ls | grep "$NETWORK" &>/dev/null; then
echo "Error: Docker network $NETWORK cannot be created. Aborting ..."
exit 1
fi
echo
echo "* Starting postgresql container: $POSTGRES_CONTAINER"
# Add this server to pgadmin using:
# - hostname: managed-gitops-postgres
# - port: 5432
# - username: postgres (default, but we also explicitly set this)
# - password: gitops
if [ "$(docker container inspect -f '{{.State.Status}}' $POSTGRES_CONTAINER 2>/dev/null)" == "running" ]; then
echo " Skip creation: '$POSTGRES_CONTAINER' container is already running."
else
docker run --name "$POSTGRES_CONTAINER" \
-v "$POSTGRES_DATA_DIR":/var/lib/postgresql/data:Z \
-e POSTGRES_PASSWORD=gitops \
-e POSTGRES_USER=postgres \
-p 5432:5432 \
--network gitops-net \
-d \
postgres:13 \
-c log_statement='all' \
-c log_min_duration_statement=0
fi
if ! docker ps | grep "$POSTGRES_CONTAINER" >/dev/null 2>&1; then
echo "Container '$POSTGRES_CONTAINER' is not running. Aborting ..."
exit 1
fi
# -c options are from https://pg.uptrace.dev/faq/#how-to-view-queries-this-library-generates
echo
echo "* Starting pgadmin container: $PGADMIN_CONTAINER"
if [ "$(docker container inspect -f '{{.State.Status}}' $PGADMIN_CONTAINER 2>/dev/null)" == "running" ]; then
echo " Skip creation: '$PGADMIN_CONTAINER' container is already running."
else
# pgadmin login/password is the email/password below
docker run --name "$PGADMIN_CONTAINER" -p 8080:80 \
-e '[email protected]' \
-e 'PGADMIN_DEFAULT_PASSWORD=gitops' \
--network gitops-net \
-d dpage/pgadmin4
fi
if ! docker ps | grep "$PGADMIN_CONTAINER" >/dev/null 2>&1; then
echo "Container '$PGADMIN_CONTAINER' is not running. Aborting ..."
exit 1
fi
echo
echo "* Waiting $RETRIES seconds until postgres server is up..."
wait-until "$POSTGRES_SERVER_IS_UP" "${RETRIES}"
echo " Done"
echo
echo "* Initializing DB"
echo " Copying db-schema.sql into the postgres container."
if ! docker cp "$SCRIPTPATH/db-schema.sql" $POSTGRES_CONTAINER:/ >/dev/null 2>&1; then
echo "db-schema.sql cannot be copied into the '$POSTGRES_CONTAINER' container"
exit 1
fi
if [ "$USE_MASTER_SCHEMA" != "true" ]; then
# After the database starts, ensure it is up to date with the latest schema.
# This is the default behaviour, and is nearly always the corrects choice.
make db-migrate
else
echo
echo "* Initializing database with master schema"
echo
# Start the database with the master schema.
# The primary (and currently, only) use for this is for the 'check-db-schema.sh' script.
# WARNING: this will cause backend to fail to start, as it expects an empty database on startup.
docker exec \
--user postgres \
-e PGPASSWORD=gitops \
-i "$POSTGRES_CONTAINER" "psql" \
-h localhost \
-d $POSTGRESQL_DATABASE \
-U postgres \
-p 5432 \
-q -f db-schema.sql
fi
echo
echo "== Dev environment initialized =="
echo " Postgres username: 'postgres'"
echo " Postgres password: 'gitops'"
echo " Postgres ip address: $(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $POSTGRES_CONTAINER)"
echo " Pgadmin username: '[email protected]'"
echo " Pgadmin password: 'gitops'"
echo