#!/bin/bash # Deploy blockchain node and explorer inside the container set -e echo "🚀 Deploying Inside Container" echo "============================" # Colors GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' print_status() { echo -e "${GREEN}[INFO]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARN]${NC} $1" } # Check if we're in the container if [ ! -f /proc/1/environ ] || ! grep -q container=lxc /proc/1/environ 2>/dev/null; then if [ "$(hostname)" != "aitbc" ]; then print_warning "This script must be run inside the aitbc container" exit 1 fi fi # Stop existing services print_status "Stopping existing services..." systemctl stop blockchain-node blockchain-rpc nginx 2>/dev/null || true # Install dependencies print_status "Installing dependencies..." apt-get update apt-get install -y python3 python3-venv python3-pip git curl nginx # Deploy blockchain node print_status "Deploying blockchain node..." cd /opt rm -rf blockchain-node # The source is already in blockchain-node-src, copy it properly cp -r blockchain-node-src blockchain-node cd blockchain-node # Check if pyproject.toml exists if [ ! -f pyproject.toml ]; then print_warning "pyproject.toml not found, looking for it..." find . -name "pyproject.toml" -type f # If it's in a subdirectory, move everything up if [ -f blockchain-node-src/pyproject.toml ]; then print_status "Moving files from nested directory..." mv blockchain-node-src/* . rmdir blockchain-node-src fi fi # Create configuration print_status "Creating configuration..." cat > .env << EOL CHAIN_ID=ait-devnet DB_PATH=./data/chain.db RPC_BIND_HOST=0.0.0.0 RPC_BIND_PORT=8082 P2P_BIND_HOST=0.0.0.0 P2P_BIND_PORT=7070 PROPOSER_KEY=proposer_key_$(date +%s) MINT_PER_UNIT=1000 COORDINATOR_RATIO=0.05 GOSSIP_BACKEND=memory EOL # Create fresh data directory rm -rf data mkdir -p data/devnet # Setup Python environment python3 -m venv .venv source .venv/bin/activate pip install --upgrade pip pip install -e . # Generate genesis export PYTHONPATH="${PWD}/src:${PWD}/scripts:${PYTHONPATH:-}" python scripts/make_genesis.py --output data/devnet/genesis.json --force # Create systemd services print_status "Creating systemd services..." cat > /etc/systemd/system/blockchain-node.service << EOL [Unit] Description=AITBC Blockchain Node After=network.target [Service] Type=exec User=root WorkingDirectory=/opt/blockchain-node Environment=PATH=/opt/blockchain-node/.venv/bin:/usr/local/bin:/usr/bin:/bin Environment=PYTHONPATH=/opt/blockchain-node/src:/opt/blockchain-node/scripts ExecStart=/opt/blockchain-node/.venv/bin/python3 -m aitbc_chain.main Restart=always RestartSec=5 [Install] WantedBy=multi-user.target EOL cat > /etc/systemd/system/blockchain-rpc.service << EOL [Unit] Description=AITBC Blockchain RPC API After=blockchain-node.service [Service] Type=exec User=root WorkingDirectory=/opt/blockchain-node Environment=PATH=/opt/blockchain-node/.venv/bin:/usr/local/bin:/usr/bin:/bin Environment=PYTHONPATH=/opt/blockchain-node/src:/opt/blockchain-node/scripts ExecStart=/opt/blockchain-node/.venv/bin/python3 -m uvicorn aitbc_chain.app:app --host 0.0.0.0 --port 8082 Restart=always RestartSec=5 [Install] WantedBy=multi-user.target EOL # Start blockchain services print_status "Starting blockchain services..." systemctl daemon-reload systemctl enable blockchain-node blockchain-rpc systemctl start blockchain-node blockchain-rpc # Deploy explorer print_status "Deploying blockchain explorer..." cd /opt rm -rf blockchain-explorer mkdir -p blockchain-explorer cd blockchain-explorer # Create HTML explorer cat > index.html << 'EOF' AITBC Blockchain Explorer

AITBC Blockchain Explorer

Current Height

-

Latest Block

-

Node Status

-

Latest Blocks

Height Hash Timestamp Transactions
Loading blocks...
EOF # Configure nginx print_status "Configuring nginx..." cat > /etc/nginx/sites-available/blockchain-explorer << EOL server { listen 3000; server_name _; root /opt/blockchain-explorer; index index.html; location / { try_files \$uri \$uri/ =404; } } EOL ln -sf /etc/nginx/sites-available/blockchain-explorer /etc/nginx/sites-enabled/ rm -f /etc/nginx/sites-enabled/default nginx -t systemctl reload nginx # Wait for services to start print_status "Waiting for services to start..." sleep 5 # Check services print_status "Checking service status..." systemctl status blockchain-node blockchain-rpc nginx --no-pager | grep -E 'Active:|Main PID:' print_success "✅ Deployment complete in container!" echo "" echo "Services:" echo " - Blockchain Node RPC: http://localhost:8082" echo " - Blockchain Explorer: http://localhost:3000" echo "" echo "These are accessible from the host via port forwarding."