github.com/iotexproject/iotex-core@v1.14.1-rc1/tools/devenv/setup.sh (about)

     1  #!/bin/bash
     2  
     3  # create 10 accounts
     4  # inject one account to the config.yaml for star server
     5  # inject all accounts to the genesis.yaml
     6  # show account addr and private key
     7  # start servcer
     8  
     9  # exit code:
    10  # 2: mkdir error
    11  # 3: build image error
    12  # 4: create account error
    13  # 5: copy config error
    14  # 6: set account to the server error
    15  # 7: show all acounts error
    16  # 8: startup error
    17  # 9: setup ioctl endpoint error
    18  
    19  # Colour codes
    20  YELLOW='\033[0;33m'
    21  RED='\033[0;31m'
    22  NC='\033[0m' # No Color
    23  
    24  # Commands
    25  IOCTL_CMD="ioctl"
    26  CREATE_10_ACCOUTS="$IOCTL_CMD account create -n 10"
    27  SET_ENDPOINT="$IOCTL_CMD config set endpoint localhost:14014 --insecure"
    28  SED_IS_GNU=0
    29  MKDIR="mkdir -p"
    30  
    31  # Dirs and files
    32  USER_DIR="${HOME}/.iotex"
    33  IOTEX_HOME="$HOME/iotex-var"
    34  DOCKER_COMPOSE_HOME="$IOTEX_HOME/docker-compose"
    35  ACCOUNTS_File="$USER_DIR/accounts"
    36  PROJECT_ABS_DIR=$(cd "$(dirname "$0")";pwd)
    37  REPO_ABS_DIR=$(cd "$PROJECT_ABS_DIR/../..";pwd)
    38  
    39  # Image
    40  IOTEX_IMAGE="iotex-core:local"
    41  
    42  pushd () {
    43      command pushd "$@" > /dev/null
    44  }
    45  
    46  popd () {
    47      command popd "$@" > /dev/null
    48  }
    49  
    50  function sedCheck() {
    51      sed --version > /dev/null 2>&1
    52      if [ $? -eq 0 ];then
    53          sed --version|grep 'GNU sed' > /dev/null 2>&1
    54          if [ $? -eq 0 ];then
    55              SED_IS_GNU=1
    56          fi
    57      fi
    58  }
    59  
    60  function checkDockerPermissions() {
    61      docker ps > /dev/null 2>&1
    62      if [ $? = 1 ];then
    63          echo -e "your $RED [$USER] $NC not privilege docker" 
    64          echo -e "please run $RED [sudo bash] $NC first"
    65          echo -e "Or docker not install "
    66          exit 1
    67      fi
    68  }
    69  
    70  function checkDockerCompose() {
    71      docker-compose --version > /dev/null 2>&1
    72      if [ $? -eq 127 ];then
    73          echo -e "$RED docker-compose command not found $NC"
    74          echo -e "Please install it first"
    75          exit 1
    76      fi
    77  }
    78  
    79  function installIoctl() {
    80      if command -v $IOCTL_CMD >/dev/null 2>&1; then
    81          echo 'Check iotex command success.'
    82      else
    83          echo "$IOCTL_CMD is not installed, start installing..."
    84          bash $REPO_ABS_DIR/install-cli.sh || exit 1
    85      fi
    86  }
    87  
    88  function setIoctlEndpoint() {
    89      $SET_ENDPOINT
    90  }
    91  
    92  function makeDir() {
    93      $MKDIR $USER_DIR
    94      $MKDIR $IOTEX_HOME
    95      $MKDIR $IOTEX_HOME/{log,data,etc,docker-compose}
    96  }
    97  
    98  function buildImage() {
    99      pushd $REPO_ABS_DIR
   100      docker build . -t $IOTEX_IMAGE
   101      popd
   102  }
   103  
   104  function copyConfigs() {
   105      \cp -f $PROJECT_ABS_DIR/docker-compose.yml $DOCKER_COMPOSE_HOME/docker-compose.yml
   106      \cp -f $REPO_ABS_DIR/config/standalone-config.yaml $IOTEX_HOME/etc/config.yaml
   107      \cp -f $REPO_ABS_DIR/config/standalone-genesis.yaml $IOTEX_HOME/etc/genesis.yaml
   108      echo "IOTEX_HOME=$IOTEX_HOME
   109  IOTEX_IMAGE=$IOTEX_IMAGE" > $DOCKER_COMPOSE_HOME/.env
   110  }
   111  
   112  function createAccounts() {
   113      if [ ! -f $ACCOUNTS_File ];then
   114          $CREATE_10_ACCOUTS > $ACCOUNTS_File
   115      fi
   116  }
   117  
   118  function setAllAccountToGenesis() {
   119      sedCheck
   120      if [ $SED_IS_GNU -eq 1 ];then
   121          cat $ACCOUNTS_File  | while read line; do if [[ $line == \"address\"* ]];then account=$(echo $line|awk -F'"' '{print $4 ": \"100000000000000000000000000000000000\""}');sed -i "/^  initBalances:/a\ \ \ \ $account" $IOTEX_HOME/etc/genesis.yaml;fi;done
   122      else
   123          cat $ACCOUNTS_File  | while read line; do if [[ $line == \"address\"* ]];then account=$(echo $line|awk -F'"' '{print $4 ": \"100000000000000000000000000000000000\""}');sed -i '' "/^  initBalances:/a\\
   124  \ \ \ \ $account
   125  " $IOTEX_HOME/etc/genesis.yaml;fi;done
   126      fi
   127  }
   128  
   129  function showAllAccount() {
   130      echo 'Available Accounts'
   131      echo '=================='
   132      i=0;cat $ACCOUNTS_File | while read line; do if [[ $line == \"address\"* ]];then printf "\t($i) "; echo $line|awk -F'"' '{print $4}';i=$((i+1));fi;done
   133      echo ''
   134      echo 'Private Keys'
   135      echo '=================='
   136      i=0;cat $ACCOUNTS_File | while read line; do if [[ $line == \"privateKey\"* ]];then printf "\t($i) "; echo $line|awk -F'"' '{print $4}';i=$((i+1));fi;done
   137      echo ''
   138  }
   139  
   140  function startup() {
   141      echo -e "Start iotex-server."
   142      pushd $DOCKER_COMPOSE_HOME
   143      docker-compose up -d
   144      docker ps | grep iotex|grep -v grep > /dev/null 2>&1
   145      popd
   146  }
   147  
   148  function registSetup() {
   149      echo "$IOTEX_HOME" > $USER_DIR/workdir
   150  }
   151  
   152  function cleanAll() {
   153      echo -e "${RED} Starting delete all files... ${NC}"
   154      read -p "Are you sure? Press any key to continue ... [Ctrl + c exit!] " key1
   155  
   156      if [ "${IOTEX_HOME}X" = "X" ] || [ "${IOTEX_HOME}X" = "/X" ];then
   157          echo -e "${RED} \$IOTEX_HOME: ${IOTEX_HOME} is wrong. ${NC}"
   158          ## For safe.
   159          return
   160      fi
   161  
   162      # Delete docker
   163      if [ -d $DOCKER_COMPOSE_HOME ];then
   164          pushd $DOCKER_COMPOSE_HOME
   165          docker-compose rm -s -f -v
   166          popd
   167      fi
   168  
   169      # Delete dir and data
   170      rm -rf $IOTEX_HOME $USER_DIR
   171  }
   172  
   173  function main() {
   174      # Check and clean
   175      checkDockerPermissions     # Determine the current user can run docker
   176      checkDockerCompose         # Determin the docker-compose is installed
   177  
   178      # If clean all
   179      if [ $# -eq 1 ] && [ "$1" == "-c" ];then
   180          cleanAll
   181          exit 0
   182      fi
   183  
   184      # Install ioctl command
   185      installIoctl
   186  
   187      # Make work space dir
   188      makeDir || exit 2
   189  
   190      # Build image
   191      if [ $# -eq 0 ] || [ "$1" != "-q" ];then
   192          buildImage || exit 3
   193      fi
   194  
   195      # Create account
   196      createAccounts || exit 4
   197  
   198      # Set config
   199      copyConfigs || exit 5
   200      setAllAccountToGenesis || exit 6
   201  
   202      # Show accounts
   203      showAllAccount || exit 7
   204  
   205      # Start server
   206      startup || exit 8
   207      echo 'Listening on 127.0.0.1:14014'
   208  
   209      # Set ioctl endpoint
   210      setIoctlEndpoint || exit 9
   211  
   212      # Register the installation directory to the user directory file for the next startup
   213      registSetup
   214  }
   215  
   216  main $@