-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy
More file actions
executable file
·343 lines (282 loc) · 13.8 KB
/
deploy
File metadata and controls
executable file
·343 lines (282 loc) · 13.8 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
#!/bin/bash
set -e
echo "🚀 MicroTraderX: Deploying to Kubernetes..."
echo ""
# Color codes
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Wait for target to be registered (worker needs time to connect and register targets)
wait_for_target() {
local target_slug="$1"
local space="$2"
local max_wait=60
local waited=0
echo " Waiting for target $target_slug to register..."
while [ $waited -lt $max_wait ]; do
if cub target list --space "$space" 2>/dev/null | grep -q "$target_slug"; then
echo " ✅ Target $target_slug registered (${waited}s)"
return 0
fi
sleep 5
waited=$((waited + 5))
if [ $((waited % 15)) -eq 0 ]; then
echo " Still waiting... (${waited}/${max_wait}s)"
fi
done
echo " ⚠️ Target $target_slug not found after ${max_wait}s (continuing anyway)"
return 1
}
# Get prefix
if [ ! -x "bin/get-prefix" ]; then
echo "❌ Error: bin/get-prefix not found"
echo " Run ./setup-structure first"
exit 1
fi
PREFIX=$(bin/get-prefix)
echo "📛 Using prefix: $PREFIX"
echo ""
# Stage selection
STAGE=${1:-7}
echo -e "${BLUE}Stage $STAGE: Deploying...${NC}"
echo ""
case $STAGE in
1)
echo "Stage 1: Deploy to single space"
# Create confighub namespace
kubectl create namespace confighub --dry-run=client -o yaml | kubectl apply -f - >/dev/null 2>&1
# Create worker if it doesn't exist
WORKER_NAME="${PREFIX}-traderx-worker"
if ! cub worker list --space ${PREFIX}-traderx 2>/dev/null | grep -q $WORKER_NAME; then
echo "Creating ConfigHub worker..."
cub worker create $WORKER_NAME --space ${PREFIX}-traderx
fi
# Install worker to Kubernetes
echo "Installing worker to Kubernetes..."
cub worker install $WORKER_NAME \
--namespace confighub \
--space ${PREFIX}-traderx \
--include-secret \
--export > /tmp/worker-manifest.yaml
kubectl apply -f /tmp/worker-manifest.yaml >/dev/null 2>&1
# Wait for worker to connect and register target
TARGET_SLUG="k8s-${WORKER_NAME}"
MAX_WAIT=30
echo "Waiting for worker to connect and register target..."
for i in $(seq 1 $MAX_WAIT); do
if cub target list --space ${PREFIX}-traderx 2>/dev/null | grep -q "$TARGET_SLUG"; then
echo "✅ Target registered (${i}s)"
break
fi
if [ $((i % 5)) -eq 0 ]; then
echo " Still waiting... (${i}/${MAX_WAIT}s)"
fi
sleep 1
if [ $i -eq $MAX_WAIT ]; then
echo "❌ Timeout: Worker target did not register after ${MAX_WAIT} seconds"
echo ""
echo "Troubleshooting:"
echo " Check worker pod: kubectl get pods -n confighub | grep $WORKER_NAME"
echo " Check worker logs: kubectl logs -n confighub -l app=$WORKER_NAME"
echo " Check worker status: cub worker list --space ${PREFIX}-traderx"
exit 1
fi
done
# Set target on unit
echo "Setting target on reference-data unit..."
cub unit set-target reference-data $TARGET_SLUG --space ${PREFIX}-traderx
if [ $? -ne 0 ]; then
echo "❌ Error: Failed to set target on reference-data unit"
exit 1
fi
# Apply unit
echo "Applying reference-data unit..."
cub unit apply reference-data --space ${PREFIX}-traderx
if [ $? -ne 0 ]; then
echo "❌ Error: Failed to apply reference-data unit"
exit 1
fi
echo -e "${GREEN}✓ Deployed reference-data to ${PREFIX}-traderx${NC}"
echo ""
echo "Check status:"
echo " kubectl get deployments -n default"
echo " kubectl get pods -n default -l app=reference-data"
echo " kubectl logs -n default -l app=reference-data"
;;
2)
echo "Stage 2: Deploy only production"
# Create worker and target
cub worker create worker --space ${PREFIX}-traderx-prod 2>/dev/null || echo "Worker already exists"
cub target create worker "{}" worker --space ${PREFIX}-traderx-prod 2>/dev/null || echo "Target already exists"
# Install worker to Kubernetes (using pinned working image)
echo "Installing worker to Kubernetes..."
cub worker install worker --space ${PREFIX}-traderx-prod --export 2>&1 | \
sed 's|ghcr.io/confighubai/confighub-worker:latest|ghcr.io/confighubai/confighub-worker@sha256:704352a1082163aee749c90b05223f882d8bacfd6e14981112b0d9f07b1d6fd0|g' | \
kubectl apply -f - >/dev/null 2>&1 || true
# Set target and apply
cub unit set-target reference-data worker --space ${PREFIX}-traderx-prod || true
cub unit apply reference-data --space ${PREFIX}-traderx-prod
echo -e "${GREEN}✓ Deployed to ${PREFIX}-traderx-prod${NC}"
echo -e "${YELLOW}Note: dev and staging are config-only (not deployed)${NC}"
;;
3)
echo "Stage 3: Deploy all regions with namespaces and links"
# Deploy namespaces first (infrastructure)
echo "Deploying namespaces..."
cub worker create worker-infra --space ${PREFIX}-traderx-infra 2>/dev/null || echo "Worker already exists"
cub target create worker-infra "{}" worker-infra --space ${PREFIX}-traderx-infra 2>/dev/null || echo "Target already exists"
# Install infra worker
cub worker install worker-infra --namespace confighub --space ${PREFIX}-traderx-infra --include-secret --export 2>&1 | \
kubectl apply -f - >/dev/null 2>&1 || true
# Wait for infra worker target to register
wait_for_target "k8s-worker-infra" "${PREFIX}-traderx-infra"
# Deploy namespace units
for region in us eu asia; do
echo " Deploying namespace for $region..."
cub unit set-target ns-$region k8s-worker-infra --space ${PREFIX}-traderx-infra || true
cub unit apply ns-$region --space ${PREFIX}-traderx-infra || true
done
# Deploy application units to each region
for region in us eu asia; do
echo " Deploying apps to ${PREFIX}-traderx-prod-$region..."
# Create worker and target
cub worker create worker-$region --space ${PREFIX}-traderx-prod-$region 2>/dev/null || echo "Worker already exists"
cub target create worker-$region "{}" worker-$region --space ${PREFIX}-traderx-prod-$region 2>/dev/null || echo "Target already exists"
# Install worker to Kubernetes
cub worker install worker-$region --namespace confighub --space ${PREFIX}-traderx-prod-$region --include-secret --export 2>&1 | \
kubectl apply -f - >/dev/null 2>&1 || true
# Wait for worker target to register
wait_for_target "k8s-worker-$region" "${PREFIX}-traderx-prod-$region"
# Set targets and apply all units (Links will resolve namespaces)
cub unit set-target reference-data k8s-worker-$region --space ${PREFIX}-traderx-prod-$region || true
cub unit set-target trade-service k8s-worker-$region --space ${PREFIX}-traderx-prod-$region || true
# Apply both units (WHERE clause doesn't support OR, only AND)
cub unit apply --space ${PREFIX}-traderx-prod-$region --unit reference-data,trade-service || true
done
echo -e "${GREEN}✓ Deployed to all 3 regions with proper namespaces${NC}"
echo ""
echo "Verify deployment (each region has its own namespace):"
echo " kubectl get all -n ${PREFIX}-traderx-prod-us"
echo " kubectl get all -n ${PREFIX}-traderx-prod-eu"
echo " kubectl get all -n ${PREFIX}-traderx-prod-asia"
echo ""
echo "Verify regional scale:"
echo " kubectl get deploy trade-service -n ${PREFIX}-traderx-prod-us -o jsonpath='{.spec.replicas}'"
echo " kubectl get deploy trade-service -n ${PREFIX}-traderx-prod-eu -o jsonpath='{.spec.replicas}'"
echo " kubectl get deploy trade-service -n ${PREFIX}-traderx-prod-asia -o jsonpath='{.spec.replicas}'"
;;
4)
echo "Stage 4: Deploy with push-upgrade demo"
# Deploy all regions
for region in us eu asia; do
echo " Deploying to ${PREFIX}-traderx-prod-$region..."
# Create worker and target
cub worker create worker-$region --space ${PREFIX}-traderx-prod-$region 2>/dev/null || echo "Worker already exists"
cub target create worker-$region "{}" worker-$region --space ${PREFIX}-traderx-prod-$region 2>/dev/null || echo "Target already exists"
# Install worker to Kubernetes (using pinned working image)
cub worker install worker-$region --namespace confighub --space ${PREFIX}-traderx-prod-$region --include-secret --export 2>&1 | \
sed 's|ghcr.io/confighubai/confighub-worker:latest|ghcr.io/confighubai/confighub-worker@sha256:704352a1082163aee749c90b05223f882d8bacfd6e14981112b0d9f07b1d6fd0|g' | \
kubectl apply -f - >/dev/null 2>&1 || true
# Wait for worker to connect
sleep 10
# Set targets and apply all units
cub unit set-target worker-$region --space ${PREFIX}-traderx-prod-$region --where "Slug != ''" || true
cub unit apply --space ${PREFIX}-traderx-prod-$region --where "Slug != ''"
done
echo ""
echo -e "${YELLOW}Demo: Update base and push to regions${NC}"
echo " 1. Update algorithm in base:"
echo " echo '{\"spec\":{\"template\":{\"spec\":{\"containers\":[{\"name\":\"trade-service\",\"env\":[{\"name\":\"TRADING_ALGORITHM\",\"value\":\"v2\"}]}]}}}}' | \\"
echo " cub unit update trade-service --space ${PREFIX}-traderx-base --patch --from-stdin"
echo ""
echo " 2. Push upgrade (keeps regional replicas!):"
echo " cub unit update --upgrade --patch --space 'traderx-prod-*'"
echo ""
echo -e "${GREEN}✓ Deployed with inheritance${NC}"
;;
5)
echo "Stage 5: Deploy + Find and Fix demo"
./deploy 4
echo ""
echo -e "${YELLOW}Find and Fix Examples:${NC}"
echo " Scale down EU after market close:"
echo " cub run set-replicas --replicas 2 \\"
echo " --space ${PREFIX}-traderx-prod-eu \\"
echo " --where \"spec.replicas > 2\""
echo ""
echo " Find old versions:"
echo " cub unit list --space '*' \\"
echo " --where \"Data CONTAINS 'trade-service:v1'\""
;;
6)
echo "Stage 6: Deploy + Changeset demo"
./deploy 4
echo ""
echo -e "${YELLOW}Changeset Example (run manually):${NC}"
echo " # Both services must update together!"
echo " cub changeset create market-data-v2"
echo " echo '{\"spec\":{\"template\":{\"spec\":{\"containers\":[{\"name\":\"reference-data\",\"image\":\"traderx/reference-data:v2\"}]}}}}' | \\"
echo " cub unit update reference-data --space ${PREFIX}-traderx-prod-us --patch --from-stdin"
echo " echo '{\"spec\":{\"template\":{\"spec\":{\"containers\":[{\"name\":\"trade-service\",\"image\":\"traderx/trade-service:v2\"}]}}}}' | \\"
echo " cub unit update trade-service --space ${PREFIX}-traderx-prod-us --patch --from-stdin"
echo " cub changeset apply market-data-v2 # Atomic!"
;;
7)
echo "Stage 7: Deploy complete system"
# Deploy dev/staging
for env in dev staging; do
echo " Deploying to ${PREFIX}-traderx-$env..."
# Create worker and target
cub worker create worker-$env --space ${PREFIX}-traderx-$env 2>/dev/null || echo "Worker already exists"
cub target create worker-$env "{}" worker-$env --space ${PREFIX}-traderx-$env 2>/dev/null || echo "Target already exists"
# Install worker to Kubernetes (using pinned working image)
cub worker install worker-$env --namespace confighub --space ${PREFIX}-traderx-$env --include-secret --export 2>&1 | \
sed 's|ghcr.io/confighubai/confighub-worker:latest|ghcr.io/confighubai/confighub-worker@sha256:704352a1082163aee749c90b05223f882d8bacfd6e14981112b0d9f07b1d6fd0|g' | \
kubectl apply -f - >/dev/null 2>&1 || true
# Wait for worker to connect
sleep 10
# Set targets and apply all units
cub unit set-target worker-$env --space ${PREFIX}-traderx-$env --where "Slug != ''" || true
cub unit apply --space ${PREFIX}-traderx-$env --where "Slug != ''"
done
# Deploy all regions
for region in us eu asia; do
echo " Deploying to ${PREFIX}-traderx-prod-$region..."
# Create worker and target
cub worker create worker-$region --space ${PREFIX}-traderx-prod-$region 2>/dev/null || echo "Worker already exists"
cub target create worker-$region "{}" worker-$region --space ${PREFIX}-traderx-prod-$region 2>/dev/null || echo "Target already exists"
# Install worker to Kubernetes (using pinned working image)
cub worker install worker-$region --namespace confighub --space ${PREFIX}-traderx-prod-$region --include-secret --export 2>&1 | \
sed 's|ghcr.io/confighubai/confighub-worker:latest|ghcr.io/confighubai/confighub-worker@sha256:704352a1082163aee749c90b05223f882d8bacfd6e14981112b0d9f07b1d6fd0|g' | \
kubectl apply -f - >/dev/null 2>&1 || true
# Wait for worker to connect
sleep 10
# Set targets and apply all units
cub unit set-target worker-$region --space ${PREFIX}-traderx-prod-$region --where "Slug != ''" || true
cub unit apply --space ${PREFIX}-traderx-prod-$region --where "Slug != ''"
done
echo -e "${GREEN}✓ Complete system deployed!${NC}"
echo ""
echo "Environments:"
echo " kubectl get deployments -n ${PREFIX}-traderx-dev"
echo " kubectl get deployments -n ${PREFIX}-traderx-staging"
echo " kubectl get deployments -n ${PREFIX}-traderx-prod-us"
echo " kubectl get deployments -n ${PREFIX}-traderx-prod-eu"
echo " kubectl get deployments -n ${PREFIX}-traderx-prod-asia"
echo ""
echo -e "${YELLOW}Emergency Bypass Example:${NC}"
echo " # Fix discovered in EU, push to Asia (skip US):"
echo " cub run set-env-var --env-var CIRCUIT_BREAKER=true \\"
echo " --unit trade-service --space ${PREFIX}-traderx-prod-eu"
echo " cub unit update trade-service --space ${PREFIX}-traderx-prod-asia \\"
echo " --merge-unit traderx-prod-eu/trade-service"
;;
*)
echo "Usage: $0 [stage]"
echo "Stages: 1-7 (default: 7)"
exit 1
;;
esac
echo ""
echo -e "${BLUE}Deployment complete!${NC}"