Skip to content

Commit

Permalink
Reduce non-verbose output in the scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewL246 committed May 24, 2024
1 parent cb564e3 commit 1307fab
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 64 deletions.
16 changes: 8 additions & 8 deletions scripts/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ fi
mkdir -p "$backup_dir"
print_info "Backing up to $backup_dir"

docker compose up -d mitmproxy-pretendo mongodb postgres minio redis
print_info "Starting necessary services..."
compose_no_progress up -d mitmproxy-pretendo mongodb postgres minio redis

print_info "Backing up MongoDB..."
run_verbose docker compose exec mongodb rm -rf /tmp/backup
# mongodump uses stderr for output
[[ -z "$show_verbose" ]] && mongodump_quiet=true
run_verbose docker compose exec mongodb mongodump -o /tmp/backup "${mongodump_quiet:+--quiet}"
run_verbose_no_errors docker compose cp mongodb:/tmp/backup "$backup_dir/mongodb"
# shellcheck disable=SC2046
run_verbose docker compose exec mongodb mongodump -o /tmp/backup $(if_not_verbose --quiet)
run_verbose compose_no_progress cp mongodb:/tmp/backup "$backup_dir/mongodb"
run_verbose docker compose exec mongodb rm -rf /tmp/backup

print_info "Backing up Postgres..."
Expand All @@ -39,14 +39,14 @@ run_verbose docker compose exec minio mc alias set minio http://minio.pretendo.c
run_verbose docker compose exec minio rm -rf /tmp/backup
run_verbose docker compose exec minio mkdir -p /tmp/backup
run_verbose docker compose exec minio mc mirror minio/ /tmp/backup
run_verbose_no_errors docker compose cp minio:/tmp/backup "$backup_dir/minio"
run_verbose compose_no_progress cp minio:/tmp/backup "$backup_dir/minio"
run_verbose docker compose exec minio rm -rf /tmp/backup

print_info "Backing up Redis..."
run_verbose docker compose exec redis redis-cli save
run_verbose_no_errors docker compose cp redis:/data/dump.rdb "$backup_dir/redis.rdb"
run_verbose compose_no_progress cp redis:/data/dump.rdb "$backup_dir/redis.rdb"

print_info "Backing up Mitmproxy..."
run_verbose_no_errors docker compose cp mitmproxy-pretendo:/home/mitmproxy/.mitmproxy "$backup_dir/mitmproxy"
run_verbose compose_no_progress cp mitmproxy-pretendo:/home/mitmproxy/.mitmproxy "$backup_dir/mitmproxy"

print_success "Backup completed successfully."
12 changes: 7 additions & 5 deletions scripts/compile-custom-inkay.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,30 @@ fi
cd "$git_base_dir/repos/Inkay"

if [[ -z "$should_reset" ]]; then
docker compose up -d mitmproxy-pretendo
print_info "Retrieving the mitmproxy CA certificate..."
compose_no_progress up -d mitmproxy-pretendo
run_command_until_success "Waiting for mitmproxy to generate a certificate..." 5 \
docker compose exec mitmproxy-pretendo ls /home/mitmproxy/.mitmproxy/mitmproxy-ca-cert.pem

# Get the current certificate
run_verbose_no_errors docker compose cp mitmproxy-pretendo:/home/mitmproxy/.mitmproxy/mitmproxy-ca-cert.pem ./data/ca.pem
compose_no_progress cp mitmproxy-pretendo:/home/mitmproxy/.mitmproxy/mitmproxy-ca-cert.pem ./data/ca.pem
else
print_info "Resetting the Inkay CA certificate..."
git restore ./data/ca.pem
print_info "Reset Inkay CA certificate."
fi

print_info "Compiling the Inkay patches..."
rm -f ./*.elf ./*.wps

# Set up the Inkay build environment and then build the patches
docker build . -t inkay-build
docker run -it --rm -v .:/app -w /app inkay-build
print_success "Inkay patches built successfully at $(pwd)/Inkay-pretendo.wps"

# Upload the new Inkay patches to the Wii U
if [[ -n "${WIIU_IP:-}" ]]; then
print_info "Uploading the new Inkay patches to your Wii U..."
tnftp -u "ftp://user:pass@$WIIU_IP/fs/vol/external01/wiiu/environments/aroma/plugins/Inkay-pretendo.wps" ./Inkay-pretendo.wps
print_info "Reboot your Wii U now to apply the new patches."
print_success "Successfully uploaded the new Inkay patches. Reboot your Wii U now to apply them."
else
print_warning "The modified patches were not uploaded to your Wii U because you did not set an IP address."
fi
12 changes: 8 additions & 4 deletions scripts/create-juxt-community.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ add_option_with_value "-i --icon-path" "icon_path" "image-path" "Path to an icon
add_option_with_value "-b --banner-path" "banner_path" "image-path" "Path to a banner image for the community" false
parse_arguments "$@"

print_info "Creating new community $name..."

create_community_script=$(cat "$git_base_dir/scripts/run-in-container/create-juxt-community.js")

# Clean up title IDs by removing non-alphanumeric characters, but keep commas
title_ids=$(echo "$title_ids" | tr -dc "a-zA-Z0-9,")

docker compose up -d juxtaposition-ui
compose_no_progress up -d juxtaposition-ui

if [[ -n "$icon_path" ]]; then
run_verbose_no_errors docker compose cp "$icon_path" juxtaposition-ui:/tmp/icon
compose_no_progress cp "$icon_path" juxtaposition-ui:/tmp/icon
fi
if [[ -n "$banner_path" ]]; then
run_verbose_no_errors docker compose cp "$banner_path" juxtaposition-ui:/tmp/banner
compose_no_progress cp "$banner_path" juxtaposition-ui:/tmp/banner
fi

docker compose exec juxtaposition-ui node -e "$create_community_script" "$name" "$description" "$title_ids" "${icon_path:+/tmp/icon}" "${banner_path:+/tmp/banner}"
run_verbose docker compose exec juxtaposition-ui node -e "$create_community_script" "$name" "$description" "$title_ids" "${icon_path:+/tmp/icon}" "${banner_path:+/tmp/banner}"

print_success "Successfully created community $name."
10 changes: 6 additions & 4 deletions scripts/internal/firstrun-minio-container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
source "$(dirname "$(realpath "$0")")/framework.sh"
parse_arguments "$@"

load_dotenv minio.env minio.local.env
print_info "Setting up the MinIO container..."

load_dotenv minio.env minio.local.env
minio_init_script=$(cat "$git_base_dir/scripts/run-in-container/minio-init.sh")

docker compose up -d minio

compose_no_progress up -d minio
run_command_until_success "Waiting for MinIO to be ready..." 5 \
docker compose exec minio mc alias set minio http://minio.pretendo.cc "$MINIO_ROOT_USER" "$MINIO_ROOT_PASSWORD"

docker compose exec minio sh -c "$minio_init_script"
run_verbose docker compose exec minio sh -c "$minio_init_script"

print_success "MinIO container is set up."
8 changes: 6 additions & 2 deletions scripts/internal/firstrun-mongodb-container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
source "$(dirname "$(realpath "$0")")/framework.sh"
parse_arguments "$@"

print_info "Setting up MongoDB container..."

mongodb_init_script=$(cat "$git_base_dir/scripts/run-in-container/mongodb-init.js")

docker compose up -d mongodb
compose_no_progress up -d mongodb

# This won't work in /docker-entrypoint-initdb.d/ because MongoDB is in a special init state where it will refuse to
# resolve anything but localhost. This needs to be run after it initializes.
Expand All @@ -15,4 +17,6 @@ docker compose up -d mongodb
run_command_until_success "Waiting for MongoDB to be ready..." 10 \
docker compose exec mongodb mongosh --eval "db.adminCommand('ping')"

docker compose exec mongodb mongosh --eval "$mongodb_init_script"
run_verbose docker compose exec mongodb mongosh --eval "$mongodb_init_script"

print_success "MongoDB container is set up."
26 changes: 26 additions & 0 deletions scripts/internal/framework.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ run_verbose_no_errors() {
fi
}

# Run a Docker Compose command without showing progress unless the verbose option is set
compose_no_progress() {
# shellcheck disable=SC2046
docker compose $(if_not_verbose --progress quiet) "$@"
}

# Run a command every 2 seconds silently until it succeeds, or show output if the max number of retries is reached.
#
# Usage: run_command_until_success wait_text max_attempts command...
Expand Down Expand Up @@ -147,6 +153,26 @@ load_dotenv() {
done
}

# Output the provided text if the verbose option is set.
#
# Usage: if_verbose text...
# Example: command $(if_verbose --verbose)
if_verbose() {
if [[ -n "$show_verbose" ]]; then
echo "$*"
fi
}

# Output the provided text unless the verbose option is set.
#
# Usage: if_not_verbose text...
# Example: command $(if_not_verbose --quiet)
if_not_verbose() {
if [[ -z "$show_verbose" ]]; then
echo "$*"
fi
}

# 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=()
Expand Down
10 changes: 6 additions & 4 deletions scripts/internal/update-account-servers-database.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
source "$(dirname "$(realpath "$0")")/framework.sh"
parse_arguments "$@"

create_server_script=$(cat "$git_base_dir/scripts/run-in-container/update-account-servers-database.js")
print_info "Setting up Pretendo account servers database..."

load_dotenv .env

dotenv_files=("friends" "miiverse-api" "wiiu-chat" "super-mario-maker")
for file in "${dotenv_files[@]}"; do
load_dotenv "$file.env" "$file.local.env"
done
create_server_script=$(cat "$git_base_dir/scripts/run-in-container/update-account-servers-database.js")

docker compose up -d account
compose_no_progress up -d account

docker compose exec -e SERVER_IP="$SERVER_IP" \
run_verbose docker compose exec -e SERVER_IP="$SERVER_IP" \
-e FRIENDS_PORT="$PN_FRIENDS_AUTHENTICATION_SERVER_PORT" \
-e FRIENDS_AES_KEY="$PN_FRIENDS_CONFIG_AES_KEY" \
-e MIIVERSE_AES_KEY="$PN_MIIVERSE_API_CONFIG_AES_KEY" \
-e WIIU_CHAT_PORT="$PN_WIIU_CHAT_AUTHENTICATION_SERVER_PORT" \
-e SMM_PORT="$PN_SMM_AUTHENTICATION_SERVER_PORT" \
account node -e "$create_server_script"

print_success "Account servers database is set up."
8 changes: 6 additions & 2 deletions scripts/internal/update-miiverse-endpoints.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
source "$(dirname "$(realpath "$0")")/framework.sh"
parse_arguments "$@"

print_info "Setting up Pretendo Miiverse endpoints database..."

create_endpoint_script=$(cat "$git_base_dir/scripts/run-in-container/update-miiverse-endpoints.js")

docker compose up -d miiverse-api
compose_no_progress up -d miiverse-api

run_verbose docker compose exec miiverse-api node -e "$create_endpoint_script"

docker compose exec miiverse-api node -e "$create_endpoint_script"
print_success "Miiverse endpoints database is set up."
6 changes: 5 additions & 1 deletion scripts/internal/update-postgres-password.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
source "$(dirname "$(realpath "$0")")/framework.sh"
parse_arguments "$@"

print_info "Updating Postgres password..."

load_dotenv postgres.env postgres.local.env

docker compose up -d postgres
compose_no_progress up -d postgres

run_command_until_success "Waiting for Postgres to be ready..." 5 \
docker compose exec postgres psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -l
Expand All @@ -15,3 +17,5 @@ run_command_until_success "Waiting for Postgres to be ready..." 5 \
# scripts
run_command_until_success "Failed to change Postgres password, retrying..." 5 \
docker compose exec postgres psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -c "ALTER USER $POSTGRES_USER PASSWORD '$POSTGRES_PASSWORD';"

print_success "Postgres password updated."
6 changes: 5 additions & 1 deletion scripts/make-pnid-dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ should have administrative permissions."
add_positional_argument "dev-pnid" "dev_pnid" "The PNID to give developer access" true
parse_arguments "$@"

print_info "Giving $dev_pnid developer access..."

update_pnid_access_level_script=$(cat "$git_base_dir/scripts/run-in-container/make-pnid-dev.js")

docker compose exec account node -e "$update_pnid_access_level_script" "$dev_pnid"
run_verbose docker compose exec account node -e "$update_pnid_access_level_script" "$dev_pnid"

print_success "Successfully set $dev_pnid's access level to developer."
22 changes: 11 additions & 11 deletions scripts/restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ if [[ -z "$force" ]]; then
fi

print_info "Stopping unnecessary services..."
docker compose down
docker compose up -d mitmproxy-pretendo mongodb postgres minio redis
compose_no_progress down
print_info "Starting necessary services..."
compose_no_progress up -d mitmproxy-pretendo mongodb postgres minio redis

print_info "Restoring MongoDB..."
run_verbose docker compose exec mongodb rm -rf /tmp/backup
run_verbose_no_errors docker compose cp "$backup_dir/mongodb" mongodb:/tmp/backup
# mongodump uses stderr for output
[[ -z "$show_verbose" ]] && mongodump_quiet=true
run_verbose docker compose exec mongodb mongorestore /tmp/backup --drop "${mongodump_quiet:+--quiet}"
run_verbose compose_no_progress cp "$backup_dir/mongodb" mongodb:/tmp/backup
# shellcheck disable=SC2046
run_verbose docker compose exec mongodb mongorestore /tmp/backup --drop $(if_not_verbose --quiet)
run_verbose docker compose exec mongodb rm -rf /tmp/backup

print_info "Restoring Postgres..."
Expand All @@ -47,19 +47,19 @@ run_verbose_no_errors docker compose exec -T postgres psql -U "$POSTGRES_USER" -
print_info "Restoring MinIO..."
run_verbose docker compose exec minio mc alias set minio http://minio.pretendo.cc "$MINIO_ROOT_USER" "$MINIO_ROOT_PASSWORD"
run_verbose docker compose exec minio rm -rf /tmp/backup
run_verbose_no_errors docker compose cp "$backup_dir/minio" minio:/tmp/backup
run_verbose compose_no_progress cp "$backup_dir/minio" minio:/tmp/backup
run_verbose docker compose exec minio mc mirror /tmp/backup minio/ --overwrite --remove
run_verbose docker compose exec minio rm -rf /tmp/backup

print_info "Restoring Redis..."
# Redis cannot be running when restoring a dump or it will overwrite the restored dump when it exits
run_verbose_no_errors docker compose stop redis
run_verbose_no_errors docker compose cp "$backup_dir/redis.rdb" redis:/data/dump.rdb
run_verbose compose_no_progress stop redis
run_verbose compose_no_progress cp "$backup_dir/redis.rdb" redis:/data/dump.rdb

print_info "Restoring Mitmproxy..."
# Mitmproxy cannot be running when restoring a backup or it will continue using its new certificate
run_verbose_no_errors docker compose stop mitmproxy-pretendo
run_verbose_no_errors docker compose cp "$backup_dir/mitmproxy" mitmproxy-pretendo:/home/mitmproxy/.mitmproxy
run_verbose compose_no_progress stop mitmproxy-pretendo
run_verbose compose_no_progress cp "$backup_dir/mitmproxy" mitmproxy-pretendo:/home/mitmproxy/.mitmproxy

# The restored backup might be using different secrets than what are currently in the .env files
"$git_base_dir/scripts/setup-environment.sh" --force
Expand Down
16 changes: 8 additions & 8 deletions scripts/setup-environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ generate_hex() {

cd "$git_base_dir/environment"

print_info "Setting up local environment variables..."

if [[ -z "$no_environment" ]]; then
if [[ -f "$git_base_dir/.env" ]]; then
source $git_base_dir/.env
Expand All @@ -52,11 +50,14 @@ if ls ./*.local.env >/dev/null 2>&1; then
fi
fi

docker compose down
print_info "Stopping containers and removing existing local environment files..."
compose_no_progress down
rm ./*.local.env
rm "$git_base_dir/.env"
fi

print_info "Setting up local environment variables..."

# Generate an AES-256-CBC key for account server tokens
account_aes_key=$(generate_hex 64)
echo "PN_ACT_CONFIG_AES_KEY=$account_aes_key" >>./account.local.env
Expand Down Expand Up @@ -123,23 +124,23 @@ boss_api_key=$(generate_password 32)
echo "PN_BOSS_CONFIG_GRPC_BOSS_SERVER_API_KEY=$boss_api_key" >>./boss.local.env

# Set up the server IP address
print_info "Using server IP address $server_ip."
if_verbose print_info "Using server IP address $server_ip."
echo "SERVER_IP=$server_ip" >>"$git_base_dir/.env"
echo "PN_FRIENDS_SECURE_SERVER_HOST=$server_ip" >>./friends.local.env
echo "PN_WIIU_CHAT_SECURE_SERVER_LOCATION=$server_ip" >>./wiiu-chat.local.env
echo "PN_SMM_SECURE_SERVER_HOST=$server_ip" >>./super-mario-maker.local.env

# Get the Wii U IP address
if [[ -n "$wiiu_ip" ]]; then
print_info "Using Wii U IP address $wiiu_ip."
if_verbose print_info "Using Wii U IP address $wiiu_ip."
echo "WIIU_IP=$wiiu_ip" >>"$git_base_dir/.env"
else
print_info "Skipping Wii U IP address."
fi

# Get the 3DS IP address
if [[ -n "$ds_ip" ]]; then
print_info "Using 3DS IP address $ds_ip."
if_verbose print_info "Using 3DS IP address $ds_ip."
echo "DS_IP=$ds_ip" >>"$git_base_dir/.env"
else
print_info "Skipping 3DS IP address."
Expand All @@ -162,7 +163,7 @@ Wii U IP address: ${wiiu_ip:-(not set)}
3DS IP address: ${ds_ip:-(not set)}
EOF

print_success "Successfully set up environment."
print_success "Successfully set up the environment."

# Some things need to be updated with the new environment variables and secrets,
# but only if the setup script isn't in progress. The MongoDB container replica
Expand All @@ -171,6 +172,5 @@ if [[ -z "${PRETENDO_SETUP_IN_PROGRESS:-}" ]]; then
print_info "Running necessary container update scripts..."
"$git_base_dir/scripts/internal/update-postgres-password.sh"
"$git_base_dir/scripts/internal/update-account-servers-database.sh"
docker compose down
print_success "Successfully updated the containers with new environment variables."
fi
2 changes: 1 addition & 1 deletion scripts/setup-submodule-patches.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ for dir in "$git_base_dir/patches/"*; do
error_count=$((error_count + 1))
fi
else
run_verbose echo "Applying patch $patch"
if_verbose "Applying patch $patch"
git apply "$patch"
fi
patch_count=$((patch_count + 1))
Expand Down
Loading

0 comments on commit 1307fab

Please sign in to comment.