summaryrefslogtreecommitdiff
path: root/setup-and-run.sh
blob: 2980d3be94911dee3754066f1dd5a38ab6f71b11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash

# Setup and run script for the remote server

set -euo pipefail

if [ $# -lt 2 ]; then
    echo "Usage: $0 <name> <port>"
    exit 1
fi

NAME="$1"
PORT="$2"

# Validate port range
if [ "$PORT" -lt 8001 ] || [ "$PORT" -gt 8003 ]; then
    echo "Error: PORT must be between 8001 and 8003 (inclusive)"
    exit 1
fi

cd ~/hackathon/"$PORT"

# Ensure ~/.local/bin is in PATH
export PATH="$HOME/.local/bin:$PATH"

# Check if uv is installed, if not install it
if ! command -v uv &> /dev/null; then
    echo "Installing uv..."
    curl -Ls https://astral.sh/uv/install.sh | sh
fi

# Check if make is installed, if not install it
if ! command -v make &> /dev/null; then
    echo "Installing make..."
    sudo apt install make -y
fi

# Kill any process using the specified port
echo "Unregistering and killing bod running on port $PORT..."
fuser -k -TERM "${PORT}/tcp" >/dev/null 2>&1 || true

# Wait until all processes on the port are terminated
echo "Waiting for port $PORT to be released..."
MAX_RETRIES=30
RETRY_COUNT=0
while lsof -Pi :"${PORT}" -sTCP:LISTEN | grep -q LISTEN && [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
    sleep 2
    RETRY_COUNT=$((RETRY_COUNT + 1))
done

if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then
    echo "Error: Port $PORT is still in use after ${MAX_RETRIES} attempts"
    exit 1
fi

# Run the code in the background so script can exit
echo "Starting bot $NAME on port $PORT..."
nohup make run PORT="$PORT" IDENTITY_KEY="$NAME" > server.log 2>&1 &
echo "Bot started!"