#!/usr/bin/env bash # Setup script for Besu node on VM # This script installs Docker, configures the node, and starts Besu set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/../lib/init.sh" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" # Configuration NODE_TYPE="${1:-validator}" NODE_INDEX="${2:-0}" CLUSTER_NAME="${CLUSTER_NAME:-defi-oracle-aks}" KEY_VAULT_NAME="${KEY_VAULT_NAME:-defi-oracle-kv}" GENESIS_FILE_PATH="${GENESIS_FILE_PATH:-/opt/besu/config/genesis.json}" log_success "Setting up Besu node: $NODE_TYPE-$NODE_INDEX" # Check if running as root if [ "$EUID" -ne 0 ]; then log_error "Please run as root or with sudo" exit 1 fi # Update system log_warn "Updating system..." apt-get update apt-get upgrade -y # Install dependencies log_warn "Installing dependencies..." apt-get install -y \ apt-transport-https \ ca-certificates \ curl \ gnupg \ lsb-release \ jq \ wget \ unzip \ software-properties-common # Install Docker if ! command -v docker &> /dev/null; then log_warn "Installing Docker..." curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null apt-get update apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin systemctl enable docker systemctl start docker usermod -aG docker $SUDO_USER log_success "✓ Docker installed" else log_success "✓ Docker already installed" fi # Install Azure CLI if ! command -v az &> /dev/null; then log_warn "Installing Azure CLI..." curl -sL https://aka.ms/InstallAzureCLIDeb | bash log_success "✓ Azure CLI installed" else log_success "✓ Azure CLI already installed" fi # Create directories log_warn "Creating directories..." mkdir -p /opt/besu/{data,config,keys,logs} chown -R $SUDO_USER:$SUDO_USER /opt/besu log_success "✓ Directories created" # Download genesis file log_warn "Downloading genesis file..." if [ -n "$GENESIS_FILE_URL" ]; then wget -q -O "$GENESIS_FILE_PATH" "$GENESIS_FILE_URL" log_success "✓ Genesis file downloaded" else log_warn "⚠ Genesis file URL not set, using local file" if [ -f "$PROJECT_ROOT/config/genesis.json" ]; then cp "$PROJECT_ROOT/config/genesis.json" "$GENESIS_FILE_PATH" log_success "✓ Genesis file copied" else log_error "✗ Genesis file not found" exit 1 fi fi # Download Besu configuration log_warn "Downloading Besu configuration..." CONFIG_FILE="/opt/besu/config/besu-config.toml" case $NODE_TYPE in validator) if [ -f "$PROJECT_ROOT/config/validators/besu-config.toml" ]; then cp "$PROJECT_ROOT/config/validators/besu-config.toml" "$CONFIG_FILE" else log_error "✗ Validator config file not found" exit 1 fi ;; sentry) if [ -f "$PROJECT_ROOT/config/sentries/besu-config.toml" ]; then cp "$PROJECT_ROOT/config/sentries/besu-config.toml" "$CONFIG_FILE" else log_error "✗ Sentry config file not found" exit 1 fi ;; rpc) if [ -f "$PROJECT_ROOT/config/rpc/besu-config.toml" ]; then cp "$PROJECT_ROOT/config/rpc/besu-config.toml" "$CONFIG_FILE" else log_error "✗ RPC config file not found" exit 1 fi ;; *) log_error "✗ Invalid node type: $NODE_TYPE" exit 1 ;; esac log_success "✓ Configuration file copied" # Download validator keys from Key Vault (if validator) if [ "$NODE_TYPE" == "validator" ]; then log_warn "Downloading validator keys from Key Vault..." # This would use Azure Managed Identity to access Key Vault # For now, we'll use a placeholder log_warn "⚠ Key download not implemented (requires Key Vault access)" fi # Create Docker Compose file log_warn "Creating Docker Compose file..." cat > /opt/besu/docker-compose.yml <> /opt/besu/docker-compose.yml <> /opt/besu/docker-compose.yml < /etc/systemd/system/besu.service <