Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,52 @@ set -e
# We do not generate /certs/cert.pem file, as for HaProxy it is admin task to mount generated cert if needed.
# ----------------------------------------------------------------------------

HP_WAIT_AGENT_HTTP=${HP_WAIT_AGENT_HTTP:-60}
HP_WAIT_SPOA=${HP_WAIT_SPOA:-60}
HP_WAIT_FRP=${HP_WAIT_FRP:-30}
HP_WAIT_INTERVAL=${HP_WAIT_INTERVAL:-0.5}

HP_VERBOSE_START=${HP_VERBOSE_START:-1}
log() {
if [ "$HP_VERBOSE_START" -eq 1 ]; then
echo "$@"
fi
}

wait_for_tcp() {
# $1 host, $2 port, $3 timeout(s), $4 interval(s)
host="$1"; port="$2"; timeout="${3:-30}"; interval="${4:-0.5}"
start_ts="$(date +%s)"
while :; do
if nc -z -w 1 "$host" "$port" >/dev/null 2>&1; then
return 0
fi
now="$(date +%s)"; elapsed=$(( now - start_ts ))
if [ "$elapsed" -ge "$timeout" ]; then
echo "ERROR: Timeout waiting for TCP $host:$port to become ready (after ${timeout}s)."
return 1
fi
sleep "$interval"
done
}

wait_for_http() {
# $1 url, $2 timeout(s), $3 interval(s)
url="$1"; timeout="${2:-60}"; interval="${3:-0.5}"
start_ts="$(date +%s)"
while :; do
if curl -fsS --max-time 2 "$url" >/dev/null 2>&1; then
return 0
fi
now="$(date +%s)"; elapsed=$(( now - start_ts ))
if [ "$elapsed" -ge "$timeout" ]; then
echo "ERROR: Timeout waiting for HTTP $url to become ready (after ${timeout}s)."
return 1
fi
sleep "$interval"
done
}

# Check if the required environment variables are set
if [ -z "$HP_FRP_ADDRESS" ]; then
echo "ERROR: HP_FRP_ADDRESS is not set."
Expand Down Expand Up @@ -306,12 +345,21 @@ fi
log "INFO: Starting Python HaProxy Agent on 127.0.0.1:8200 and 127.0.0.1:9600..."
nohup python3 /usr/local/bin/haproxy_agent.py &

sleep 1s
# Wait deterministically for the agent to be ready (HTTP) and for SPOA (TCP)
log "INFO: Waiting for HaRP Agent HTTP (GET http://127.0.0.1:8200/info) to be ready..."
wait_for_http "http://127.0.0.1:8200/info" "$HP_WAIT_AGENT_HTTP" "$HP_WAIT_INTERVAL"

log "INFO: Waiting for SPOA port 127.0.0.1:9600..."
wait_for_tcp "127.0.0.1" "9600" "$HP_WAIT_SPOA" "$HP_WAIT_INTERVAL"

log "INFO: Starting FRP server on ${HP_FRP_ADDRESS}..."
frps -c /frps.toml &

sleep 1s
# Wait for FRP port to be listening before starting frpc
LOCAL_FRP_HOST="$FRP_HOST"
[ "$LOCAL_FRP_HOST" = "0.0.0.0" ] && LOCAL_FRP_HOST="127.0.0.1"
log "INFO: Waiting for FRP server port ${LOCAL_FRP_HOST}:${FRP_PORT}..."
wait_for_tcp "$LOCAL_FRP_HOST" "$FRP_PORT" "$HP_WAIT_FRP" "$HP_WAIT_INTERVAL"

if [ -e "/var/run/docker.sock" ]; then
log "INFO: Starting FRP client for Docker Engine..."
Expand Down