|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copyright IBM Corp. All Rights Reserved. |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | +# |
| 7 | + |
| 8 | +# if version not passed in, default to latest released version |
| 9 | +export VERSION=1.2.0 |
| 10 | +# if ca version not passed in, default to latest released version |
| 11 | +export CA_VERSION=$VERSION |
| 12 | +# current version of thirdparty images (couchdb, kafka and zookeeper) released |
| 13 | +export THIRDPARTY_IMAGE_VERSION=0.4.10 |
| 14 | +export ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]'|sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')") |
| 15 | +export MARCH=$(uname -m) |
| 16 | + |
| 17 | +printHelp() { |
| 18 | + echo "Usage: bootstrap.sh [version [ca_version [thirdparty_version]]] [options]" |
| 19 | + echo |
| 20 | + echo "options:" |
| 21 | + echo "-h : this help" |
| 22 | + echo "-d : bypass docker image download" |
| 23 | + echo "-s : bypass fabric-samples repo clone" |
| 24 | + echo "-b : bypass download of platform-specific binaries" |
| 25 | + echo |
| 26 | + echo "e.g. bootstrap.sh 1.2.0 -s" |
| 27 | + echo "would download docker images and binaries for version 1.2.0" |
| 28 | +} |
| 29 | + |
| 30 | +dockerFabricPull() { |
| 31 | + local FABRIC_TAG=$1 |
| 32 | + for IMAGES in peer orderer ccenv tools; do |
| 33 | + echo "==> FABRIC IMAGE: $IMAGES" |
| 34 | + echo |
| 35 | + docker pull hyperledger/fabric-$IMAGES:$FABRIC_TAG |
| 36 | + docker tag hyperledger/fabric-$IMAGES:$FABRIC_TAG hyperledger/fabric-$IMAGES |
| 37 | + done |
| 38 | +} |
| 39 | + |
| 40 | +dockerThirdPartyImagesPull() { |
| 41 | + local THIRDPARTY_TAG=$1 |
| 42 | + for IMAGES in couchdb kafka zookeeper; do |
| 43 | + echo "==> THIRDPARTY DOCKER IMAGE: $IMAGES" |
| 44 | + echo |
| 45 | + docker pull hyperledger/fabric-$IMAGES:$THIRDPARTY_TAG |
| 46 | + docker tag hyperledger/fabric-$IMAGES:$THIRDPARTY_TAG hyperledger/fabric-$IMAGES |
| 47 | + done |
| 48 | +} |
| 49 | + |
| 50 | +dockerCaPull() { |
| 51 | + local CA_TAG=$1 |
| 52 | + echo "==> FABRIC CA IMAGE" |
| 53 | + echo |
| 54 | + docker pull hyperledger/fabric-ca:$CA_TAG |
| 55 | + docker tag hyperledger/fabric-ca:$CA_TAG hyperledger/fabric-ca |
| 56 | +} |
| 57 | + |
| 58 | +samplesInstall() { |
| 59 | + # clone (if needed) hyperledger/fabric-samples and checkout corresponding |
| 60 | + # version to the binaries and docker images to be downloaded |
| 61 | + if [ -d first-network ]; then |
| 62 | + # if we are in the fabric-samples repo, checkout corresponding version |
| 63 | + echo "===> Checking out v${VERSION} branch of hyperledger/fabric-samples" |
| 64 | + git checkout v${VERSION} |
| 65 | + elif [ -d fabric-samples ]; then |
| 66 | + # if fabric-samples repo already cloned and in current directory, |
| 67 | + # cd fabric-samples and checkout corresponding version |
| 68 | + echo "===> Checking out v${VERSION} branch of hyperledger/fabric-samples" |
| 69 | + cd fabric-samples && git checkout v${VERSION} |
| 70 | + else |
| 71 | + echo "===> Cloning hyperledger/fabric-samples repo and checkout v${VERSION}" |
| 72 | + git clone -b master https://github.com/hyperledger/fabric-samples.git && cd fabric-samples && git checkout v${VERSION} |
| 73 | + fi |
| 74 | +} |
| 75 | + |
| 76 | +# Incrementally downloads the .tar.gz file locally first, only decompressing it |
| 77 | +# after the download is complete. This is slower than binaryDownload() but |
| 78 | +# allows the download to be resumed. |
| 79 | +binaryIncrementalDownload() { |
| 80 | + local BINARY_FILE=$1 |
| 81 | + local URL=$2 |
| 82 | + curl -f -s -C - ${URL} -o ${BINARY_FILE} || rc=$? |
| 83 | + # Due to limitations in the current Nexus repo: |
| 84 | + # curl returns 33 when there's a resume attempt with no more bytes to download |
| 85 | + # curl returns 2 after finishing a resumed download |
| 86 | + # with -f curl returns 22 on a 404 |
| 87 | + if [ "$rc" = 22 ]; then |
| 88 | + # looks like the requested file doesn't actually exist so stop here |
| 89 | + return 22 |
| 90 | + fi |
| 91 | + if [ -z "$rc" ] || [ $rc -eq 33 ] || [ $rc -eq 2 ]; then |
| 92 | + # The checksum validates that RC 33 or 2 are not real failures |
| 93 | + echo "==> File downloaded. Verifying the md5sum..." |
| 94 | + localMd5sum=$(md5sum ${BINARY_FILE} | awk '{print $1}') |
| 95 | + remoteMd5sum=$(curl -s ${URL}.md5) |
| 96 | + if [ "$localMd5sum" == "$remoteMd5sum" ]; then |
| 97 | + echo "==> Extracting ${BINARY_FILE}..." |
| 98 | + tar xzf ./${BINARY_FILE} --overwrite |
| 99 | + echo "==> Done." |
| 100 | + rm -f ${BINARY_FILE} ${BINARY_FILE}.md5 |
| 101 | + else |
| 102 | + echo "Download failed: the local md5sum is different from the remote md5sum. Please try again." |
| 103 | + rm -f ${BINARY_FILE} ${BINARY_FILE}.md5 |
| 104 | + exit 1 |
| 105 | + fi |
| 106 | + else |
| 107 | + echo "Failure downloading binaries (curl RC=$rc). Please try again and the download will resume from where it stopped." |
| 108 | + exit 1 |
| 109 | + fi |
| 110 | +} |
| 111 | + |
| 112 | +# This will attempt to download the .tar.gz all at once, but will trigger the |
| 113 | +# binaryIncrementalDownload() function upon a failure, allowing for resume |
| 114 | +# if there are network failures. |
| 115 | +binaryDownload() { |
| 116 | + local BINARY_FILE=$1 |
| 117 | + local URL=$2 |
| 118 | + echo "===> Downloading: " ${URL} |
| 119 | + # Check if a previous failure occurred and the file was partially downloaded |
| 120 | + if [ -e ${BINARY_FILE} ]; then |
| 121 | + echo "==> Partial binary file found. Resuming download..." |
| 122 | + binaryIncrementalDownload ${BINARY_FILE} ${URL} |
| 123 | + else |
| 124 | + curl ${URL} | tar xz || rc=$? |
| 125 | + if [ ! -z "$rc" ]; then |
| 126 | + echo "==> There was an error downloading the binary file. Switching to incremental download." |
| 127 | + echo "==> Downloading file..." |
| 128 | + binaryIncrementalDownload ${BINARY_FILE} ${URL} |
| 129 | + else |
| 130 | + echo "==> Done." |
| 131 | + fi |
| 132 | + fi |
| 133 | +} |
| 134 | + |
| 135 | +binariesInstall() { |
| 136 | + echo "===> Downloading version ${FABRIC_TAG} platform specific fabric binaries" |
| 137 | + binaryDownload ${BINARY_FILE} https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/${ARCH}-${VERSION}/${BINARY_FILE} |
| 138 | + if [ $? -eq 22 ]; then |
| 139 | + echo |
| 140 | + echo "------> ${FABRIC_TAG} platform specific fabric binary is not available to download <----" |
| 141 | + echo |
| 142 | + fi |
| 143 | + |
| 144 | + echo "===> Downloading version ${CA_TAG} platform specific fabric-ca-client binary" |
| 145 | + binaryDownload ${CA_BINARY_FILE} https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric-ca/hyperledger-fabric-ca/${ARCH}-${CA_VERSION}/${CA_BINARY_FILE} |
| 146 | + if [ $? -eq 22 ]; then |
| 147 | + echo |
| 148 | + echo "------> ${CA_TAG} fabric-ca-client binary is not available to download (Available from 1.1.0-rc1) <----" |
| 149 | + echo |
| 150 | + fi |
| 151 | +} |
| 152 | + |
| 153 | +dockerInstall() { |
| 154 | + which docker >& /dev/null |
| 155 | + NODOCKER=$? |
| 156 | + if [ "${NODOCKER}" == 0 ]; then |
| 157 | + echo "===> Pulling fabric Images" |
| 158 | + dockerFabricPull ${FABRIC_TAG} |
| 159 | + echo "===> Pulling fabric ca Image" |
| 160 | + dockerCaPull ${CA_TAG} |
| 161 | + echo "===> Pulling thirdparty docker images" |
| 162 | + dockerThirdPartyImagesPull ${THIRDPARTY_TAG} |
| 163 | + echo |
| 164 | + echo "===> List out hyperledger docker images" |
| 165 | + docker images | grep hyperledger* |
| 166 | + else |
| 167 | + echo "=========================================================" |
| 168 | + echo "Docker not installed, bypassing download of Fabric images" |
| 169 | + echo "=========================================================" |
| 170 | + fi |
| 171 | +} |
| 172 | + |
| 173 | +DOCKER=true |
| 174 | +SAMPLES=true |
| 175 | +BINARIES=true |
| 176 | + |
| 177 | +# Parse commandline args pull out |
| 178 | +# version and/or ca-version strings first |
| 179 | +if [ ! -z $1 -a ${1:0:1} != "-" ]; then |
| 180 | + VERSION=$1;shift |
| 181 | + if [ ! -z $1 -a ${1:0:1} != "-" ]; then |
| 182 | + CA_VERSION=$1;shift |
| 183 | + if [ ! -z $1 -a ${1:0:1} != "-" ]; then |
| 184 | + THIRDPARTY_IMAGE_VERSION=$1;shift |
| 185 | + fi |
| 186 | + fi |
| 187 | +fi |
| 188 | + |
| 189 | +# prior to 1.2.0 architecture was determined by uname -m |
| 190 | +if [[ $VERSION =~ ^1\.[0-1]\.* ]]; then |
| 191 | + export FABRIC_TAG=${MARCH}-${VERSION} |
| 192 | + export CA_TAG=${MARCH}-${CA_VERSION} |
| 193 | + export THIRDPARTY_TAG=${MARCH}-${THIRDPARTY_IMAGE_VERSION} |
| 194 | +else |
| 195 | + # starting with 1.2.0, multi-arch images will be default |
| 196 | + : ${CA_TAG:="$CA_VERSION"} |
| 197 | + : ${FABRIC_TAG:="$VERSION"} |
| 198 | + : ${THIRDPARTY_TAG:="$THIRDPARTY_IMAGE_VERSION"} |
| 199 | +fi |
| 200 | + |
| 201 | +BINARY_FILE=hyperledger-fabric-${ARCH}-${VERSION}.tar.gz |
| 202 | +CA_BINARY_FILE=hyperledger-fabric-ca-${ARCH}-${CA_VERSION}.tar.gz |
| 203 | + |
| 204 | +# then parse opts |
| 205 | +while getopts "h?dsb" opt; do |
| 206 | + case "$opt" in |
| 207 | + h|\?) |
| 208 | + printHelp |
| 209 | + exit 0 |
| 210 | + ;; |
| 211 | + d) DOCKER=false |
| 212 | + ;; |
| 213 | + s) SAMPLES=false |
| 214 | + ;; |
| 215 | + b) BINARIES=false |
| 216 | + ;; |
| 217 | + esac |
| 218 | +done |
| 219 | + |
| 220 | +if [ "$SAMPLES" == "true" ]; then |
| 221 | + echo |
| 222 | + echo "Installing hyperledger/fabric-samples repo" |
| 223 | + echo |
| 224 | + samplesInstall |
| 225 | +fi |
| 226 | +if [ "$BINARIES" == "true" ]; then |
| 227 | + echo |
| 228 | + echo "Installing Hyperledger Fabric binaries" |
| 229 | + echo |
| 230 | + binariesInstall |
| 231 | +fi |
| 232 | +if [ "$DOCKER" == "true" ]; then |
| 233 | + echo |
| 234 | + echo "Installing Hyperledger Fabric docker images" |
| 235 | + echo |
| 236 | + dockerInstall |
| 237 | +fi |
0 commit comments