位置
/fabric/scripts/bootstrap.sh
0.Clone hyperledger/fabric-samples repo
# clone 仓库,并且进入文件夹
git clone -b main https://github.com/hyperledger/fabric-samples.git && cd fabric-samples
# 切换分支
git checkout -q release-1.4
1. Pull Hyperledger Fabric binaries 下载hyperledger fabirc 二进制文件
# 》》pullBinaries -> 》》download
curl -L --retry 5 --retry-delay 3 "https://github.com/hyperledger/fabric/releases/download/v2.3.1/hyperledger-fabric-darwin-amd64-2.3.1.tar.gz" | tar xz
curl -L --retry 5 --retry-delay 3 "https://github.com/hyperledger/fabric-ca/releases/download/v1.4.9/hyperledger-fabric-ca-darwin-amd64-1.4.9.tar.gz" | tar xz
2. Pull Hyperledger Fabric docker images 拉取 docker镜像
# 》》pullDockerImages -> 》》dockerPull
# Pulling fabric Images 分别拉取 peer orderer ccenv tools baseos
# hyperledger/fabric-peer:2.3.1
docker pull hyperledger/fabric-peer:2.3.1
docker tag hyperledger/fabric-peer:2.3.1 hyperledger/fabric-peer
docker tag hyperledger/fabric-peer:2.3.1 hyperledger/fabric-peer:2.3
#hyperledger/fabric-orderer:2.3.1
docker pull hyperledger/fabric-orderer:2.3.1
docker tag hyperledger/fabric-orderer:2.3.1 hyperledger/fabric-orderer
docker tag hyperledger/fabric-orderer:2.3.1 hyperledger/fabric-orderer:2.3
# hyperledger/fabric-ccenv:2.3.1
docker pull hyperledger/fabric-ccenv:2.3.1
docker tag hyperledger/fabric-ccenv:2.3.1 hyperledger/fabric-ccenv
docker tag hyperledger/fabric-ccenv:2.3.1 hyperledger/fabric-ccenv:2.3
# hyperledger/fabric-tools:2.3.1
docker pull hyperledger/fabric-tools:2.3.1
docker tag hyperledger/fabric-tools:2.3.1 hyperledger/fabric-tools
docker tag hyperledger/fabric-tools:2.3.1 hyperledger/fabric-tools:2.3
# hyperledger/fabric-baseos:2.3.1
docker pull hyperledger/fabric-baseos:2.3.1
docker tag hyperledger/fabric-baseos:2.3.1 hyperledger/fabric-baseos
docker tag hyperledger/fabric-baseos:2.3.1 hyperledger/fabric-baseos:2.3
# Pulling fabric ca Image 拉取证书
# hyperledger/fabric-ca:1.4.9
docker pull hyperledger/fabric-ca:1.4.9
docker tag hyperledger/fabric-ca:1.4.9 hyperledger/fabric-ca
docker tag hyperledger/fabric-ca:1.4.9 hyperledger/fabric-ca:1.4
# 查看下载下来hyperledger docker镜像
docker images | grep hyperledger
hyperledger/fabric-baseos latest 5272411bf370 9 months ago 88.7MB
hyperledger/fabric-ca latest 743a758fae29 11 months ago 154MB
hyperledger/fabric-tools latest a026b435e575 11 months ago 1.49GB
hyperledger/fabric-ccenv latest c5fbec1827ad 11 months ago 1.36GB
hyperledger/fabric-orderer latest df155b01ed80 11 months ago 123MB
hyperledger/fabric-peer latest 5d5fbecd1efe 11 months ago 131MB
hyperledger/fabric-baseos amd64-0.4.20 121a92cc3fc0 13 months ago 85MB
整体的代码
#!/bin/bash
#
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
# 定义相关的版本号
# if version not passed in, default to latest released version
VERSION=2.3.1
# if ca version not passed in, default to latest released version
CA_VERSION=1.4.9
ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]'|sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')")
# linux-amd64
MARCH=$(uname -m)
# uname -m cpu 型号 x86_64
#打印
printHelp() {
echo "Usage: bootstrap.sh [version [ca_version]] [options]"
echo
echo "options:"
echo "-h : this help"
echo "-d : bypass docker image download"
echo "-s : bypass fabric-samples repo clone"
echo "-b : bypass download of platform-specific binaries"
echo
echo "e.g. bootstrap.sh 2.3.1 1.4.9 -s"
echo "will download docker images and binaries for Fabric v2.3.1 and Fabric CA v1.4.9"
}
# dockerPull() pulls docker images from fabric and chaincode repositories
# note, if a docker image doesn't exist for a requested release, it will simply
# be skipped, since this script doesn't terminate upon errors.
#下载fabirc对应的Docker
dockerPull() {
#three_digit_image_tag is passed in, e.g. "1.4.7"
three_digit_image_tag=$1
shift
#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
two_digit_image_tag=$(echo "$three_digit_image_tag" | cut -d'.' -f1,2)
while [[ $# -gt 0 ]]
do
image_name="$1"
echo "====> hyperledger/fabric-$image_name:$three_digit_image_tag"
docker pull "hyperledger/fabric-$image_name:$three_digit_image_tag"
docker tag "hyperledger/fabric-$image_name:$three_digit_image_tag" "hyperledger/fabric-$image_name"
docker tag "hyperledger/fabric-$image_name:$three_digit_image_tag" "hyperledger/fabric-$image_name:$two_digit_image_tag"
shift
done
}
#拷贝案例项目
cloneSamplesRepo() {
# clone (if needed) hyperledger/fabric-samples and checkout corresponding
# version to the binaries and docker images to be downloaded
if [ -d first-network ]; then
# if we are in the fabric-samples repo, checkout corresponding version
echo "==> Already in fabric-samples repo"
elif [ -d fabric-samples ]; then
# if fabric-samples repo already cloned and in current directory,
# cd fabric-samples
echo "===> Changing directory to fabric-samples"
cd fabric-samples
else
echo "===> Cloning hyperledger/fabric-samples repo"
git clone -b main https://github.com/hyperledger/fabric-samples.git && cd fabric-samples
fi
if GIT_DIR=.git git rev-parse v${VERSION} >/dev/null 2>&1; then
echo "===> Checking out v${VERSION} of hyperledger/fabric-samples"
git checkout -q v${VERSION}
else
echo "fabric-samples v${VERSION} does not exist, defaulting main"
git checkout -q main
fi
}
# This will download the .tar.gz
download() {
local BINARY_FILE=$1
local URL=$2
echo "===> Downloading: " "${URL}"
curl -L --retry 5 --retry-delay 3 "${URL}" | tar xz || rc=$?
if [ -n "$rc" ]; then
echo "==> There was an error downloading the binary file."
return 22
else
echo "==> Done."
fi
}
# 下载bin 的二进制文件
pullBinaries() {
echo "===> Downloading version ${FABRIC_TAG} platform specific fabric binaries"
download "${BINARY_FILE}" "https://github.com/hyperledger/fabric/releases/download/v${VERSION}/${BINARY_FILE}"
if [ $? -eq 22 ]; then
echo
echo "------> ${FABRIC_TAG} platform specific fabric binary is not available to download <----"
echo
exit
fi
echo "===> Downloading version ${CA_TAG} platform specific fabric-ca-client binary"
download "${CA_BINARY_FILE}" "https://github.com/hyperledger/fabric-ca/releases/download/v${CA_VERSION}/${CA_BINARY_FILE}"
if [ $? -eq 22 ]; then
echo
echo "------> ${CA_TAG} fabric-ca-client binary is not available to download (Available from 1.1.0-rc1) <----"
echo
exit
fi
}
# 下载Docker映像
pullDockerImages() {
command -v docker >& /dev/null
NODOCKER=$?
if [ "${NODOCKER}" == 0 ]; then
FABRIC_IMAGES=(peer orderer ccenv tools)
case "$VERSION" in
2.*)
FABRIC_IMAGES+=(baseos)
shift
;;
esac
echo "FABRIC_IMAGES:" "${FABRIC_IMAGES[@]}"
echo "===> Pulling fabric Images"
dockerPull "${FABRIC_TAG}" "${FABRIC_IMAGES[@]}"
echo "===> Pulling fabric ca Image"
CA_IMAGE=(ca)
dockerPull "${CA_TAG}" "${CA_IMAGE[@]}"
echo "===> List out hyperledger docker images"
docker images | grep hyperledger
else
echo "========================================================="
echo "Docker not installed, bypassing download of Fabric images"
echo "========================================================="
fi
}
DOCKER=true
SAMPLES=true
BINARIES=true
# 函数入口
# Parse commandline args pull out
# version and/or ca-version strings first
if [ -n "$1" ] && [ "${1:0:1}" != "-" ]; then # 如果入参的第一个参数(字符串) 不为空,长度不为0 并且
VERSION=$1;shift # 先把第一个参数赋值给变量 VERSION 然后把所有参数左移(即 把原来的 $0销毁 $1 变为$0,$2变成$1 后面的以此类推)
if [ -n "$1" ] && [ "${1:0:1}" != "-" ]; then # 判断新的$1 即原来的 $2 参数 不为空时 执行代码快代码
CA_VERSION=$1;shift # 赋值给CA_VERSION 继续左移参
if [ -n "$1" ] && [ "${1:0:1}" != "-" ]; then # 判断新的$2 即原来的 $3 参数 不为空时 执行代码快代码
THIRDPARTY_IMAGE_VERSION=$1;shift
fi
fi
fi
# prior to 1.2.0 architecture was determined by uname -m
if [[ $VERSION =~ ^1\.[0-1]\.* ]]; then # 如果版本是 1.0.* 或者 1.1.*
export FABRIC_TAG=${MARCH}-${VERSION} # 设置临时环境变量 FABRIC_TAG 值如: x86_64-1.0.X
export CA_TAG=${MARCH}-${CA_VERSION} # 设置临时环境变量 CA_TAG 值如: x86_64-1.0.X
export THIRDPARTY_TAG=${MARCH}-${THIRDPARTY_IMAGE_VERSION} # 设置 THIRDPARTY_TAG 为: x86_64-xxxx
else # 如果是1.2.X以后的
# starting with 1.2.0, multi-arch images will be default 如果是 1.2.0版本的就直接赋值变量
: "${CA_TAG:="$CA_VERSION"}"
: "${FABRIC_TAG:="$VERSION"}"
: "${THIRDPARTY_TAG:="$THIRDPARTY_IMAGE_VERSION"}"
fi
# 指定 fabric和fabric-ca的二进制文件
BINARY_FILE=hyperledger-fabric-${ARCH}-${VERSION}.tar.gz
CA_BINARY_FILE=hyperledger-fabric-ca-${ARCH}-${CA_VERSION}.tar.gz
# then parse opts
while getopts "h?dsb" opt; do ## 表示运行当前脚本时可以携带 -h或者-? 及 -d -s -b 等选项
case "$opt" in
h|\?) ## 如果是 -h 或者-? 选项时 调用使用说明函数 printHelp 打印出本脚本的使用说明 且结束脚本
printHelp
exit 0
;;
d) DOCKER=false ## -d 选项时,修改 DOCKER 变量为 false 不调用 《pullDockerImages》
;;
s) SAMPLES=false ## -s 选项时, 修改 SAMPLES 变量选项为 false 不调用《cloneSamplesRepo 》函数
;;
b) BINARIES=false ## -b 选项时, 修改 BINARIES 变量选项为 false 不调用《pullBinaries》
;;
esac
done
## 当 SAMPLES 变量值为 true时 执行代码块,(如果我们脚本没有携带参数 -s 也就是不会执行上面 while中的代码片段,则调用 samplesInstall 函数)
if [ "$SAMPLES" == "true" ]; then
echo
echo "Clone hyperledger/fabric-samples repo"
echo
cloneSamplesRepo
fi
if [ "$BINARIES" == "true" ]; then
echo
echo "Pull Hyperledger Fabric binaries"
echo
pullBinaries
fi
if [ "$DOCKER" == "true" ]; then
echo
echo "Pull Hyperledger Fabric docker images"
echo
pullDockerImages
fi