/ testnet / scripts / deploy-testnet.sh
deploy-testnet.sh
  1  #!/usr/bin/env bash
  2  # =============================================================================
  3  # ALPHA/DELTA Network - Testnet Deployment Script
  4  # =============================================================================
  5  # This script deploys the testnet using ac-dc fleet management.
  6  #
  7  # Prerequisites:
  8  #   - ac-dc binary installed and in PATH
  9  #   - SSH keys configured for all nodes
 10  #   - Node VMs provisioned and accessible
 11  #
 12  # Usage:
 13  #   ./deploy-testnet.sh [options]
 14  #
 15  # Options:
 16  #   --validators-only    Deploy only validator nodes
 17  #   --rpc-only          Deploy only RPC nodes
 18  #   --dry-run           Show what would be deployed without making changes
 19  #   --skip-genesis      Skip genesis generation (use existing)
 20  #   --config-dir DIR    Configuration directory (default: ./config)
 21  #   --help              Show this help message
 22  
 23  set -euo pipefail
 24  
 25  # Script directory
 26  SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
 27  CONFIG_DIR="${SCRIPT_DIR}/../config"
 28  TESTNET_DIR="${SCRIPT_DIR}/.."
 29  
 30  # Colors for output
 31  RED='\033[0;31m'
 32  GREEN='\033[0;32m'
 33  YELLOW='\033[1;33m'
 34  BLUE='\033[0;34m'
 35  NC='\033[0m' # No Color
 36  
 37  # Default options
 38  VALIDATORS_ONLY=false
 39  RPC_ONLY=false
 40  DRY_RUN=false
 41  SKIP_GENESIS=false
 42  
 43  # Parse command line arguments
 44  while [[ $# -gt 0 ]]; do
 45      case $1 in
 46          --validators-only)
 47              VALIDATORS_ONLY=true
 48              shift
 49              ;;
 50          --rpc-only)
 51              RPC_ONLY=true
 52              shift
 53              ;;
 54          --dry-run)
 55              DRY_RUN=true
 56              shift
 57              ;;
 58          --skip-genesis)
 59              SKIP_GENESIS=true
 60              shift
 61              ;;
 62          --config-dir)
 63              CONFIG_DIR="$2"
 64              shift 2
 65              ;;
 66          --help)
 67              grep -E '^#' "$0" | grep -v '#!/' | sed 's/^# //' | sed 's/^#//'
 68              exit 0
 69              ;;
 70          *)
 71              echo -e "${RED}Unknown option: $1${NC}"
 72              exit 1
 73              ;;
 74      esac
 75  done
 76  
 77  log_info() {
 78      echo -e "${BLUE}[INFO]${NC} $1"
 79  }
 80  
 81  log_success() {
 82      echo -e "${GREEN}[SUCCESS]${NC} $1"
 83  }
 84  
 85  log_warn() {
 86      echo -e "${YELLOW}[WARN]${NC} $1"
 87  }
 88  
 89  log_error() {
 90      echo -e "${RED}[ERROR]${NC} $1"
 91  }
 92  
 93  # Check prerequisites
 94  check_prerequisites() {
 95      log_info "Checking prerequisites..."
 96  
 97      # Check ac-dc is installed
 98      if ! command -v ac-dc &> /dev/null; then
 99          log_error "ac-dc binary not found in PATH"
100          log_info "Install with: cargo install --path /path/to/ac-dc/crates/acdc"
101          exit 1
102      fi
103  
104      # Check configuration files exist
105      if [[ ! -f "${CONFIG_DIR}/fleet-testnet.toml" ]]; then
106          log_error "Fleet configuration not found: ${CONFIG_DIR}/fleet-testnet.toml"
107          exit 1
108      fi
109  
110      if [[ ! -f "${CONFIG_DIR}/adnet-testnet.toml" ]]; then
111          log_error "ADNet configuration not found: ${CONFIG_DIR}/adnet-testnet.toml"
112          exit 1
113      fi
114  
115      # Check SSH key exists
116      SSH_KEY=$(grep -oP 'key_path = "\K[^"]+' "${CONFIG_DIR}/fleet-testnet.toml" | head -1)
117      SSH_KEY="${SSH_KEY/#\~/$HOME}"
118      if [[ ! -f "${SSH_KEY}" ]]; then
119          log_warn "SSH key not found: ${SSH_KEY}"
120          log_info "Generate with: ssh-keygen -t ed25519 -f ${SSH_KEY}"
121      fi
122  
123      log_success "Prerequisites check passed"
124  }
125  
126  # Generate genesis files if needed
127  generate_genesis() {
128      if [[ "${SKIP_GENESIS}" == "true" ]]; then
129          log_info "Skipping genesis generation (--skip-genesis)"
130          return
131      fi
132  
133      log_info "Generating genesis files..."
134  
135      GENESIS_DIR="${TESTNET_DIR}/genesis"
136      mkdir -p "${GENESIS_DIR}"
137  
138      # Generate validator keys
139      log_info "Generating validator keys..."
140      for i in {1..5}; do
141          if [[ "${DRY_RUN}" == "true" ]]; then
142              log_info "[DRY-RUN] Would generate keys for validator-${i}"
143          else
144              # In real deployment, use adnet to generate keys
145              # adnet alpha account new --output "${GENESIS_DIR}/validator-${i}.json"
146              log_info "Key generation for validator-${i} (placeholder)"
147          fi
148      done
149  
150      # Generate genesis.json for both chains
151      if [[ "${DRY_RUN}" == "true" ]]; then
152          log_info "[DRY-RUN] Would generate genesis files"
153      else
154          log_info "Genesis file generation (placeholder - requires key material)"
155      fi
156  
157      log_success "Genesis files prepared"
158  }
159  
160  # Deploy to validators
161  deploy_validators() {
162      if [[ "${RPC_ONLY}" == "true" ]]; then
163          log_info "Skipping validators (--rpc-only)"
164          return
165      fi
166  
167      log_info "Deploying to validator nodes..."
168  
169      local tags="validator"
170  
171      if [[ "${DRY_RUN}" == "true" ]]; then
172          log_info "[DRY-RUN] Would deploy to validators with tags: ${tags}"
173          ac-dc fleet status --config "${CONFIG_DIR}/fleet-testnet.toml" --tags "${tags}" || true
174      else
175          # Step 1: Check system requirements
176          log_info "Checking system requirements on validators..."
177          ac-dc fleet exec --config "${CONFIG_DIR}/fleet-testnet.toml" \
178              --tags "${tags}" \
179              --command "ac-dc check --role validator"
180  
181          # Step 2: Install adnet binary
182          log_info "Installing adnet binary on validators..."
183          ac-dc fleet exec --config "${CONFIG_DIR}/fleet-testnet.toml" \
184              --tags "${tags}" \
185              --command "ac-dc install --method binary --component adnet"
186  
187          # Step 3: Deploy configuration
188          log_info "Deploying configuration to validators..."
189          for i in {1..5}; do
190              local host="testnet-validator-${i}.ac-dc.network"
191              scp -i "${SSH_KEY}" \
192                  "${CONFIG_DIR}/adnet-testnet.toml" \
193                  "ac-dc@${host}:/etc/adnet/config.toml"
194          done
195  
196          # Step 4: Deploy genesis files
197          log_info "Deploying genesis files to validators..."
198          for i in {1..5}; do
199              local host="testnet-validator-${i}.ac-dc.network"
200              scp -i "${SSH_KEY}" \
201                  "${TESTNET_DIR}/genesis/"* \
202                  "ac-dc@${host}:/var/lib/adnet/genesis/"
203          done
204  
205          # Step 5: Start services
206          log_info "Starting adnet services on validators..."
207          ac-dc fleet exec --config "${CONFIG_DIR}/fleet-testnet.toml" \
208              --tags "${tags}" \
209              --command "ac-dc start --node-type validator"
210      fi
211  
212      log_success "Validator deployment complete"
213  }
214  
215  # Deploy to RPC nodes
216  deploy_rpc_nodes() {
217      if [[ "${VALIDATORS_ONLY}" == "true" ]]; then
218          log_info "Skipping RPC nodes (--validators-only)"
219          return
220      fi
221  
222      log_info "Deploying to RPC nodes..."
223  
224      local tags="rpc"
225  
226      if [[ "${DRY_RUN}" == "true" ]]; then
227          log_info "[DRY-RUN] Would deploy to RPC nodes with tags: ${tags}"
228      else
229          # Step 1: Check system requirements
230          log_info "Checking system requirements on RPC nodes..."
231          ac-dc fleet exec --config "${CONFIG_DIR}/fleet-testnet.toml" \
232              --tags "${tags}" \
233              --command "ac-dc check --role client"
234  
235          # Step 2: Install adnet binary
236          log_info "Installing adnet binary on RPC nodes..."
237          ac-dc fleet exec --config "${CONFIG_DIR}/fleet-testnet.toml" \
238              --tags "${tags}" \
239              --command "ac-dc install --method binary --component adnet"
240  
241          # Step 3: Deploy configuration (with RPC-specific settings)
242          log_info "Deploying configuration to RPC nodes..."
243          # RPC nodes use client configuration
244          ac-dc fleet exec --config "${CONFIG_DIR}/fleet-testnet.toml" \
245              --tags "${tags}" \
246              --command "ac-dc configure --node-type client --network testnet"
247  
248          # Step 4: Start services
249          log_info "Starting adnet services on RPC nodes..."
250          ac-dc fleet exec --config "${CONFIG_DIR}/fleet-testnet.toml" \
251              --tags "${tags}" \
252              --command "ac-dc start --node-type client"
253      fi
254  
255      log_success "RPC node deployment complete"
256  }
257  
258  # Deploy monitoring
259  deploy_monitoring() {
260      if [[ "${VALIDATORS_ONLY}" == "true" ]] || [[ "${RPC_ONLY}" == "true" ]]; then
261          log_info "Skipping monitoring deployment"
262          return
263      fi
264  
265      log_info "Deploying monitoring infrastructure..."
266  
267      local tags="monitoring"
268  
269      if [[ "${DRY_RUN}" == "true" ]]; then
270          log_info "[DRY-RUN] Would deploy monitoring with tags: ${tags}"
271      else
272          # Deploy Prometheus configuration
273          log_info "Deploying Prometheus configuration..."
274          ac-dc fleet exec --config "${CONFIG_DIR}/fleet-testnet.toml" \
275              --tags "${tags}" \
276              --command "ac-dc monitor setup --prometheus"
277  
278          # Generate and deploy Grafana dashboards
279          log_info "Deploying Grafana dashboards..."
280          ac-dc monitor dashboard --output "/tmp/adnet-dashboard.json"
281          # Deploy to monitoring node
282      fi
283  
284      log_success "Monitoring deployment complete"
285  }
286  
287  # Verify deployment
288  verify_deployment() {
289      log_info "Verifying deployment..."
290  
291      if [[ "${DRY_RUN}" == "true" ]]; then
292          log_info "[DRY-RUN] Would verify deployment"
293          return
294      fi
295  
296      # Check fleet status
297      log_info "Checking fleet status..."
298      ac-dc fleet status --config "${CONFIG_DIR}/fleet-testnet.toml"
299  
300      # Run health checks
301      log_info "Running health checks..."
302      ac-dc fleet exec --config "${CONFIG_DIR}/fleet-testnet.toml" \
303          --parallel \
304          --command "ac-dc health"
305  
306      # Verify consensus
307      log_info "Verifying consensus status..."
308      ac-dc fleet exec --config "${CONFIG_DIR}/fleet-testnet.toml" \
309          --tags "validator" \
310          --command "ac-dc status --format json | jq '.consensus'"
311  
312      log_success "Deployment verification complete"
313  }
314  
315  # Main deployment flow
316  main() {
317      echo "=============================================="
318      echo "  ALPHA/DELTA Network - Testnet Deployment"
319      echo "=============================================="
320      echo ""
321  
322      if [[ "${DRY_RUN}" == "true" ]]; then
323          log_warn "Running in DRY-RUN mode - no changes will be made"
324          echo ""
325      fi
326  
327      check_prerequisites
328      generate_genesis
329      deploy_validators
330      deploy_rpc_nodes
331      deploy_monitoring
332      verify_deployment
333  
334      echo ""
335      echo "=============================================="
336      log_success "Testnet deployment complete!"
337      echo "=============================================="
338      echo ""
339      echo "Next steps:"
340      echo "  1. Verify all nodes are syncing: ac-dc fleet status"
341      echo "  2. Check consensus: ac-dc fleet exec --tags validator --command 'ac-dc status'"
342      echo "  3. Test faucet: curl https://testnet-faucet.ac-dc.network/drip/<address>"
343      echo "  4. Monitor: https://testnet-monitor.ac-dc.network/grafana"
344      echo ""
345  }
346  
347  main "$@"