Harden tloto-deploy and document Gitea CI/CD runbook
Some checks failed
CI / test-and-build (push) Failing after 33s

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Your Name 2026-11-17 20:07:47 +02:00
parent 07ccda0349
commit e802ee23d5
8 changed files with 192 additions and 19 deletions

View File

@ -0,0 +1,31 @@
#!/usr/bin/env bash
# Archive one-shot bootstrap/diag scripts from ~ after backup. Keeps credentials and stacks.
set -euo pipefail
BACKUP="$HOME/backup-oneshots-$(date -u +%Y%m%dT%H%M%SZ).tar.gz"
cd "$HOME"
FILES=(
allow-docker-pg.sh
diag-pg-docker2.sh
fix-pg-for-docker.sh
fix-pg-docker.sh
remote-bootstrap-env-db.sh
remote-bootstrap-phase-a.sh
remote-migrate-and-up.sh
remote-reset-db-migrate-up.sh
tighten-pg-listen.sh
verify-harden.sh
)
EXIST=()
for f in "${FILES[@]}"; do
[[ -f "$f" ]] && EXIST+=("$f")
done
if [[ ${#EXIST[@]} -eq 0 ]]; then
echo "Nothing to clean"
exit 0
fi
tar -czf "$BACKUP" "${EXIST[@]}"
mkdir -p "$HOME/archive-oneshots"
mv "${EXIST[@]}" "$HOME/archive-oneshots/"
echo "Archived to $BACKUP and moved into ~/archive-oneshots/"
ls -la "$BACKUP"
echo "CLEANUP_OK"

View File

@ -0,0 +1,46 @@
#!/usr/bin/env bash
set -euo pipefail
echo "== pull app =="
cd ~/games_app
# use tokenized fetch
TOKEN="$(grep -A1 'Push token' ~/gitea-push-credentials.txt | tail -1 | tr -d ' \r')"
git remote set-url origin "http://tloto-admin:${TOKEN}@127.0.0.1:3000/tloto-admin/games_app.git"
git fetch origin main
git reset --hard origin/main
git remote set-url origin "http://127.0.0.1:3000/tloto-admin/games_app.git"
cp -a /tmp/games_app.env.bak .env 2>/dev/null || true
# ensure .env still present
test -f .env
chmod 600 .env
echo "== refresh deploy tool =="
bash deploy/release/install-deploy-tool.sh
echo "== ufw registry =="
sudo ufw allow from 172.16.0.0/12 to any port 5000 proto tcp || true
echo "== tloto-deploy dry path (current HEAD, skip migrate) =="
SHA="$(git rev-parse HEAD)"
/opt/tloto/bin/tloto-deploy --ref "$SHA" --mode http --skip-migrate
echo "== verify manual npm scripts =="
cd ~/games_app
sudo npm run phase-b:status || sudo docker compose ps
curl -fsS http://127.0.0.1:9080/health | head -c 100; echo
curl -fsS http://127.0.0.1:9080/api/v1/setup/status; echo
echo "== actions status =="
sudo -u postgres psql -d gitea -c "SELECT id, title, status, started, stopped FROM action_run ORDER BY id DESC LIMIT 5;"
sudo docker logs tloto_act_runner --tail 30
echo "== cleanup oneshots =="
bash ~/gitea/cleanup-oneshots.sh 2>/dev/null || true
echo "== edge (optional HTTP git.tloto.net host header) =="
cd ~/edge
# copy compose from repo if present
cp -a ~/games_app/deploy/edge/* ~/edge/ 2>/dev/null || true
sudo docker compose --profile edge up -d 2>/dev/null || echo "edge skip/fail non-fatal"
echo "FINALIZE_OK"

View File

@ -0,0 +1,30 @@
#!/usr/bin/env bash
set -euo pipefail
cp ~/games_app/deploy/release/tloto-deploy.sh /tmp/tloto-deploy.sh 2>/dev/null || true
# expect scripts already uploaded beside this file in ~/gitea
if [[ -f ~/gitea/tloto-deploy.sh ]]; then
cp ~/gitea/tloto-deploy.sh ~/games_app/deploy/release/tloto-deploy.sh
fi
sudo cp ~/games_app/deploy/release/tloto-deploy.sh /opt/tloto/bin/tloto-deploy
sudo chmod 755 /opt/tloto/bin/tloto-deploy
bash ~/gitea/write-fetch-url.sh
SHA="$(cd ~/games_app && git rev-parse HEAD)"
/opt/tloto/bin/tloto-deploy --ref "$SHA" --mode http --skip-migrate
echo "--- current.json ---"
cat /var/lib/tloto/current.json
echo
bash ~/gitea/cleanup-oneshots.sh
curl -fsS http://127.0.0.1:9080/health | head -c 100; echo
cd ~/games_app
sudo npm run db:migrate
echo MIGRATE_CMD_OK
sudo -u postgres psql -d gitea -c "SELECT id, left(title,40) AS title, status FROM action_run ORDER BY id DESC LIMIT 5;"
sudo docker logs tloto_act_runner --tail 40
# edge
mkdir -p ~/edge/certs
cp -a ~/games_app/deploy/edge/docker-compose.yml ~/edge/ 2>/dev/null || true
cp -a ~/games_app/deploy/edge/nginx.conf ~/edge/ 2>/dev/null || true
cd ~/edge && sudo docker compose --profile edge up -d || true
curl -fsS http://127.0.0.1:3000/api/v1/version; echo
echo FINALIZE2_OK

View File

@ -0,0 +1,17 @@
#!/usr/bin/env bash
set -euo pipefail
for i in $(seq 1 90); do
status="$(sudo -u postgres psql -d gitea -tAc 'SELECT status FROM action_run ORDER BY id DESC LIMIT 1')"
status="$(echo "$status" | tr -d '[:space:]')"
echo "poll $i status=$status"
# 3 success, 4 failure, 5 cancelled
if [[ "$status" == "3" || "$status" == "4" || "$status" == "5" ]]; then
sudo -u postgres psql -d gitea -c "SELECT id, left(title,50) AS title, status, started, stopped FROM action_run ORDER BY id DESC LIMIT 5;"
sudo docker logs tloto_act_runner --tail 50
exit 0
fi
sleep 10
done
echo "CI_TIMEOUT"
sudo docker logs tloto_act_runner --tail 80
exit 1

View File

@ -0,0 +1,6 @@
#!/usr/bin/env bash
set -euo pipefail
TOKEN="$(grep -A1 'Push token' "$HOME/gitea-push-credentials.txt" | tail -1 | tr -d ' \r')"
echo "http://tloto-admin:${TOKEN}@127.0.0.1:3000/tloto-admin/games_app.git" > "$HOME/.tloto-git-fetch-url"
chmod 600 "$HOME/.tloto-git-fetch-url"
echo "FETCH_URL_OK"

View File

@ -29,7 +29,7 @@ if ! grep -q "$MARKER" "$AUTH" 2>/dev/null; then
fi
chmod 600 "$AUTH"
cat > /opt/tloto/bin/tloto-deploy-ssh-wrapper <<'WRAP'
sudo tee /opt/tloto/bin/tloto-deploy-ssh-wrapper >/dev/null <<'WRAP'
#!/usr/bin/env bash
set -euo pipefail
# SSH forced command: only allow tloto-deploy with safe args from SSH_ORIGINAL_COMMAND
@ -44,13 +44,15 @@ if [[ ! "$cmd" =~ ^/opt/tloto/bin/tloto-deploy([[:space:]]|$) ]]; then
exit 1
fi
# Disallow shell metacharacters
if [[ "$cmd" =~ [;&|<>`$()] ]]; then
if [[ "$cmd" =~ [\;\&\|\<\>\`\$\(\)] ]]; then
echo "refusing unsafe characters" >&2
exit 1
fi
exec bash -lc "$cmd"
WRAP
sudo chmod 755 /opt/tloto/bin/tloto-deploy-ssh-wrapper
# Convenience symlink for interactive deploy user sessions
sudo ln -sfn /opt/tloto/bin/tloto-deploy /usr/local/bin/tloto-deploy
echo "Installed /opt/tloto/bin/tloto-deploy"
echo "CI private key: $KEY_DIR/id_ed25519 (add as Gitea secret DEPLOY_SSH_KEY)"

View File

@ -90,38 +90,65 @@ doc = {
Path("$path").write_text(json.dumps(doc, indent=2) + "\n")
if "$status" == "success":
Path("$CURRENT_FILE").write_text(json.dumps(doc, indent=2) + "\n")
print(path)
print("$path")
PY
}
REGISTRY="${TLOTO_REGISTRY:-127.0.0.1:5000}"
run_db_migrate() {
# Preserve documented command when Node is installed; else equivalent docker compose.
if command -v npm >/dev/null 2>&1; then
sudo npm run db:migrate
else
sudo env TLOTO_IMAGE_TAG="${TLOTO_IMAGE_TAG:-local}" TLOTO_REGISTRY="$REGISTRY" \
docker compose run --rm --no-deps --build backend node dist/db/migrate.js
fi
}
compose_up() {
local sha_tag="${TLOTO_IMAGE_TAG:-local}"
export TLOTO_IMAGE_TAG="$sha_tag"
export TLOTO_REGISTRY="$REGISTRY"
# Prefer pre-built registry images when present (CI); else build via existing npm scripts.
# Prefer pre-built registry images when present (CI); else build via npm or docker compose.
if [[ "$sha_tag" != "local" ]] \
&& sudo docker image inspect "${REGISTRY}/tloto-backend:${sha_tag}" >/dev/null 2>&1 \
&& sudo docker image inspect "${REGISTRY}/tloto-web:${sha_tag}" >/dev/null 2>&1; then
log "using registry images tag=$sha_tag"
if [[ "$MODE" == "https" ]]; then
sudo -E docker compose --profile https-dns --profile clamav up -d
sudo env TLOTO_IMAGE_TAG="$sha_tag" TLOTO_REGISTRY="$REGISTRY" \
docker compose --profile https-dns --profile clamav up -d
sudo docker restart vps_gateway_nginx >/dev/null 2>&1 || true
else
sudo -E docker compose --profile http --profile clamav up -d
sudo env TLOTO_IMAGE_TAG="$sha_tag" TLOTO_REGISTRY="$REGISTRY" \
docker compose --profile http --profile clamav up -d
sudo docker restart vps_gateway_nginx >/dev/null 2>&1 || true
fi
else
log "building via npm phase-b:up* (tag=$sha_tag)"
if [[ "$MODE" == "https" ]]; then
sudo -E npm run phase-b:up:https
log "building via phase-b:up* equivalent (tag=$sha_tag)"
if command -v npm >/dev/null 2>&1; then
if [[ "$MODE" == "https" ]]; then
sudo env TLOTO_IMAGE_TAG="$sha_tag" TLOTO_REGISTRY="$REGISTRY" npm run phase-b:up:https
else
sudo env TLOTO_IMAGE_TAG="$sha_tag" TLOTO_REGISTRY="$REGISTRY" npm run phase-b:up
fi
else
sudo -E npm run phase-b:up
if [[ "$MODE" == "https" ]]; then
sudo env TLOTO_IMAGE_TAG="$sha_tag" TLOTO_REGISTRY="$REGISTRY" \
docker compose --profile https-dns --profile clamav up -d --build
else
sudo env TLOTO_IMAGE_TAG="$sha_tag" TLOTO_REGISTRY="$REGISTRY" \
docker compose --profile http --profile clamav up -d --build
fi
sudo docker restart vps_gateway_nginx >/dev/null 2>&1 || true
fi
# Record tags for future rollback
sudo docker tag "games_app-backend:latest" "${REGISTRY}/tloto-backend:${sha_tag}" 2>/dev/null || true
sudo docker tag "games_app-prediqo-web:latest" "${REGISTRY}/tloto-web:${sha_tag}" 2>/dev/null || true
sudo docker tag "${REGISTRY}/tloto-backend:local" "${REGISTRY}/tloto-backend:${sha_tag}" 2>/dev/null \
|| sudo docker tag "games_app-backend:latest" "${REGISTRY}/tloto-backend:${sha_tag}" 2>/dev/null \
|| true
sudo docker tag "${REGISTRY}/tloto-web:local" "${REGISTRY}/tloto-web:${sha_tag}" 2>/dev/null \
|| sudo docker tag "games_app-prediqo-web:latest" "${REGISTRY}/tloto-web:${sha_tag}" 2>/dev/null \
|| true
sudo docker push "${REGISTRY}/tloto-backend:${sha_tag}" 2>/dev/null || true
sudo docker push "${REGISTRY}/tloto-web:${sha_tag}" 2>/dev/null || true
fi
@ -174,16 +201,25 @@ checkout_ref() {
if [[ ! -d .git ]]; then
die "$APP_DIR is not a git checkout — clone from Gitea first"
fi
git fetch --all --tags --prune
git checkout --force "$ref"
git reset --hard "$ref"
# Prefer local object; otherwise fetch (optional token file for HTTP origin)
if ! git cat-file -e "${ref}^{commit}" 2>/dev/null; then
if [[ -f "${HOME}/.tloto-git-fetch-url" ]]; then
local url
url="$(cat "${HOME}/.tloto-git-fetch-url")"
git fetch --prune "$url" '+refs/heads/*:refs/remotes/origin/*' '+refs/tags/*:refs/tags/*' >/dev/null
else
git fetch --all --tags --prune >/dev/null 2>&1 || true
fi
fi
git checkout --force -q "$ref"
git reset --hard -q "$ref"
git rev-parse HEAD
}
export_image_tags() {
local sha="$1"
# Optional override for compose image tags (compose-image-tags support)
export TLOTO_IMAGE_TAG="$sha"
# Short tag for registries; full sha stays in release JSON
export TLOTO_IMAGE_TAG="${sha:0:12}"
export COMPOSE_DOCKER_CLI_BUILD=1
}
@ -208,8 +244,8 @@ deploy_sha() {
if [[ ! -f "$marker" ]] || [[ "$(cat "$marker")" != "$now" ]]; then
backup_db >/dev/null
fi
log "npm run db:migrate"
sudo npm run db:migrate
log "db:migrate"
run_db_migrate
echo "$now" > "$marker"
else
log "skip migrate"

View File

@ -686,6 +686,11 @@ sudo docker compose run --rm --no-deps --build backend \
Ensuite seulement : créer un user de test → Dépôt → micro USDT **BEP20** (priorité) → (plus tard) financer les hots puis activer les retraits dans Admin Settings (réseau + kill-switch global).
### Gitea / CI/CD (nouveau VPS)
Voir le runbook dédié : [GITEA-CICD.md](./GITEA-CICD.md) — dépôt Gitea, Actions (CI), mécanisme unique `tloto-deploy`, pgAdmin via tunnel SSH.
Les commandes `npm run db:migrate` et `npm run phase-b:up:https` restent le cœur du déploiement manuel et sont appelées (ou équivalentes Docker) par `tloto-deploy`.
### Sur le VPS
À la racine du projet (`games_app/`) :