-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·349 lines (291 loc) · 8.4 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·349 lines (291 loc) · 8.4 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/bin/bash
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
NAMESPACE="agent-scheduler"
K8S_NODE="desktop-control-plane"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Image definitions: local_name:k8s_name
declare -A IMAGES=(
["scheduler:latest"]="docker.io/library/scheduler:latest"
["scheduler-ui:latest"]="docker.io/library/scheduler-ui:latest"
["agent-db-api:latest"]="docker.io/library/agent-db-api:latest"
["agent-ctl:latest"]="docker.io/library/agent-ctl:latest"
)
# Deployment names for restart
DEPLOYMENTS=("scheduler" "ui" "agent-db-api" "agent-ctl")
# Functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
check_prerequisites() {
log_info "Checking prerequisites..."
# Check kubectl
if ! command -v kubectl &> /dev/null; then
log_error "kubectl not found. Please install kubectl."
exit 1
fi
# Check docker
if ! command -v docker &> /dev/null; then
log_error "docker not found. Please install docker."
exit 1
fi
# Check kubernetes context
local context=$(kubectl config current-context 2>/dev/null)
if [[ "$context" != "docker-desktop" ]]; then
log_warn "Current context is '$context', expected 'docker-desktop'"
read -p "Continue anyway? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
# Check if node is accessible
if ! docker exec "$K8S_NODE" echo "OK" &> /dev/null; then
log_error "Cannot access Kubernetes node '$K8S_NODE'. Is Docker Desktop Kubernetes running?"
exit 1
fi
log_success "Prerequisites check passed"
}
load_image() {
local image=$1
log_info "Loading image: $image"
# Check if image exists locally
if ! docker image inspect "$image" &> /dev/null; then
log_error "Image '$image' not found locally. Please build it first."
return 1
fi
# Load into containerd
docker save "$image" | docker exec -i "$K8S_NODE" ctr -n k8s.io images import -
if [ $? -eq 0 ]; then
log_success "Loaded: $image"
else
log_error "Failed to load: $image"
return 1
fi
}
load_all_images() {
log_info "Loading images into Kubernetes containerd..."
local failed=0
for local_image in "${!IMAGES[@]}"; do
if ! load_image "$local_image"; then
((failed++))
fi
done
if [ $failed -gt 0 ]; then
log_error "$failed image(s) failed to load"
return 1
fi
log_success "All images loaded successfully"
}
apply_manifests() {
log_info "Applying Kubernetes manifests..."
# Apply in order
local manifests=(
"00-namespace.yaml"
"01-configmap.yaml"
"02-secrets.yaml"
"07-database-schema-configmap.yaml"
"08-agent-db-schema-configmap.yaml"
"03-postgres-statefulset.yaml"
"04-scheduler-deployment.yaml"
"05-ui-deployment.yaml"
"09-agent-db-deployment.yaml"
"10-agent-ctl-deployment.yaml"
"06-ingress.yaml"
)
for manifest in "${manifests[@]}"; do
local file="$SCRIPT_DIR/$manifest"
if [ -f "$file" ]; then
log_info "Applying $manifest..."
kubectl apply -f "$file"
else
log_warn "Manifest not found: $manifest"
fi
done
log_success "Manifests applied"
}
restart_deployments() {
log_info "Restarting deployments..."
for deployment in "${DEPLOYMENTS[@]}"; do
log_info "Restarting $deployment..."
kubectl rollout restart deployment/"$deployment" -n "$NAMESPACE" 2>/dev/null || true
done
log_success "Deployments restarted"
}
wait_for_pods() {
log_info "Waiting for pods to be ready..."
local timeout=120
local start_time=$(date +%s)
while true; do
local not_ready=$(kubectl get pods -n "$NAMESPACE" --no-headers 2>/dev/null | grep -v "Running\|Completed" | grep -v "Terminating" | wc -l)
if [ "$not_ready" -eq 0 ]; then
log_success "All pods are running"
return 0
fi
local elapsed=$(($(date +%s) - start_time))
if [ $elapsed -ge $timeout ]; then
log_error "Timeout waiting for pods (${timeout}s)"
kubectl get pods -n "$NAMESPACE"
return 1
fi
echo -ne "\r${BLUE}[INFO]${NC} Waiting for $not_ready pod(s)... (${elapsed}s/${timeout}s)"
sleep 2
done
}
verify_deployment() {
log_info "Verifying deployment..."
echo ""
echo "=== Pods ==="
kubectl get pods -n "$NAMESPACE"
echo ""
echo "=== Deployments ==="
kubectl get deployments -n "$NAMESPACE"
echo ""
echo "=== Services ==="
kubectl get services -n "$NAMESPACE"
echo ""
echo "=== Ingress ==="
kubectl get ingress -n "$NAMESPACE"
# Test endpoints
echo ""
log_info "Testing endpoints..."
local endpoints=(
"http://scheduler.local/|UI"
"http://scheduler.local/api/v1/health|Scheduler API"
"http://scheduler.local/admin/api/warehouses|Admin API"
)
for endpoint in "${endpoints[@]}"; do
local url="${endpoint%%|*}"
local name="${endpoint##*|}"
local status=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 "$url" 2>/dev/null || echo "000")
if [[ "$status" =~ ^2 ]] || [[ "$status" =~ ^3 ]]; then
log_success "$name ($url): $status"
else
log_warn "$name ($url): $status"
fi
done
}
show_help() {
cat << EOF
Usage: $0 [OPTIONS] [COMMAND]
Deploy applications to Kubernetes (Docker Desktop)
Commands:
all Full deployment: load images, apply manifests, restart (default)
images Load Docker images into Kubernetes containerd
apply Apply Kubernetes manifests only
restart Restart all deployments
status Show current deployment status
verify Verify deployment and test endpoints
help Show this help message
Options:
-n, --no-restart Don't restart deployments after applying
-f, --force Skip confirmation prompts
-i, --image NAME Load only specified image (can be repeated)
Examples:
$0 # Full deployment
$0 images # Load images only
$0 apply --no-restart # Apply manifests without restarting
$0 -i scheduler:latest # Load only scheduler image
$0 status # Check current status
EOF
}
# Parse arguments
COMMAND="all"
NO_RESTART=false
FORCE=false
SPECIFIC_IMAGES=()
while [[ $# -gt 0 ]]; do
case $1 in
-n|--no-restart)
NO_RESTART=true
shift
;;
-f|--force)
FORCE=true
shift
;;
-i|--image)
SPECIFIC_IMAGES+=("$2")
shift 2
;;
all|images|apply|restart|status|verify|help)
COMMAND=$1
shift
;;
*)
log_error "Unknown option: $1"
show_help
exit 1
;;
esac
done
# Main execution
case $COMMAND in
help)
show_help
exit 0
;;
status)
kubectl get pods,deployments,services -n "$NAMESPACE"
exit 0
;;
verify)
verify_deployment
exit 0
;;
images)
check_prerequisites
if [ ${#SPECIFIC_IMAGES[@]} -gt 0 ]; then
for img in "${SPECIFIC_IMAGES[@]}"; do
load_image "$img"
done
else
load_all_images
fi
;;
apply)
check_prerequisites
apply_manifests
if [ "$NO_RESTART" = false ]; then
restart_deployments
wait_for_pods
fi
;;
restart)
restart_deployments
wait_for_pods
;;
all)
check_prerequisites
if [ ${#SPECIFIC_IMAGES[@]} -gt 0 ]; then
for img in "${SPECIFIC_IMAGES[@]}"; do
load_image "$img"
done
else
load_all_images
fi
apply_manifests
if [ "$NO_RESTART" = false ]; then
restart_deployments
fi
wait_for_pods
verify_deployment
;;
esac
echo ""
log_success "Done!"