Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): improved error handling for dev env setup script #500

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 37 additions & 31 deletions backend/setup-dev-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,10 @@
# backend can only be built and run if the database is also running, due to
# sqlx.

# Create .env file.
env_file=.env
if [ -f "$env_file" ]; then
while true; do
printf "You already have a $env_file file, are you sure you want to continue, as this will override your $env_file file? [yn] "
read -r answer
case "$answer" in
y)
# Continue with execution.
break
;;
n)
echo "Aborting"
exit 0
;;
*)
# Try again.
echo "Invalid answer. Please type either 'y' for yes, or 'n' for no."
continue
;;
esac
done
fi

echo "Overwriting $env_file file"

cat << 'EOF' > "$env_file"
# Write .env file to temporary file.
tmp_env_file="$(mktemp)"
trap 'rm -rf "$tmp_env_file"' EXIT INT TERM
cat << 'EOF' > "$tmp_env_file"
DATABASE_URL="postgres://user:password@localhost:5432/chaos"
JWT_SECRET="test_secret"
GOOGLE_CLIENT_ID="test"
Expand All @@ -44,6 +21,35 @@ S3_ENDPOINT="https://chaos-storage.s3.ap-southeast-1.amazonaws.com"
S3_REGION_NAME="ap-southeast-1"
EOF

# Check the user has all required tools installed.
for cmd in "which cargo" "which docker && docker info" "which docker-compose || docker compose"; do
if ! eval "$cmd" 1>/dev/null 2>&1; then
echo "The command '$cmd' failed, indicating you might not have that tool installed." >&2
exit 1
fi
done

# Create .env file.
env_file=.env

# The .env file already exists.
if [ -f "$env_file" ]; then
# If existing env file differs from new one, save the existing env file to a backup file.
if ! diff "$env_file" "$tmp_env_file" > /dev/null; then
backup_env_file="$env_file"
while [ -f "$backup_env_file" ]; do
# Append `.backup` to backup filename until we find a filename that doesn't exist yet.
backup_env_file="$backup_env_file.backup"
done

echo "Saving existing env file '$env_file' to backup env file '$backup_env_file'"
mv "$env_file" "$backup_env_file"
fi
fi

echo "Overwriting $env_file file"
cp "$tmp_env_file" "$env_file"

# Install sqlx if it isn't installed yet.
if ! which sqlx >/dev/null; then
echo "Installing sqlx"
Expand All @@ -58,18 +64,18 @@ docker_compose_file_name="setup-test-database.yml"
docker_compose_file_path="$this_script_dir/$docker_compose_file_name"

echo 'Starting up postgres database in docker'
docker-compose -f "$docker_compose_file_path" up --detach
docker-compose -f "$docker_compose_file_path" up --detach || exit 1

# Ensure the docker container gets killed once this script exits.
trap 'echo "shutting down $docker_compose_file_path" && docker-compose -f "$docker_compose_file_path" down' EXIT
trap 'echo "shutting down $docker_compose_file_path" && docker-compose -f "$docker_compose_file_path" down' EXIT INT TERM

# Wait for the database to be ready.
echo "Waiting for database to be ready"
sleep 3

# Setup sqlx.
echo "Setting up sqlx"
sqlx database create
sqlx migrate run
sqlx database create || exit 1
sqlx migrate run || exit 1

"$SHELL"
Loading