-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·164 lines (132 loc) · 3.57 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·164 lines (132 loc) · 3.57 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="${ENV_FILE:-${ROOT_DIR}/.env.prod}"
COMPOSE_FILE="${COMPOSE_FILE:-${ROOT_DIR}/compose.prod.yml}"
usage() {
cat <<'EOF'
Usage:
bash deploy.sh up
bash deploy.sh issue-le DOMAIN=e345ee.ru [CERTBOT_EMAIL=you@example.com]
bash deploy.sh sync-le DOMAIN=e345ee.ru
bash deploy.sh restart
Defaults:
ENV_FILE=.env.prod
COMPOSE_FILE=compose.prod.yml
Notes:
- Run on the SERVER.
- Ensure ports 80/443 are open and DNS points domain to this server.
EOF
}
require_cmd() {
local cmd="$1"
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "ERROR: missing command: $cmd" >&2
exit 1
fi
}
load_env() {
if [[ ! -f "${ENV_FILE}" ]]; then
echo "ERROR: env file not found: ${ENV_FILE}" >&2
echo "Create it from .env.prod.example (see README)." >&2
exit 1
fi
set -a
source "${ENV_FILE}"
set +a
}
ensure_tls_files() {
local tls_dir="${TLS_CERTS_DIR:?TLS_CERTS_DIR is required}"
mkdir -p "$tls_dir"
if [[ -f "$tls_dir/fullchain.pem" && -f "$tls_dir/privkey.pem" ]]; then
return 0
fi
echo "TLS cert not found in $tls_dir; creating temporary self-signed (1 day)" >&2
if command -v openssl >/dev/null 2>&1; then
openssl req -x509 -nodes -newkey rsa:2048 -days 1 \
-keyout "$tls_dir/privkey.pem" \
-out "$tls_dir/fullchain.pem" \
-subj "/CN=${DOMAIN:-e345ee.ru}"
else
require_cmd docker
docker run --rm -v "$tls_dir:/out" alpine:3.20 sh -euc '
apk add --no-cache openssl >/dev/null
openssl req -x509 -nodes -newkey rsa:2048 -days 1 \
-keyout /out/privkey.pem \
-out /out/fullchain.pem \
-subj "/CN=${DOMAIN:-e345ee.ru}"
'
fi
chmod 600 "$tls_dir/privkey.pem" || true
}
compose() {
require_cmd docker
docker compose --env-file "${ENV_FILE}" -f "${COMPOSE_FILE}" "$@"
}
cmd_up() {
load_env
ensure_tls_files
compose up -d --build
echo "OK: stack is up. Check: https://$(echo "${REACT_APP_API_URL:-}" | sed -E 's#^https?://##')/healthz" >&2
}
cmd_restart() {
load_env
compose restart gateway
}
cmd_issue_le() {
load_env
local domain="${DOMAIN:-}"
if [[ -z "$domain" ]]; then
domain="${REACT_APP_API_URL:-}"
domain="${domain#http://}"
domain="${domain#https://}"
domain="${domain%%/*}"
fi
if [[ -z "$domain" ]]; then
echo "ERROR: set DOMAIN (e.g. DOMAIN=e345ee.ru)" >&2
exit 1
fi
require_cmd docker
local webroot="${ROOT_DIR}/infra/certbot/www"
mkdir -p "$webroot"
local email_args
if [[ -n "${CERTBOT_EMAIL:-}" ]]; then
email_args=("--agree-tos" "-m" "${CERTBOT_EMAIL}" "--non-interactive")
else
email_args=("--register-unsafely-without-email" "--agree-tos" "--non-interactive")
fi
echo "Issuing Let's Encrypt cert for ${domain} via webroot..." >&2
docker run --rm \
-v "${webroot}:/var/www/certbot" \
-v "/etc/letsencrypt:/etc/letsencrypt" \
certbot/certbot:latest \
certonly --webroot -w /var/www/certbot -d "${domain}" \
"${email_args[@]}"
DOMAIN="${domain}" TLS_CERTS_DIR="${TLS_CERTS_DIR}" bash "${ROOT_DIR}/infra/certbot/sync-certs.sh"
compose restart gateway
echo "OK: certificate issued and gateway restarted" >&2
}
cmd_sync_le() {
load_env
local domain="${DOMAIN:-e345ee.ru}"
DOMAIN="$domain" TLS_CERTS_DIR="${TLS_CERTS_DIR}" bash "${ROOT_DIR}/infra/certbot/sync-certs.sh"
compose restart gateway
}
main() {
cd "$ROOT_DIR"
local cmd="${1:-}"
shift || true
case "$cmd" in
up) cmd_up "$@" ;;
restart) cmd_restart "$@" ;;
issue-le) cmd_issue_le "$@" ;;
sync-le) cmd_sync_le "$@" ;;
-h|--help|help|"") usage ;;
*)
echo "ERROR: unknown command: $cmd" >&2
usage
exit 1
;;
esac
}
main "$@"