From 23724c589ce1907bf54f942f842209c0a9d78313 Mon Sep 17 00:00:00 2001 From: Tianyu Liu Date: Fri, 2 May 2025 20:38:15 +0200 Subject: [PATCH] Affine deploy script based on official compose file --- affine/compose.yml | 74 ++++++++++++++++++++++++++++++++ affine/deploy.sh | 105 +++++++++++++++++++++++++++++++++++++++++++++ affine/env.sh | 36 ++++++++++++++++ 3 files changed, 215 insertions(+) create mode 100644 affine/compose.yml create mode 100755 affine/deploy.sh create mode 100644 affine/env.sh diff --git a/affine/compose.yml b/affine/compose.yml new file mode 100644 index 0000000..78bdb45 --- /dev/null +++ b/affine/compose.yml @@ -0,0 +1,74 @@ +name: affine +services: + affine: + image: ghcr.io/toeverything/affine-graphql:${AFFINE_REVISION:-stable} + container_name: affine_server + ports: + - '${PORT:-3010}:3010' + depends_on: + redis: + condition: service_healthy + postgres: + condition: service_healthy + affine_migration: + condition: service_completed_successfully + volumes: + # custom configurations + - ${UPLOAD_LOCATION}:/root/.affine/storage + - ${CONFIG_LOCATION}:/root/.affine/config + env_file: + - .env + environment: + - REDIS_SERVER_HOST=redis + - DATABASE_URL=postgresql://${DB_USERNAME}:${DB_PASSWORD}@postgres:5432/${DB_DATABASE:-affine} + restart: unless-stopped + + affine_migration: + image: ghcr.io/toeverything/affine-graphql:${AFFINE_REVISION:-stable} + container_name: affine_migration_job + volumes: + # custom configurations + - ${UPLOAD_LOCATION}:/root/.affine/storage + - ${CONFIG_LOCATION}:/root/.affine/config + command: ['sh', '-c', 'node ./scripts/self-host-predeploy.js'] + env_file: + - .env + environment: + - REDIS_SERVER_HOST=redis + - DATABASE_URL=postgresql://${DB_USERNAME}:${DB_PASSWORD}@postgres:5432/${DB_DATABASE:-affine} + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + + redis: + image: redis + container_name: affine_redis + healthcheck: + test: ['CMD', 'redis-cli', '--raw', 'incr', 'ping'] + interval: 10s + timeout: 5s + retries: 5 + restart: unless-stopped + + postgres: + image: postgres:16 + container_name: affine_postgres + volumes: + - ${DB_DATA_LOCATION}:/var/lib/postgresql/data + environment: + POSTGRES_USER: ${DB_USERNAME} + POSTGRES_PASSWORD: ${DB_PASSWORD} + POSTGRES_DB: ${DB_DATABASE:-affine} + POSTGRES_INITDB_ARGS: '--data-checksums' + # you better set a password for you database + # or you may add 'POSTGRES_HOST_AUTH_METHOD=trust' to ignore postgres security policy + POSTGRES_HOST_AUTH_METHOD: trust + healthcheck: + test: + ['CMD', 'pg_isready', '-U', "${DB_USERNAME}", '-d', "${DB_DATABASE:-affine}"] + interval: 10s + timeout: 5s + retries: 5 + restart: unless-stopped diff --git a/affine/deploy.sh b/affine/deploy.sh new file mode 100755 index 0000000..fc66b5a --- /dev/null +++ b/affine/deploy.sh @@ -0,0 +1,105 @@ +#!/bin/bash +set -e + +. ./env.sh + +if systemctl --user list-units --full --all | grep -q "$CONTAINER_PREFIX-$CONTAINER_SERVER.service"; then + systemctl --user stop $CONTAINER_PREFIX-$CONTAINER_SERVER.service +fi + +if systemctl --user list-units --full --all | grep -q "$CONTAINER_PREFIX-$CONTAINER_POSTGRES.service"; then + systemctl --user stop $CONTAINER_PREFIX-$CONTAINER_POSTGRES.service +fi + +if systemctl --user list-units --full --all | grep -q "$CONTAINER_PREFIX-$CONTAINER_REDIS.service"; then + systemctl --user stop $CONTAINER_PREFIX-$CONTAINER_REDIS.service +fi + +if ! podman network exists $NETWORK; then + podman network create $NETWORK +fi + +mkdir -p ./systemd-units +podman create \ + --name $CONTAINER_REDIS \ + --network $NETWORK \ + -p $REDIS_SERVER_PORT:$REDIS_SERVER_PORT \ + docker.io/library/redis +podman generate systemd \ + --new \ + --name $CONTAINER_REDIS \ + --files --restart-policy always --container-prefix=affine > /dev/null +mv $CONTAINER_PREFIX-$CONTAINER_REDIS.service ./systemd-units/ + + +podman create \ + --name $CONTAINER_POSTGRES \ + --network $NETWORK \ + -p $DATABASE_PORT:$DATABASE_PORT \ + -e POSTGRES_USER=$DB_USERNAME \ + -e POSTGRES_PASSWORD=$DB_PASSWORD \ + -e POSTGRES_DB=$DB_DATABASE \ + -e POSTGRES_HOST_AUTH_METHOD=trust \ + -v "$DB_DATA_LOCATION:/var/lib/postgresql/data:Z" \ + docker.io/library/postgres:16 + +podman generate systemd \ + --new \ + --name $CONTAINER_POSTGRES \ + --files --restart-policy always --container-prefix=affine > /dev/null +mv $CONTAINER_PREFIX-$CONTAINER_POSTGRES.service ./systemd-units/ + +USER_SYSTEMD="$HOME/.config/systemd/user" +mkdir -p $USER_SYSTEMD +cp ./systemd-units/*.service $USER_SYSTEMD +systemctl --user daemon-reexec +systemctl --user daemon-reload +systemctl --user enable $CONTAINER_PREFIX-$CONTAINER_REDIS.service +systemctl --user enable $CONTAINER_PREFIX-$CONTAINER_POSTGRES.service +systemctl --user start $CONTAINER_PREFIX-$CONTAINER_REDIS.service +systemctl --user start $CONTAINER_PREFIX-$CONTAINER_POSTGRES.service + + +echo "Wait for PostgreSQL..." +until podman exec affine_postgres pg_isready -U "$DB_USERNAME" -d "$DB_DATABASE" > /dev/null 2>&1; do + sleep 2 +done +echo "PostgreSQL ready" + + +podman run --rm \ + --name affine_migration_job \ + --network $NETWORK \ + -e REDIS_SERVER_HOST=$REDIS_SERVER_HOST \ + -e DATABASE_URL="postgresql://$DB_USERNAME:$DB_PASSWORD@$DATABASE_HOST:$DATABASE_PORT/$DB_DATABASE" \ + -v "$UPLOAD_LOCATION:/root/.affine/storage:Z" \ + -v "$CONFIG_LOCATION:/root/.affine/config:Z" \ + ghcr.io/toeverything/affine-graphql:$AFFINE_REVISION \ + sh -c 'node ./scripts/self-host-predeploy.js' + + +podman create \ + --name $CONTAINER_SERVER \ + --network $NETWORK \ + -p $AFFINE_PORT:3010 \ + -e REDIS_SERVER_HOST=$REDIS_SERVER_HOST \ + -e DATABASE_URL="postgresql://$DB_USERNAME:$DB_PASSWORD@$DATABASE_HOST:$DATABASE_PORT/$DB_DATABASE" \ + -v "$UPLOAD_LOCATION:/root/.affine/storage:Z" \ + -v "$CONFIG_LOCATION:/root/.affine/config:Z" \ + ghcr.io/toeverything/affine-graphql:$AFFINE_REVISION + +podman generate systemd \ + --new \ + --name $CONTAINER_SERVER \ + --files --restart-policy always --container-prefix=affine > /dev/null +mv $CONTAINER_PREFIX-$CONTAINER_SERVER.service ./systemd-units/ +sed -i "/^\[Unit\]/a After=$CONTAINER_PREFIX-$CONTAINER_POSTGRES.service $CONTAINER_PREFIX-$CONTAINER_REDIS.service\nRequires=$CONTAINER_PREFIX-$CONTAINER_POSTGRES.service $CONTAINER_PREFIX-$CONTAINER_REDIS.service" ./systemd-units/$CONTAINER_PREFIX-$CONTAINER_SERVER.service + +cp ./systemd-units/$CONTAINER_PREFIX-$CONTAINER_SERVER.service $USER_SYSTEMD +systemctl --user daemon-reload +systemctl --user enable $CONTAINER_PREFIX-$CONTAINER_SERVER.service +systemctl --user start $CONTAINER_PREFIX-$CONTAINER_SERVER.service + +rm -r ./systemd-units + +sudo loginctl enable-linger $USER \ No newline at end of file diff --git a/affine/env.sh b/affine/env.sh new file mode 100644 index 0000000..15aee6e --- /dev/null +++ b/affine/env.sh @@ -0,0 +1,36 @@ +# select a revision to deploy, available values: stable, beta, canary +AFFINE_REVISION=stable + +NETWORK="affine_net" + +# set the port for the server container it will expose the server on +AFFINE_PORT=3010 + +# set the host for the server for outgoing links +# AFFINE_SERVER_HTTPS=true +# AFFINE_SERVER_HOST=affine.yourdomain.com +# or +# AFFINE_SERVER_EXTERNAL_URL=https://affine.yourdomain.com + +# position of the database data to persist +DB_DATA_LOCATION=$HOME/.affine/self-host/postgres/pgdata +# position of the upload data(images, files, etc.) to persist +UPLOAD_LOCATION=$HOME/.affine/self-host/storage +# position of the configuration files to persist +CONFIG_LOCATION=$HOME/.affine/self-host/config + +REDIS_SERVER_HOST="host.containers.internal" +REDIS_SERVER_PORT="6379" + +DATABASE_HOST="host.containers.internal" +DATABASE_PORT="5432" + +# database credentials +DB_USERNAME=affine +DB_PASSWORD= +DB_DATABASE=affine + +CONTAINER_PREFIX="affine" +CONTAINER_REDIS="affine_redis" +CONTAINER_POSTGRES="affine_postgres" +CONTAINER_SERVER="affine_server" \ No newline at end of file