Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·72 lines (58 loc) · 2.28 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·72 lines (58 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env bash
set -Eeuo pipefail
APP_ROOT=${APP_ROOT:-/opt/nasak-sms}
REPO_DIR=${REPO_DIR:-$APP_ROOT/repository}
SHARED_DIR=${SHARED_DIR:-$APP_ROOT/shared}
COMPOSE_ENV=${COMPOSE_ENV:-$SHARED_DIR/compose.env}
COMPOSE_FILE=${COMPOSE_FILE:-$REPO_DIR/compose.production.yml}
required_files=("$COMPOSE_ENV" "$SHARED_DIR/api.env")
for file in "${required_files[@]}"; do
if [[ ! -s "$file" ]]; then
echo "Required runtime file is missing or empty: $file" >&2
exit 1
fi
done
chmod 600 "$COMPOSE_ENV" "$SHARED_DIR/api.env"
cd "$REPO_DIR"
export GIT_COMMIT
if [[ -z ${GIT_COMMIT:-} ]]; then
GIT_COMMIT=$(git rev-parse --short=12 HEAD 2>/dev/null || printf 'unknown')
fi
compose=(docker compose --env-file "$COMPOSE_ENV" -f "$COMPOSE_FILE")
"${compose[@]}" config --quiet
"${compose[@]}" build --pull
"${compose[@]}" up -d --remove-orphans
for _ in $(seq 1 60); do
if curl -fsS http://127.0.0.1:3800/health >/dev/null && curl -fsS http://127.0.0.1:3801/ >/dev/null; then
break
fi
sleep 2
done
curl -fsS http://127.0.0.1:3800/health >/dev/null
curl -fsS http://127.0.0.1:3801/ >/dev/null
read_env_value() {
local key=$1 file=$2
sed -n "s/^${key}=//p" "$file" | tail -n 1 | tr -d '\r'
}
EVENTS_QUEUE_USER_ID=$(read_env_value EVENTS_QUEUE_USER_ID "$SHARED_DIR/api.env")
EVENTS_QUEUE_USER_API_KEY=$(read_env_value EVENTS_QUEUE_USER_API_KEY "$SHARED_DIR/api.env")
POSTGRES_USER=$(read_env_value POSTGRES_USER "$COMPOSE_ENV")
POSTGRES_DB=$(read_env_value POSTGRES_DB "$COMPOSE_ENV")
POSTGRES_USER=${POSTGRES_USER:-httpsms}
POSTGRES_DB=${POSTGRES_DB:-httpsms}
if [[ -z ${EVENTS_QUEUE_USER_ID:-} || -z ${EVENTS_QUEUE_USER_API_KEY:-} ]]; then
echo "System queue user is not configured." >&2
exit 1
fi
"${compose[@]}" exec -T postgres psql \
--username "$POSTGRES_USER" \
--dbname "$POSTGRES_DB" \
--set=system_user_id="$EVENTS_QUEUE_USER_ID" \
--set=system_api_key="$EVENTS_QUEUE_USER_API_KEY" <<'SQL'
INSERT INTO users (id, api_key, email, timezone, subscription_name, created_at, updated_at)
VALUES (:'system_user_id', :'system_api_key', 'system@sms.nasak.ir', 'Asia/Tehran', 'free', NOW(), NOW())
ON CONFLICT (id) DO UPDATE SET api_key = EXCLUDED.api_key, updated_at = NOW();
SQL
"${compose[@]}" restart api
"$REPO_DIR/ops/health-check.sh"
echo "Nasak SMS deployed from commit $GIT_COMMIT"