peppa/scripts/server-move/mac-route.sh

66 lines
3.6 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# PEP-230: адреса *.telecom.quest приходят на Mac (туннель Cloudflare → Traefik). Этот скрипт ставит
# SSH-туннель Mac → сервер (launchd quest.telecom.peppa.tunnel) и маршрут в Traefik:
# mac-route.sh trial — только пробное имя peppa-srv.telecom.quest (живой сайт не трогает)
# mac-route.sh final — peppa.telecom.quest и все неизвестные Mac адреса → сервер
# mac-route.sh off — убрать маршрут (откат: peppa.telecom.quest снова отвечает с Mac)
set -euo pipefail
MODE=${1:?trial|final|off}
SRV=${PEPPA_SERVER:-vibe42@82.200.135.122}
KEY=${PEPPA_SSH_KEY:-$HOME/.peppa/ssh/id_ed25519}
DYN=${PEPPA_TRAEFIK_DYNAMIC:-/Users/timur/Dev/_Zorro/_INFRA/traefik/dynamic}
PL=$HOME/Library/LaunchAgents/quest.telecom.peppa.tunnel.plist
if [ "$MODE" = off ]; then
rm -f "$DYN/peppa-server.yml"; [ -f "$DYN/peppa.yml.before-server" ] && mv "$DYN/peppa.yml.before-server" "$DYN/peppa.yml"
echo "маршрут на сервер убран"; exit 0
fi
cat > "$PL" <<P
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"><dict>
<key>Label</key><string>quest.telecom.peppa.tunnel</string>
<key>ProgramArguments</key><array>
<string>/usr/bin/ssh</string><string>-i</string><string>$KEY</string>
<string>-o</string><string>BatchMode=yes</string><string>-o</string><string>ExitOnForwardFailure=yes</string>
<string>-o</string><string>ServerAliveInterval=15</string><string>-o</string><string>ServerAliveCountMax=3</string>
<string>-N</string><string>-L</string><string>127.0.0.1:18880:127.0.0.1:8880</string><string>$SRV</string>
</array>
<key>RunAtLoad</key><true/><key>KeepAlive</key><true/><key>ThrottleInterval</key><integer>5</integer>
<key>StandardErrorPath</key><string>$HOME/.peppa/tunnel.log</string><key>StandardOutPath</key><string>$HOME/.peppa/tunnel.log</string>
</dict></plist>
P
pkill -f "18880:127.0.0.1:8880" 2>/dev/null || true
launchctl bootout "gui/$(id -u)/quest.telecom.peppa.tunnel" 2>/dev/null || true
launchctl bootstrap "gui/$(id -u)" "$PL"
for i in 1 2 3 4 5 6 7 8 9 10; do curl -s -m 3 -o /dev/null http://127.0.0.1:18880/ && break; sleep 1; done
if [ "$MODE" = trial ]; then RULE='Host(`peppa-srv.telecom.quest`)'; EXTRA=""; else
RULE='Host(`peppa.telecom.quest`) || Host(`peppa-srv.telecom.quest`)'
# Всё, для чего у Mac нет своего маршрута (продукты проектов, выкаченные уже на сервере), — на сервер.
EXTRA='
peppa-server-rest:
rule: "HostRegexp(`^.+\\.telecom\\.quest$`)"
service: peppa-server
entryPoints: [web]
priority: 1'
[ -f "$DYN/peppa.yml" ] && mv "$DYN/peppa.yml" "$DYN/peppa.yml.before-server"
fi
cat > "$DYN/peppa-server.yml" <<Y
# Peppa FACTORY работает на сервере (PEP-230). Запросы уходят туда по SSH-туннелю
# (launchd quest.telecom.peppa.tunnel: Mac 127.0.0.1:18880 → сервер 127.0.0.1:8880, Traefik Peppa).
# Откат: scripts/server-move/mac-route.sh off
http:
routers:
peppa-server:
rule: "$RULE"
service: peppa-server
entryPoints: [web]
priority: 100000$EXTRA
services:
peppa-server:
loadBalancer:
servers:
- url: "http://host.docker.internal:18880"
Y
sleep 4
curl -s -m 15 -o /dev/null -w "peppa-srv.telecom.quest → %{http_code}\n" https://peppa-srv.telecom.quest/api/health