Files
SHiNE-server/deploy/scripts/configure_shine_main_r2.sh
T

79 lines
2.7 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
REMOTE_HOST="${REMOTE_HOST:-player@t2.shineup.me}"
ssh "$REMOTE_HOST" 'python3 -' <<'PY'
from pathlib import Path
import shutil
t2 = Path('/home/player/t2/server/application.properties')
main = Path('/home/player/SHiNE/main/server/application.properties')
keys = ('r2.endpoint', 'r2.accessKeyId', 'r2.secretAccessKey', 'r2.publicBucket', 'r2.dmBucket')
source = {}
for line in t2.read_text().splitlines():
if '=' in line and not line.lstrip().startswith('#'):
k, v = line.split('=', 1)
source[k.strip()] = v.strip()
if any(not source.get(k) for k in keys):
raise SystemExit('t2 R2 settings incomplete; no change')
original = main.read_text()
if any(line.startswith('r2.endpoint=') and line.split('=', 1)[1].strip() for line in original.splitlines()):
raise SystemExit('Production R2 already configured; refusing to overwrite')
backup = main.with_name('application.properties.before-r2')
if backup.exists():
raise SystemExit('R2 backup already exists; no change')
shutil.copy2(main, backup)
updated = []
seen = set()
for line in original.splitlines():
key = line.split('=', 1)[0].strip()
if key in keys:
updated.append(key + '=' + source[key])
seen.add(key)
else:
updated.append(line)
for key in keys:
if key not in seen:
updated.append(key + '=' + source[key])
main.write_text('\n'.join(updated) + '\n')
main.chmod(0o600)
print('Production R2 settings copied from t2 without exposing credentials')
PY
ssh "$REMOTE_HOST" 'python3 -' <<'PY'
from pathlib import Path
import subprocess
path = Path('/etc/caddy/Caddyfile')
text = path.read_text()
old = ''' handle {
root * /home/player/SHiNE/main/UI
try_files {path} /index.html
file_server
}
}'''
new = ''' @backend path /status /api/status /ws /ws/* /api/files/user-server /api/files/prepare /api/dm-files/prepare /files/* /dm-files/*
handle @backend {
reverse_proxy 127.0.0.1:7105
}
handle {
root * /home/player/SHiNE/main/UI
try_files {path} {path}/index.html /index.html
file_server
}
}'''
if text.count(old) != 1:
raise SystemExit('Expected shineup.me Caddy block not found exactly once; no change')
backup = Path('/etc/caddy/Caddyfile.before-shine-main-r2')
if backup.exists():
raise SystemExit('Caddy backup already exists; no change')
subprocess.run(['sudo', '-n', 'cp', str(path), str(backup)], check=True)
tmp = Path('/tmp/shine-main-Caddyfile-r2')
tmp.write_text(text.replace(old, new))
subprocess.run(['sudo', '-n', 'install', '-o', 'player', '-g', 'player', '-m', '0664', str(tmp), str(path)], check=True)
tmp.unlink()
PY
ssh "$REMOTE_HOST" 'sudo -n systemctl reload caddy; sudo -n systemctl is-active --quiet caddy'