Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·166 lines (141 loc) · 5.59 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·166 lines (141 loc) · 5.59 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/bash
set -e
# Linear Algebra Game Docker Deployment Script for Duke VM
# This script sets up and runs the Linear Algebra Game using Docker
echo "=== Linear Algebra Game Docker Deployment Script ==="
echo "VM: vcm-48491.vm.duke.edu"
echo
# Configuration
GAME_DIR="$HOME/LinearAlgebraGame"
GAME_PORT=8080
CONTAINER_NAME="linear-algebra-game"
SERVICE_NAME="linear-algebra-game-docker"
echo "1. Installing Docker if not present..."
if ! command -v docker &> /dev/null; then
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg lsb-release
# Add Docker's official GPG key
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
# Set up the repository
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install Docker Engine
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
# Add current user to docker group
sudo usermod -aG docker $USER
echo "Docker installed. You may need to log out and back in for group changes to take effect."
else
echo "Docker already installed"
fi
echo "2. Installing Git if not present..."
sudo apt-get install -y git
echo "3. Cloning/updating game repository..."
if [ -d "$GAME_DIR" ]; then
echo "Game directory exists, pulling latest changes..."
cd "$GAME_DIR"
git pull
else
echo "Cloning game repository..."
git clone https://github.com/ZRTMRH/LinearAlgebraGame.git "$GAME_DIR"
fi
cd "$GAME_DIR"
echo "4. Pulling lean4game Docker image..."
# Try pulling without sudo first (user might be in docker group)
if docker pull ghcr.io/leanprover-community/lean4game:latest 2>/dev/null; then
echo "Image pulled successfully"
else
echo "Trying with sudo..."
if sudo docker pull ghcr.io/leanprover-community/lean4game:latest 2>/dev/null; then
echo "Image pulled successfully with sudo"
else
echo "Failed to pull from ghcr.io, trying Docker Hub..."
# Try alternative Docker Hub image if it exists
if sudo docker pull leanprovercommunity/lean4game:latest 2>/dev/null; then
echo "Using Docker Hub image"
IMAGE_NAME="leanprovercommunity/lean4game:latest"
else
echo "Warning: Could not pull lean4game image. Will try to build locally."
echo "You may need to authenticate with GitHub Container Registry."
echo "Run: docker login ghcr.io -u YOUR_GITHUB_USERNAME"
echo "Then re-run this script."
exit 1
fi
fi
fi
# Set image name variable for later use
IMAGE_NAME="${IMAGE_NAME:-ghcr.io/leanprover-community/lean4game:latest}"
echo "5. Stopping any existing container..."
sudo docker stop $CONTAINER_NAME 2>/dev/null || true
sudo docker rm $CONTAINER_NAME 2>/dev/null || true
echo "6. Starting the game container..."
sudo docker run -d \
--name $CONTAINER_NAME \
--restart unless-stopped \
-p $GAME_PORT:8080 \
-v "$GAME_DIR":/game \
$IMAGE_NAME
echo "7. Creating systemd service for Docker container management..."
sudo tee /etc/systemd/system/${SERVICE_NAME}.service > /dev/null <<EOF
[Unit]
Description=Linear Algebra Game Docker Container
Requires=docker.service
After=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/docker start $CONTAINER_NAME
ExecStop=/usr/bin/docker stop $CONTAINER_NAME
ExecReload=/usr/bin/docker restart $CONTAINER_NAME
[Install]
WantedBy=multi-user.target
EOF
echo "8. Enabling the service..."
sudo systemctl daemon-reload
sudo systemctl enable ${SERVICE_NAME}
echo "9. Setting up auto-restart after VM power-on..."
# The Docker container with --restart unless-stopped will automatically restart
# when Docker daemon starts, but we'll add extra insurance
sudo tee /usr/local/bin/ensure-game-running.sh > /dev/null <<EOF
#!/bin/bash
# Wait for Docker to be ready
sleep 20
# Check if container exists
if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}\$"; then
# Start the container if it's not running
if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}\$"; then
docker start ${CONTAINER_NAME}
fi
else
# Recreate the container if it doesn't exist
cd $GAME_DIR
docker run -d \
--name $CONTAINER_NAME \
--restart unless-stopped \
-p $GAME_PORT:8080 \
-v "$GAME_DIR":/game \
ghcr.io/leanprover-community/lean4game:latest
fi
EOF
sudo chmod +x /usr/local/bin/ensure-game-running.sh
# Add to root's crontab for system startup
echo "@reboot /usr/local/bin/ensure-game-running.sh" | sudo crontab -
echo
echo "=== Deployment Complete ==="
echo "The Linear Algebra Game is now running on port $GAME_PORT"
echo "Access it at: http://vcm-48491.vm.duke.edu:$GAME_PORT"
echo
echo "Useful Docker commands:"
echo " sudo docker logs $CONTAINER_NAME # View logs"
echo " sudo docker restart $CONTAINER_NAME # Restart container"
echo " sudo docker stop $CONTAINER_NAME # Stop container"
echo " sudo docker start $CONTAINER_NAME # Start container"
echo " sudo docker ps # List running containers"
echo
echo "Service commands:"
echo " sudo systemctl status ${SERVICE_NAME} # Check service status"
echo " sudo systemctl restart ${SERVICE_NAME} # Restart via systemd"
echo
echo "Note: The VM powers down at 6 AM daily. The container will auto-restart when powered back on."