-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup-structure
More file actions
executable file
·270 lines (225 loc) · 10.6 KB
/
setup-structure
File metadata and controls
executable file
·270 lines (225 loc) · 10.6 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
#!/bin/bash
set -e
echo "🏗️ MicroTraderX: Setting up ConfigHub structure..."
echo ""
# Color codes for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Get or create unique prefix
if [ ! -x "bin/get-prefix" ]; then
echo "❌ Error: bin/get-prefix not found or not executable"
exit 1
fi
PREFIX=$(bin/get-prefix)
echo "📛 Using prefix: $PREFIX"
echo ""
# Stage selection
STAGE=${1:-7}
echo -e "${BLUE}Stage $STAGE: Setting up structure...${NC}"
echo ""
case $STAGE in
1)
echo "Stage 1: Hello TraderX - Single service, single space"
cub space create ${PREFIX}-traderx || true
cub unit create --space ${PREFIX}-traderx reference-data k8s/reference-data.yaml || true
echo -e "${GREEN}✓ Created ${PREFIX}-traderx/reference-data${NC}"
;;
2)
echo "Stage 2: Three Environments"
cub space create ${PREFIX}-traderx || true
cub unit create --space ${PREFIX}-traderx reference-data k8s/reference-data.yaml || true
for env in dev staging prod; do
echo " Creating ${PREFIX}-traderx-$env..."
cub space create ${PREFIX}-traderx-$env || true
# Create unit with upstream link (inheritance pattern)
cub unit create reference-data \
--space ${PREFIX}-traderx-$env \
--upstream-space ${PREFIX}-traderx \
--upstream-unit reference-data || true
done
echo -e "${GREEN}✓ Created 3 environments (dev, staging, prod)${NC}"
;;
3)
echo "Stage 3: Three Regions with Links"
# Create base space for shared config
cub space create ${PREFIX}-traderx-base || true
cub unit create --space ${PREFIX}-traderx-base reference-data k8s/reference-data.yaml || true
cub unit create --space ${PREFIX}-traderx-base trade-service k8s/trade-service.yaml || true
# Create infrastructure space for namespaces
cub space create ${PREFIX}-traderx-infra || true
cub unit create --space ${PREFIX}-traderx-infra ns-base k8s/namespace-base.yaml || true
# Create regions with upstream relationships and links
for region in us eu asia; do
echo " Creating ${PREFIX}-traderx-prod-$region with inheritance and links..."
cub space create ${PREFIX}-traderx-prod-$region || true
# Create app units (inherit from base)
cub unit create reference-data \
--space ${PREFIX}-traderx-prod-$region \
--upstream-space ${PREFIX}-traderx-base \
--upstream-unit reference-data || true
cub unit create trade-service \
--space ${PREFIX}-traderx-prod-$region \
--upstream-space ${PREFIX}-traderx-base \
--upstream-unit trade-service || true
# Create namespace unit for this region
cub unit create ns-$region \
--space ${PREFIX}-traderx-infra \
--upstream-space ${PREFIX}-traderx-infra \
--upstream-unit ns-base || true
# Customize namespace name
cub run set-string-path \
--resource-type v1/Namespace \
--path metadata.name \
--attribute-value ${PREFIX}-traderx-prod-$region \
--unit ns-$region \
--space ${PREFIX}-traderx-infra \
--quiet || true
# Link app units to namespace (resolves confighubplaceholder)
# Syntax: cub link create --space <space> --json <link-name> <from-unit> <to-unit> [<to-space>]
cub link create --space ${PREFIX}-traderx-prod-$region \
--json app-to-ns-ref reference-data ns-$region ${PREFIX}-traderx-infra || true
cub link create --space ${PREFIX}-traderx-prod-$region \
--json app-to-ns-trade trade-service ns-$region ${PREFIX}-traderx-infra || true
# Link trade-service to reference-data (service dependency - same space)
cub link create --space ${PREFIX}-traderx-prod-$region \
--json trade-to-ref trade-service reference-data || true
done
# Regional customizations based on trading volume
echo " Customizing regional scale..."
# Use ConfigHub functions to set replicas (not --patch which patches Unit metadata, not config data)
cub run set-replicas --replicas 3 --unit trade-service --space ${PREFIX}-traderx-prod-us || true # NYSE hours
cub run set-replicas --replicas 5 --unit trade-service --space ${PREFIX}-traderx-prod-eu || true # Peak trading
cub run set-replicas --replicas 2 --unit trade-service --space ${PREFIX}-traderx-prod-asia || true # Overnight
echo -e "${GREEN}✓ Created base + 3 regions with links${NC}"
echo ""
echo "Links introduced (brief intro - full details in Stage 8):"
echo " • App → Namespace (resolves confighubplaceholder)"
echo " • trade-service → reference-data (service dependency)"
echo ""
echo "Structure:"
echo " ${PREFIX}-traderx-base/ (shared config)"
echo " ${PREFIX}-traderx-infra/ (namespace units)"
echo " ${PREFIX}-traderx-prod-us/ (3 replicas - NYSE)"
echo " ${PREFIX}-traderx-prod-eu/ (5 replicas - Peak)"
echo " ${PREFIX}-traderx-prod-asia/ (2 replicas - Overnight)"
;;
4)
echo "Stage 4: Push-Upgrade with Inheritance"
# Create base space
cub space create ${PREFIX}-traderx-base || true
cub unit create --space ${PREFIX}-traderx-base reference-data k8s/reference-data.yaml || true
cub unit create --space ${PREFIX}-traderx-base trade-service k8s/trade-service.yaml || true
# Create regions with upstream links (canonical pattern)
for region in us eu asia; do
echo " Creating ${PREFIX}-traderx-prod-$region with inheritance..."
cub space create ${PREFIX}-traderx-prod-$region || true
# Use space/slug notation (not unit IDs) - canonical pattern from acmetodo
cub unit create reference-data \
--space ${PREFIX}-traderx-prod-$region \
--upstream-space ${PREFIX}-traderx-base \
--upstream-unit reference-data || true
cub unit create trade-service \
--space ${PREFIX}-traderx-prod-$region \
--upstream-space ${PREFIX}-traderx-base \
--upstream-unit trade-service || true
done
# Regional customizations (preserved during upgrade!)
echo " Customizing regional scale (will be preserved)..."
echo '{"spec":{"replicas":3}}' | cub unit update trade-service \
--space ${PREFIX}-traderx-prod-us --patch --from-stdin || true
echo '{"spec":{"replicas":5}}' | cub unit update trade-service \
--space ${PREFIX}-traderx-prod-eu --patch --from-stdin || true
echo '{"spec":{"replicas":2}}' | cub unit update trade-service \
--space ${PREFIX}-traderx-prod-asia --patch --from-stdin || true
echo -e "${GREEN}✓ Created base + regions with inheritance${NC}"
echo " Regional scale will be preserved during upgrades!"
;;
5)
echo "Stage 5: Find and Fix - Bulk Operations"
# Reuse Stage 4 structure
./setup-structure 4
echo ""
echo -e "${YELLOW}Find and Fix Examples:${NC}"
echo " Find high-replica services in EU:"
echo " cub unit list --space '${PREFIX}-traderx-prod-eu' --where \"Data CONTAINS 'replicas: 5'\""
echo ""
echo " Scale down after market close:"
echo " cub run set-replicas --replicas 2 --space ${PREFIX}-traderx-prod-eu --where \"Data CONTAINS 'replicas: 5'\""
;;
6)
echo "Stage 6: Atomic Updates with Changesets"
./setup-structure 4
echo ""
echo -e "${YELLOW}Changeset Example:${NC}"
echo " Create changeset:"
echo " cub changeset create market-data-v2"
echo " Add related changes:"
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 " Apply atomically:"
echo " cub changeset apply market-data-v2"
;;
7)
echo "Stage 7: Complete System with All Features"
# Create base
cub space create ${PREFIX}-traderx-base || true
cub unit create --space ${PREFIX}-traderx-base reference-data k8s/reference-data.yaml || true
cub unit create --space ${PREFIX}-traderx-base trade-service k8s/trade-service.yaml || true
# Create dev/staging (canonical pattern)
for env in dev staging; do
echo " Creating ${PREFIX}-traderx-$env..."
cub space create ${PREFIX}-traderx-$env || true
# Use space/slug notation (not unit IDs) - canonical pattern from acmetodo
cub unit create reference-data \
--space ${PREFIX}-traderx-$env \
--upstream-space ${PREFIX}-traderx-base \
--upstream-unit reference-data || true
cub unit create trade-service \
--space ${PREFIX}-traderx-$env \
--upstream-space ${PREFIX}-traderx-base \
--upstream-unit trade-service || true
done
# Create regions (canonical pattern)
for region in us eu asia; do
echo " Creating ${PREFIX}-traderx-prod-$region..."
cub space create ${PREFIX}-traderx-prod-$region || true
# Use space/slug notation (not unit IDs) - canonical pattern from acmetodo
cub unit create reference-data \
--space ${PREFIX}-traderx-prod-$region \
--upstream-space ${PREFIX}-traderx-base \
--upstream-unit reference-data || true
cub unit create trade-service \
--space ${PREFIX}-traderx-prod-$region \
--upstream-space ${PREFIX}-traderx-base \
--upstream-unit trade-service || true
done
# Regional customizations
echo " Customizing regional scale..."
echo '{"spec":{"replicas":3}}' | cub unit update trade-service \
--space ${PREFIX}-traderx-prod-us --patch --from-stdin || true
echo '{"spec":{"replicas":5}}' | cub unit update trade-service \
--space ${PREFIX}-traderx-prod-eu --patch --from-stdin || true
echo '{"spec":{"replicas":2}}' | cub unit update trade-service \
--space ${PREFIX}-traderx-prod-asia --patch --from-stdin || true
echo -e "${GREEN}✓ Complete system created!${NC}"
echo ""
echo "Structure:"
echo " ${PREFIX}-traderx-base/ (shared configs)"
echo " ${PREFIX}-traderx-dev/ (development)"
echo " ${PREFIX}-traderx-staging/ (staging)"
echo " ${PREFIX}-traderx-prod-us/ (3 replicas - NYSE)"
echo " ${PREFIX}-traderx-prod-eu/ (5 replicas - Peak)"
echo " ${PREFIX}-traderx-prod-asia/ (2 replicas - Overnight)"
;;
*)
echo "Usage: $0 [stage]"
echo "Stages: 1-7 (default: 7)"
exit 1
;;
esac
echo ""
echo -e "${BLUE}Next: Run './deploy $STAGE' to deploy to Kubernetes${NC}"