|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +CONFIG_PATH="./.github/actions/start-local-network/config.yaml" |
| 6 | +COMPOSE_PATH="./.github/actions/start-local-network/gas_station_compose.yml" |
| 7 | +CONFIG_BACKUP="$CONFIG_PATH.backup" |
| 8 | +IOTA_LOG="iota_network.log" |
| 9 | + |
| 10 | +if [ "$1" == "start" ]; then |
| 11 | + echo "Starting local IOTA network with gas station..." |
| 12 | + |
| 13 | + # Backup config file |
| 14 | + if [ -f "$CONFIG_BACKUP" ]; then |
| 15 | + echo "Backup already exists, skipping backup" |
| 16 | + else |
| 17 | + cp "$CONFIG_PATH" "$CONFIG_BACKUP" |
| 18 | + echo "Backed up config to $CONFIG_BACKUP" |
| 19 | + fi |
| 20 | + |
| 21 | + # Start PostgreSQL |
| 22 | + echo "Starting PostgreSQL..." |
| 23 | + docker start postgres || docker run -d --name postgres -e POSTGRES_PASSWORD=postgrespw -e POSTGRES_INITDB_ARGS="-U postgres" -p 5432:5432 postgres:15 -c max_connections=1000 |
| 24 | + |
| 25 | + # Start IOTA network |
| 26 | + echo "Starting IOTA network..." |
| 27 | + RUST_LOG="info,consensus=warn,iota_core=warn,fastcrypto_tbls=off,iota_indexer=warn,iota_data_ingestion_core=error,iota_graphql_rpc=warn" iota start --force-regenesis --with-faucet --with-indexer --with-graphql >> "$IOTA_LOG" 2>&1 & |
| 28 | + IOTA_PID=$! |
| 29 | + |
| 30 | + # Use all 9's private key for gas station |
| 31 | + keyWithFlag="AJmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ" # iotaprivkey1qzvenxvenxvenxvenxvenxvenxvenxvenxvenxvenxvenxvenxvejj8c0wa |
| 32 | + address="0xa7c2cf9d8f8d95ff69d7a598c49c77acc36253f496f064a533ad306879b40bfa" |
| 33 | + |
| 34 | + echo "Setting keypair in config..." |
| 35 | + sed -i "s|<keypair>|$keyWithFlag|g" "$CONFIG_PATH" |
| 36 | + |
| 37 | + echo "Waiting for network to start and requesting faucet coins..." |
| 38 | + success=false |
| 39 | + for i in {1..30}; do |
| 40 | + sleep 1 |
| 41 | + if iota client faucet --url http://127.0.0.1:9123/gas --address $address >/dev/null 2>&1; then |
| 42 | + success=true |
| 43 | + break |
| 44 | + fi |
| 45 | + done |
| 46 | + if ! $success; then |
| 47 | + echo "Failed to request faucet coins after 30 seconds" |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | + |
| 51 | + echo "Starting Gas Station..." |
| 52 | + # Set gas station auth |
| 53 | + export GAS_STATION_AUTH=test |
| 54 | + docker compose -f "$COMPOSE_PATH" -p start-local-network up -d |
| 55 | + |
| 56 | + echo "Waiting for gas station to be ready..." |
| 57 | + success=false |
| 58 | + for i in {1..30}; do |
| 59 | + sleep 1 |
| 60 | + if curl --silent --fail http://0.0.0.0:9527/version >/dev/null 2>&1; then |
| 61 | + success=true |
| 62 | + break |
| 63 | + fi |
| 64 | + done |
| 65 | + if ! $success; then |
| 66 | + echo "Gas station did not become ready after 30 seconds" |
| 67 | + exit 1 |
| 68 | + fi |
| 69 | + |
| 70 | + echo "Local network and gas station started successfully!" |
| 71 | + echo "IOTA PID: $IOTA_PID" |
| 72 | + echo "Logs are being written to $IOTA_LOG" |
| 73 | + echo "To view logs: $0 logs" |
| 74 | + echo "To stop, run: $0 stop" |
| 75 | + |
| 76 | +elif [ "$1" == "stop" ]; then |
| 77 | + echo "Stopping local IOTA network and gas station..." |
| 78 | + |
| 79 | + # Stop gas station |
| 80 | + echo "Stopping Gas Station..." |
| 81 | + # Flush Redis data before stopping |
| 82 | + redis-cli FLUSHALL || echo "Could not flush Redis data" |
| 83 | + docker compose -f "$COMPOSE_PATH" -p start-local-network down |
| 84 | + |
| 85 | + # Remove Redis volume to clean persisted data |
| 86 | + echo "Removing Redis data volume..." |
| 87 | + docker volume rm start-local-network_redis_data || echo "Redis volume not found or already removed" |
| 88 | + |
| 89 | + # Stop IOTA network |
| 90 | + echo "Stopping IOTA network..." |
| 91 | + pkill -f "iota start" || echo "IOTA process not found or already stopped" |
| 92 | + |
| 93 | + # Stop PostgreSQL |
| 94 | + echo "Stopping PostgreSQL..." |
| 95 | + docker stop postgres || echo "PostgreSQL not running" |
| 96 | + |
| 97 | + # Restore config |
| 98 | + if [ -f "$CONFIG_BACKUP" ]; then |
| 99 | + mv "$CONFIG_BACKUP" "$CONFIG_PATH" |
| 100 | + echo "Restored config from backup" |
| 101 | + else |
| 102 | + echo "No backup found, config may have been manually modified" |
| 103 | + fi |
| 104 | + |
| 105 | + # Clean up log file |
| 106 | + if [ -f "$IOTA_LOG" ]; then |
| 107 | + rm "$IOTA_LOG" |
| 108 | + echo "Removed log file $IOTA_LOG" |
| 109 | + fi |
| 110 | + |
| 111 | + echo "Local network and gas station stopped." |
| 112 | + |
| 113 | +elif [ "$1" == "logs" ]; then |
| 114 | + if [ -f "$IOTA_LOG" ]; then |
| 115 | + tail -f "$IOTA_LOG" |
| 116 | + else |
| 117 | + echo "Log file $IOTA_LOG not found. Start the network first." |
| 118 | + fi |
| 119 | + |
| 120 | +else |
| 121 | + echo "Usage: $0 start|stop|logs" |
| 122 | + echo " start: Start the local IOTA network with gas station" |
| 123 | + echo " stop: Stop the local IOTA network and gas station" |
| 124 | + echo " logs: View the latest IOTA network logs (follow mode)" |
| 125 | +fi |
0 commit comments