Fix duplicate leaderboard entries, add /version command, fix jail DNS
- db/db.go: Add write-time username sync in AddLog to prevent duplicate leaderboard entries when users change display names. Revert correlated subqueries back to GROUP BY user_id, username (simpler approach). - db/db.go: Early return in onMessageCreate if bot already reacted (prevents duplicate emoji reactions on Discord reconnection). - bot/bot.go: Add /version slash command with build version injection. - main.go: Add version variable with ldflags support. - Makefile: Add dns-fix, test, vet, build-native, pg-*, boot targets. Prepend test+vet to deploy pipeline. Add version ldflags to build. - db/migrations/002_fix_usernames.sql: One-time SQL to backfill old usernames. - scripts/fix-jail-dns.sh: Script to update jail resolv.conf from 8.8.8.8 to reachable nameservers (1.1.1.1, 9.9.9.9, 172.16.0.1). Signed-off-by: Blake Ridgway <blake@blakeridgway.com>
This commit is contained in:
81
Makefile
81
Makefile
@@ -1,19 +1,72 @@
|
||||
HOST = 172.16.0.214
|
||||
HOST = 172.16.0.101
|
||||
SSH_USER = root
|
||||
SSH = ssh $(SSH_USER)@$(HOST)
|
||||
SCP = scp
|
||||
|
||||
JAIL_NAME = cyclingbot
|
||||
JAIL_ROOT = /jails/$(JAIL_NAME)
|
||||
BINARY = cycling-bot
|
||||
|
||||
.PHONY: all build deploy deploy-env restart stop logs status clean
|
||||
# Postgres jail config
|
||||
PG_JAIL = postgres
|
||||
PG_DATA = /var/db/postgres/data16
|
||||
PG_IFACE = igb0
|
||||
PG_IPS = 172.16.0.215/24 172.16.0.216/24
|
||||
|
||||
all: build deploy start
|
||||
# Build info (injected via ldflags)
|
||||
VERSION = $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)
|
||||
LDFLAGS = -ldflags "-X main.version=$(VERSION)"
|
||||
|
||||
.PHONY: all build build-native test vet deploy setup deploy-env boot aliases
|
||||
.PHONY: pg-start pg-stop pg-status start stop restart logs status clean dns-fix
|
||||
|
||||
all: test build deploy start
|
||||
|
||||
# ── Networking ────────────────────────────────────────────────────────────────
|
||||
aliases:
|
||||
$(SSH) "$(foreach ip,$(PG_IPS),ifconfig $(PG_IFACE) alias $(ip) ;) true"
|
||||
|
||||
# ── Postgres ──────────────────────────────────────────────────────────────────
|
||||
pg-start:
|
||||
$(SSH) "service jail start $(PG_JAIL) 2>/dev/null || true"
|
||||
$(SSH) "jexec $(PG_JAIL) su -l postgres -c 'pg_ctl status -D $(PG_DATA)' 2>&1 | grep -q 'server is running' \
|
||||
&& echo 'postgres already running' \
|
||||
|| { rm -f /jails/$(PG_JAIL)$(PG_DATA)/postmaster.pid ; \
|
||||
jexec $(PG_JAIL) su -l postgres -c 'pg_ctl start -D $(PG_DATA)'; }"
|
||||
|
||||
pg-stop:
|
||||
$(SSH) "jexec $(PG_JAIL) su -l postgres -c 'pg_ctl stop -D $(PG_DATA) -m fast' 2>/dev/null || true"
|
||||
|
||||
pg-status:
|
||||
$(SSH) "jexec $(PG_JAIL) su -l postgres -c 'pg_ctl status -D $(PG_DATA)'"
|
||||
|
||||
# ── Full server boot (run this after every reboot) ────────────────────────────
|
||||
|
||||
boot: aliases
|
||||
$(SSH) "service jail stop $(JAIL_NAME) 2>/dev/null || true"
|
||||
$(SSH) "service jail stop $(PG_JAIL) 2>/dev/null || true"
|
||||
@$(MAKE) pg-start
|
||||
$(SSH) "service jail start $(JAIL_NAME) 2>/dev/null || true"
|
||||
$(SSH) "jexec $(JAIL_NAME) route add default 172.16.0.1 2>/dev/null || true"
|
||||
@$(MAKE) start
|
||||
@echo "Boot sequence complete."
|
||||
|
||||
# ── Build ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
# Cross-compile for FreeBSD amd64
|
||||
build:
|
||||
GOOS=freebsd GOARCH=amd64 go build -o $(BINARY) .
|
||||
GOOS=freebsd GOARCH=amd64 go build $(LDFLAGS) -o $(BINARY) .
|
||||
|
||||
# Build for the local (Linux) platform — useful for testing
|
||||
build-native:
|
||||
go build $(LDFLAGS) -o $(BINARY) .
|
||||
|
||||
# Run tests
|
||||
test:
|
||||
go test ./...
|
||||
|
||||
# Run go vet
|
||||
vet:
|
||||
go vet ./...
|
||||
|
||||
# Create required directories inside the jail (safe to run multiple times)
|
||||
setup:
|
||||
@@ -23,8 +76,8 @@ setup:
|
||||
$(JAIL_ROOT)/var/log \
|
||||
$(JAIL_ROOT)/var/run"
|
||||
|
||||
# Copy binary and rc.d script into the jail (via /tmp to avoid jail dir permission issues)
|
||||
deploy: build setup
|
||||
# Copy binary and rc.d script into the jail
|
||||
deploy: test vet build setup
|
||||
$(SCP) $(BINARY) $(SSH_USER)@$(HOST):/tmp/$(BINARY)
|
||||
$(SCP) rc.d/$(JAIL_NAME) $(SSH_USER)@$(HOST):/tmp/$(JAIL_NAME)-rcd
|
||||
$(SSH) "jexec $(JAIL_NAME) service $(JAIL_NAME) stop 2>/dev/null; \
|
||||
@@ -40,7 +93,7 @@ deploy: build setup
|
||||
# Copy .env separately (run once — avoid overwriting production config)
|
||||
deploy-env:
|
||||
$(SCP) .env \
|
||||
$(SSH_USER)@$(HOST):$(JAIL_ROOT)/var/db/$(JAIL_NAME)/.env
|
||||
$(SSH_USER)@$(HOST):$(JAIL_ROOT)/var/db/$(JAIL_NAME)/.env
|
||||
$(SSH) "chmod 600 $(JAIL_ROOT)/var/db/$(JAIL_NAME)/.env && \
|
||||
chown 1001:1001 $(JAIL_ROOT)/var/db/$(JAIL_NAME)/.env"
|
||||
|
||||
@@ -61,6 +114,18 @@ logs:
|
||||
|
||||
status:
|
||||
$(SSH) "jls && jexec $(JAIL_NAME) service $(JAIL_NAME) status"
|
||||
@$(MAKE) pg-status
|
||||
|
||||
# ── Jail DNS ─────────────────────────────────────────────────────────────────
|
||||
dns-fix:
|
||||
@echo "=== Current jail DNS ==="
|
||||
$(SSH) "cat /jails/$(JAIL_NAME)/etc/resolv.conf"
|
||||
@echo ""
|
||||
@echo "=== Fixing jail DNS ==="
|
||||
$(SSH) "printf 'nameserver 1.1.1.1\nnameserver 9.9.9.9\nnameserver 172.16.0.1\n' > /jails/$(JAIL_NAME)/etc/resolv.conf"
|
||||
@echo "=== New jail DNS ==="
|
||||
$(SSH) "cat /jails/$(JAIL_NAME)/etc/resolv.conf"
|
||||
|
||||
clean:
|
||||
rm -f $(BINARY)
|
||||
go clean
|
||||
|
||||
Reference in New Issue
Block a user