github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/scripts/install-fabric.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright hechain. All Rights Reserved.
     4  #
     5  # SPDX-License-Identifier: Apache-2.0
     6  #
     7  
     8  # A modified version of the Fabric bootstrap script
     9  # Use positional arguments to select componenets to install
    10  #
    11  # Has exactly the same functional power of bootstrap.sh
    12  
    13  
    14  ### START OF CODE GENERATED BY Argbash v2.9.0  ###
    15  # Argbash is a bash code generator used to get arguments parsing right.
    16  # Argbash is FREE SOFTWARE, see https://argbash.io for more info
    17  # Generated online by https://argbash.io/generate
    18  
    19  
    20  # Default values
    21  _positionals=()
    22  _arg_comp=('' )
    23  
    24  # if version not passed in, default to latest released version
    25  # if ca version not passed in, default to latest released version
    26  _arg_fabric_version="2.4.3"
    27  _arg_ca_version="1.5.2"
    28  
    29  ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]'|sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')")
    30  MARCH=$(uname -m)
    31  
    32  die()
    33  {
    34  	local _ret="${2:-1}"
    35  	test "${_PRINT_HELP:-no}" = yes && print_help >&2
    36  	echo "$1" >&2
    37  	exit "${_ret}"
    38  }
    39  
    40  
    41  begins_with_short_option()
    42  {
    43  	local first_option all_short_options='fc'
    44  	first_option="${1:0:1}"
    45  	test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
    46  }
    47  
    48  
    49  
    50  print_help()
    51  {
    52  	printf 'Usage: %s [-f|--fabric-version <arg>] [-c|--ca-version <arg>] <comp-1> [<comp-2>] ... [<comp-n>] ...\n' "$0"
    53  	printf '\t%s\n' "<comp>: Component to install one or more of  d[ocker]|b[inary]|s[amples]. If none specified, all will be installed"
    54  	printf '\t%s\n' "-f, --fabric-version: FabricVersion (default: '2.4.3')"
    55  	printf '\t%s\n' "-c, --ca-version: Fabric CA Version (default: '1.5.2')"
    56  }
    57  
    58  
    59  parse_commandline()
    60  {
    61  	_positionals_count=0
    62  	while test $# -gt 0
    63  	do
    64  		_key="$1"
    65  		case "$_key" in
    66  			-f|--fabric-version)
    67  				test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
    68  				_arg_fabric_version="$2"
    69  				shift
    70  				;;
    71  			--fabric-version=*)
    72  				_arg_fabric_version="${_key##--fabric-version=}"
    73  				;;
    74  			-f*)
    75  				_arg_fabric_version="${_key##-f}"
    76  				;;
    77  			-c|--ca-version)
    78  				test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
    79  				_arg_ca_version="$2"
    80  				shift
    81  				;;
    82  			-h|--help)
    83  				print_help
    84  				exit 0
    85  				;;
    86  			-h*)
    87  				print_help
    88  				exit 0
    89  				;;
    90  			--ca-version=*)
    91  				_arg_ca_version="${_key##--ca-version=}"
    92  				;;
    93  			-c*)
    94  				_arg_ca_version="${_key##-c}"
    95  				;;
    96  			*)
    97  				_last_positional="$1"
    98  				_positionals+=("$_last_positional")
    99  				_positionals_count=$((_positionals_count + 1))
   100  				;;
   101  		esac
   102  		shift
   103  	done
   104  }
   105  
   106  
   107  handle_passed_args_count()
   108  {
   109  	local _required_args_string="'comp'"
   110  	# test "${_positionals_count}" -ge 1 || _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require at least 1 (namely: $_required_args_string), but got only ${_positionals_count}." 1
   111  }
   112  
   113  
   114  assign_positional_args()
   115  {
   116  	local _positional_name _shift_for=$1
   117  	_positional_names="_arg_comp "
   118  	_our_args=$((${#_positionals[@]} - 1))
   119  	for ((ii = 0; ii < _our_args; ii++))
   120  	do
   121  		_positional_names="$_positional_names _arg_comp[$((ii + 1))]"
   122  	done
   123  
   124  	shift "$_shift_for"
   125  	for _positional_name in ${_positional_names}
   126  	do
   127  		test $# -gt 0 || break
   128  		eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1
   129  		shift
   130  	done
   131  }
   132  
   133  # End of ARGBASH code
   134  
   135  # dockerPull() pulls docker images from fabric and chaincode repositories
   136  # note, if a docker image doesn't exist for a requested release, it will simply
   137  # be skipped, since this script doesn't terminate upon errors.
   138  
   139  dockerPull() {
   140      #three_digit_image_tag is passed in, e.g. "1.4.7"
   141      three_digit_image_tag=$1
   142      shift
   143      #two_digit_image_tag is derived, e.g. "1.4", especially useful as a local tag for two digit references to most recent baseos, ccenv, javaenv, nodeenv patch releases
   144      two_digit_image_tag=$(echo "$three_digit_image_tag" | cut -d'.' -f1,2)
   145      while [[ $# -gt 0 ]]
   146      do
   147          image_name="$1"
   148          echo "====> hyperledger/fabric-$image_name:$three_digit_image_tag"
   149          docker pull "hyperledger/fabric-$image_name:$three_digit_image_tag"
   150          docker tag "hyperledger/fabric-$image_name:$three_digit_image_tag" "hyperledger/fabric-$image_name"
   151          docker tag "hyperledger/fabric-$image_name:$three_digit_image_tag" "hyperledger/fabric-$image_name:$two_digit_image_tag"
   152          shift
   153      done
   154  }
   155  
   156  cloneSamplesRepo() {
   157      # clone (if needed) hyperledger/fabric-samples and checkout corresponding
   158      # version to the binaries and docker images to be downloaded
   159      if [ -d test-network ]; then
   160          # if we are in the fabric-samples repo, checkout corresponding version
   161          echo "==> Already in fabric-samples repo"
   162      elif [ -d fabric-samples ]; then
   163          # if fabric-samples repo already cloned and in current directory,
   164          # cd fabric-samples
   165          echo "===> Changing directory to fabric-samples"
   166          cd fabric-samples
   167      else
   168          echo "===> Cloning hyperledger/fabric-samples repo"
   169          git clone -b main https://github.com/hyperledger/fabric-samples.git && cd fabric-samples
   170      fi
   171  
   172      if GIT_DIR=.git git rev-parse v${VERSION} >/dev/null 2>&1; then
   173          echo "===> Checking out v${VERSION} of hyperledger/fabric-samples"
   174          git checkout -q v${VERSION}
   175      else
   176          echo "fabric-samples v${VERSION} does not exist, defaulting to main. fabric-samples main branch is intended to work with recent versions of fabric."
   177          git checkout -q main
   178      fi
   179  }
   180  
   181  # This will download the .tar.gz
   182  download() {
   183      local BINARY_FILE=$1
   184      local URL=$2
   185      local DEST_DIR=$(pwd)
   186      echo "===> Downloading: " "${URL}"
   187      if [ -d fabric-samples ]; then
   188         DEST_DIR="fabric-samples"
   189      fi
   190      echo "===> Will unpack to: ${DEST_DIR}"
   191      curl -L --retry 5 --retry-delay 3 "${URL}" | tar xz -C ${DEST_DIR}|| rc=$?
   192      if [ -n "$rc" ]; then
   193          echo "==> There was an error downloading the binary file."
   194          return 22
   195      else
   196          echo "==> Done."
   197      fi
   198  }
   199  
   200  pullBinaries() {
   201      echo "===> Downloading version ${FABRIC_TAG} platform specific fabric binaries"
   202      download "${BINARY_FILE}" "https://github.com/hechain20/hechain/releases/download/v${VERSION}/${BINARY_FILE}"
   203      if [ $? -eq 22 ]; then
   204          echo
   205          echo "------> ${FABRIC_TAG} platform specific fabric binary is not available to download <----"
   206          echo
   207          exit
   208      fi
   209  
   210      echo "===> Downloading version ${CA_TAG} platform specific fabric-ca-client binary"
   211      download "${CA_BINARY_FILE}" "https://github.com/hyperledger/fabric-ca/releases/download/v${CA_VERSION}/${CA_BINARY_FILE}"
   212      if [ $? -eq 22 ]; then
   213          echo
   214          echo "------> ${CA_TAG} fabric-ca-client binary is not available to download  (Available from 1.1.0-rc1) <----"
   215          echo
   216          exit
   217      fi
   218  }
   219  
   220  pullDockerImages() {
   221      command -v docker >& /dev/null
   222      NODOCKER=$?
   223      if [ "${NODOCKER}" == 0 ]; then
   224          FABRIC_IMAGES=(peer orderer ccenv tools)
   225          case "$VERSION" in
   226          2.*)
   227              FABRIC_IMAGES+=(baseos)
   228              shift
   229              ;;
   230          esac
   231          echo "FABRIC_IMAGES:" "${FABRIC_IMAGES[@]}"
   232          echo "===> Pulling fabric Images"
   233          dockerPull "${FABRIC_TAG}" "${FABRIC_IMAGES[@]}"
   234          echo "===> Pulling fabric ca Image"
   235          CA_IMAGE=(ca)
   236          dockerPull "${CA_TAG}" "${CA_IMAGE[@]}"
   237          echo "===> List out hyperledger docker images"
   238          docker images | grep hyperledger
   239      else
   240          echo "========================================================="
   241          echo "Docker not installed, bypassing download of Fabric images"
   242          echo "========================================================="
   243      fi
   244  }
   245  
   246  
   247  # Main code starts here
   248  parse_commandline "$@"
   249  handle_passed_args_count
   250  assign_positional_args 1 "${_positionals[@]}"
   251  
   252  
   253  VERSION=$_arg_fabric_version
   254  CA_VERSION=$_arg_ca_version
   255  
   256  # prior to 1.2.0 architecture was determined by uname -m
   257  if [[ $VERSION =~ ^1\.[0-1]\.* ]]; then
   258      export FABRIC_TAG=${MARCH}-${VERSION}
   259      export CA_TAG=${MARCH}-${CA_VERSION}
   260  else
   261      # starting with 1.2.0, multi-arch images will be default
   262      : "${CA_TAG:="$CA_VERSION"}"
   263      : "${FABRIC_TAG:="$VERSION"}"
   264  fi
   265  
   266  BINARY_FILE=hyperledger-fabric-${ARCH}-${VERSION}.tar.gz
   267  CA_BINARY_FILE=hyperledger-fabric-ca-${ARCH}-${CA_VERSION}.tar.gz
   268  
   269  # if nothing has been specified, assume everything
   270  if [[ ${_arg_comp[@]} =~ ^$ ]]; then
   271      echo "Getting all samples, binaries, and docker images"
   272      _arg_comp=('samples','binary','docker')
   273  fi
   274  
   275  # Process samples first then the binaries. So if the fabric-samples dir is present
   276  # the binaries will go there
   277  if [[ "${_arg_comp[@]}" =~ s(amples)? ]]; then
   278          echo
   279          echo "Clone hyperledger/fabric-samples repo"
   280          echo
   281          cloneSamplesRepo
   282  fi
   283  
   284  if [[ "${_arg_comp[@]}" =~ b(inary)? ]]; then
   285          echo
   286          echo "Pull Hechain binaries"
   287          echo
   288          pullBinaries
   289  fi
   290  
   291  if [[ ${_arg_comp[@]} =~ d(ocker)? ]]; then
   292          echo
   293          echo "Pull Hechain docker images"
   294          echo
   295          pullDockerImages
   296  fi