1
0
Fork 0
mirror of https://github.com/anyproto/any-sync-dockercompose.git synced 2025-06-08 05:47:03 +09:00

Merge pull request #109 from anyproto/OPS-467

OPS-467 - added env replacement test, fixes
This commit is contained in:
Kirill Shklyaev 2024-11-20 17:37:05 +03:00 committed by GitHub
commit fe09af1eac
Signed by: github
GPG key ID: B5690EEEBB952194
13 changed files with 265 additions and 10 deletions

1
.gitignore vendored
View file

@ -6,3 +6,4 @@
/.env
/.env.override
/account_stop.json
/backup/

View file

@ -1,33 +1,49 @@
.DEFAULT_GOAL := start
# Check if the 's' flag (silent/quiet mode) is present in MAKEFLAGS
ifeq ($(findstring s,$(MAKEFLAGS)),s)
QUIET_MODE := true
DOCKER_COMPOSE := docker compose --progress=quiet
else
QUIET_MODE := false
DOCKER_COMPOSE := docker compose
endif
# targets
generate_env:
ifeq ($(QUIET_MODE),true)
docker buildx build --quiet --load --tag generateconfig-env --file Dockerfile-generateconfig-env . >/dev/null
else
docker buildx build --load --tag generateconfig-env --file Dockerfile-generateconfig-env .
endif
docker run --rm \
--volume ${CURDIR}/:/code/ \
generateconfig-env
start: generate_env
docker compose up --detach --remove-orphans
$(DOCKER_COMPOSE) up --detach --remove-orphans --quiet-pull
ifeq ($(QUIET_MODE),false)
@echo "Done! Upload your self-hosted network configuration file ${CURDIR}/etc/client.yml into the client app"
@echo "See: https://doc.anytype.io/anytype-docs/data-and-security/self-hosting#switching-between-networks"
endif
stop:
docker compose stop
$(DOCKER_COMPOSE) stop
clean:
docker system prune --all --volumes
pull:
docker compose pull
$(DOCKER_COMPOSE) pull
down:
docker compose down --remove-orphans
$(DOCKER_COMPOSE) down --remove-orphans
logs:
docker compose logs --follow
$(DOCKER_COMPOSE) logs --follow
# build with "plain" log for debug
build:
docker compose build --no-cache --progress plain
$(DOCKER_COMPOSE) build --no-cache --progress plain
restart: down start
update: pull down start

View file

@ -217,6 +217,7 @@ services:
# any-sync-netcheck
netcheck:
image: "ghcr.io/anyproto/any-sync-tools:${ANY_SYNC_TOOLS_VERSION}"
pull_policy: always
restart: unless-stopped
depends_on:
- any-sync-consensusnode
@ -226,12 +227,11 @@ services:
- any-sync-node-2
- any-sync-node-3
volumes:
- ./:/code
- "${STORAGE_DIR}:/code/storage"
command: ["tail", "-f", "/dev/null"]
stop_signal: SIGKILL
tty: true
healthcheck:
test: any-sync-netcheck -c /code/storage/docker-generateconfig/nodes.yml 2>&1| grep -P 'netcheck\s+success'
interval: 60s
test: any-sync-netcheck -c /code/storage/docker-generateconfig/nodes.yml 2>&1| grep -E 'netcheck\s+success'
interval: 10s
start_period: 5s

View file

@ -27,7 +27,7 @@ else:
inputYamlFile = sys.argv[1]
outputYamlFile = sys.argv[2]
externalListenHosts = envVars.get('EXTERNAL_LISTEN_HOSTS', '127.0.0.1').split()
externalListenHosts = envVars.get('EXTERNAL_LISTEN_HOSTS', '').split()
externalListenHost = envVars.get('EXTERNAL_LISTEN_HOST', None)
if externalListenHost and externalListenHost not in externalListenHosts:
externalListenHosts.append(externalListenHost)

174
tests/main.sh Executable file
View file

@ -0,0 +1,174 @@
#!/bin/bash
# Color codes for output
BLUE='\033[1;34m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
DATE=$(date +%Y%m%d-%H%M%S)
TESTS_DIR="$(dirname $0)"
PROJECT_DIR="$TESTS_DIR/.."
BACKUP_DIR="$PROJECT_DIR/backup/$DATE"
DEBUG=false
# set make cmd
if $DEBUG; then
MAKE="make -C $PROJECT_DIR"
else
MAKE="make --quiet -C $PROJECT_DIR"
fi
# show variables
if $DEBUG; then
echo -e "${YELLOW}Variables:${NC}"
cat <<EOF
TESTS_DIR=$TESTS_DIR
PROJECT_DIR=$PROJECT_DIR
BACKUP_DIR=$BACKUP_DIR
EOF
fi
# load variables
source $TESTS_DIR/variables
# record the start time in seconds since the epoch
TEST_START_TIME=$(date +%s)
# Checks network status of Any Sync services and displays result
runNetcheck() {
sleep 3
# wait for netcheck to finish checking
while true; do
STATUS=$( docker inspect --format='{{json .State.Health.Status}}' $(docker compose ps --quiet netcheck) )
if [[ "$STATUS" != "starting" ]]; then
$DEBUG && echo "Container 'netcheck' health status: '$STATUS', continue"
break
else
$DEBUG && echo "Container 'netcheck' health status: '$STATUS', waiting..."
sleep 5
fi
done
if [[ "$STATUS" != "healthy" ]]; then
echo -e "${GREEN} Netcheck - OK [✔] ${NC}"
else
echo -e "${RED} Netcheck - FAILED [✖] ${NC}"
echo -e "${RED} Please investigate the issue. And after that, you can restore the previous storage state by executing the following commands:"
cat <<EOF
$MAKE down && $MAKE cleanEtcStorage
mv $BACKUP_DIR/etc $PROJECT_DIR/
mv $BACKUP_DIR/storage $PROJECT_DIR/
mv $BACKUP_DIR/.env.override $PROJECT_DIR/
rmdir $BACKUP_DIR
EOF
#restoreBackup
exit 1
fi
}
# Verifies if Minio bucket was created successfully
checkBucketCreation() {
if docker compose logs create-bucket | grep -q "Bucket created successfully"; then
echo -e "${GREEN} Minio bucket creation - OK [✔] ${NC}"
else
echo -e "${RED} Minio bucket creation - FAILED [✖] ${NC}"
restoreBackup
exit 1
fi
}
restoreBackup() {
echo -e "${YELLOW}Finish! Backup restore...${NC}"
$MAKE down && $MAKE cleanEtcStorage
mv $BACKUP_DIR/etc $PROJECT_DIR/
mv $BACKUP_DIR/storage $PROJECT_DIR/
mv $BACKUP_DIR/.env.override $PROJECT_DIR/
rmdir $BACKUP_DIR
}
runTest(){
local PARAM_1=$1
if [[ $PARAM_1 == 'cleanData' ]]; then
local CLEAN_DATA=true
else
local CLEAN_DATA=false
fi
if $CLEAN_DATA; then
echo -e "${YELLOW}Testing without user data storage...${NC}"
else
echo -e "${YELLOW}Testing with user data storage...${NC}"
fi
local RUN_TEST_START_TIME=$(date +%s) # record the start time in seconds since the epoch
for TEST in $TESTS_DIR/run.d/*.sh; do
echo -e "${YELLOW}Executing test: $TEST ${NC}"
local TEST_FILE_NAME=$(basename $TEST)
if [[ $TEST_FILE_NAME == 'setAnySyncPort.sh' ]]; then
if ! $CLEAN_DATA; then
echo "skipping for exist storage"
continue
fi
fi
# record the start time in seconds since the epoch
local START_TIME=$(date +%s)
# source the test file
source $TEST
# restart stand
if $CLEAN_DATA; then
$MAKE down && $MAKE cleanEtcStorage && $MAKE start
local STATUS_CODE=$?
else
$MAKE down && $MAKE start
local STATUS_CODE=$?
fi
local END_TIME=$(date +%s) # record the end time in seconds since the epoch
local ELAPSED_TIME=$((END_TIME - START_TIME)) # calculate the elapsed time
echo -e "${BLUE}Test $TEST took $ELAPSED_TIME seconds to complete${NC}" # log the time taken for the test
# check 'make start' status
if [[ $STATUS_CODE -eq 0 ]]; then
# if successful, run checks
runNetcheck
if $CLEAN_DATA; then
checkBucketCreation
fi
echo -e "${GREEN} $TEST - OK [✔] ${NC}"
else
# if failed, log the error, stop services, restore backup, and exit
echo -e "${RED} $TEST - FAILED [✖] ${NC}"
$MAKE down
restoreBackup
exit 1
fi
done
local RUN_TEST_END_TIME=$(date +%s) # record the end time in seconds since the epoch
local RUN_TEST_ELAPSED_TIME=$((RUN_TEST_END_TIME - RUN_TEST_START_TIME)) # calculate the elapsed time
echo -e "${BLUE}runTest took $RUN_TEST_ELAPSED_TIME seconds to complete${NC}" # log the time taken for the test
echo
}
# create data backup
echo -e "${YELLOW}Pre-start user data backup...${NC}"
$MAKE down
install -d $BACKUP_DIR
cp -r $PROJECT_DIR/etc $BACKUP_DIR/
cp -r $PROJECT_DIR/storage $BACKUP_DIR/
cp $PROJECT_DIR/.env.override $BACKUP_DIR/
# run tests
runTest notCleanData
runTest cleanData
# restore backup and exit
restoreBackup
# logging run time
TEST_END_TIME=$(date +%s) # record the end time in seconds since the epoch
TEST_ELAPSED_TIME=$((TEST_END_TIME - TEST_START_TIME)) # calculate the elapsed time
echo -e "${BLUE}$0 took $TEST_ELAPSED_TIME seconds to complete${NC}" # log the time taken for the test
exit 0

View file

@ -0,0 +1,9 @@
# Writes Any Sync ports to .env.override file
cat <<EOF > $PROJECT_DIR/.env.override
ANY_SYNC_NODE_1_PORT=$ANY_SYNC_NODE_1_PORT
ANY_SYNC_NODE_2_PORT=$ANY_SYNC_NODE_2_PORT
ANY_SYNC_NODE_3_PORT=$ANY_SYNC_NODE_3_PORT
ANY_SYNC_COORDINATOR_PORT=$ANY_SYNC_COORDINATOR_PORT
ANY_SYNC_FILENODE_PORT=$ANY_SYNC_FILENODE_PORT
ANY_SYNC_CONSENSUSNODE_PORT=$ANY_SYNC_CONSENSUSNODE_PORT
EOF

View file

@ -0,0 +1,7 @@
# Sets the default version for Any Sync components in .env.override
cat <<EOF > $PROJECT_DIR/.env.override
ANY_SYNC_NODE_VERSION=${ANY_SYNC_NODE_VERSION[0]}
ANY_SYNC_FILENODE_VERSION=${ANY_SYNC_FILENODE_VERSION[0]}
ANY_SYNC_COORDINATOR_VERSION=${ANY_SYNC_COORDINATOR_VERSION[0]}
ANY_SYNC_CONSENSUSNODE_VERSION=${ANY_SYNC_CONSENSUSNODE_VERSION[0]}
EOF

View file

@ -0,0 +1,7 @@
# Sets production version for Any Sync components in .env.override
cat <<EOF > $PROJECT_DIR/.env.override
ANY_SYNC_NODE_VERSION=${ANY_SYNC_NODE_VERSION[1]}
ANY_SYNC_FILENODE_VERSION=${ANY_SYNC_FILENODE_VERSION[1]}
ANY_SYNC_COORDINATOR_VERSION=${ANY_SYNC_COORDINATOR_VERSION[1]}
ANY_SYNC_CONSENSUSNODE_VERSION=${ANY_SYNC_CONSENSUSNODE_VERSION[1]}
EOF

View file

@ -0,0 +1,7 @@
# Sets staging version for Any Sync components in .env.override
cat <<EOF > $PROJECT_DIR/.env.override
ANY_SYNC_NODE_VERSION=${ANY_SYNC_NODE_VERSION[2]}
ANY_SYNC_FILENODE_VERSION=${ANY_SYNC_FILENODE_VERSION[2]}
ANY_SYNC_COORDINATOR_VERSION=${ANY_SYNC_COORDINATOR_VERSION[2]}
ANY_SYNC_CONSENSUSNODE_VERSION=${ANY_SYNC_CONSENSUSNODE_VERSION[2]}
EOF

View file

@ -0,0 +1,4 @@
# Sets the external listen host in .env.override
cat <<EOF > $PROJECT_DIR/.env.override
EXTERNAL_LISTEN_HOST=\"$EXTERNAL_LISTEN_HOST\"
EOF

View file

@ -0,0 +1,4 @@
# Sets multiple external listen hosts in .env.override
cat <<EOF > $PROJECT_DIR/.env.override
EXTERNAL_LISTEN_HOSTS=\"$EXTERNAL_LISTEN_HOSTS\"
EOF

View file

@ -0,0 +1,7 @@
# Writes Minio port configurations to .env.override
cat <<EOF > $PROJECT_DIR/.env.override
MINIO_PORT=$MINIO_PORT
EXTERNAL_MINIO_PORT=$EXTERNAL_MINIO_PORT
MINIO_WEB_PORT=$MINIO_WEB_PORT
EXTERNAL_MINIO_WEB_PORT=$EXTERNAL_MINIO_WEB_PORT
EOF

19
tests/variables Normal file
View file

@ -0,0 +1,19 @@
ANY_SYNC_NODE_1_PORT=10001
ANY_SYNC_NODE_2_PORT=10002
ANY_SYNC_NODE_3_PORT=10003
ANY_SYNC_COORDINATOR_PORT=20001
ANY_SYNC_FILENODE_PORT=30001
ANY_SYNC_CONSENSUSNODE_PORT=40001
EXTERNAL_LISTEN_HOST="home.lan"
EXTERNAL_LISTEN_HOSTS="stage.lan 192.168.0.5"
ANY_SYNC_NODE_VERSION=("v0.4.11" "prod" "stage1")
ANY_SYNC_FILENODE_VERSION=("v0.8.1" "prod" "stage1")
ANY_SYNC_COORDINATOR_VERSION=("v0.4.2" "prod" "stage1")
ANY_SYNC_CONSENSUSNODE_VERSION=("v0.2.0" "prod" "stage1")
MINIO_PORT=2000
EXTERNAL_MINIO_PORT=2001
MINIO_WEB_PORT=3000
EXTERNAL_MINIO_WEB_PORT=3001