Skip to content

Commit

Permalink
fix docker-compose.yml and add backup.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
fono09 committed Sep 25, 2023
1 parent 31d0da4 commit b046a4f
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
/redis
/elasticsearch

# Ignore posgres backup
/postgres_backups

# Ignore Apple files
.DS_Store

Expand Down
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "BlueGreenCompose"]
path = BlueGreenCompose
url = git@github.com:fono09/BlueGreenCompose.git
1 change: 0 additions & 1 deletion BlueGreenCompose
Submodule BlueGreenCompose deleted from fc730e
33 changes: 33 additions & 0 deletions backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash
set -euxo pipefail

cd `dirname $0`
pwd

BACKUP_DIR=postgres_backups
BACKUP_LIMIT=5
BACKUP_COUNT=`ls $BACKUP_DIR | wc -l`
SERVICE_NAME=mastodon_db_backup

get_service_state(){
echo `docker service ps --format '{{ .DesiredState }}' $1`
}

if [[ `get_service_state $SERVICE_NAME` != "Running" ]]; then
docker service scale mastodon_db_backup=1
fi

while [[ `get_service_state $SERVICE_NAME` == "Running" ]]; do
sleep 10
done

docker service scale mastodon_db_backup=0

if [[ $BACKUP_COUNT -lt $BACKUP_LIMIT ]]; then
exit 0
fi

DELETE_COUNT=$((BACKUP_COUNT - BACKUP_LIMIT))

cd $BACKUP_DIR
ls | sort -n | head -$DELETE_COUNT | xargs rm -r
127 changes: 92 additions & 35 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: '3'
services:
db:
db: &db
restart: always
image: postgres:14-alpine
shm_size: 256mb
Expand All @@ -18,6 +18,20 @@ services:
placement:
constraints: [node.hostname == okaya-srv-0]

db_backup:
<<: *db
command: sh -c "pg_basebackup -F t -D /backups/`date +%Y%m%d` -z -h db -U postgres -P"
healthcheck:
test: ""
volumes:
- ./postgres_backups:/backups
deploy:
placement:
constraints: [node.hostname == okaya-srv-0]
replicas: 0
restart_policy:
condition: none

redis:
restart: always
image: redis:7-alpine
Expand All @@ -31,36 +45,37 @@ services:
placement:
constraints: [node.hostname == okaya-srv-0]

# es:
# restart: always
# image: docker.elastic.co/elasticsearch/elasticsearch:7.17.4
# environment:
# - "ES_JAVA_OPTS=-Xms512m -Xmx512m -Des.enforce.bootstrap.checks=true"
# - "xpack.license.self_generated.type=basic"
# - "xpack.security.enabled=false"
# - "xpack.watcher.enabled=false"
# - "xpack.graph.enabled=false"
# - "xpack.ml.enabled=false"
# - "bootstrap.memory_lock=true"
# - "cluster.name=es-mastodon"
# - "discovery.type=single-node"
# - "thread_pool.write.queue_size=1000"
# networks:
# - external_network
# - internal_network
# healthcheck:
# test: ["CMD-SHELL", "curl --silent --fail localhost:9200/_cluster/health || exit 1"]
# volumes:
# - ./elasticsearch:/usr/share/elasticsearch/data
# ulimits:
# memlock:
# soft: -1
# hard: -1
# nofile:
# soft: 65536
# hard: 65536
# ports:
# - '127.0.0.1:9200:9200'
es:
restart: always
image: docker.elastic.co/elasticsearch/elasticsearch:7.17.4
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m -Des.enforce.bootstrap.checks=true"
- "xpack.license.self_generated.type=basic"
- "xpack.security.enabled=false"
- "xpack.watcher.enabled=false"
- "xpack.graph.enabled=false"
- "xpack.ml.enabled=false"
- "bootstrap.memory_lock=true"
- "cluster.name=es-mastodon"
- "discovery.type=single-node"
- "thread_pool.write.queue_size=1000"
networks:
- external_network
- internal_network
healthcheck:
test: ["CMD-SHELL", "curl --silent --fail localhost:9200/_cluster/health || exit 1"]
volumes:
- ./elasticsearch:/usr/share/elasticsearch/data
ulimits:
memlock:
soft: -1
hard: -1
nofile:
soft: 65536
hard: 65536
deploy:
placement:
constraints: [node.hostname == okaya-srv-0]

web: &web
build: .
Expand All @@ -80,7 +95,7 @@ services:
depends_on:
- db
- redis
# - es
- es
volumes:
- public-system:/mastodon/public/system
environment:
Expand All @@ -90,6 +105,48 @@ services:
mode: replicated
replicas: 3
endpoint_mode: vip

before_migration:
<<: *web
command: bash -c "bundle exec rails db:migrate"
healthcheck:
test: ""
ports: []
environment:
- SKIP_POST_DEPLOYMENT_MIGRATIONS=true
deploy:
placement:
constraints: [node.hostname == okaya-srv-0]
replicas: 0
restart_policy:
condition: none

after_migration:
<<: *web
command: bash -c "bundle exec rails db:migrate"
healthcheck:
test: ""
ports: []
deploy:
placement:
constraints: [node.hostname == okaya-srv-0]
replicas: 0
restart_policy:
condition: none

es_index:
<<: *web
command: bash -c "bin/tootctl search deploy --reset-chewy --concurrency=8"
healthcheck:
test: ""
ports: []
deploy:
placement:
constraints: [node.hostname == okaya-srv-0]
replicas: 0
restart_policy:
condition: none


streaming:
build: .
Expand All @@ -110,9 +167,9 @@ services:
- db
- redis
deploy:
endpoint_mode: dnsrr
placement:
constraints: [node.hostname == okaya-srv-0]
mode: replicated
replicas: 3
endpoint_mode: vip

sidekiq:
build: .
Expand Down

0 comments on commit b046a4f

Please sign in to comment.