hyperledger-fabric1.4


我们在fabric1.4的下载安装的时候,下载了bootstrap.sh,并利用它来拉取镜像和下载并二进制可执行文件。但是在这个过程bootstrap.sh做了啥?这就是下面要分析的内容


一. 命令分析

执行bootstrap.sh 时使用了以下命令

1
curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh | bash -s -- 1.4.3 1.4.3 0.4.15

其中 -sSL 代表含义如下:

1
2
3
-s, --silent        Silent mode
-S, --show-error Show error even when -s is used
-L, --location Follow redirects

s :沉默模式 ,不输出请求过程的任何信息
S :当使用沉默模式的时候,如果出现错误事件需要显示出来
L :短连接进行重定向,原本命令为

1
curl -sSL http://bit.ly/2ysbOFE | bash -s -- 1.4.3 1.4.3 0.4.15

其中链接为短链接,短链接会重新定向到真正的目标地址。但是使用的时候短链接失效,所以使用了官方教程中提供的长链接。

命令中的竖线 “|” 表示管道,即上个命令输出的内容,交给下一个命令执行。这里表示将下载的bootstrap.sh下载后交给bash执行。

后面跟着的 1.4.3 1.4.3 0.4.15分别表示fabric 、fabric-ca 、第三方镜像(couchdb, kafka 和 zookeeper)版本 后面还可以跟着可选命令 -h -d -s -b 具体含义下面分析:

1
2
3
4
5
6
7
8
9
10
11
12
printHelp() {
echo "Usage: bootstrap.sh [version [ca_version [thirdparty_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 1.4.3 -s"
echo "would download docker images and binaries for version 1.4.3"
}

从bootstrap.sh中提取出来的帮助函数可以看出

  • -h : 提示
  • -d :跳过docker镜像下载
  • -s :跳过fabric-sample的克隆
  • -b :跳过指定平台二进制文件下载

具体执行过程如下,整个脚本主要做3件事

  • 克隆fabric-sample
  • 下载特定平台的fabric二进制可执行文件和配置文件
  • 下载指定版本的fabric的docker镜像
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    DOCKER=true
    SAMPLES=true
    BINARIES=true
    # then parse opts
    while getopts "h?dsb" opt; do
    case "$opt" in
    h|\?)
    printHelp
    exit 0
    ;;
    d) DOCKER=false
    ;;
    s) SAMPLES=false
    ;;
    b) BINARIES=false
    ;;
    esac
    done

    if [ "$SAMPLES" == "true" ]; then
    echo
    echo "Installing hyperledger/fabric-samples repo"
    echo
    samplesInstall
    fi
    if [ "$BINARIES" == "true" ]; then
    echo
    echo "Installing Hyperledger Fabric binaries"
    echo
    binariesInstall
    fi
    if [ "$DOCKER" == "true" ]; then
    echo
    echo "Installing Hyperledger Fabric docker images"
    echo
    dockerInstall
    fi
    其中 samplesInstall、 binariesInstall、 dockerInstall分别代表克隆fabric-sample、下载二进制可执行文件、下载docker镜像。

二. 功能具体如下分析:

2.1 samplesInstall

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
samplesInstall() {
# 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 "===> Checking out v${VERSION} of hyperledger/fabric-samples"
git checkout v${VERSION}
elif [ -d fabric-samples ]; then
# if fabric-samples repo already cloned and in current directory,
# cd fabric-samples and checkout corresponding version
echo "===> Checking out v${VERSION} of hyperledger/fabric-samples"
cd fabric-samples && git checkout v${VERSION}
else
echo "===> Cloning hyperledger/fabric-samples repo and checkout v${VERSION}"
git clone -b master https://github.com/hyperledger/fabric-samples.git && cd fabric-samples && git checkout v${VERSION}
fi
}

git clone -b master https://github.com/hyperledger/fabric-samples.git && cd fabric-samples && git checkout v${VERSION} 该命令即表示从github仓库克隆fabric-sample并进入该目录后检出指定版本。

2.2 binariesInstall

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
binariesInstall() {
echo "===> Downloading version ${FABRIC_TAG} platform specific fabric binaries"
binaryDownload "${BINARY_FILE}" "https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/${ARCH}-${VERSION}/${BINARY_FILE}"
if [ $? -eq 22 ]; then
echo
echo "------> ${FABRIC_TAG} platform specific fabric binary is not available to download <----"
echo
fi

echo "===> Downloading version ${CA_TAG} platform specific fabric-ca-client binary"
binaryDownload "${CA_BINARY_FILE}" "https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric-ca/hyperledger-fabric-ca/${ARCH}-${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
fi
}

该函数调用binaryDownload到指定地址下载二进制文件压缩包。

2.2.1 binaryDownload
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# This will attempt to download the .tar.gz all at once, but will trigger the
# binaryIncrementalDownload() function upon a failure, allowing for resume
# if there are network failures.
binaryDownload() {
local BINARY_FILE=$1
local URL=$2
echo "===> Downloading: " "${URL}"
# Check if a previous failure occurred and the file was partially downloaded
if [ -e "${BINARY_FILE}" ]; then
echo "==> Partial binary file found. Resuming download..."
binaryIncrementalDownload "${BINARY_FILE}" "${URL}"
else
curl "${URL}" | tar xz || rc=$?
if [ -n "$rc" ]; then
echo "==> There was an error downloading the binary file. Switching to incremental download."
echo "==> Downloading file..."
binaryIncrementalDownload "${BINARY_FILE}" "${URL}"
else
echo "==> Done."
fi
fi
}

curl "${URL}" | tar xz || rc=$? 使用curl命令下载tar包,并用tar命令解压。解压后的可执行文件出现在bin目录下,但由于下载过程网络原因,会花费较多时间或者下载失败。所以也可以通过编译fabric源码生成。通过编译生成二进制可执行文件的方式在上一篇fabric1.4下载安装中已经讲过。
生成的可执行文件有configtxgen configtxlator cryptogen discover idemixgen orderer peer作用如下

文件 作用
configtxgen 负责生成配置的区块和配置交易
configtxlator 配置信息解读
cryptogen 负责生成组织和成员身份证明文件
discover fabric发现服务
idemixgen 身份混合机制
orderer 负责启动排序节点
peer 负责启动节点

2.3 dockerInstall

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
dockerInstall() {
command -v docker >& /dev/null
NODOCKER=$?
if [ "${NODOCKER}" == 0 ]; then
echo "===> Pulling fabric Images"
dockerFabricPull "${FABRIC_TAG}"
echo "===> Pulling fabric ca Image"
dockerCaPull "${CA_TAG}"
echo "===> Pulling thirdparty docker images"
dockerThirdPartyImagesPull "${THIRDPARTY_TAG}"
echo
echo "===> List out hyperledger docker images"
docker images | grep hyperledger
else
echo "========================================================="
echo "Docker not installed, bypassing download of Fabric images"
echo "========================================================="
fi
}

关键下载语句如下所示:

dockerFabricPull "${FABRIC_TAG}"

dockerCaPull "${CA_TAG}"

dockerThirdPartyImagesPull "${THIRDPARTY_TAG}"

拉取对应的docker镜像并打标签,最后执行==docker images | grep hyperledger==查看是否下载完成所有镜像。

2.3.1 dockerFabricPull
1
2
3
4
5
6
7
8
9
10
11
12
# dockerFabricPull() 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.
dockerFabricPull() {
local FABRIC_TAG=$1
for IMAGES in peer orderer ccenv tools baseos nodeenv javaenv; do
echo "==> FABRIC IMAGE: $IMAGES"
echo
docker pull "hyperledger/fabric-$IMAGES:$FABRIC_TAG"
docker tag "hyperledger/fabric-$IMAGES:$FABRIC_TAG" "hyperledger/fabric-$IMAGES"
done
}
2.3.2 dockerCaPull
1
2
3
4
5
6
7
dockerCaPull() {
local CA_TAG=$1
echo "==> FABRIC CA IMAGE"
echo
docker pull "hyperledger/fabric-ca:$CA_TAG"
docker tag "hyperledger/fabric-ca:$CA_TAG" "hyperledger/fabric-ca"
}
2.3.3 dockerThirdPartyImagesPull
1
2
3
4
5
6
7
8
9
10
dockerThirdPartyImagesPull() {
local THIRDPARTY_TAG=$1
for IMAGES in couchdb kafka zookeeper; do
echo "==> THIRDPARTY DOCKER IMAGE: $IMAGES"
echo
docker pull "hyperledger/fabric-$IMAGES:$THIRDPARTY_TAG"
docker tag "hyperledger/fabric-$IMAGES:$THIRDPARTY_TAG" "hyperledger/fabric-$IMAGES"
done
}

  • 镜像说明
名称 说明 是否必需
hyperledger/fabric-peer peer节点镜像文件
hyperledger/fabric-orderer orderer节点镜像文件
hyperledger/fabric-ccenv GOLANG链码基础镜像文件
hyperledger/fabric-tools 包含crytogen、configtxgen、configtxlator等工具的镜像文件
hyperledger/fabric-ca fabric-ca镜像文件
hyperledger/fabric-couchdb couchdb镜像文件
hyperledger/fabric-kafka kafka镜像文件
hyperledger/fabric-zookeeper zookeeper镜像文件
hyperledger/fabric-javaenv java链码的基础镜像文件
注: 非必需的镜像文件说明只有在需要使用的时候选择即可

最后更新: 2019年10月21日 17:25

原始链接: https://silence-linhl.github.io/blog/2019/10/21/fabric-bootstrap/

× 请我吃糖~
打赏二维码