-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathman-up.sh
More file actions
executable file
·120 lines (98 loc) · 3.33 KB
/
Copy pathman-up.sh
File metadata and controls
executable file
·120 lines (98 loc) · 3.33 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
#!/usr/bin/env bash
set -euo pipefail
# Bring the SageMath container *up* (man-up, man pages…).
# Default behavior: start detached, print a Windows-friendly URL, then follow logs.
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
COMPOSE_FILE="${SCRIPT_DIR}/podman-compose.yml"
CONTAINER_NAME="${CONTAINER_NAME:-sagemath}"
PORT="${PORT:-8888}"
SAGE_TAG="${SAGE_TAG:-10.7}"
IMAGE="${IMAGE:-localhost/sagequeue-sagemath:${SAGE_TAG}-pycryptosat}"
BUILD_IMAGE="${BUILD_IMAGE:-${SCRIPT_DIR}/bin/build-image.sh}"
usage() {
cat <<'USAGE'
Usage: ./man-up.sh [--follow] [--open]
--no-follow Start the stack but do not follow logs (returns immediately).
--open Try to open http://localhost:8888 in the default Windows browser.
Environment:
CONTAINER_NAME default: sagemath
PORT default: 8888
USAGE
}
FOLLOW=0
OPEN=0
while [[ $# -gt 0 ]]; do
case "$1" in
--follow|-f) FOLLOW=1 ;;
--open|-o) OPEN=1 ;;
--help|-h) usage; exit 0 ;;
*)
echo "Unknown option: $1" >&2
usage >&2
exit 2
;;
esac
shift
done
require_cmd() {
command -v "$1" >/dev/null 2>&1 || {
echo "Missing required command: $1" >&2
exit 127
}
}
require_cmd podman
# Prefer the repo-local venv podman-compose if it exists (so you don't have to 'source bin/activate').
if [[ -x "${SCRIPT_DIR}/.venv/bin/podman-compose" ]]; then
PODMAN_COMPOSE="${SCRIPT_DIR}/.venv/bin/podman-compose"
else
require_cmd podman-compose
PODMAN_COMPOSE="podman-compose"
fi
if [[ ! -f "${COMPOSE_FILE}" ]]; then
echo "Compose file not found: ${COMPOSE_FILE}" >&2
exit 1
fi
# Sanity check: rootless podman must be able to create /run/user/$UID/... on WSL2.
if ! podman ps >/dev/null 2>&1; then
echo "podman is not usable as this user." >&2
echo "If you're on WSL2 and see /run/user/<uid> permission errors," >&2
echo "enable systemd in /etc/wsl.conf ([boot] systemd=true) and restart WSL." >&2
podman ps || true
exit 1
fi
# Create bind-mount directories expected by podman-compose.yml (safe if they already exist).
mkdir -p "$HOME/Jupyter" "$HOME/.jupyter" "$HOME/.sagequeue-dot_sage" "$HOME/.sagequeue-local" "$HOME/.sagequeue-config" "$HOME/.sagequeue-cache"
# Ensure the local Sage image exists before compose can try to pull localhost/...
if ! podman image exists "$IMAGE"; then
if [[ ! -x "$BUILD_IMAGE" ]]; then
echo "Missing executable image builder: $BUILD_IMAGE" >&2
exit 2
fi
"$BUILD_IMAGE" --sage-tag "$SAGE_TAG"
fi
# Start / update the stack.
"$PODMAN_COMPOSE" -f "${COMPOSE_FILE}" up -d
echo
# Best-effort token extraction (wait up to ~30s for Jupyter to print it)
TOKEN=""
for _ in {1..30}; do
TOKEN="$(podman logs --tail 2000 "${CONTAINER_NAME}" 2>&1 \
| grep -Eo 'token=[0-9a-f]+' \
| tail -n 1 || true)"
[[ -n "${TOKEN}" ]] && break
sleep 1
done
if [[ -n "${TOKEN}" ]]; then
echo "Token (if needed): ${TOKEN}"
echo "URL (with token): http://localhost:${PORT}/tree?${TOKEN}"
else
echo "Token (if needed): podman logs --tail 2000 ${CONTAINER_NAME} 2>&1 | grep -Eo 'token=[0-9a-f]+' | tail -n 1"
fi
if [[ "${OPEN}" == "1" ]]; then
if command -v powershell.exe >/dev/null 2>&1; then
powershell.exe -NoProfile -Command "Start-Process 'http://localhost:${PORT}'" >/dev/null 2>&1 || true
fi
fi
if [[ "${FOLLOW}" == "1" ]]; then
exec podman logs -f "${CONTAINER_NAME}"
fi