From b854de4538dda93bb9c43a264fcb228a86c77d62 Mon Sep 17 00:00:00 2001 From: Werner Kroneman Date: Sat, 7 Dec 2024 09:54:41 +0100 Subject: [PATCH] Cleaned up the startup script --- run_with_db.sh | 114 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 83 insertions(+), 31 deletions(-) diff --git a/run_with_db.sh b/run_with_db.sh index 4144e8f485..389afc7cb5 100755 --- a/run_with_db.sh +++ b/run_with_db.sh @@ -1,21 +1,35 @@ # Set up a postgresql database and a redis server for the integration tests. -# Create a unique temporary directory for the unix socket and data: -export PGDATA=$(mktemp -d) +# Set up a PostgreSQL database for the integration tests. +# +# Preconditions: +# - PostgreSQL must be installed on the system. +# - The `initdb`, `pg_ctl`, and `psql` commands must be available in the PATH. +# +# Postconditions: +# - A temporary PostgreSQL data directory will be created and initialized. +# - PostgreSQL will be started using a Unix socket. +# - A PostgreSQL user and database named "sharkey" will be created. +create_tmp_psql() { + export PGDATA=$(mktemp -d) -# Initialize the PostgreSQL data directory -initdb -D $PGDATA -U postgres + # Initialize the PostgreSQL data directory + initdb -D $PGDATA -U postgres -# Start PostgreSQL and Redis using unix sockets -pg_ctl -D $PGDATA -l $PGDATA/logfile start -o "-k $PGDATA --listen-addresses=''" -until pg_isready -h $PGDATA; do sleep 1; done -echo "PostgreSQL started with unix socket at $PGDATA" + # Start PostgreSQL using Unix sockets + pg_ctl -D $PGDATA -l $PGDATA/logfile start -o "-k $PGDATA --listen-addresses=''" + until pg_isready -h $PGDATA; do sleep 1; done + echo "PostgreSQL started with Unix socket at $PGDATA" -# Create a "sharkey" user and database -psql -h $PGDATA -c "CREATE USER sharkey" -U postgres -psql -h $PGDATA -c "CREATE DATABASE sharkey OWNER sharkey" -U postgres + # Create a "sharkey" user and database + psql -h $PGDATA -c "CREATE USER sharkey" -U postgres + psql -h $PGDATA -c "CREATE DATABASE sharkey OWNER sharkey" -U postgres +} -# Grab an empty TCP port: +# Function to find a random unused port +# It generates a random port number between 2000 and 65000 +# and checks if it is in use. If it is in use, it recursively +# calls itself until an unused port is found. function random_unused_port { local port=$(shuf -i 2000-65000 -n 1) netstat -lat | grep $port > /dev/null @@ -26,23 +40,61 @@ function random_unused_port { fi } -export REDIS_PORT=$(random_unused_port) -redis-server --port $REDIS_PORT --loglevel warning & -until redis-cli -p $REDIS_PORT ping; do sleep 1; done -echo "Redis started with unix socket at $REDISSOCK" - -cp .config/example.yml .config/default.yml -yq -i -Y '.db.host = "'$PGDATA'"' .config/default.yml -yq -i -Y '.redis.port = "'$REDIS_PORT'"' .config/default.yml - -#export MISSKEY_CONFIG_YML=.config/default.yml -pnpm run migrateandstart - -# Open Firefox - -# Stop PostgreSQL and Redis -cleanup() { - pg_ctl -D $PGDATA stop - redis-cli -s $REDISSOCK shutdown +# Function to create a Redis server on a random unused port +# It exports the port number to the REDIS_PORT environment variable +# and starts the Redis server with log level set to warning. +# It waits until the Redis server is ready to accept connections. +create_redis_on_random_port() { + export REDIS_PORT=$(random_unused_port) + redis-server --port $REDIS_PORT --loglevel warning & + until redis-cli -p $REDIS_PORT ping; do sleep 1; done + echo "Redis started on port $REDIS_PORT" } -trap cleanup EXIT + +pick_sharkey_port() { + export SHARKEY_PORT=$(random_unused_port) +} + +# Function to copy the example configuration file and update it with the PostgreSQL and Redis settings. +# Arguments: +# $1: The destination file path (optional). Defaults to `.config/default.yml`. +copy_and_update_config() { + local dest_file=.config/default.yml + cp .config/example.yml $dest_file + yq -i -Y ".db.host = \"$PGDATA\"" $dest_file + yq -i -Y ".redis.port = $REDIS_PORT" $dest_file + yq -i -Y ".port = $SHARKEY_PORT" $dest_file +} + +# Function to stop the PostgreSQL server; $PGDATA must be set +stop_psql() { + pg_ctl -D "$PGDATA" stop +} + +# Function to stop the Redis server +stop_redis() { + redis-cli -p "$REDIS_PORT" shutdown +} + +# Function to stop both PostgreSQL and Redis servers +stop_databases() { + stop_psql + stop_redis +} + +# Function to start a fresh instance of the Sharkey application +# It sets up a temporary PostgreSQL database and a Redis server on a random port, +# updates the configuration file, and runs the application. +start_fresh_sharkey() { + create_tmp_psql + create_redis_on_random_port + pick_sharkey_port + trap stop_databases EXIT + + copy_and_update_config + pnpm run migrateandstart +} + +start_fresh_sharkey + +firefox http://localhost:$SHARKEY_PORT --marionette --new-instance