-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartup.sh
58 lines (47 loc) · 2.13 KB
/
startup.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
#!/usr/bin/env bash
# note: this script is sourced from the CircleCI orb, and CircleCI executes the code with `set -eo pipefail`
if [[ -z "${CLOUDSQL_PROXY_SA}" ]]; then
echo "'CLOUDSQL_PROXY_SA' environment variable not defined. Place the contents of the service account json in this environment variable" 1>&2
exit 1
else
# Replace \\n with an actual newline, for .env file compatibility
echo "${CLOUDSQL_PROXY_SA}" > /tmp/sa.json
fi
if [[ -z "${GCLOUD_SQL_INSTANCE}" ]]; then
echo "'GCLOUD_SQL_INSTANCE' environment variable not defined. Specify the cloudsql instance in this environment variable" 1>&2
exit 1
fi
export GOOSE_DBSTRING="user='${POSTGRES_USER}' password='${POSTGRES_PASSWORD}' dbname='${POSTGRES_DATABASE}' sslmode=disable host=localhost port=${POSTGRES_PORT}"
echo "Starting the cloudsql proxy"
touch /tmp/cloudsql.log
exec /proxy/cloud_sql_proxy -dir=/cloudsql -instances="$GCLOUD_SQL_INSTANCE" -credential_file=/tmp/sa.json > /tmp/cloudsql.log 2>&1 &
# Set true in the shared memory
# Check whether the ready message was received
echo "1" >/dev/shm/cloudsql_ready
# Wait for the cloudsql connection to go up
echo "Waiting for ready message from cloudsql proxy..."
# The tail grep command will get stuck if no ready message arrives, so we set a timer to kill it
# Even if the grep terminates naturally, the tail is running in the background so it needs to be killed
delayTailKill() {
sleep 10
# When killing the tail, set the shared memory to zero
echo "0" >/dev/shm/cloudsql_ready
pkill -s 0 tail
}
delayTailKill &>/dev/null &
( tail -f -n +1 /tmp/cloudsql.log & ) | grep -q "Ready for new connections" || true
# Remove SA json from the tmp folder
# Nobody should have access either way, but just to check
rm /tmp/sa.json
# Print the log for debug purposes
tail -n +1 /tmp/cloudsql.log
# Read from shared memory to see if the tail was killed or the ready message arrived
if (( "$(</dev/shm/cloudsql_ready)" != "1" ));
then
echo "Waiting for cloudsql connection timed out. Exitting."
# Dump the log so you can see what went wrong
cat /tmp/cloudsql.log
exit 1
else
echo "Cloudsql proxy ready"
fi