diff options
Diffstat (limited to 'cmd/ship')
| -rw-r--r-- | cmd/ship/deploy_impl_v2.go | 10 |
1 files changed, 2 insertions, 8 deletions
diff --git a/cmd/ship/deploy_impl_v2.go b/cmd/ship/deploy_impl_v2.go index 5b68dc3..ec5c4a3 100644 --- a/cmd/ship/deploy_impl_v2.go +++ b/cmd/ship/deploy_impl_v2.go | |||
| @@ -308,14 +308,8 @@ func allocatePort(client *ssh.Client, name string) (int, error) { | |||
| 308 | } | 308 | } |
| 309 | 309 | ||
| 310 | // Allocate new port atomically using flock | 310 | // Allocate new port atomically using flock |
| 311 | // This reads next_port, increments it, and writes back while holding a lock | 311 | // Scans existing port files to avoid collisions even if next_port is stale |
| 312 | allocScript := ` | 312 | allocScript := `flock -x /etc/ship/.port.lock sh -c 'mkdir -p /etc/ship/ports; NEXT=$(cat /etc/ship/next_port 2>/dev/null || echo 9000); MAX=8999; for f in /etc/ship/ports/*; do [ -f "$f" ] && P=$(cat "$f" 2>/dev/null) && [ "$P" -gt "$MAX" ] 2>/dev/null && MAX=$P; done; PORT=$(( NEXT > MAX ? NEXT : MAX + 1 )); echo $((PORT + 1)) > /etc/ship/next_port; echo $PORT'` |
| 313 | flock -x /etc/ship/.port.lock sh -c ' | ||
| 314 | mkdir -p /etc/ship/ports | ||
| 315 | PORT=$(cat /etc/ship/next_port 2>/dev/null || echo 9000) | ||
| 316 | echo $((PORT + 1)) > /etc/ship/next_port | ||
| 317 | echo $PORT | ||
| 318 | '` | ||
| 319 | out, err = client.RunSudo(allocScript) | 313 | out, err = client.RunSudo(allocScript) |
| 320 | if err != nil { | 314 | if err != nil { |
| 321 | return 0, fmt.Errorf("failed to allocate port: %w", err) | 315 | return 0, fmt.Errorf("failed to allocate port: %w", err) |
