51 lines
1.1 KiB
Bash
51 lines
1.1 KiB
Bash
|
|
#!/bin/sh
|
||
|
|
set -eu
|
||
|
|
|
||
|
|
# Port allocation for zero-downtime deployments
|
||
|
|
#
|
||
|
|
# Reads remote state to determine next available port.
|
||
|
|
# State files:
|
||
|
|
# $base/$project/base_port - starting port number
|
||
|
|
# $base/$project/releases/*/assigned_port - port for each release
|
||
|
|
#
|
||
|
|
# Algorithm:
|
||
|
|
# 1. read base_port (default 3100)
|
||
|
|
# 2. find all assigned_port files in releases/
|
||
|
|
# 3. return smallest unused port >= base_port
|
||
|
|
|
||
|
|
ssh=deploy-peoplesgrocers-website
|
||
|
|
base=/home/peoplesgrocers
|
||
|
|
project=salience
|
||
|
|
|
||
|
|
ssh $ssh "
|
||
|
|
set -eu
|
||
|
|
|
||
|
|
base=$base
|
||
|
|
project=$project
|
||
|
|
|
||
|
|
test -d \$base/\$project || { echo 'project dir does not exist' >&2; exit 1; }
|
||
|
|
|
||
|
|
# read or initialize base_port
|
||
|
|
if test -f \$base/\$project/base_port; then
|
||
|
|
base_port=\$(cat \$base/\$project/base_port)
|
||
|
|
else
|
||
|
|
base_port=3100
|
||
|
|
echo \$base_port > \$base/\$project/base_port
|
||
|
|
fi
|
||
|
|
|
||
|
|
# find all assigned ports
|
||
|
|
assigned=\$(find \$base/\$project/releases -name assigned_port -exec cat {} \; 2>/dev/null | sort -n || true)
|
||
|
|
|
||
|
|
# find first unused port
|
||
|
|
port=\$base_port
|
||
|
|
while true; do
|
||
|
|
found=0
|
||
|
|
for p in \$assigned; do
|
||
|
|
test \$p -eq \$port && { found=1; break; }
|
||
|
|
done
|
||
|
|
test \$found -eq 0 && break
|
||
|
|
port=\$((port + 1))
|
||
|
|
done
|
||
|
|
|
||
|
|
echo \$port
|
||
|
|
"
|