-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·288 lines (227 loc) · 9.84 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·288 lines (227 loc) · 9.84 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env bash
# Phoenix — root deploy script
#
# Builds, loads, and deploys all Phoenix services to the k3s cluster.
# Safe to re-run: apply is idempotent, rollout restart handles image refresh.
#
# Usage:
# ./deploy.sh deploy everything
# ./deploy.sh sim chaos deploy specific services only
# ./deploy.sh --restart skip build+load, just restart running pods
# ./deploy.sh --restart dashboard restart one service only
#
# Services: sim | chaos | faultlib | graph | dashboard | agent
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Load .env if present (secrets — gitignored)
if [ -f "$SCRIPT_DIR/.env" ]; then
set -a; source "$SCRIPT_DIR/.env"; set +a
fi
# ── config ────────────────────────────────────────────────────────────────────
NAMESPACE="phoenix-system"
NODES=(192.168.139.42 192.168.139.77 192.168.139.45)
SSH_KEY="$HOME/.orbstack/ssh/id_ed25519"
SSH_OPTS="-i $SSH_KEY -o StrictHostKeyChecking=no -o ConnectTimeout=10"
ALL_SERVICES=(sim chaos faultlib graph dashboard agent)
# services that have k8s/rbac.yaml
RBAC_SERVICES=(chaos graph agent)
# Lookup helpers — bash 3.x compatible (no associative arrays)
svc_image() { case "$1" in sim) echo phoenix-sim ;; chaos) echo phoenix-chaos ;; faultlib) echo phoenix-faultlib ;; graph) echo phoenix-graph ;; dashboard) echo phoenix-dashboard ;; agent) echo phoenix-agent ;; esac }
svc_deploy() { case "$1" in sim) echo phoenix-sim ;; chaos) echo phoenix-chaos ;; faultlib) echo phoenix-faultlib ;; graph) echo phoenix-graph ;; dashboard) echo phoenix-dashboard ;; agent) echo phoenix-agent ;; esac }
svc_port() { case "$1" in sim) echo "8083:80" ;; chaos) echo "8082:80" ;; faultlib) echo "8081:80" ;; graph) echo "8080:80" ;; dashboard) echo "3000:80" ;; agent) echo "8084:80" ;; esac }
# ── colors ────────────────────────────────────────────────────────────────────
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BOLD='\033[1m'
NC='\033[0m'
log() { echo -e "${CYAN}==>${NC} $*"; }
ok() { echo -e "${GREEN} ✓${NC} $*"; }
warn() { echo -e "${YELLOW} !${NC} $*"; }
die() { echo -e "${RED} ✗${NC} $*" >&2; exit 1; }
# ── argument parsing ──────────────────────────────────────────────────────────
RESTART_ONLY=false
SELECTED=()
for arg in "$@"; do
case "$arg" in
--restart) RESTART_ONLY=true ;;
sim|chaos|faultlib|graph|dashboard|agent) SELECTED+=("$arg") ;;
*) die "Unknown argument: $arg. Valid services: ${ALL_SERVICES[*]}" ;;
esac
done
# Default to all services if none specified
if [ ${#SELECTED[@]} -eq 0 ]; then
SELECTED=("${ALL_SERVICES[@]}")
fi
# ── preflight ─────────────────────────────────────────────────────────────────
preflight() {
log "Preflight checks..."
if ! kubectl get nodes &>/dev/null; then
die "kubectl can't reach the cluster. Run: kubectl config use-context argus"
fi
if ! $RESTART_ONLY && ! docker info &>/dev/null; then
die "Docker is not running. Open OrbStack."
fi
# Ensure namespace exists
if ! kubectl get namespace "$NAMESPACE" &>/dev/null; then
log "Creating namespace $NAMESPACE..."
kubectl apply -f "$SCRIPT_DIR/sim/k8s/namespace.yaml"
fi
ok "Cluster reachable · namespace $NAMESPACE ready"
# Create agent secret if deploying agent and key is set
if [[ " ${SELECTED[*]} " == *" agent "* ]]; then
local api_key="${OPENAI_API_KEY:-}"
if [ -z "$api_key" ]; then
warn "OPENAI_API_KEY not set — agent will start but cannot call OpenAI. Set it and re-run."
else
kubectl create secret generic phoenix-agent-secrets \
--from-literal=openai-api-key="$api_key" \
--namespace="$NAMESPACE" \
--dry-run=client -o yaml | kubectl apply -f - &>/dev/null
ok "Agent secret applied"
fi
fi
}
# ── node image load (parallel across nodes) ───────────────────────────────────
load_to_nodes() {
local image="$1"
local pids=()
local failed=()
log " Loading ${BOLD}${image}:latest${NC} to ${#NODES[@]} nodes in parallel..."
for node in "${NODES[@]}"; do
(
docker save "${image}:latest" | \
ssh $SSH_OPTS "kaushikkumaran@${node}" \
"sudo k3s ctr images import -" \
2>/dev/null
) &
pids+=($!)
done
for i in "${!pids[@]}"; do
if ! wait "${pids[$i]}"; then
failed+=("${NODES[$i]}")
fi
done
if [ ${#failed[@]} -gt 0 ]; then
die "Failed to load image to nodes: ${failed[*]}"
fi
ok " Image loaded to all ${#NODES[@]} nodes"
}
# ── build dashboard npm first ─────────────────────────────────────────────────
build_dashboard_frontend() {
log " Building dashboard frontend (npm)..."
cd "$SCRIPT_DIR/dashboard"
npm ci --silent
npm run build --silent
ok " Frontend built → dist/"
cd "$SCRIPT_DIR"
}
# ── deploy one service ────────────────────────────────────────────────────────
deploy_service() {
local svc="$1"
local dir="$SCRIPT_DIR/$svc"
local image; image="$(svc_image "$svc")"
local deploy; deploy="$(svc_deploy "$svc")"
echo ""
echo -e "${BOLD}── $svc ──────────────────────────────────────────────${NC}"
if $RESTART_ONLY; then
if kubectl get deployment "$deploy" -n "$NAMESPACE" &>/dev/null; then
log " Restarting $deploy..."
kubectl rollout restart deployment/"$deploy" -n "$NAMESPACE"
kubectl rollout status deployment/"$deploy" -n "$NAMESPACE" --timeout=120s
ok " $svc restarted"
else
warn " $deploy not found in cluster — skipping (run without --restart to deploy)"
fi
return
fi
# Dashboard: build frontend before docker build
if [ "$svc" = "dashboard" ]; then
build_dashboard_frontend
fi
log " Building Docker image ${image}:latest..."
if [ "$svc" = "dashboard" ]; then
docker build \
--build-arg "VITE_ARGUS_URL=${VITE_ARGUS_URL:-}" \
--build-arg "VITE_SENTINEL_URL=${VITE_SENTINEL_URL:-}" \
-t "${image}:latest" "$dir" --quiet
else
docker build -t "${image}:latest" "$dir" --quiet
fi
ok " Image built"
load_to_nodes "$image"
log " Applying k8s manifests..."
# Namespace (only services that have it)
if [ -f "$dir/k8s/namespace.yaml" ]; then
kubectl apply -f "$dir/k8s/namespace.yaml" --output=name 2>/dev/null | sed 's/^/ /'
fi
# RBAC (chaos, graph)
if [ -f "$dir/k8s/rbac.yaml" ]; then
kubectl apply -f "$dir/k8s/rbac.yaml" --output=name 2>/dev/null | sed 's/^/ /'
fi
kubectl apply -f "$dir/k8s/deployment.yaml" --output=name 2>/dev/null | sed 's/^/ /'
kubectl apply -f "$dir/k8s/service.yaml" --output=name 2>/dev/null | sed 's/^/ /'
# Force restart so pods pick up the new image (same tag, new bytes)
kubectl rollout restart deployment/"$deploy" -n "$NAMESPACE" 2>/dev/null || true
log " Waiting for rollout..."
kubectl rollout status deployment/"$deploy" -n "$NAMESPACE" --timeout=120s
ok " $svc is live"
}
# ── port-forwards ────────────────────────────────────────────────────────────
PF_PIDS=()
cleanup() {
echo ""
log "Shutting down port-forwards..."
for pid in "${PF_PIDS[@]}"; do
kill "$pid" 2>/dev/null || true
done
echo -e "${GREEN} ✓${NC} Done."
}
start_port_forwards() {
echo ""
log "Starting port-forwards..."
# Kill any existing port-forwards for these services to avoid conflicts
for svc in "${SELECTED[@]}"; do
local local_port; local_port="$(svc_port "$svc")"; local_port="${local_port%%:*}"
lsof -ti :"$local_port" 2>/dev/null | xargs kill -9 2>/dev/null || true
done
sleep 0.5
for svc in "${SELECTED[@]}"; do
local port; port="$(svc_port "$svc")"
local name; name="$(svc_deploy "$svc")"
local local_port="${port%%:*}"
kubectl port-forward -n "$NAMESPACE" "svc/$name" "$port" \
--address=127.0.0.1 &>/dev/null &
PF_PIDS+=($!)
ok " $name → http://localhost:${local_port}"
done
trap cleanup EXIT INT TERM
echo ""
echo -e "${GREEN}${BOLD}Phoenix is up.${NC}"
echo ""
# Print the dashboard URL prominently if deployed
if [[ " ${SELECTED[*]} " == *" dashboard "* ]]; then
echo -e " ${BOLD}Dashboard:${NC} http://localhost:3000"
fi
echo ""
echo -e "${BOLD}Logs:${NC}"
for svc in "${SELECTED[@]}"; do
local name; name="$(svc_deploy "$svc")"
printf " %-12s kubectl logs -n %s -l app=%s -f\n" "$svc" "$NAMESPACE" "$name"
done
echo ""
echo -e " ${YELLOW}Ctrl+C to stop port-forwards${NC}"
echo ""
# Wait for all port-forward processes — keep script alive
wait "${PF_PIDS[@]}" 2>/dev/null || true
}
# ── main ──────────────────────────────────────────────────────────────────────
echo ""
echo -e "${BOLD}Phoenix deploy${NC} · services: ${SELECTED[*]}${RESTART_ONLY:+ · restart-only mode}"
echo ""
preflight
for svc in "${SELECTED[@]}"; do
deploy_service "$svc"
done
start_port_forwards