-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage.sh
More file actions
executable file
·104 lines (89 loc) · 2.89 KB
/
manage.sh
File metadata and controls
executable file
·104 lines (89 loc) · 2.89 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
#!/usr/bin/env bash
# manage.sh - Manage the Codex-Z.ai Proxy container
set -euo pipefail
IMAGE_NAME="codex-zai-proxy"
CONTAINER_NAME="codex-zai-proxy"
PROXY_PORT=4891
PROJECT_DIR="$(cd "$(dirname "$0")" && pwd)"
ENV_FILE="${PROJECT_DIR}/.env"
_usage() {
cat <<EOF
Usage: $(basename "$0") {build|start|stop|restart|logs|status|shell|rebuild}
Commands:
build Build the container image
start Start the proxy container
stop Stop and remove the proxy container
restart Stop then start
logs Follow container logs
status Show container status
shell Open a shell in the container
rebuild Rebuild and restart
test Run a quick health check
EOF
}
_build() {
echo "Building ${IMAGE_NAME}..."
podman build -t "${IMAGE_NAME}" "${PROJECT_DIR}"
echo "Build complete."
}
_start() {
if podman ps -q --filter "name=${CONTAINER_NAME}" | grep -q .; then
echo "Container ${CONTAINER_NAME} is already running."
return 0
fi
if [ ! -f "${ENV_FILE}" ]; then
echo "ERROR: ${ENV_FILE} not found. Copy .env.example to .env and set ZAI_API_KEY."
exit 1
fi
# Check if key is set
if grep -q '^ZAI_API_KEY=$' "${ENV_FILE}" 2>/dev/null || grep -q '^ZAI_API_KEY=""' "${ENV_FILE}" 2>/dev/null; then
echo "WARNING: ZAI_API_KEY appears empty in .env"
fi
echo "Starting ${CONTAINER_NAME} on 127.0.0.1:${PROXY_PORT}..."
podman run -d \
--name "${CONTAINER_NAME}" \
--env-file "${ENV_FILE}" \
--publish "127.0.0.1:${PROXY_PORT}:${PROXY_PORT}" \
--restart unless-stopped \
--security-opt no-new-privileges \
--cap-drop ALL \
--read-only \
--tmpfs /tmp:rw,noexec,nosuid,size=64m \
"${IMAGE_NAME}"
echo "Started. Health: http://127.0.0.1:${PROXY_PORT}/health"
}
_stop() {
if podman ps -aq --filter "name=${CONTAINER_NAME}" | grep -q .; then
echo "Stopping ${CONTAINER_NAME}..."
podman rm -f "${CONTAINER_NAME}" 2>/dev/null || true
echo "Stopped."
else
echo "Container ${CONTAINER_NAME} is not running."
fi
}
_logs() {
podman logs -f "${CONTAINER_NAME}" 2>/dev/null || echo "No logs (container not found)"
}
_status() {
podman ps -a --filter "name=${CONTAINER_NAME}" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" 2>/dev/null
}
_shell() {
podman exec -it "${CONTAINER_NAME}" /bin/bash 2>/dev/null || echo "Container not running"
}
_test() {
echo "Testing proxy health..."
curl -s "http://127.0.0.1:${PROXY_PORT}/health" | python3 -m json.tool 2>/dev/null || \
echo "FAILED: Proxy not responding on 127.0.0.1:${PROXY_PORT}"
}
case "${1:-}" in
build) _build ;;
start) _start ;;
stop) _stop ;;
restart) _stop; _start ;;
logs) _logs ;;
status) _status ;;
shell) _shell ;;
rebuild) _stop; _build; _start ;;
test) _test ;;
*) _usage ;;
esac