-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·365 lines (308 loc) · 11.7 KB
/
deploy.sh
File metadata and controls
executable file
·365 lines (308 loc) · 11.7 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/bin/bash
# ==============================================================================
# UnitOne AgentGateway - Local E2E Test Script
# ==============================================================================
#
# Runs E2E tests locally using Docker. For production deployments, use Terraform.
#
# Usage:
# ./deploy.sh [OPTIONS]
#
# Options:
# --skip-build Skip ACR build, use existing image
# --skip-tests Start services but skip running tests
# --clean Clean up containers before starting
# --registry NAME ACR registry name (default from ACR_REGISTRY env var)
# --tag TAG Image tag (default: latest)
# --stop Stop running services and exit
# -h, --help Show this help message
#
# Examples:
# ./deploy.sh # Full: build + deploy + test
# ./deploy.sh --skip-build # Use existing ACR image
# ./deploy.sh --skip-tests # Start services only
# ./deploy.sh --stop # Stop services
#
# Production Deployment:
# For production, use Terraform:
# cd terraform && terraform apply
#
# ==============================================================================
set -e
# Get terraform output helper
get_tf_output() {
local key=$1
if [ -f "terraform/terraform.tfstate" ] || [ -d "terraform/.terraform" ]; then
(cd terraform && terraform output -raw "$key" 2>/dev/null) || echo ""
fi
}
# Configuration - try terraform first, then env var, then error
TF_ACR=$(get_tf_output acr_name)
REGISTRY="${REGISTRY:-${ACR_REGISTRY:-${TF_ACR}}}"
if [ -z "$REGISTRY" ]; then
echo "Error: ACR registry not configured. Run './agw setup' first or set ACR_REGISTRY env var."
exit 1
fi
TAG="${TAG:-latest}"
SKIP_BUILD=false
SKIP_TESTS=false
CLEAN=false
STOP_ONLY=false
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'
# ==============================================================================
# Helper Functions
# ==============================================================================
print_header() {
echo ""
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BOLD}${CYAN} $1${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
}
print_step() { echo -e "${CYAN}▶${NC} $1"; }
print_success() { echo -e "${GREEN}✓${NC} $1"; }
print_warning() { echo -e "${YELLOW}⚠${NC} $1"; }
print_error() { echo -e "${RED}✗${NC} $1"; }
print_info() { echo -e "${BLUE}ℹ${NC} $1"; }
show_help() {
cat << 'HELP'
AgentGateway - Local E2E Test Script
Runs E2E tests locally using Docker. For production deployments, use Terraform.
Usage:
./deploy.sh [OPTIONS]
Options:
--skip-build Skip ACR build, use existing image
--skip-tests Start services but skip running tests
--clean Clean up containers before starting
--registry NAME ACR registry name (from terraform or ACR_REGISTRY env)
--tag TAG Image tag (default: latest)
--stop Stop running services and exit
-h, --help Show this help message
Examples:
./deploy.sh # Full: build + deploy + test
./deploy.sh --skip-build # Use existing ACR image
./deploy.sh --skip-tests # Start services only
./deploy.sh --stop # Stop services
Production Deployment:
For production, use Terraform:
cd ../terraform/environments/dev/agentgateway && terraform apply
HELP
exit 0
}
check_command() {
command -v "$1" &> /dev/null
}
# ==============================================================================
# Parse Arguments
# ==============================================================================
while [[ $# -gt 0 ]]; do
case $1 in
--registry) REGISTRY="$2"; shift 2 ;;
--tag) TAG="$2"; shift 2 ;;
--skip-build) SKIP_BUILD=true; shift ;;
--skip-tests) SKIP_TESTS=true; shift ;;
--clean) CLEAN=true; shift ;;
--stop) STOP_ONLY=true; shift ;;
-h|--help) show_help ;;
*) print_error "Unknown option: $1"; exit 1 ;;
esac
done
# ==============================================================================
# Stop Services
# ==============================================================================
stop_services() {
print_header "Stopping Services"
cd tests/docker
docker compose down -v 2>/dev/null || true
print_success "Services stopped"
exit 0
}
[ "$STOP_ONLY" = true ] && stop_services
# ==============================================================================
# Prerequisites Check
# ==============================================================================
check_prerequisites() {
print_header "Checking Prerequisites"
local failed=0
print_step "Checking Azure CLI..."
if check_command "az"; then
print_success "Azure CLI installed"
if az account show &> /dev/null; then
print_success "Logged in: $(az account show --query name -o tsv)"
else
print_warning "Not logged in (run: az login)"
failed=1
fi
else
print_error "Azure CLI not installed"
failed=1
fi
print_step "Checking Docker..."
if check_command "docker" && docker info &> /dev/null; then
print_success "Docker running"
else
print_error "Docker not running"
failed=1
fi
print_step "Checking Docker Compose..."
if docker compose version &> /dev/null; then
print_success "Docker Compose available"
else
print_error "Docker Compose not available"
failed=1
fi
print_step "Checking submodule..."
if [ -f "agentgateway/Cargo.toml" ]; then
print_success "Submodule: $(cd agentgateway && git rev-parse --short HEAD)"
else
print_error "Submodule not initialized (run: git submodule update --init)"
failed=1
fi
print_step "Checking test servers..."
if [ -f "testservers/Dockerfile" ]; then
print_success "Test servers found"
else
print_error "Test servers not found"
failed=1
fi
[ $failed -eq 1 ] && { print_error "Prerequisites failed"; exit 1; }
print_success "All prerequisites met"
}
# ==============================================================================
# ACR Build
# ==============================================================================
build_in_acr() {
print_header "Building in ACR"
print_info "Registry: ${REGISTRY}.azurecr.io"
print_info "Image: unitone-agentgateway:${TAG}"
echo ""
print_step "Logging in to ACR..."
az acr login --name "$REGISTRY" || { print_error "ACR login failed"; exit 1; }
print_success "Logged in"
# Clean large target directory
if [ -d "agentgateway/target" ]; then
local size=$(du -sm agentgateway/target 2>/dev/null | cut -f1)
if [ "$size" -gt 1000 ]; then
print_warning "Cleaning ${size}MB target directory..."
rm -rf agentgateway/target
fi
fi
print_step "Starting build (20-30 min)..."
local start=$(date +%s)
az acr build \
--registry "$REGISTRY" \
--image "unitone-agentgateway:${TAG}" \
--image "unitone-agentgateway:$(date +%Y%m%d-%H%M%S)" \
--platform linux/amd64 \
--file Dockerfile.acr .
local elapsed=$(( $(date +%s) - start ))
print_success "Build complete in $((elapsed/60))m $((elapsed%60))s"
}
# ==============================================================================
# Run E2E Tests
# ==============================================================================
run_e2e_tests() {
print_header "E2E Test Mode"
print_info "Steps:"
echo " 1. Build in ACR (unless --skip-build)"
echo " 2. Start test servers + gateway locally"
echo " 3. Run security guard tests"
echo ""
# Clean if requested
if [ "$CLEAN" = true ]; then
print_step "Cleaning containers..."
cd tests/docker && docker compose down -v 2>/dev/null || true
cd "$SCRIPT_DIR"
print_success "Cleaned"
fi
# Build
if [ "$SKIP_BUILD" = false ]; then
build_in_acr
else
print_info "Using existing image: ${REGISTRY}.azurecr.io/unitone-agentgateway:${TAG}"
fi
# Start services
print_header "Starting Services"
cd tests/docker
# Export ACR_REGISTRY for docker-compose
export ACR_REGISTRY="${REGISTRY}.azurecr.io"
export IMAGE_TAG="${TAG}"
print_step "Logging in to ACR..."
az acr login --name "$REGISTRY" &>/dev/null && print_success "ACR login" || print_warning "ACR login failed"
print_step "Pulling image..."
docker compose pull agentgateway 2>&1 && print_success "Pulled" || print_warning "Using cached"
print_step "Starting containers..."
docker compose up -d --build mcp-test-servers agentgateway
print_step "Waiting for healthy..."
for i in {1..30}; do
health=$(docker compose ps agentgateway --format json 2>/dev/null | jq -r '.Health // "starting"' 2>/dev/null || echo "starting")
[ "$health" = "healthy" ] && break
echo -n "."
sleep 2
done
echo ""
if [ "$health" = "healthy" ]; then
print_success "Services healthy"
else
print_error "Services unhealthy"
docker compose logs --tail=20
exit 1
fi
echo ""
docker compose ps
echo ""
# Run tests
if [ "$SKIP_TESTS" = false ]; then
print_header "Running Tests"
docker compose run --rm test-runner
local code=$?
echo ""
[ $code -eq 0 ] && print_success "All tests passed!" || print_error "Tests failed"
else
print_info "Skipping tests (--skip-tests)"
fi
cd "$SCRIPT_DIR"
# Summary
print_header "Summary"
echo -e "${GREEN}Services running:${NC}"
echo " Gateway: http://localhost:8080"
echo " UI: http://localhost:8080/ui"
echo ""
echo -e "${CYAN}Routes with security guards:${NC}"
echo " /pii-test - PII detection + tool poisoning"
echo " /poison - Tool poisoning guard"
echo " /rug-pull - Rug pull guard"
echo ""
echo -e "${YELLOW}Commands:${NC}"
echo " Logs: cd tests/docker && docker compose logs -f"
echo " Retest: cd tests/docker && docker compose run --rm test-runner"
echo " Stop: ./deploy.sh --stop"
echo ""
echo -e "${BLUE}Production deployment:${NC}"
echo " Use Terraform: cd ../terraform/environments/dev/agentgateway"
echo " terraform apply"
echo ""
}
# ==============================================================================
# Main
# ==============================================================================
echo ""
echo -e "${BOLD}${BLUE}╔═══════════════════════════════════════════════════════════╗${NC}"
echo -e "${BOLD}${BLUE}║${NC} ${BOLD}${CYAN}UnitOne AgentGateway - Local E2E Testing${NC} ${BOLD}${BLUE}║${NC}"
echo -e "${BOLD}${BLUE}╚═══════════════════════════════════════════════════════════╝${NC}"
echo ""
print_info "Registry: ${REGISTRY}.azurecr.io"
print_info "Tag: ${TAG}"
check_prerequisites
run_e2e_tests
print_success "Done!"
echo ""