Improve handling of repeated commands

This prevents infinite loops during setup.
This commit is contained in:
Matthew Lopez 2024-05-11 17:32:38 -04:00
parent 7275d3ba26
commit bedec9d604
No known key found for this signature in database
GPG Key ID: 302A6EE3D63B7E0E
7 changed files with 37 additions and 26 deletions

View File

@ -21,10 +21,8 @@ cd "$git_base_dir/repos/Inkay"
if [[ -z "$should_reset" ]]; then
docker compose up -d mitmproxy-pretendo
while ! docker compose exec mitmproxy-pretendo ls /home/mitmproxy/.mitmproxy/mitmproxy-ca-cert.pem >/dev/null 2>&1; do
print_info "Waiting for mitmproxy to generate a certificate..."
sleep 1
done
run_command_until_success "docker compose exec mitmproxy-pretendo ls /home/mitmproxy/.mitmproxy/mitmproxy-ca-cert.pem" \
"Waiting for mitmproxy to generate a certificate..." 4
# Get the current certificate
docker compose cp mitmproxy-pretendo:/home/mitmproxy/.mitmproxy/mitmproxy-ca-cert.pem ./data/ca.pem

View File

@ -8,4 +8,8 @@ minio_init_script=$(cat "$git_base_dir/scripts/run-in-container/minio-init.sh")
docker compose up -d minio
# sh -c is needed to expand environment variables inside the container
run_command_until_success "docker compose exec minio sh -c 'mc alias set minio http://minio.pretendo.cc \"\$MINIO_ROOT_USER\" \"\$MINIO_ROOT_PASSWORD\"'" \
"Waiting for MinIO to be ready..." 2
docker compose exec minio sh -c "$minio_init_script"

View File

@ -13,9 +13,7 @@ docker compose up -d mongodb
# This needs to be run after it initializes.
# https://github.com/docker-library/mongo/issues/339
while ! docker compose exec mongodb mongosh --eval "db.adminCommand('ping')" >/dev/null 2>&1; do
print_info "Waiting for mongodb to be ready..."
sleep 2
done
run_command_until_success "docker compose exec mongodb mongosh --eval 'db.adminCommand(\"ping\")'" \
"Waiting for MongoDB to be ready..."
docker compose exec mongodb mongosh --eval "$mongodb_init_script"

View File

@ -82,6 +82,28 @@ print_success() {
echo "${term_bold}${term_green}${*}${term_reset}"
}
# Run a command every second silently until it succeeds, or show output if the max number of retries is reached.
#
# Usage: run_command_until_success command wait_text [max_attempts]
# Example: run_command_until_success "docker compose exec mongodb mongosh --eval 'db.adminCommand(\"ping\")'" "Waiting
# for MongoDB to be ready..." 10
run_command_until_success() {
local command="${1:?${FUNCNAME[0]}: Command is required}"
local wait_text="${2:?${FUNCNAME[0]}: Wait text is required}"
local max_attempts="${3:-10}"
local count=0
while ! eval $command >/dev/null 2>&1; do
count=$((count + 1))
if [ $count -ge $max_attempts ]; then
print_error "Max attempts reached. Showing error info..."
eval $command
fi
print_info "$wait_text"
sleep 2
done
}
# Argument parsing framework
# Uses a lot of Bash parameter expansion tricks: https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion
argument_variables=()
@ -129,7 +151,7 @@ add_option() {
# required, the script will exit with an error if the option is not provided. If a default value is set, the variable
# will be set to the default value if the option is provided without a value.
#
# Usage: add_option_with_value options variable_name value_name description required default_value
# Usage: add_option_with_value options variable_name value_name description required [default_value]
# Example: add_option_with_value "-o --option" "option_value" "option-value" "Sets the option value" false "default value"
add_option_with_value() {
local options="${1:?${FUNCNAME[0]}: Option arguments are required}"

View File

@ -13,14 +13,10 @@ source "$git_base_dir/environment/postgres.local.env"
docker compose up -d postgres
while ! docker compose exec postgres psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -c "\l" >/dev/null 2>&1; do
print_info "Waiting for PostgreSQL to start..."
sleep 2
done
run_command_until_success "docker compose exec postgres psql -v ON_ERROR_STOP=1 -U '$POSTGRES_USER' -c '\l'" \
"Waiting for Postgres to be ready..."
# During the first run, this sometimes fails because the entrypoint script
# restarts the server after running the initdb scripts
while ! docker compose exec postgres psql -U "$POSTGRES_USER" -c "ALTER USER $POSTGRES_USER PASSWORD '$POSTGRES_PASSWORD';"; do
print_warning "Failed to change Postgres password, retrying..."
sleep 2
done
run_command_until_success "docker compose exec postgres psql -U '$POSTGRES_USER' -c \"ALTER USER $POSTGRES_USER PASSWORD '$POSTGRES_PASSWORD';\"" \
"Failed to change Postgres password, retrying..." 5

View File

@ -2,11 +2,6 @@
set -eu
while ! mc alias set minio http://minio.pretendo.cc "$MINIO_ROOT_USER" "$MINIO_ROOT_PASSWORD" >/dev/null 2>&1; do
echo "Waiting for MinIO to start..."
sleep 1
done
buckets="pn-cdn pn-boss super-mario-maker"
# Create buckets and allow public access

View File

@ -21,10 +21,8 @@ cd "$git_base_dir/console-files"
if [[ -z "$should_reset" ]]; then
docker compose up -d mitmproxy-pretendo
while ! docker compose exec mitmproxy-pretendo ls /home/mitmproxy/.mitmproxy/mitmproxy-ca-cert.pem >/dev/null 2>&1; do
print_info "Waiting for mitmproxy to generate a certificate..."
sleep 1
done
run_command_until_success "docker compose exec mitmproxy-pretendo ls /home/mitmproxy/.mitmproxy/mitmproxy-ca-cert.pem" \
"Waiting for mitmproxy to generate a certificate..." 4
# Get the current certificate
docker compose cp mitmproxy-pretendo:/home/mitmproxy/.mitmproxy/mitmproxy-ca-cert.pem ./mitmproxy-ca-cert.pem