Repository navigation
Expand file tree
/
Copy pathsetup.sh
More file actions
179 lines (156 loc) · 5.55 KB
/
Copy pathsetup.sh
File metadata and controls
179 lines (156 loc) · 5.55 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
#!/usr/bin/env bash
set -euo pipefail
# TheLounge Preview Service — Droplet Setup
# Run on the demo.thelounge.chat droplet as root.
#
# Prerequisites:
# export PREVIEW_GITHUB_CLIENT_ID=...
# export PREVIEW_GITHUB_CLIENT_SECRET=...
# export PORKBUN_API_KEY=...
# export PORKBUN_SECRET_KEY=...
#
# Usage:
# scp -r preview-service/ root@<ip>:/opt/preview-service/
# ssh root@<ip> bash /opt/preview-service/setup.sh
INSTALL_DIR="/opt/preview-service"
DATA_DIR="/opt/preview/data"
BUILD_DIR="/opt/preview/builds"
# --- Preflight checks ---
if [ -z "${PREVIEW_GITHUB_CLIENT_ID:-}" ] || [ -z "${PREVIEW_GITHUB_CLIENT_SECRET:-}" ]; then
echo "ERROR: PREVIEW_GITHUB_CLIENT_ID and PREVIEW_GITHUB_CLIENT_SECRET must be set."
echo " export PREVIEW_GITHUB_CLIENT_ID=..."
echo " export PREVIEW_GITHUB_CLIENT_SECRET=..."
exit 1
fi
if [ -z "${PORKBUN_API_KEY:-}" ] || [ -z "${PORKBUN_SECRET_KEY:-}" ]; then
echo "ERROR: PORKBUN_API_KEY and PORKBUN_SECRET_KEY must be set (for wildcard TLS cert)."
echo " export PORKBUN_API_KEY=..."
echo " export PORKBUN_SECRET_KEY=..."
exit 1
fi
if [ ! -d "$INSTALL_DIR" ]; then
echo "ERROR: Copy the preview-service source to $INSTALL_DIR first."
echo " scp -r ./preview-service root@<ip>:$INSTALL_DIR"
exit 1
fi
# --- Install Docker (if not present) ---
if ! command -v docker &>/dev/null; then
echo "==> Installing Docker..."
apt-get update -qq
apt-get install -y -qq docker.io docker-compose-v2
systemctl enable --now docker
else
echo "==> Docker already installed"
apt-get install -y -qq docker-compose-v2 2>/dev/null || true
fi
# --- Install Go (if not present) ---
GO_VERSION="1.22.5"
if ! command -v /usr/local/go/bin/go &>/dev/null; then
echo "==> Installing Go ${GO_VERSION}..."
wget -q "https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz" -O /tmp/go.tar.gz
tar -C /usr/local -xzf /tmp/go.tar.gz
rm /tmp/go.tar.gz
echo 'export PATH=$PATH:/usr/local/go/bin' > /etc/profile.d/go.sh
export PATH=$PATH:/usr/local/go/bin
else
echo "==> Go already installed"
export PATH=$PATH:/usr/local/go/bin
fi
# --- Build the Go binary ---
echo "==> Building preview-service..."
mkdir -p "$DATA_DIR" "$BUILD_DIR"
cd "$INSTALL_DIR"
/usr/local/go/bin/go build -o /usr/local/bin/preview-service .
# --- Start Ergo IRC server ---
echo "==> Starting Ergo IRC server..."
docker compose up -d
# --- Wildcard TLS cert via certbot + Porkbun DNS challenge ---
echo "==> Setting up TLS certificate..."
apt-get install -y -qq certbot python3-certbot-nginx pipx
# Install certbot-dns-porkbun via pipx (provides DNS challenge support)
if ! /root/.local/bin/certbot plugins 2>/dev/null | grep -q dns-porkbun; then
pipx install certbot-dns-porkbun --include-deps
fi
# Write Porkbun credentials (restricted permissions)
PORKBUN_CREDS="/etc/letsencrypt/porkbun.ini"
cat > "$PORKBUN_CREDS" <<CREDS
dns_porkbun_key = ${PORKBUN_API_KEY}
dns_porkbun_secret = ${PORKBUN_SECRET_KEY}
CREDS
chmod 600 "$PORKBUN_CREDS"
# Request wildcard cert (if not already present)
if [ ! -d "/etc/letsencrypt/live/preview.thelounge.chat" ]; then
/root/.local/bin/certbot certonly \
--authenticator dns-porkbun \
--dns-porkbun-credentials "$PORKBUN_CREDS" \
--dns-porkbun-propagation-seconds 60 \
-d "preview.thelounge.chat" \
-d "*.preview.thelounge.chat" \
--non-interactive \
--agree-tos \
--email admin@thelounge.chat
else
echo " TLS cert already exists, skipping"
fi
# --- Configure nginx ---
echo "==> Configuring nginx..."
# Ensure WebSocket upgrade map exists
if ! grep -q 'connection_upgrade' /etc/nginx/nginx.conf; then
sed -i '/http {/a \ map $http_upgrade $connection_upgrade {\n default upgrade;\n '\'''\'' close;\n }' /etc/nginx/nginx.conf
fi
# Ensure sites-enabled is included
if ! grep -q 'sites-enabled' /etc/nginx/nginx.conf; then
sed -i '/http {/a \ include /etc/nginx/sites-enabled/*;' /etc/nginx/nginx.conf
fi
cp "$INSTALL_DIR/nginx/preview.thelounge.chat.conf" /etc/nginx/sites-available/preview.thelounge.chat
ln -sf /etc/nginx/sites-available/preview.thelounge.chat /etc/nginx/sites-enabled/preview.thelounge.chat
nginx -t && systemctl reload nginx
# --- Write env file (secrets live here, not in the systemd unit) ---
echo "==> Writing environment file..."
ENV_FILE="/etc/preview-service.env"
cat > "$ENV_FILE" <<EOF
DOMAIN=preview.thelounge.chat
GITHUB_REPO=thelounge/thelounge
LISTEN_ADDR=:8080
DATA_DIR=${DATA_DIR}
BUILD_DIR=${BUILD_DIR}
ERGO_HOST=ergo
GITHUB_ORG=thelounge
INSTANCE_TTL=6h
GITHUB_CLIENT_ID=${PREVIEW_GITHUB_CLIENT_ID}
GITHUB_CLIENT_SECRET=${PREVIEW_GITHUB_CLIENT_SECRET}
EOF
chmod 600 "$ENV_FILE"
# --- Create systemd service ---
echo "==> Creating systemd service..."
cat > /etc/systemd/system/preview-service.service <<'EOF'
[Unit]
Description=TheLounge Preview Service
After=network.target docker.service
Requires=docker.service
[Service]
Type=simple
WorkingDirectory=/opt/preview-service
ExecStart=/usr/local/bin/preview-service
Restart=always
RestartSec=5
EnvironmentFile=/etc/preview-service.env
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now preview-service
echo ""
echo "==> Done!"
echo ""
echo " Dashboard: https://preview.thelounge.chat"
echo " Ergo IRC: ergo:6667 (Docker network)"
echo ""
echo " Make sure DNS records are set:"
echo " A preview.thelounge.chat → $(curl -s ifconfig.me)"
echo " A *.preview.thelounge.chat → $(curl -s ifconfig.me)"
echo ""
echo " Check status:"
echo " systemctl status preview-service"
echo " systemctl status nginx"
echo " docker compose -f $INSTALL_DIR/docker-compose.yml ps"