-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·482 lines (419 loc) · 16.8 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·482 lines (419 loc) · 16.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/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
EXTENSION_NAME="o2-lambda-extension"
BUILD_DIR="target/lambda"
# Architecture targets for Lambda
TARGETS=("x86_64-unknown-linux-musl" "aarch64-unknown-linux-musl")
ARCH_NAMES=("x86_64" "arm64")
# Default to building all architectures, or use environment variable
BUILD_TARGETS="${BUILD_TARGETS:-all}"
echo -e "${BLUE}🚀 Building OpenObserve Lambda Extension${NC}"
if [ "$BUILD_TARGETS" = "all" ]; then
echo -e "${BLUE}📦 Building for all architectures: x86_64 + arm64${NC}"
else
# Find the architecture name for the specified target
arch_name="unknown"
for i in "${!TARGETS[@]}"; do
if [ "${TARGETS[$i]}" = "$BUILD_TARGETS" ]; then
arch_name="${ARCH_NAMES[$i]}"
break
fi
done
echo -e "${BLUE}📦 Building for architecture: $arch_name ($BUILD_TARGETS)${NC}"
fi
# Check if required tools are installed
check_requirements() {
echo -e "${YELLOW}📋 Checking requirements...${NC}"
if ! command -v cargo &> /dev/null; then
echo -e "${RED}❌ Error: cargo is not installed${NC}"
exit 1
fi
if ! command -v zip &> /dev/null; then
echo -e "${RED}❌ Error: zip is not installed${NC}"
exit 1
fi
echo -e "${GREEN}✅ All requirements met${NC}"
}
# Add targets if not already added
setup_targets() {
echo -e "${YELLOW}🎯 Setting up build targets...${NC}"
if [ "$BUILD_TARGETS" = "all" ]; then
for target in "${TARGETS[@]}"; do
echo -e "${BLUE} Adding target: $target${NC}"
rustup target add $target
done
else
echo -e "${BLUE} Adding target: $BUILD_TARGETS${NC}"
rustup target add $BUILD_TARGETS
fi
echo -e "${GREEN}✅ Targets setup completed${NC}"
}
# Clean previous builds
clean_build() {
echo -e "${YELLOW}🧹 Cleaning previous builds...${NC}"
rm -rf $BUILD_DIR
cargo clean
}
# Build the extension for a specific target
build_for_target() {
local target=$1
local arch_name=$2
echo -e "${YELLOW}🔨 Building for $arch_name ($target)...${NC}"
# Check if we're on macOS and need Docker
if [[ "$OSTYPE" == "darwin"* ]]; then
echo -e "${BLUE}🐳 Using Docker for cross-compilation on macOS...${NC}"
if ! command -v docker &> /dev/null; then
echo -e "${RED}❌ Error: Docker is required for cross-compilation on macOS${NC}"
echo -e "${YELLOW}Please install Docker Desktop from https://www.docker.com/products/docker-desktop${NC}"
exit 1
fi
# Use cross-compilation container for reliable builds
docker run --rm \
-v "$PWD":/workspace \
-w /workspace \
--platform linux/amd64 \
rust:1.89 sh -c "
# Install musl tools and cross-compilation support
apt-get update &&
apt-get install -y musl-tools musl-dev build-essential &&
# Add the specific target
rustup target add $target &&
# Set up cross-compilation environment
if [ '$target' = 'x86_64-unknown-linux-musl' ]; then
# Native compilation on x86_64 container
export CC=musl-gcc
elif [ '$target' = 'aarch64-unknown-linux-musl' ]; then
# Install ARM64 cross-compiler
apt-get install -y gcc-aarch64-linux-gnu &&
export CC_aarch64_unknown_linux_musl=aarch64-linux-gnu-gcc &&
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=aarch64-linux-gnu-gcc
fi &&
# Build the project
cargo build --release --target $target
"
else
# Set environment variables for cross-compilation on Linux
if [ "$target" = "x86_64-unknown-linux-musl" ]; then
export CC_x86_64_unknown_linux_musl=musl-gcc
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER=musl-gcc
elif [ "$target" = "aarch64-unknown-linux-musl" ]; then
export CC_aarch64_unknown_linux_musl=aarch64-linux-musl-gcc
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=aarch64-linux-musl-gcc
fi
# Build with musl target for Lambda
cargo build --release --target $target
fi
if [ $? -ne 0 ]; then
echo -e "${RED}❌ Build failed for $arch_name${NC}"
return 1
fi
echo -e "${GREEN}✅ Build completed successfully for $arch_name${NC}"
}
# Build extensions for all specified targets
build_extensions() {
echo -e "${YELLOW}🔨 Building extensions...${NC}"
if [ "$BUILD_TARGETS" = "all" ]; then
for i in "${!TARGETS[@]}"; do
build_for_target "${TARGETS[$i]}" "${ARCH_NAMES[$i]}"
if [ $? -ne 0 ]; then
echo -e "${RED}❌ Build process failed${NC}"
exit 1
fi
done
else
# Find the architecture name for the specified target
arch_name="unknown"
for i in "${!TARGETS[@]}"; do
if [ "${TARGETS[$i]}" = "$BUILD_TARGETS" ]; then
arch_name="${ARCH_NAMES[$i]}"
break
fi
done
build_for_target "$BUILD_TARGETS" "$arch_name"
if [ $? -ne 0 ]; then
echo -e "${RED}❌ Build process failed${NC}"
exit 1
fi
fi
echo -e "${GREEN}✅ All builds completed successfully${NC}"
}
# Create the Lambda layer structure for a specific target
create_layer_structure_for_target() {
local target=$1
local arch_name=$2
local base_dir="$BUILD_DIR/$arch_name"
local package_dir="$base_dir/extensions"
echo -e "${YELLOW}📁 Creating Lambda layer structure for $arch_name...${NC}"
# /opt/extensions/ — Rust sidecar binary
mkdir -p "$package_dir"
cp "target/$target/release/$EXTENSION_NAME" "$package_dir/"
chmod +x "$package_dir/$EXTENSION_NAME"
# /opt/otel-instrument — Node.js bootstrap script (AWS_LAMBDA_EXEC_WRAPPER target)
cp otel-instrument "$base_dir/otel-instrument"
chmod +x "$base_dir/otel-instrument"
# /opt/nodejs/node_modules/ — OTel auto-instrumentation packages
# Install them fresh into the layer structure
local nodejs_dir="$base_dir/nodejs"
mkdir -p "$nodejs_dir"
echo -e "${BLUE} 📦 Installing OTel node_modules for layer...${NC}"
cat > "$nodejs_dir/package.json" <<'EOF'
{
"name": "o2-otel-layer",
"version": "1.0.0",
"dependencies": {
"@opentelemetry/auto-instrumentations-node": "^0.57.0",
"@opentelemetry/api": "^1.9.0"
}
}
EOF
# Install into the nodejs dir (becomes /opt/nodejs/node_modules in Lambda)
if command -v npm &> /dev/null; then
(cd "$nodejs_dir" && npm install --omit=dev --no-package-lock 2>&1 | tail -3)
else
echo -e "${RED}❌ npm not found — skipping node_modules install${NC}"
echo -e "${YELLOW}Run: cd $nodejs_dir && npm install --omit=dev${NC}"
fi
echo -e "${GREEN}✅ Layer structure created for $arch_name${NC}"
}
# Create layer structures for all built targets
create_layer_structures() {
echo -e "${YELLOW}📁 Creating Lambda layer structures...${NC}"
if [ "$BUILD_TARGETS" = "all" ]; then
for i in "${!TARGETS[@]}"; do
create_layer_structure_for_target "${TARGETS[$i]}" "${ARCH_NAMES[$i]}"
done
else
# Find the architecture name for the specified target
arch_name="unknown"
for i in "${!TARGETS[@]}"; do
if [ "${TARGETS[$i]}" = "$BUILD_TARGETS" ]; then
arch_name="${ARCH_NAMES[$i]}"
break
fi
done
create_layer_structure_for_target "$BUILD_TARGETS" "$arch_name"
fi
echo -e "${GREEN}✅ All layer structures created${NC}"
}
# Create deployment package for a specific architecture
create_package_for_target() {
local arch_name=$1
local package_name="o2-lambda-extension-$arch_name.zip"
echo -e "${YELLOW}📦 Creating deployment package for $arch_name...${NC}"
cd "$BUILD_DIR/$arch_name"
zip -r "../../$package_name" extensions/ otel-instrument nodejs/
cd - > /dev/null
PACKAGE_SIZE=$(du -h "target/$package_name" | cut -f1)
echo -e "${GREEN}✅ Package created: target/$package_name ($PACKAGE_SIZE)${NC}"
}
# Create deployment packages for all built targets
create_packages() {
echo -e "${YELLOW}📦 Creating deployment packages...${NC}"
if [ "$BUILD_TARGETS" = "all" ]; then
for i in "${!ARCH_NAMES[@]}"; do
create_package_for_target "${ARCH_NAMES[$i]}"
done
echo -e "${BLUE}📋 Available packages:${NC}"
for arch in "${ARCH_NAMES[@]}"; do
if [ -f "target/o2-lambda-extension-$arch.zip" ]; then
PACKAGE_SIZE=$(du -h "target/o2-lambda-extension-$arch.zip" | cut -f1)
echo -e " - target/o2-lambda-extension-$arch.zip ($PACKAGE_SIZE)"
fi
done
else
# Find the architecture name for the specified target
arch_name="unknown"
for i in "${!TARGETS[@]}"; do
if [ "${TARGETS[$i]}" = "$BUILD_TARGETS" ]; then
arch_name="${ARCH_NAMES[$i]}"
break
fi
done
create_package_for_target "$arch_name"
fi
echo -e "${GREEN}✅ All packages created${NC}"
}
# Validate package for a specific architecture
validate_package_for_target() {
local arch_name=$1
local package_dir="$BUILD_DIR/$arch_name/extensions"
local package_name="o2-lambda-extension-$arch_name.zip"
echo -e "${YELLOW}🔍 Validating package for $arch_name...${NC}"
# Check if the binary exists and is executable
if [ ! -x "$package_dir/$EXTENSION_NAME" ]; then
echo -e "${RED}❌ Error: Extension binary for $arch_name is not executable${NC}"
return 1
fi
# Check binary size (should be reasonably small)
BINARY_SIZE=$(du -h "$package_dir/$EXTENSION_NAME" | cut -f1)
echo -e "${BLUE}📊 Binary size ($arch_name): $BINARY_SIZE${NC}"
# List package contents
echo -e "${BLUE}📋 Package contents ($arch_name):${NC}"
if [ -f "target/$package_name" ]; then
unzip -l "target/$package_name"
else
echo -e "${RED}❌ Package file not found: target/$package_name${NC}"
return 1
fi
echo -e "${GREEN}✅ Package validation completed for $arch_name${NC}"
}
# Validate all packages
validate_packages() {
echo -e "${YELLOW}🔍 Validating packages...${NC}"
if [ "$BUILD_TARGETS" = "all" ]; then
for arch in "${ARCH_NAMES[@]}"; do
validate_package_for_target "$arch"
if [ $? -ne 0 ]; then
echo -e "${RED}❌ Validation failed for $arch${NC}"
exit 1
fi
done
else
# Find the architecture name for the specified target
arch_name="unknown"
for i in "${!TARGETS[@]}"; do
if [ "${TARGETS[$i]}" = "$BUILD_TARGETS" ]; then
arch_name="${ARCH_NAMES[$i]}"
break
fi
done
validate_package_for_target "$arch_name"
if [ $? -ne 0 ]; then
echo -e "${RED}❌ Validation failed${NC}"
exit 1
fi
fi
echo -e "${GREEN}✅ All package validations completed${NC}"
}
# Main execution flow
main() {
echo -e "${BLUE}Starting build process...${NC}\n"
check_requirements
setup_targets
clean_build
build_extensions
create_layer_structures
create_packages
validate_packages
echo -e "\n${GREEN}🎉 Build completed successfully!${NC}"
if [ "$BUILD_TARGETS" = "all" ]; then
echo -e "${BLUE}📦 Packages created for all architectures:${NC}"
for arch in "${ARCH_NAMES[@]}"; do
if [ -f "target/o2-lambda-extension-$arch.zip" ]; then
echo -e " - target/o2-lambda-extension-$arch.zip (for AWS Lambda $arch)"
fi
done
else
arch_name="unknown"
for i in "${!TARGETS[@]}"; do
if [ "${TARGETS[$i]}" = "$BUILD_TARGETS" ]; then
arch_name="${ARCH_NAMES[$i]}"
break
fi
done
echo -e "${BLUE}📦 Package: target/o2-lambda-extension-$arch_name.zip${NC}"
fi
echo -e "${BLUE}🚀 Ready to deploy as Lambda layers${NC}"
# Show deployment instructions
echo -e "\n${YELLOW}📚 Deployment Instructions:${NC}"
if [ "$BUILD_TARGETS" = "all" ]; then
echo -e "1. Choose the appropriate package for your Lambda architecture:"
echo -e " - target/o2-lambda-extension-x86_64.zip for x86_64 Lambda functions"
echo -e " - target/o2-lambda-extension-arm64.zip for arm64 Lambda functions"
echo -e "2. Upload the chosen package to AWS Lambda as a new layer"
else
arch_name="unknown"
for i in "${!TARGETS[@]}"; do
if [ "${TARGETS[$i]}" = "$BUILD_TARGETS" ]; then
arch_name="${ARCH_NAMES[$i]}"
break
fi
done
echo -e "1. Upload target/o2-lambda-extension-$arch_name.zip to AWS Lambda as a new layer"
echo -e "2. Ensure your Lambda function architecture matches: $arch_name"
fi
echo -e "3. Set the following environment variables on your Lambda function:"
echo -e " - O2_ORGANIZATION_ID=your_organization_id"
echo -e " - O2_AUTHORIZATION_HEADER=\"Basic your_base64_encoded_credentials\""
echo -e " - O2_ENDPOINT=https://api.openobserve.ai (optional)"
echo -e " - O2_STREAM=default (optional)"
echo -e "4. Add the layer to your Lambda function"
echo -e "5. The extension will automatically start capturing and forwarding logs"
echo -e "\n${BLUE}💡 Architecture Notes:${NC}"
echo -e "• x86_64: Traditional Intel/AMD 64-bit architecture (most common)"
echo -e "• arm64: AWS Graviton2/3 processors (better price-performance for many workloads)"
echo -e "• Layer architecture must match your Lambda function architecture"
}
# Handle script arguments
case "${1:-build}" in
"build")
main
;;
"clean")
echo -e "${YELLOW}🧹 Cleaning build artifacts...${NC}"
rm -rf $BUILD_DIR
cargo clean
echo -e "${GREEN}✅ Clean completed${NC}"
;;
"test")
echo -e "${YELLOW}🧪 Running tests...${NC}"
cargo test
;;
"check")
echo -e "${YELLOW}🔍 Running cargo check...${NC}"
if [ "$BUILD_TARGETS" = "all" ]; then
for target in "${TARGETS[@]}"; do
echo -e "${BLUE}Checking target: $target${NC}"
cargo check --target $target
done
else
cargo check --target $BUILD_TARGETS
fi
;;
"help"|"-h"|"--help")
echo -e "${BLUE}OpenObserve Lambda Extension Build Script${NC}"
echo ""
echo "Usage: $0 [command]"
echo ""
echo "Commands:"
echo " build Build and package the extension for all architectures (default)"
echo " clean Clean build artifacts"
echo " test Run tests"
echo " check Run cargo check for target(s)"
echo " help Show this help message"
echo ""
echo "Default Behavior:"
echo " • Builds for BOTH x86_64 AND arm64 architectures by default"
echo " • Creates separate packages for each architecture:"
echo " - target/o2-lambda-extension-x86_64.zip"
echo " - target/o2-lambda-extension-arm64.zip"
echo ""
echo "Environment Variables:"
echo " BUILD_TARGETS Override default multi-architecture build:"
echo " 'all' - Build for all architectures (default)"
echo " 'x86_64-unknown-linux-musl' - Build for x86_64 only"
echo " 'aarch64-unknown-linux-musl' - Build for arm64 only"
echo ""
echo "Examples:"
echo " $0 # Build both x86_64 + arm64 (default)"
echo " $0 build # Same as above"
echo " BUILD_TARGETS=x86_64-unknown-linux-musl $0 # Build x86_64 only"
echo " BUILD_TARGETS=aarch64-unknown-linux-musl $0 # Build arm64 only"
echo ""
echo "Architecture Guide:"
echo " • x86_64: Traditional Intel/AMD processors (most common)"
echo " • arm64: AWS Graviton2/3 processors (better price-performance)"
;;
*)
echo -e "${RED}❌ Unknown command: $1${NC}"
echo "Use '$0 help' for usage information"
exit 1
;;
esac