Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·398 lines (354 loc) · 16.1 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·398 lines (354 loc) · 16.1 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/env bash
set -euo pipefail
APP_NAME="paper"
APP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PREFERRED_PORT=7860
PORT_REQUESTED="${PORT:-}"
DOMAIN="${DOMAIN:-paper.voidall.com}"
DATA_DIR="${DATA_DIR:-/var/lib/paper}"
BUILD_DIR="$APP_DIR"
# Always use the toolchain actually installed on this machine. Without this,
# any `go` invocation in a module whose go.mod demands a newer Go silently
# tries to download that toolchain — and fails on VPSes without proxy access.
export GOTOOLCHAIN=local
# ── Colors ────────────────────────────────────────────────────────────────────
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; NC='\033[0m'
info() { echo -e "${GREEN}[✓]${NC} $*"; }
warn() { echo -e "${YELLOW}[!]${NC} $*"; }
error() { echo -e "${RED}[✗]${NC} $*"; exit 1; }
step() { echo -e "\n${BLUE}──${NC} $*"; }
echo ""
echo " Paper — VPS deploy (Go, Cloudflare Tunnel)"
echo " ──────────────────────────────────────────────"
# ── Port and route safety ─────────────────────────────────────────────────────
find_cf_config() {
local candidate
for candidate in /etc/cloudflared/config.yml /root/.cloudflared/config.yml "$HOME/.cloudflared/config.yml"; do
[ -f "$candidate" ] && { printf '%s' "$candidate"; return; }
done
}
tunnel_rules() {
[ -f "${1:-}" ] || return 0
python3 - "$1" <<'PYEOF'
import re, sys
text = open(sys.argv[1]).read()
for host, svc in re.findall(r"-\s*hostname:\s*(\S+)\s*\n\s*service:\s*(\S+)", text):
m = re.search(r":(\d+)\s*$", svc)
print(host, m.group(1) if m else "")
PYEOF
}
check_tunnel_route() {
local config="$1" domain="$2" port="$3"
local host rule_port routed="" clash=""
while read -r host rule_port; do
[ -z "$rule_port" ] && continue
if [ "$host" = "$domain" ]; then
routed="$rule_port"
elif [ "$rule_port" = "$port" ]; then
clash="$host"
fi
done < <(tunnel_rules "$config")
if [ -n "$routed" ]; then
[ "$routed" = "$port" ] && { printf 'OK'; return; }
printf 'WRONGPORT %s' "$routed"
return
fi
[ -n "$clash" ] && { printf 'CLASH %s' "$clash"; return; }
printf 'MISSING'
}
port_holder() {
ss -ltnp 2>/dev/null | awk -v p=":$1\$" '$4 ~ p {print $NF; exit}'
}
wait_for_port() {
local timeout="${1:-30}" port="$2" waited=0 appname="${3:-}"
while [ "$waited" -lt "$timeout" ]; do
port_holder "$port" >/dev/null && return 0
if [ -n "$appname" ] && [ "$(systemctl is-active "$appname.service" 2>/dev/null)" != active ]; then
warn "$appname exited before binding port $port."
return 1
fi
sleep 1; waited=$((waited + 1))
done
return 1
}
port_is_ours_or_free() {
local port="$1" me="$2" holder
holder=$(port_holder "$port")
[ -z "$holder" ] && return 0
[ "$(unit_of "$holder")" = "$me.service" ]
}
assert_port_available() {
local port="$1" me="$2" holder
port_is_ours_or_free "$port" "$me" && return 0
holder=$(port_holder "$port")
error "Port $port is already in use by: $holder
Starting '$me' here would crash-loop on EADDRINUSE, so this stops now.
Owning service: $(unit_of "$holder" || true)
Free the port, or pick another: PORT=<free-port> bash deploy.sh"
}
# The systemd unit of the process in an ss "users:(...)" field, if any.
unit_of() {
local pid
pid=$(printf '%s' "$1" | grep -o 'pid=[0-9]*' | head -1 | cut -d= -f2)
[ -n "$pid" ] && ps -o unit= -p "$pid" 2>/dev/null
}
# Write /etc/systemd/system/$APP_NAME.service and (re)start it, so systemd runs
# the app at boot and restarts it if it dies. Args: working dir, then the
# command. Environment comes from the SERVICE_ENV array, plus this script's PATH.
# Mode 600: SERVICE_ENV may carry credentials (e.g. a proxy URL).
install_service() {
local workdir="$1" unit="/etc/systemd/system/$APP_NAME.service" var
shift
{
printf '[Unit]\nDescription=%s (%s)\nAfter=network-online.target\nWants=network-online.target\n\n' "$APP_NAME" "$DOMAIN"
printf '[Service]\nUser=%s\nGroup=%s\nWorkingDirectory=%s\n' "$(id -un)" "$(id -gn)" "$workdir"
for var in "PATH=$PATH" "${SERVICE_ENV[@]}"; do
var=${var//\\/\\\\}; var=${var//\"/\\\"}; var=${var//%/%%}
printf 'Environment="%s"\n' "$var"
done
printf 'ExecStart=%s\nRestart=always\nRestartSec=3\n\n[Install]\nWantedBy=multi-user.target\n' "$*"
} | sudo install -m 600 /dev/stdin "$unit"
sudo systemctl daemon-reload
sudo systemctl enable --quiet "$APP_NAME.service"
sudo systemctl restart "$APP_NAME.service"
}
resolve_port() {
local preferred="$1" domain="$2" me="$3" config="$4"
local routed="" taken=" " host rule_port candidate
while read -r host rule_port; do
[ -z "$rule_port" ] && continue
[ "$host" = "$domain" ] && routed="$rule_port"
taken="$taken$rule_port "
done < <(tunnel_rules "$config")
if [ -n "${PORT_REQUESTED:-}" ]; then
PORT="$PORT_REQUESTED"
assert_port_available "$PORT" "$me"
info "Port $PORT (set explicitly)"
return
fi
if [ -n "$routed" ]; then
PORT="$routed"
assert_port_available "$PORT" "$me"
info "Port $PORT (from the existing $domain tunnel rule)"
return
fi
for candidate in $(seq "$preferred" $((preferred + 60))); do
case "$taken" in *" $candidate "*) continue ;; esac
port_is_ours_or_free "$candidate" "$me" || continue
PORT="$candidate"
[ "$candidate" = "$preferred" ] && info "Port $PORT" \
|| info "Port $preferred is taken — using $PORT instead"
return
done
error "No free port in $preferred..$((preferred + 60)). Check: ss -ltnp"
}
print_routes() {
local config="$1" domain="$2"
if [ -z "$config" ] || [ ! -f "$config" ]; then
warn "No cloudflared config found — cannot list the routes."
return
fi
UNITS="$(ps -eo pid=,unit= 2>/dev/null || true)" \
LISTENERS="$(ss -ltnp 2>/dev/null || true)" \
python3 - "$config" "$domain" <<'PYEOF'
import os, re, sys
config, current = sys.argv[1], sys.argv[2]
GREEN, YELLOW, DIM, BOLD, NC = "\033[0;32m", "\033[1;33m", "\033[2m", "\033[1m", "\033[0m"
rules = []
for host, svc in re.findall(r"-\s*hostname:\s*(\S+)\s*\n\s*service:\s*(\S+)",
open(config).read()):
m = re.search(r":(\d+)\s*$", svc)
rules.append((host, m.group(1) if m else "?"))
listening = {}
for line in os.environ.get("LISTENERS", "").splitlines():
addr = re.search(r"\s(\S+):(\d+)\s", line)
pid = re.search(r"pid=(\d+)", line)
if addr:
listening[addr.group(2)] = pid.group(1) if pid else ""
by_pid = {}
for line in os.environ.get("UNITS", "").splitlines():
parts = line.split()
if len(parts) == 2 and parts[1].endswith(".service"):
by_pid[parts[0]] = parts[1][:-len(".service")]
rows = []
for host, port in rules:
pid = listening.get(port)
if pid is None:
state, app = "not running", "—"
else:
state, app = "online", by_pid.get(pid) or "(not a service)"
rows.append((host, port, state, app, host == current))
if not rows:
print(" (no hostname rules in the tunnel config)")
sys.exit()
w_host = max(6, max(len(r[0]) for r in rows))
w_app = max(7, max(len(r[3]) for r in rows))
w_state = max(6, max(len(r[2]) for r in rows))
line = " " + "─" * (w_host + w_app + w_state + 17)
print()
print(f" {BOLD}Cloudflare Tunnel routes{NC} {DIM}({config}){NC}")
print(line)
print(f" {BOLD}{'DOMAIN'.ljust(w_host)} {'PORT'.rjust(5)} {'STATUS'.ljust(w_state + 2)} {'SERVICE'.ljust(w_app)}{NC}")
print(line)
for host, port, state, app, is_me in rows:
dot = f"{GREEN}●{NC}" if state == "online" else f"{YELLOW}○{NC}"
here = f" {GREEN}← this app{NC}" if is_me else ""
print(f" {host.ljust(w_host)} {port.rjust(5)} {dot} {state.ljust(w_state)} {app.ljust(w_app)}{here}")
print(line)
PYEOF
}
ensure_dns_route() {
local domain="$1" config="$2" tunnel
tunnel=$(grep -E '^tunnel:' "$config" 2>/dev/null | awk '{print $2}' | tr -d '"' || true)
[ -z "$tunnel" ] && tunnel="${domain%%.*}"
if command -v cloudflared >/dev/null 2>&1 && cloudflared tunnel list 2>/dev/null | grep -qw "$tunnel"; then
cloudflared tunnel route dns --overwrite-dns "$tunnel" "$domain" \
&& info "DNS route ensured: $domain → tunnel '$tunnel'" \
|| warn "Failed to add DNS route — add manually: cloudflared tunnel route dns $tunnel $domain"
else
warn "Tunnel '$tunnel' not found — add DNS manually: cloudflared tunnel route dns <TUNNEL_NAME> $domain"
fi
}
step "Port"
command -v ss >/dev/null 2>&1 || warn "'ss' not found (install iproute2) — cannot verify a port is free."
CF_CONFIG="$(find_cf_config)"
resolve_port "$PREFERRED_PORT" "$DOMAIN" "$APP_NAME" "$CF_CONFIG"
# ── 1. systemd ────────────────────────────────────────────────────────────────────────
step "systemd"
command -v systemctl >/dev/null 2>&1 || error "systemd not found — the app runs as a systemd service."
sudo -n true 2>/dev/null || warn "sudo will ask for your password (to install the service)"
info "$(systemctl --version | head -1)"
# ── 2. Build ──────────────────────────────────────────────────────────────────
step "Build"
if ! command -v go &>/dev/null; then
error "Go not found. Install it (https://go.dev/dl/) then re-run."
fi
info "Go $(go version | awk '{print $3}') building..."
(cd "$APP_DIR" && CGO_ENABLED=0 go build -o paper .)
info "Binary built"
# ── 2b. Data directory + optional filesystem quota ────────────────────────────
step "Data directory"
# The service runs as this user, so it must own the data directory.
# /var/lib needs root to create: use sudo once, then hand the directory over.
# Without sudo, fall back to a directory in $HOME.
RUN_USER="$(id -un)"
ensure_data_dir() {
local dir="$1"
if mkdir -p "$dir" 2>/dev/null && [ -w "$dir" ]; then
return 0
fi
command -v sudo >/dev/null 2>&1 || return 1
info "Creating $dir with sudo (owned by $RUN_USER)..."
sudo mkdir -p "$dir" && sudo chown "$(id -u):$(id -g)" "$dir" && [ -w "$dir" ]
}
if ! ensure_data_dir "$DATA_DIR"; then
FALLBACK_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/paper"
warn "Cannot create or write $DATA_DIR (no sudo?) — using $FALLBACK_DIR instead"
DATA_DIR="$FALLBACK_DIR"
ensure_data_dir "$DATA_DIR" || error "Cannot create $DATA_DIR"
fi
chmod 700 "$DATA_DIR" || error "Cannot set permissions on $DATA_DIR"
info "Data directory: $DATA_DIR (owner: $RUN_USER)"
# Opt-in loopback filesystem quota. The app has its own 100 MB write budget,
# but an OS-level limit defends against a compromised process or unexpected
# files. Set QUOTA_SIZE_MB=e.g. 110 (slightly above MAX_TOTAL_SIZE_MB) to use.
# Mounting needs root, so this step uses sudo.
if [ -n "${QUOTA_SIZE_MB:-}" ]; then
QUOTA_IMG="$APP_DIR/data/quota.img"
if mountpoint -q "$DATA_DIR" 2>/dev/null; then
info "$DATA_DIR is already its own filesystem — quota active"
else
mkdir -p "$APP_DIR/data"
if [ ! -f "$QUOTA_IMG" ]; then
info "Creating ${QUOTA_SIZE_MB}MB loopback filesystem image..."
dd if=/dev/zero of="$QUOTA_IMG" bs=1M count="$QUOTA_SIZE_MB" status=none \
&& mkfs.ext4 -F -q "$QUOTA_IMG" \
|| warn "Could not create quota image — continuing without quota"
fi
if [ -f "$QUOTA_IMG" ] && sudo mount -o loop "$QUOTA_IMG" "$DATA_DIR" 2>/dev/null; then
# A fresh filesystem's root is owned by root: give it back to the app user.
sudo chown "$(id -u):$(id -g)" "$DATA_DIR" && chmod 700 "$DATA_DIR"
info "Mounted ${QUOTA_SIZE_MB}MB filesystem at $DATA_DIR"
else
warn "Could not mount quota image (sudo? loop device?) — continuing without quota"
fi
fi
fi
# ── 3. Start / restart the service ────────────────────────────────────────────
step "Service"
info "Starting '$APP_NAME' (paper binary) on 127.0.0.1:$PORT..."
# Bound to 127.0.0.1, so every request arrives through the Cloudflare Tunnel:
# CF-Connecting-IP (set by Cloudflare, not the visitor) is the real client IP
# used for per-client rate limiting.
SERVICE_ENV=(LISTEN_ADDR=127.0.0.1 "LISTEN_PORT=$PORT" "STATIC_DIR=$APP_DIR" "DATA_DIR=$DATA_DIR"
"TRUST_PROXY_HEADER=${TRUST_PROXY_HEADER:-CF-Connecting-IP}")
install_service "$APP_DIR" "$APP_DIR/paper"
if ! wait_for_port 30 "$PORT" "$APP_NAME"; then
error "'$APP_NAME' did not bind port $PORT within 30s.
Check its logs: journalctl -u $APP_NAME -n 50"
fi
info "'$APP_NAME' up — answering on 127.0.0.1:$PORT"
# ── 4. Cloudflare Tunnel ──────────────────────────────────────────────────────
step "Cloudflare Tunnel"
if [ -z "$CF_CONFIG" ]; then
warn "cloudflared config not found. Ensure this ingress rule exists:"
echo " - hostname: $DOMAIN"
echo " service: http://localhost:$PORT"
else
ROUTE=$(check_tunnel_route "$CF_CONFIG" "$DOMAIN" "$PORT")
case "$ROUTE" in
OK)
info "$DOMAIN → localhost:$PORT already routed — no change needed"
ensure_dns_route "$DOMAIN" "$CF_CONFIG" ;;
CLASH*)
error "Port $PORT is already routed to ${ROUTE#CLASH } in $CF_CONFIG.
Adding $DOMAIN on the same port would give two hostnames one app.
Pick a free port: PORT=<free-port> bash deploy.sh" ;;
WRONGPORT*)
error "$DOMAIN is routed to ${ROUTE#WRONGPORT } in $CF_CONFIG, not port $PORT.
This deploy would look successful while the tunnel kept serving the old app.
Fix the rule by hand, or deploy on that port: PORT=<that-port> bash deploy.sh" ;;
MISSING)
sudo cp "$CF_CONFIG" "${CF_CONFIG}.bak"
sudo python3 - "$CF_CONFIG" "$DOMAIN" "$PORT" <<'PYEOF'
import sys, re
config_path, domain, port = sys.argv[1], sys.argv[2], sys.argv[3]
new_rule = f" - hostname: {domain}\n service: http://localhost:{port}\n"
content = open(config_path).read()
m = re.search(r'^(\s*- service:\s*http_status:\d+\s*)$', content, re.MULTILINE)
content = (content[:m.start()] + new_rule + content[m.start():]) if m else (content.rstrip() + "\n" + new_rule)
open(config_path, 'w').write(content)
print("Config updated.")
PYEOF
info "Added $DOMAIN → localhost:$PORT (existing rules untouched; backup at ${CF_CONFIG}.bak)"
ensure_dns_route "$DOMAIN" "$CF_CONFIG"
systemctl is-active --quiet cloudflared 2>/dev/null && sudo systemctl restart cloudflared && info "cloudflared restarted" \
|| warn "Restart cloudflared manually: sudo systemctl restart cloudflared" ;;
esac
fi
# ── 5. Verify it actually came up ─────────────────────────────────────────────
step "Health"
sleep 2
if curl -fsS -o /dev/null "http://127.0.0.1:$PORT/health" 2>/dev/null; then
info "Responding on 127.0.0.1:$PORT"
else
warn "No response yet on 127.0.0.1:$PORT — check: journalctl -u $APP_NAME -n 50"
fi
# ── Done ──────────────────────────────────────────────────────────────────────
echo ""
print_routes "$CF_CONFIG" "$DOMAIN"
echo ""
echo " ─────────────────────────────────────────"
info "Done!"
echo ""
echo " Go server on 127.0.0.1:$PORT"
echo " Tunnel: https://$DOMAIN"
echo ""
echo " Useful commands:"
echo " journalctl -u $APP_NAME -f — live logs"
echo " sudo systemctl restart $APP_NAME — restart"
echo " systemctl status $APP_NAME — process status"
echo " (or Cockpit → Services → $APP_NAME)"
echo ""
echo " To update: git pull && bash deploy.sh"
echo ""