Files

114 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
branch="${1:-main}"
target_dir="${2:-/home/user/services/egbim-homepage}"
env_file="${3:-}"
db_dump_path="${4:-egbim_DB.sql}"
db_import_marker=".db_import_done"
case "$target_dir" in
'~')
target_dir="$HOME"
;;
'~/'*)
target_dir="$HOME/${target_dir#\~/}"
;;
esac
mkdir -p "$target_dir"
cd "$target_dir"
if [[ ! -d .git ]]; then
git clone --branch "$branch" https://gitea.hmac.kr/b24014/egbim_homepage.git .
else
git fetch origin
git checkout "$branch"
git reset --hard "origin/$branch"
fi
if [[ -n "$env_file" ]]; then
cp "$env_file" .env
fi
load_env_file() {
local env_path="$1"
local line=""
local key=""
local value=""
while IFS= read -r line || [[ -n "$line" ]]; do
line="${line%$'\r'}"
if [[ -z "$line" || "$line" =~ ^[[:space:]]*# ]]; then
continue
fi
key="${line%%=*}"
value="${line#*=}"
key="$(printf '%s' "$key" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"
if [[ -z "$key" ]]; then
continue
fi
if [[ "$value" =~ ^\'.*\'$ || "$value" =~ ^\".*\"$ ]]; then
value="${value:1:${#value}-2}"
fi
export "$key=$value"
done < "$env_path"
}
if [[ -f .env ]]; then
load_env_file .env
fi
mkdir -p \
egbim/data/cache \
egbim/data/session \
egbim/data/log \
eng/data/cache \
eng/data/session \
eng/data/log
mkdir -p docker/mysql/init
docker_compose_up_with_retry() {
local max_attempts=3
local attempt=1
while (( attempt <= max_attempts )); do
echo "[deploy] docker compose up attempt ${attempt}/${max_attempts}"
if docker compose up -d --build; then
return 0
fi
if (( attempt == max_attempts )); then
echo "[deploy] docker compose up failed after ${max_attempts} attempts" >&2
echo "[deploy] check server docker/systemd state: systemctl status docker, systemctl status dbus, journalctl -u docker -n 200" >&2
return 1
fi
echo "[deploy] docker compose up failed, retrying shortly..." >&2
sleep 10
attempt=$((attempt + 1))
done
}
docker_compose_up_with_retry
if [[ -f "$db_dump_path" && ! -f "$db_import_marker" ]]; then
for _ in $(seq 1 30); do
if docker compose exec -T db mariadb-admin ping -h localhost -uroot "-p${MARIADB_ROOT_PASSWORD}" >/dev/null 2>&1; then
break
fi
sleep 2
done
docker compose exec -T db mariadb-admin ping -h localhost -uroot "-p${MARIADB_ROOT_PASSWORD}" >/dev/null 2>&1
docker compose exec -T db mariadb -u"${G5_MYSQL_USER}" "-p${G5_MYSQL_PASSWORD}" "${G5_MYSQL_DB}" < "$db_dump_path"
touch "$db_import_marker"
fi