Skip to content

Commit

Permalink
feat(dev-infra): allow running apps without docker
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieusieben committed Dec 5, 2023
1 parent 538ae55 commit 93513ee
Showing 1 changed file with 75 additions and 2 deletions.
77 changes: 75 additions & 2 deletions packages/dev-infra/_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,79 @@ export_redis_env() {
export REDIS_HOST="127.0.0.1:6380"
}

pg_clear() {
local pg_uri=$1

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
psql "${pg_uri}" -c "DROP SCHEMA \"${schema_name}\" CASCADE;" &
done
wait
}

pg_init() {
local pg_uri=$1

psql "${pg_uri}" -c "CREATE SCHEMA IF NOT EXISTS \"public\";"
}

redis_clear() {
local redis_uri=$1
redis-cli -u "${redis_uri}" flushall
}

main_native() {
local services=${SERVICES}
local postgres_url_env_var=`[[ $services == *"db_test"* ]] && echo "DB_TEST_POSTGRES_URL" || echo "DB_POSTGRES_URL"`
local redis_host_env_var=`[[ $services == *"redis_test"* ]] && echo "REDIS_TEST_HOST" || echo "REDIS_HOST"`

postgres_url="${!postgres_url_env_var}"
redis_host="${!redis_host_env_var}"

if [ -n "${postgres_url}" ]; then
echo "Using ${postgres_url_env_var} (${postgres_url}) to connect to postgres."
pg_init "${postgres_url}" &> /dev/null
else
echo "Postgres connection string missing did you set ${postgres_url_env_var}?"
exit 1
fi

if [ -n "${redis_host}" ]; then
echo "Using ${redis_host_env_var} (${redis_host}) to connect to Redis."
else
echo "Redis connection string missing did you set ${redis_host_env_var}?"
echo "Continuing without Redis..."
fi

cleanup() {
local services=$@

if [ -n "${redis_host}" ] && [[ $services == *"redis_test"* ]]; then
redis_clear "redis://${redis_host}" &> /dev/null
fi

if [ -n "${postgres_url}" ] && [[ $services == *"db_test"* ]]; then
pg_clear "${postgres_url}" &> /dev/null
fi
}

# trap SIGINT and performs cleanup
trap "on_sigint ${services}" INT
on_sigint() {
cleanup $@
exit $?
}

# Run the arguments as a command
DB_POSTGRES_URL="${postgres_url}" \
REDIS_HOST="${redis_host}" \
"$@"
code=$?

cleanup ${services}

exit ${code}
}

main_docker() {
# Expect a SERVICES env var to be set with the docker service names
local services=${SERVICES}
Expand Down Expand Up @@ -98,8 +171,8 @@ main_docker() {
# Main entry point
main() {
if ! docker ps >/dev/null 2>&1; then
echo "Docker unavailable."
exit 1
echo "Docker unavailable. Running on host."
main_native $@
else
main_docker $@
fi
Expand Down

0 comments on commit 93513ee

Please sign in to comment.