Files
SocialPub/tools/pasture/peers/shared.sh
T

50 lines
3.0 KiB
Bash
Raw Normal View History

# Services several peers share: one Postgres and one MySQL (each peer gets its own database), and one Redis (each its own
# db number). A dozen peers keep their pools on the one Postgres, and Postgres's default 100 connections ran out while
# the town seeded (Sharkey's API answered 500, Misskey's queue dropped deliveries): it takes 400.
2026-10-03 12:30:05 +02:00
shared_postgres_up() {
podman container exists pasture-postgres && return 0
podman run -d --replace --name pasture-postgres --network $net --network-alias postgres \
-e POSTGRES_USER=pasture -e POSTGRES_PASSWORD=pasture -e POSTGRES_DB=pasture docker.io/library/postgres:17-alpine \
-c max_connections=400 >/dev/null
2026-10-03 12:30:05 +02:00
for _ in $(seq 1 60); do podman exec pasture-postgres pg_isready -U pasture >/dev/null 2>&1 && return 0; sleep 1; done
echo "postgres did not start" >&2; return 1
}
shared_redis_up() {
podman container exists pasture-redis && return 0
podman run -d --replace --name pasture-redis --network $net --network-alias redis docker.io/library/redis:7-alpine >/dev/null
}
# pg_db <name> [extension...]: a database of the shared Postgres for one peer, with the extensions it needs
pg_db() {
local name=$1; shift
podman exec pasture-postgres sh -c "psql -U pasture -tc \"select 1 from pg_database where datname='$name'\" | grep -q 1 || createdb -U pasture $name"
for ext in "$@"; do
podman exec pasture-postgres psql -U pasture -d "$name" -qc "create extension if not exists \"$ext\";" >/dev/null
done
}
2026-10-05 10:54:32 +02:00
# a PostGIS of its own for the peers that need the extension (Mobilizon), beside the shared Postgres that has not got it.
# Its first start runs a server on the socket alone to set itself up: only TCP answers when it is ready
shared_postgis_up() {
podman container exists pasture-postgis && return 0
podman run -d --replace --name pasture-postgis --network $net --network-alias postgis --label pasture=1 \
-e POSTGRES_USER=pasture -e POSTGRES_PASSWORD=pasture -e POSTGRES_DB=pasture docker.io/postgis/postgis:17-3.5-alpine >/dev/null
for _ in $(seq 1 90); do podman exec pasture-postgis pg_isready -h 127.0.0.1 -U pasture >/dev/null 2>&1 && return 0; sleep 1; done
echo "postgis did not start" >&2; return 1
}
shared_mysql_up() {
podman container exists pasture-mysql && return 0
podman run -d --replace --name pasture-mysql --network $net --network-alias mysql \
-e MYSQL_ROOT_PASSWORD=pasture -e MYSQL_USER=pasture -e MYSQL_PASSWORD=pasture docker.io/library/mysql:8.4 >/dev/null
# its first start runs a temporary server without networking to set itself up: only TCP answers when it is ready
for _ in $(seq 1 90); do podman exec pasture-mysql mysqladmin ping -h127.0.0.1 --protocol=tcp -uroot -ppasture --silent >/dev/null 2>&1 && return 0; sleep 2; done
echo "mysql did not start" >&2; return 1
}
# mysql_db <name>: a database of the shared MySQL for one peer, the pasture user owning it
mysql_db() {
podman exec pasture-mysql mysql -uroot -ppasture -e \
"create database if not exists \`$1\` character set utf8mb4 collate utf8mb4_unicode_ci; grant all on \`$1\`.* to 'pasture'@'%';" 2>/dev/null
}