-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathstart-copilot-sandbox.sh
More file actions
executable file
·72 lines (60 loc) · 2.06 KB
/
start-copilot-sandbox.sh
File metadata and controls
executable file
·72 lines (60 loc) · 2.06 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
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
CONFIG_FILE="$SCRIPT_DIR/sandbox.json"
DOCKERFILE="$SCRIPT_DIR/Dockerfile.sandbox"
cd "$REPO_ROOT"
if [ ! -f "$CONFIG_FILE" ]; then
echo "Error: sandbox.json not found in $SCRIPT_DIR" >&2
exit 1
fi
# Read config from sandbox.json
IMAGE_NAME=$(python3 -c "import json; print(json.load(open('$CONFIG_FILE'))['image_name'])")
mapfile -t WATCH_FILES < <(python3 -c "
import json
for f in json.load(open('$CONFIG_FILE')).get('watch_files', []):
print(f)
")
WATCH_FILES+=("$DOCKERFILE")
needs_build() {
# Build if image doesn't exist
if ! docker image inspect "$IMAGE_NAME" &>/dev/null; then
echo "Image '$IMAGE_NAME' not found."
return 0
fi
# Get image creation timestamp
image_time=$(docker image inspect "$IMAGE_NAME" --format '{{.Created}}')
image_epoch=$(date -d "$image_time" +%s 2>/dev/null || date -jf "%Y-%m-%dT%H:%M:%S" "${image_time%%.*}" +%s 2>/dev/null)
# Check if any watched files are newer than the image
for f in "${WATCH_FILES[@]}"; do
if [ -f "$f" ]; then
file_epoch=$(stat -c %Y "$f" 2>/dev/null || stat -f %m "$f" 2>/dev/null)
if [ "$file_epoch" -gt "$image_epoch" ]; then
echo "Rebuild needed: '$f' is newer than image."
return 0
fi
fi
done
echo "Image '$IMAGE_NAME' is up to date."
return 1
}
REBUILT=false
if needs_build; then
echo "Building sandbox image..."
docker build -t "$IMAGE_NAME" -f "$DOCKERFILE" .
echo "Build complete."
REBUILT=true
fi
echo "Starting Copilot sandbox..."
# If sandbox already exists and image was rebuilt, remove the old sandbox
if [ "$REBUILT" = true ] && docker sandbox ls 2>/dev/null | grep -q "copilot"; then
echo "Removing old sandbox to apply new image..."
docker sandbox rm copilot 2>/dev/null || true
fi
# Use --template only when sandbox doesn't exist yet
if docker sandbox ls 2>/dev/null | grep -q "copilot"; then
exec docker sandbox run copilot
else
exec docker sandbox run --template "$IMAGE_NAME" copilot "$REPO_ROOT"
fi