forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1#!/usr/bin/env sh
2
3# Exit if any command fails
4set -e
5
6get_container_id() {
7 local compose_file=$1
8 local service=$2
9 if [ -z "${compose_file}" ] || [ -z "${service}" ]; then
10 echo "usage: get_container_id <compose_file> <service>"
11 exit 1
12 fi
13
14 # first line of jq normalizes for docker compose breaking change, see docker/compose#10958
15 docker compose --file $compose_file ps --format json --status running \
16 | jq -sc '.[] | if type=="array" then .[] else . end' | jq -s \
17 | jq -r '.[]? | select(.Service == "'${service}'") | .ID'
18}
19
20# Exports all environment variables
21export_env() {
22 export_pg_env
23}
24
25# Exports postgres environment variables
26export_pg_env() {
27 # Based on creds in compose.yaml
28 export PGPORT=5433
29 export PGHOST=localhost
30 export PGUSER=pg
31 export PGPASSWORD=password
32 export PGDATABASE=postgres
33 export DB_POSTGRES_URL="postgresql://pg:password@127.0.0.1:5433/postgres"
34}
35
36
37pg_clear() {
38 local pg_uri=$1
39
40 for schema_name in `psql "${pg_uri}" -c "SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT LIKE 'pg_%' AND schema_name NOT LIKE 'information_schema';" -t`; do
41 psql "${pg_uri}" -c "DROP SCHEMA \"${schema_name}\" CASCADE;"
42 done
43}
44
45pg_init() {
46 local pg_uri=$1
47
48 psql "${pg_uri}" -c "CREATE SCHEMA IF NOT EXISTS \"public\";"
49}
50
51main_native() {
52 local services=${SERVICES}
53 local postgres_url_env_var=`[[ $services == *"db_test"* ]] && echo "DB_TEST_POSTGRES_URL" || echo "DB_POSTGRES_URL"`
54
55 postgres_url="${!postgres_url_env_var}"
56
57 if [ -n "${postgres_url}" ]; then
58 echo "Using ${postgres_url_env_var} (${postgres_url}) to connect to postgres."
59 pg_init "${postgres_url}"
60 else
61 echo "Postgres connection string missing did you set ${postgres_url_env_var}?"
62 exit 1
63 fi
64
65 cleanup() {
66 local services=$@
67
68 if [ -n "${postgres_url}" ] && [[ $services == *"db_test"* ]]; then
69 pg_clear "${postgres_url}" &> /dev/null
70 fi
71 }
72
73 # trap SIGINT and performs cleanup
74 trap "on_sigint ${services}" INT
75 on_sigint() {
76 cleanup $@
77 exit $?
78 }
79
80 # Run the arguments as a command
81 DB_POSTGRES_URL="${postgres_url}" \
82 "$@"
83 code=$?
84
85 cleanup ${services}
86
87 exit ${code}
88}
89
90main_docker() {
91 # Expect a SERVICES env var to be set with the docker service names
92 local services=${SERVICES}
93
94 dir=$(dirname $0)
95 compose_file="${dir}/docker-compose.yaml"
96
97 # whether this particular script started the container(s)
98 started_container=false
99
100 # performs cleanup as necessary, i.e. taking down containers
101 # if this script started them
102 cleanup() {
103 local services=$@
104 echo # newline
105 if $started_container; then
106 docker compose --file $compose_file rm --force --stop --volumes ${services}
107 fi
108 }
109
110 # trap SIGINT and performs cleanup
111 trap "on_sigint ${services}" INT
112 on_sigint() {
113 cleanup $@
114 exit $?
115 }
116
117 # check if all services are running already
118 not_running=false
119 for service in $services; do
120 container_id=$(get_container_id $compose_file $service)
121 if [ -z $container_id ]; then
122 not_running=true
123 break
124 fi
125 done
126
127 # if any are missing, recreate all services
128 if $not_running; then
129 started_container=true
130 docker compose --file $compose_file up --wait --force-recreate ${services}
131 else
132 echo "all services ${services} are already running"
133 fi
134
135 # do not exit when following commands fail, so we can intercept exit code & tear down docker
136 set +e
137
138 # setup environment variables and run args
139 export_env
140 "$@"
141 # save return code for later
142 code=$?
143
144 # performs cleanup as necessary
145 cleanup ${services}
146 exit ${code}
147}
148
149# Main entry point
150main() {
151 if ! docker ps >/dev/null 2>&1; then
152 echo "Docker unavailable. Running on host."
153 main_native $@
154 else
155 main_docker $@
156 fi
157}