Bluesky app fork with some witchin' additions 馃挮
witchsky.app
bluesky
fork
client
1#!/usr/bin/env sh
2
3get_container_id() {
4 local compose_file=$1
5 local service=$2
6 if [ -z "${compose_file}" ] || [ -z "${service}" ]; then
7 echo "usage: get_container_id <compose_file> <service>"
8 exit 1
9 fi
10
11 docker compose -f $compose_file ps --format json --status running \
12 | jq -r '.[]? | select(.Service == "'${service}'") | .ID'
13}
14
15# Exports all environment variables
16export_env() {
17 export_pg_env
18 export_redis_env
19}
20
21# Exports postgres environment variables
22export_pg_env() {
23 # Based on creds in compose.yaml
24 export PGPORT=5433
25 export PGHOST=localhost
26 export PGUSER=pg
27 export PGPASSWORD=password
28 export PGDATABASE=postgres
29 export DB_POSTGRES_URL="postgresql://pg:password@127.0.0.1:5433/postgres"
30}
31
32# Exports redis environment variables
33export_redis_env() {
34 export REDIS_HOST="127.0.0.1:6380"
35}
36
37# Main entry point
38main() {
39 # Expect a SERVICES env var to be set with the docker service names
40 local services=${SERVICES}
41
42 dir=$(dirname $0)
43 compose_file="${dir}/docker-compose.yaml"
44
45 # whether this particular script started the container(s)
46 started_container=false
47
48 # trap SIGINT and performs cleanup as necessary, i.e.
49 # taking down containers if this script started them
50 trap "on_sigint ${services}" INT
51 on_sigint() {
52 local services=$@
53 echo # newline
54 if $started_container; then
55 docker compose -f $compose_file rm -f --stop --volumes ${services}
56 fi
57 exit $?
58 }
59
60 # check if all services are running already
61 not_running=false
62 for service in $services; do
63 container_id=$(get_container_id $compose_file $service)
64 if [ -z $container_id ]; then
65 not_running=true
66 break
67 fi
68 done
69
70 # if any are missing, recreate all services
71 if $not_running; then
72 docker compose -f $compose_file up --wait --force-recreate ${services}
73 started_container=true
74 else
75 echo "all services ${services} are already running"
76 fi
77
78 # setup environment variables and run args
79 export_env
80 "$@"
81 # save return code for later
82 code=$?
83
84 # performs cleanup as necessary, i.e. taking down containers
85 # if this script started them
86 echo # newline
87 if $started_container; then
88 docker compose -f $compose_file rm -f --stop --volumes ${services}
89 fi
90
91 exit ${code}
92}