Skip to content

Commit 227fe90

Browse files
committed
feat: add idempotent LNbits deploy script
scripts/deploy-lnbits.sh — runs on every deploy: 1. Stop LNbits (systemctl stop) 2. Backup DB (sqlite3 .backup → gzip, rotate last 10) 3. Git pull (fetch + reset --hard origin/main) 4. Upgrade deps (pip install -e .[all]) 5. Start LNbits + health check Updated deploy-lnbits-droplet.yml: - First deploy: clone + setup-droplet.sh (full install) - Subsequent deploys: pull + deploy-lnbits.sh (upgrade)
1 parent c79a10c commit 227fe90

File tree

2 files changed

+165
-4
lines changed

2 files changed

+165
-4
lines changed

.github/workflows/deploy-lnbits-droplet.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,14 @@ jobs:
7373
set -euo pipefail
7474
if [ ! -d ~/coinpayportal ]; then
7575
git clone https://github.com/profullstack/coinpayportal.git ~/coinpayportal
76+
cd ~/coinpayportal
77+
sudo bash scripts/setup-droplet.sh
78+
else
79+
cd ~/coinpayportal
80+
git fetch origin master
81+
git reset --hard origin/master
82+
sudo bash scripts/deploy-lnbits.sh
7683
fi
77-
cd ~/coinpayportal
78-
git fetch origin master
79-
git reset --hard origin/master
80-
sudo bash scripts/setup-droplet.sh
8184
8285
- name: Verify LNURLp endpoint
8386
shell: bash

scripts/deploy-lnbits.sh

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#!/usr/bin/env bash
2+
#
3+
# CoinPay LNbits Deploy / Upgrade Script
4+
# Idempotent — safe to run on every deploy.
5+
#
6+
# Flow: stop lnbits → backup db → git pull → upgrade deps → start lnbits
7+
#
8+
# Usage:
9+
# bash scripts/deploy-lnbits.sh
10+
#
11+
set -euo pipefail
12+
13+
LNBITS_DIR="/opt/lnbits"
14+
LNBITS_DATA="/opt/lnbits-data"
15+
BACKUP_DIR="/opt/lnbits-backups"
16+
MAX_BACKUPS=10
17+
18+
echo "═══════════════════════════════════════════"
19+
echo " CoinPay LNbits Deploy"
20+
echo " $(date -u '+%Y-%m-%d %H:%M:%S UTC')"
21+
echo "═══════════════════════════════════════════"
22+
23+
# ─────────────────────────────────────────────
24+
# 1. Stop LNbits
25+
# ─────────────────────────────────────────────
26+
echo "▶ [1/5] Stopping LNbits..."
27+
28+
if systemctl is-active --quiet lnbits; then
29+
systemctl stop lnbits
30+
echo " LNbits stopped"
31+
else
32+
echo " LNbits was not running"
33+
fi
34+
35+
# ─────────────────────────────────────────────
36+
# 2. Backup database
37+
# ─────────────────────────────────────────────
38+
echo "▶ [2/5] Backing up database..."
39+
40+
mkdir -p "${BACKUP_DIR}"
41+
42+
DB_FILE="${LNBITS_DATA}/database.sqlite3"
43+
if [ -f "${DB_FILE}" ]; then
44+
TIMESTAMP=$(date -u '+%Y%m%d-%H%M%S')
45+
BACKUP_FILE="${BACKUP_DIR}/lnbits-db-${TIMESTAMP}.sqlite3"
46+
47+
# Use sqlite3 .backup for a consistent snapshot (if sqlite3 is available)
48+
if command -v sqlite3 &>/dev/null; then
49+
sqlite3 "${DB_FILE}" ".backup '${BACKUP_FILE}'"
50+
else
51+
cp "${DB_FILE}" "${BACKUP_FILE}"
52+
fi
53+
54+
# Compress the backup
55+
if command -v gzip &>/dev/null; then
56+
gzip "${BACKUP_FILE}"
57+
BACKUP_FILE="${BACKUP_FILE}.gz"
58+
fi
59+
60+
BACKUP_SIZE=$(du -h "${BACKUP_FILE}" | cut -f1)
61+
echo " Backed up to ${BACKUP_FILE} (${BACKUP_SIZE})"
62+
63+
# Rotate old backups — keep only the last N
64+
BACKUP_COUNT=$(ls -1 "${BACKUP_DIR}"/lnbits-db-*.sqlite3* 2>/dev/null | wc -l)
65+
if [ "${BACKUP_COUNT}" -gt "${MAX_BACKUPS}" ]; then
66+
ls -1t "${BACKUP_DIR}"/lnbits-db-*.sqlite3* | tail -n +$((MAX_BACKUPS + 1)) | xargs rm -f
67+
echo " Rotated old backups (keeping last ${MAX_BACKUPS})"
68+
fi
69+
else
70+
echo " No database found at ${DB_FILE} — skipping backup"
71+
fi
72+
73+
# ─────────────────────────────────────────────
74+
# 3. Update LNbits (git pull)
75+
# ─────────────────────────────────────────────
76+
echo "▶ [3/5] Updating LNbits..."
77+
78+
if [ -d "${LNBITS_DIR}/.git" ]; then
79+
cd "${LNBITS_DIR}"
80+
81+
BEFORE=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
82+
83+
git fetch origin
84+
git reset --hard origin/main 2>/dev/null || git reset --hard origin/master 2>/dev/null || {
85+
echo " ⚠ git reset failed — trying pull instead"
86+
git pull --ff-only
87+
}
88+
89+
AFTER=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
90+
91+
if [ "${BEFORE}" = "${AFTER}" ]; then
92+
echo " Already up to date (${AFTER})"
93+
else
94+
echo " Updated: ${BEFORE}${AFTER}"
95+
fi
96+
else
97+
echo " LNbits not a git repo at ${LNBITS_DIR} — run setup-droplet.sh first"
98+
# Start lnbits back up before exiting on error
99+
systemctl start lnbits 2>/dev/null || true
100+
exit 1
101+
fi
102+
103+
# ─────────────────────────────────────────────
104+
# 4. Upgrade dependencies
105+
# ─────────────────────────────────────────────
106+
echo "▶ [4/5] Upgrading dependencies..."
107+
108+
cd "${LNBITS_DIR}"
109+
110+
if [ -d ".venv" ]; then
111+
.venv/bin/pip install --upgrade pip -q 2>/dev/null || true
112+
.venv/bin/pip install -e ".[all]" -q 2>/dev/null || .venv/bin/pip install -e . -q
113+
echo " Dependencies updated"
114+
else
115+
echo " No virtualenv found — run setup-droplet.sh first"
116+
systemctl start lnbits 2>/dev/null || true
117+
exit 1
118+
fi
119+
120+
# ─────────────────────────────────────────────
121+
# 5. Start LNbits
122+
# ─────────────────────────────────────────────
123+
echo "▶ [5/5] Starting LNbits..."
124+
125+
systemctl start lnbits
126+
127+
# Wait for LNbits to be ready
128+
READY=false
129+
for i in $(seq 1 30); do
130+
if curl -s -o /dev/null -w "%{http_code}" "http://127.0.0.1:5000/api/v1/health" 2>/dev/null | grep -q "200"; then
131+
READY=true
132+
break
133+
fi
134+
sleep 1
135+
done
136+
137+
if [ "$READY" = true ]; then
138+
echo " ✅ LNbits is running and healthy"
139+
else
140+
# Check if process is at least running
141+
if systemctl is-active --quiet lnbits; then
142+
echo " ⚠ LNbits is running but health check timed out (may still be starting)"
143+
else
144+
echo " ❌ LNbits failed to start — check: journalctl -u lnbits -n 50"
145+
exit 1
146+
fi
147+
fi
148+
149+
echo ""
150+
echo "═══════════════════════════════════════════"
151+
echo " ✅ Deploy complete!"
152+
echo " $(date -u '+%Y-%m-%d %H:%M:%S UTC')"
153+
echo "═══════════════════════════════════════════"
154+
echo ""
155+
echo " LNbits: $(cd ${LNBITS_DIR} && git rev-parse --short HEAD 2>/dev/null || echo 'unknown')"
156+
echo " Status: systemctl status lnbits"
157+
echo " Logs: journalctl -u lnbits -f"
158+
echo " Backup: ${BACKUP_DIR}/"

0 commit comments

Comments
 (0)