299 lines
8.3 KiB
Bash
299 lines
8.3 KiB
Bash
|
|
#!/bin/bash
|
||
|
|
# Demo 4: Cross-Compilation Strategy - Rust Meetup 2025
|
||
|
|
# Demostración de capacidades multi-arquitectura
|
||
|
|
|
||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
echo "🔧 Demo 4: Cross-Compilation Strategy"
|
||
|
|
echo "====================================="
|
||
|
|
|
||
|
|
# Colores para output
|
||
|
|
RED='\033[0;31m'
|
||
|
|
GREEN='\033[0;32m'
|
||
|
|
YELLOW='\033[1;33m'
|
||
|
|
BLUE='\033[0;34m'
|
||
|
|
NC='\033[0m' # No Color
|
||
|
|
|
||
|
|
log() {
|
||
|
|
echo -e "${GREEN}[$(date +'%H:%M:%S')]${NC} $1"
|
||
|
|
}
|
||
|
|
|
||
|
|
warn() {
|
||
|
|
echo -e "${YELLOW}[$(date +'%H:%M:%S')] WARN:${NC} $1"
|
||
|
|
}
|
||
|
|
|
||
|
|
error() {
|
||
|
|
echo -e "${RED}[$(date +'%H:%M:%S')] ERROR:${NC} $1"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Parte 1: Auto-detección de arquitectura
|
||
|
|
echo ""
|
||
|
|
log "Parte 1: Auto-detección de arquitectura"
|
||
|
|
echo "========================================"
|
||
|
|
|
||
|
|
detect_arch() {
|
||
|
|
local arch=$(uname -m)
|
||
|
|
case $arch in
|
||
|
|
x86_64)
|
||
|
|
echo "x86_64-unknown-linux-gnu"
|
||
|
|
;;
|
||
|
|
aarch64|arm64)
|
||
|
|
echo "aarch64-unknown-linux-gnu"
|
||
|
|
;;
|
||
|
|
armv7l)
|
||
|
|
echo "armv7-unknown-linux-gnueabihf"
|
||
|
|
;;
|
||
|
|
*)
|
||
|
|
echo "unsupported"
|
||
|
|
;;
|
||
|
|
esac
|
||
|
|
}
|
||
|
|
|
||
|
|
CURRENT_ARCH=$(detect_arch)
|
||
|
|
log "Arquitectura detectada: $CURRENT_ARCH"
|
||
|
|
log "Sistema: $(uname -s) $(uname -m)"
|
||
|
|
|
||
|
|
# Mostrar targets soportados
|
||
|
|
echo ""
|
||
|
|
log "Targets de compilación soportados:"
|
||
|
|
SUPPORTED_TARGETS=(
|
||
|
|
"x86_64-unknown-linux-gnu"
|
||
|
|
"aarch64-unknown-linux-gnu"
|
||
|
|
"x86_64-apple-darwin"
|
||
|
|
"aarch64-apple-darwin"
|
||
|
|
"x86_64-pc-windows-gnu"
|
||
|
|
)
|
||
|
|
|
||
|
|
for target in "${SUPPORTED_TARGETS[@]}"; do
|
||
|
|
if [ "$target" == "$CURRENT_ARCH" ]; then
|
||
|
|
echo " ✅ $target (actual)"
|
||
|
|
else
|
||
|
|
echo " 📦 $target"
|
||
|
|
fi
|
||
|
|
done
|
||
|
|
|
||
|
|
# Parte 2: Instalación de cross-compilation tools
|
||
|
|
echo ""
|
||
|
|
log "Parte 2: Setup de herramientas cross-compilation"
|
||
|
|
echo "================================================"
|
||
|
|
|
||
|
|
echo "$ cargo install cross --git https://github.com/cross-rs/cross"
|
||
|
|
log "✅ Cross-compilation tool instalado"
|
||
|
|
|
||
|
|
echo "$ rustup target add aarch64-unknown-linux-gnu"
|
||
|
|
echo "$ rustup target add x86_64-unknown-linux-gnu"
|
||
|
|
log "✅ Targets de Rust añadidos"
|
||
|
|
|
||
|
|
# Parte 3: Configuración para herramientas Rust
|
||
|
|
echo ""
|
||
|
|
log "Parte 3: Configuración para herramientas del ecosistema"
|
||
|
|
echo "======================================================"
|
||
|
|
|
||
|
|
# Simular Cross.toml configuration
|
||
|
|
cat << 'EOF'
|
||
|
|
📄 Cross.toml configuración:
|
||
|
|
```toml
|
||
|
|
[build.env]
|
||
|
|
passthrough = [
|
||
|
|
"CARGO_HOME",
|
||
|
|
"CARGO_TARGET_DIR",
|
||
|
|
]
|
||
|
|
|
||
|
|
[target.aarch64-unknown-linux-gnu]
|
||
|
|
dockerfile = "./docker/Dockerfile.aarch64"
|
||
|
|
image = "rust-cross:aarch64"
|
||
|
|
|
||
|
|
[target.x86_64-unknown-linux-gnu]
|
||
|
|
dockerfile = "./docker/Dockerfile.x86_64"
|
||
|
|
image = "rust-cross:x86_64"
|
||
|
|
```
|
||
|
|
EOF
|
||
|
|
|
||
|
|
# Parte 4: Build script inteligente
|
||
|
|
echo ""
|
||
|
|
log "Parte 4: Build script multi-arquitectura"
|
||
|
|
echo "========================================"
|
||
|
|
|
||
|
|
build_for_target() {
|
||
|
|
local target=$1
|
||
|
|
local component=$2
|
||
|
|
|
||
|
|
log "Compilando $component para $target..."
|
||
|
|
|
||
|
|
# Simular proceso de build
|
||
|
|
case $component in
|
||
|
|
"youki")
|
||
|
|
echo " 🦀 Compilando youki (OCI runtime)..."
|
||
|
|
echo " 📦 Features: seccomp, systemd, v2"
|
||
|
|
;;
|
||
|
|
"cosmian-kms-client")
|
||
|
|
echo " 🔐 Compilando cliente Cosmian KMS..."
|
||
|
|
echo " 📦 Features: tls, async-tokio"
|
||
|
|
;;
|
||
|
|
"polkadot-node")
|
||
|
|
echo " 🕸️ Compilando nodo Polkadot..."
|
||
|
|
echo " 📦 Features: runtime-benchmarks"
|
||
|
|
;;
|
||
|
|
esac
|
||
|
|
|
||
|
|
sleep 1
|
||
|
|
log "✅ $component compilado para $target"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Simular builds para diferentes componentes
|
||
|
|
RUST_COMPONENTS=("youki" "cosmian-kms-client" "polkadot-node")
|
||
|
|
|
||
|
|
for component in "${RUST_COMPONENTS[@]}"; do
|
||
|
|
build_for_target "x86_64-unknown-linux-gnu" "$component"
|
||
|
|
build_for_target "aarch64-unknown-linux-gnu" "$component"
|
||
|
|
done
|
||
|
|
|
||
|
|
# Parte 5: Packaging inteligente
|
||
|
|
echo ""
|
||
|
|
log "Parte 5: Packaging multi-arquitectura"
|
||
|
|
echo "===================================="
|
||
|
|
|
||
|
|
package_release() {
|
||
|
|
local target=$1
|
||
|
|
local version=${2:-"v1.0.0"}
|
||
|
|
|
||
|
|
log "Empaquetando release para $target..."
|
||
|
|
|
||
|
|
# Simular estructura de package
|
||
|
|
echo " 📁 provisioning-$version-$target/"
|
||
|
|
echo " ├── bin/"
|
||
|
|
echo " │ ├── provisioning"
|
||
|
|
echo " │ ├── youki"
|
||
|
|
echo " │ └── cosmian-kms-client"
|
||
|
|
echo " ├── lib/"
|
||
|
|
echo " │ └── nu_plugins/"
|
||
|
|
echo " ├── templates/"
|
||
|
|
echo " └── install.sh"
|
||
|
|
|
||
|
|
echo " 📦 Creando tarball..."
|
||
|
|
echo " ✅ provisioning-$version-$target.tar.gz"
|
||
|
|
}
|
||
|
|
|
||
|
|
for target in "x86_64-unknown-linux-gnu" "aarch64-unknown-linux-gnu"; do
|
||
|
|
package_release "$target" "v1.0.0"
|
||
|
|
done
|
||
|
|
|
||
|
|
# Parte 6: Deploy automático según arquitectura
|
||
|
|
echo ""
|
||
|
|
log "Parte 6: Deploy inteligente por arquitectura"
|
||
|
|
echo "============================================"
|
||
|
|
|
||
|
|
simulate_deploy() {
|
||
|
|
local server_ip=$1
|
||
|
|
local server_arch=$2
|
||
|
|
|
||
|
|
log "Desplegando en servidor $server_ip ($server_arch)..."
|
||
|
|
|
||
|
|
# Detectar arquitectura del servidor (simulado)
|
||
|
|
echo " 🔍 Detectando arquitectura del servidor..."
|
||
|
|
echo " 📡 SSH: ssh user@$server_ip 'uname -m'"
|
||
|
|
echo " 📋 Resultado: $server_arch"
|
||
|
|
|
||
|
|
# Seleccionar package correcto
|
||
|
|
local package_name="provisioning-v1.0.0-$server_arch-unknown-linux-gnu.tar.gz"
|
||
|
|
echo " 📦 Seleccionando package: $package_name"
|
||
|
|
|
||
|
|
# Simular deploy
|
||
|
|
echo " 🚀 Transfiriendo archivos..."
|
||
|
|
echo " 📥 scp $package_name user@$server_ip:/tmp/"
|
||
|
|
echo " 🔧 Instalando remotamente..."
|
||
|
|
echo " ✅ Deploy completado"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Simular deploy en diferentes arquitecturas
|
||
|
|
simulate_deploy "203.0.113.10" "x86_64"
|
||
|
|
simulate_deploy "203.0.113.11" "aarch64"
|
||
|
|
simulate_deploy "203.0.113.12" "x86_64"
|
||
|
|
|
||
|
|
# Parte 7: Ventajas del enfoque
|
||
|
|
echo ""
|
||
|
|
log "Parte 7: Ventajas de cross-compilation nativa"
|
||
|
|
echo "=============================================="
|
||
|
|
|
||
|
|
echo "💰 Costos optimizados:"
|
||
|
|
echo " - ARM instances ~20% más baratas que x86"
|
||
|
|
echo " - Graviton3: $0.04/hora vs t3.medium: $0.05/hora"
|
||
|
|
echo " - Ahorro anual: ~$87 por instancia"
|
||
|
|
|
||
|
|
echo ""
|
||
|
|
echo "⚡ Performance:"
|
||
|
|
echo " - Binarios nativos vs containers emulados"
|
||
|
|
echo " - youki en ARM: ~15% menos overhead vs Docker"
|
||
|
|
echo " - Polkadot: ~25% mejor throughput en Graviton"
|
||
|
|
|
||
|
|
echo ""
|
||
|
|
echo "🌐 Alcance:"
|
||
|
|
echo " - Edge computing: ARM dominante en IoT"
|
||
|
|
echo " - Mobile development: aarch64 nativo"
|
||
|
|
echo " - Apple Silicon: Mejor developer experience"
|
||
|
|
|
||
|
|
echo ""
|
||
|
|
echo "🔧 Operaciones:"
|
||
|
|
echo " - Un solo build system para todo"
|
||
|
|
echo " - Deploy automático según arquitectura destino"
|
||
|
|
echo " - Testing unificado cross-platform"
|
||
|
|
|
||
|
|
# Parte 8: Comparación con alternativas
|
||
|
|
echo ""
|
||
|
|
log "Parte 8: vs. Alternativas tradicionales"
|
||
|
|
echo "======================================="
|
||
|
|
|
||
|
|
echo "📊 Comparación con Docker multi-arch:"
|
||
|
|
echo ""
|
||
|
|
printf "%-20s %-15s %-15s\n" "Aspecto" "Cross-compile" "Docker buildx"
|
||
|
|
printf "%-20s %-15s %-15s\n" "Build time" "~3 min" "~15 min"
|
||
|
|
printf "%-20s %-15s %-15s\n" "Binary size" "~25MB" "~250MB"
|
||
|
|
printf "%-20s %-15s %-15s\n" "Runtime overhead" "0%" "5-10%"
|
||
|
|
printf "%-20s %-15s %-15s\n" "Cold start" "Inmediato" "~2-3s"
|
||
|
|
printf "%-20s %-15s %-15s\n" "Complexity" "Media" "Alta"
|
||
|
|
|
||
|
|
# Parte 9: Script de automatización
|
||
|
|
echo ""
|
||
|
|
log "Parte 9: Script de automatización completo"
|
||
|
|
echo "=========================================="
|
||
|
|
|
||
|
|
cat << 'EOF'
|
||
|
|
#!/bin/bash
|
||
|
|
# build-all.sh - Build script completo
|
||
|
|
|
||
|
|
TARGETS=("x86_64-unknown-linux-gnu" "aarch64-unknown-linux-gnu")
|
||
|
|
COMPONENTS=("youki" "cosmian-kms-client" "polkadot-node")
|
||
|
|
|
||
|
|
for target in "${TARGETS[@]}"; do
|
||
|
|
for component in "${COMPONENTS[@]}"; do
|
||
|
|
echo "Building $component for $target..."
|
||
|
|
cross build --release --target "$target" \
|
||
|
|
--manifest-path "tools/$component/Cargo.toml"
|
||
|
|
done
|
||
|
|
|
||
|
|
# Package release
|
||
|
|
mkdir -p "dist/$target"
|
||
|
|
cp target/$target/release/* "dist/$target/"
|
||
|
|
tar czf "provisioning-$target.tar.gz" -C "dist/$target" .
|
||
|
|
done
|
||
|
|
|
||
|
|
echo "✅ All builds completed!"
|
||
|
|
EOF
|
||
|
|
|
||
|
|
echo ""
|
||
|
|
log "🎯 Demo cross-compilation completada!"
|
||
|
|
echo ""
|
||
|
|
echo "⭐ Capacidades demostradas:"
|
||
|
|
echo " - Auto-detección de arquitectura"
|
||
|
|
echo " - Build multi-target automatizado"
|
||
|
|
echo " - Packaging inteligente"
|
||
|
|
echo " - Deploy automático por arquitectura"
|
||
|
|
echo " - Optimizaciones de costo y performance"
|
||
|
|
|
||
|
|
echo ""
|
||
|
|
echo "🚀 Para implementar en tu proyecto:"
|
||
|
|
echo "1. cargo install cross"
|
||
|
|
echo "2. rustup target add aarch64-unknown-linux-gnu"
|
||
|
|
echo "3. Configurar Cross.toml"
|
||
|
|
echo "4. Crear scripts de automatización"
|
||
|
|
echo "5. ¡Disfrutar de deploys multi-arch!"
|