00 導言
? ? ??在上一講中霉赡,我們手把手帶著大家一步一步《在Ubuntu 18.04上搭建HyperLedger Fabric 1.2.0 環(huán)境》,其中有兩個步驟是:下載bootstrap.sh引導腳本和執(zhí)行該引導腳本难礼。很多朋友執(zhí)行這兩個步驟時可能會有疑問:為什么要下載這個腳本?執(zhí)行這個腳本的作用是什么玫锋?
? ? ??別急蛾茉,接下來我們會深入分析一下這個文件的源碼,探個究竟撩鹿。不過在分析之前谦炬,我們先來回顧一下上一講是怎么下載和執(zhí)行這個腳本的。
02 如何下載和執(zhí)行bootstrap.sh腳本
? ? ??我們先來看看官方是怎么說的节沦。官方提供的命令如下:
$ curl -sSL http://bit.ly/2ysbOFE | bash -s 1.2.0 1.2.0 0.4.10
? ? ??我們來解釋一下這條命令:curl -sSL http://bit.ly/2ysbOFE键思,這條命令是通過請求短鏈接http://bit.ly/2ysbOFE,該短鏈接會重定向到真正的目標地址甫贯,目標地址會響應我們要的內(nèi)容吼鳞,響應的內(nèi)容就是bootstrap.sh文件里的內(nèi)容。很不幸的是叫搁,上面的短鏈接已經(jīng)失效赖条,不過我們已經(jīng)找到了一個有效的長鏈接:https://raw.githubusercontent.com/hyperledger/fabric/release-1.2/scripts/bootstrap.sh,用它替換掉短鏈接就可以了常熙。于是我們得到了下面的命令:
$ curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/release-1.2/scripts/bootstrap.sh | bash -s 1.2.0 1.2.0 0.4.10
? ? ??這里要解釋一下-sSL的作用纬乍,小s指的是沉默模式,表示不輸出請求過程的任何信息裸卫,只輸出最終響應的內(nèi)容仿贬,大S指的是如果有錯顯示出來,L指的是短鏈接要進行重定向墓贿。
? ? ??接下來是一根豎線 |茧泪,學過linux的朋友應該都知道,這根豎線表示管道聋袋,意思是上一個命令輸出的內(nèi)容队伟,交給下一個命令去執(zhí)行,這里表示交給bash去執(zhí)行幽勒。
? ? ??bash 后面是-s 嗜侮,-s后面的所有內(nèi)容將做為參數(shù)傳給bootstrap.sh。1.2.0 1.2.0 0.4.10這3個對應的是 Fabric, Fabric-ca 和 thirdparty(couchdb, kafka and zookeeper) 這三個模塊的版本。如果我們想要安裝更早的版本锈颗,我們可以修改它們顷霹,腳本程序會知道下載對應的版本。
? ? ??上面實際上是兩條命令通過管道( | )連接在一起執(zhí)行击吱,實際上我們也可以將它們分開:
$ curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/release-1.2/scripts/bootstrap.sh -o bootstrap.sh
$ chmod 755 bootstrap.sh
$ sudo ./bootstrap.sh 1.2.0 1.2.0 0.4.10
03 bootstrap.sh到底幫我們做了什么淋淀?
? ? ??bootstrap.sh實際上幫我們作了如下幾件事情:
? ? ??1. 從github上克隆 hyperledger/fabric-samples并進入該目錄,然后檢出適當?shù)陌姹?br>
? ? ??2. 在fabric-samples目錄下安裝特定平臺的 Hyperledger Fabric 二進制可執(zhí)行文件 和配置文件
? ? ??3.下載 指定版本的Hyperledger Fabric 的docker鏡像
? ? ?? 整個腳本幫我們做了上面這三件事情覆醇,bootstrap.sh中對應的代碼如下:
218 if [ "$SAMPLES" == "true" ]; then
219 echo
220 echo "Installing hyperledger/fabric-samples repo"
221 echo
222 samplesInstall
223 fi
224 if [ "$BINARIES" == "true" ]; then
225 echo
226 echo "Installing Hyperledger Fabric binaries"
227 echo
228 binariesInstall
229 fi
230 if [ "$DOCKER" == "true" ]; then
231 echo
232 echo "Installing Hyperledger Fabric docker images"
233 echo
234 dockerInstall
235 fi
? ? ??其中samplesInstall對應的是克隆fabric-samples朵纷,binariesInstall對應的是安裝二進制文件,dockerInstall對應的是下載docker鏡像永脓。
接下來我們分析一下它們對應的代碼部分袍辞。
04 第一件事:克隆fabric-samples并檢出適當?shù)陌姹?/h3>
? ? ?? 對應bootstrap.sh中的samplesInstall函數(shù),代碼如下:
56 samplesInstall() {
57 # clone (if needed) hyperledger/fabric-samples and checkout corresponding
58 # version to the binaries and docker images to be downloaded
59 if [ -d first-network ]; then
60 # if we are in the fabric-samples repo, checkout corresponding version
61 echo "===> Checking out v${VERSION} branch of hyperledger/fabric-samples"
62 git checkout v${VERSION}
63 elif [ -d fabric-samples ]; then
64 # if fabric-samples repo already cloned and in current directory,
65 # cd fabric-samples and checkout corresponding version
66 echo "===> Checking out v${VERSION} branch of hyperledger/fabric-samples"
67 cd fabric-samples && git checkout v${VERSION}
68 else
69 echo "===> Cloning hyperledger/fabric-samples repo and checkout v${VERSION}"
70 git clone -b master https://github.com/hyperledger/fabric-samples.git && cd fabric-samples && git checkout v${VERSION}
71 fi
72 }
? ? ??我們可以看到第70行代碼憨奸,這行代碼的作用就是 從github上克隆 hyperledger/fabric-samples革屠,然后進入該目錄凿试,并檢出指定的版本排宰。
? ? ??該倉庫的地址是:
https://github.com/hyperledger/fabric-samples.git
05 第二件事:安裝二進制可執(zhí)行文件 和配置文件
? ? ?? 對應bootstrap.sh中的binariesInstall函數(shù),代碼如下:
133 binariesInstall() {
134 echo "===> Downloading version ${FABRIC_TAG} platform specific fabric binaries"
135 binaryDownload ${BINARY_FILE} https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/${ARCH}-${VERSION}/${BINARY_FILE}
136 if [ $? -eq 22 ]; then
137 echo
138 echo "------> ${FABRIC_TAG} platform specific fabric binary is not available to download <----"
139 echo
140 fi
141
142 echo "===> Downloading version ${CA_TAG} platform specific fabric-ca-client binary"
143 binaryDownload ${CA_BINARY_FILE} https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric-ca/hyperledger-fabric-ca/${ARCH}-${CA_VERSION}/${CA_BINARY_FILE}
144 if [ $? -eq 22 ]; then
145 echo
146 echo "------> ${CA_TAG} fabric-ca-client binary is not available to download (Available from 1.1.0-rc1) <----"
147 echo
148 fi
149 }
? ? ??可以看到第135行和第143行分別調(diào)用binaryDownload函數(shù)去下面這兩個地址下載fabric和fabric-ca相應的二進制文件打成的壓縮包那婉。實際上我們也可以使用瀏覽器或者下載軟件單獨下載它們板甘。
1.https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.2.0/hyperledger-fabric-linux-amd64-1.2.0.tar.gz
2.https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric-ca/hyperledger-fabric-ca/linux-amd64-1.2.0/hyperledger-fabric-ca-linux-amd64-1.2.0.tar.gz
? ? ??我們接下來看看binaryDownload函數(shù):
113 binaryDownload() {
114 local BINARY_FILE=$1
115 local URL=$2
116 echo "===> Downloading: " ${URL}
117 # Check if a previous failure occurred and the file was partially downloaded
118 if [ -e ${BINARY_FILE} ]; then
119 echo "==> Partial binary file found. Resuming download..."
120 binaryIncrementalDownload ${BINARY_FILE} ${URL}
121 else
122 curl ${URL} | tar xz || rc=$?
123 if [ ! -z "$rc" ]; then
124 echo "==> There was an error downloading the binary file. Switching to incremental download."
125 echo "==> Downloading file..."
126 binaryIncrementalDownload ${BINARY_FILE} ${URL}
127 else
128 echo "==> Done."
129 fi
130 fi
131 }
? ? ??可以看到第122行,執(zhí)行curl命令去下載tar包,并用tar命令解壓详炬。解壓后的命令出現(xiàn)在bin目錄下盐类。實際上,這些二進制命令我們也可以通過編譯hyperledger fabric的源碼生成呛谜!后面我們有文章會講到怎么編譯源碼去生成這些二進制文件在跳。
-rwxrwxr-x 1 1001 1001 16784432 7月 4 03:04 configtxgen*
-rwxrwxr-x 1 1001 1001 17925784 7月 4 03:04 configtxlator*
-rwxrwxr-x 1 1001 1001 8660280 7月 4 03:04 cryptogen*
-rwxrwxr-x 1 1001 1001 17611704 7月 4 03:04 discover*
-rwxrwxr-x 1 1001 1001 14298688 7月 4 04:41 fabric-ca-client*
-rwxrwxr-x 1 1001 1001 817 7月 4 03:04 get-docker-images.sh*
-rwxrwxr-x 1 1001 1001 7152824 7月 4 03:04 idemixgen*
-rwxrwxr-x 1 1001 1001 22075240 7月 4 03:04 orderer*
-rwxrwxr-x 1 1001 1001 29880000 7月 4 03:04 peer*
? ? ??我們解釋一下其中這些命令的作用:
名稱 | 作用 |
---|---|
peer | 負責啟動節(jié)點,存儲區(qū)塊鏈數(shù)據(jù)隐岛,運行維護鏈碼 |
order | 負責啟動排序節(jié)點猫妙,對交易進行排序,并將排序好的交易打包成模塊 |
cryptogen | 生成組織結(jié)構(gòu)和身份文件 |
configtxgen | 生成配置區(qū)塊和配置交易 |
configtxlator | 解讀配置信息 |
fabric-ca-client | fabric-ca客戶端命令 |
discover | fabric發(fā)現(xiàn)服務的客戶端命令 |
idemixgen | 身份混合機制 |
06 第三件事:下載docker鏡像
? ? ??對應bootstrap.sh中的dockerInstall函數(shù)聚凹,代碼如下:
151 dockerInstall() {
152 which docker >& /dev/null
153 NODOCKER=$?
154 if [ "${NODOCKER}" == 0 ]; then
155 echo "===> Pulling fabric Images"
156 dockerFabricPull ${FABRIC_TAG}
157 echo "===> Pulling fabric ca Image"
158 dockerCaPull ${CA_TAG}
159 echo "===> Pulling thirdparty docker images"
160 dockerThirdPartyImagesPull ${THIRDPARTY_TAG}
161 echo
162 echo "===> List out hyperledger docker images"
163 docker images | grep hyperledger*
164 else
165 echo "========================================================="
166 echo "Docker not installed, bypassing download of Fabric images"
167 echo "========================================================="
168 fi
169 }
? ? ??可以看到第156行割坠,第158行穷劈,第160行称开,分別調(diào)用了dockerFabricPull 贬芥、dockerCaPull 竣蹦、dockerThirdPartyImagesPull 三個函數(shù)去拉取相應的docker鏡像并打標簽宜肉。最后下載完還執(zhí)行docker images | grep hyperledger*(第163行)查看是否已下載好下面所列的鏡像岔擂。
hyperledger/fabric-ca 1.2.0 66cc132bd09c 4 weeks ago 252MB
hyperledger/fabric-tools 1.2.0 379602873003 4 weeks ago 1.51GB
hyperledger/fabric-ccenv 1.2.0 6acf31e2d9a4 4 weeks ago 1.43GB
hyperledger/fabric-orderer 1.2.0 4baf7789a8ec 4 weeks ago 152MB
hyperledger/fabric-peer 1.2.0 82c262e65984 4 weeks ago 159MB
hyperledger/fabric-zookeeper 0.4.10 2b51158f3898 4 weeks ago 1.44GB
hyperledger/fabric-kafka 0.4.10 936aef6db0e6 4 weeks ago 1.45GB
hyperledger/fabric-couchdb 0.4.10 3092eca241fc 4 weeks ago 1.61GB
hyperledger/fabric-baseos amd64-0.4.10 52190e831002 4 weeks ago 132MB
hyperledger/fabric-ca x86_64-1.0.4 8e691b3509bf 9 months ago 238MB
hyperledger/fabric-javaenv x86_64-1.0.4 a517b70135c7 9 months ago 1.41GB
? ? ??dockerFabricPull函數(shù)代碼如下:
28 dockerFabricPull() {
29 local FABRIC_TAG=$1
30 for IMAGES in peer orderer ccenv tools; do
31 echo "==> FABRIC IMAGE: $IMAGES"
32 echo
33 docker pull hyperledger/fabric-$IMAGES:$FABRIC_TAG
34 docker tag hyperledger/fabric-$IMAGES:$FABRIC_TAG hyperledger/fabric-$IMAGES
35 done
36 }
? ? ??dockerCaPull 函數(shù)代碼如下:
48 dockerCaPull() {
49 local CA_TAG=$1
50 echo "==> FABRIC CA IMAGE"
51 echo
52 docker pull hyperledger/fabric-ca:$CA_TAG
53 docker tag hyperledger/fabric-ca:$CA_TAG hyperledger/fabric-ca
54 }
? ? ??dockerThirdPartyImagesPull 函數(shù)代碼如下:
38 dockerThirdPartyImagesPull() {
39 local THIRDPARTY_TAG=$1
40 for IMAGES in couchdb kafka zookeeper; do
41 echo "==> THIRDPARTY DOCKER IMAGE: $IMAGES"
42 echo
43 docker pull hyperledger/fabric-$IMAGES:$THIRDPARTY_TAG
44 docker tag hyperledger/fabric-$IMAGES:$THIRDPARTY_TAG hyperledger/fabric-$IMAGES
45 done
46 }
? ? ??上面dockerThirdPartyImagesPull 函數(shù)內(nèi)部使用了一個for循環(huán)(第40行)挨個去下載couchdb玲昧、kafka耙饰、zookeeper三個鏡像。
? ? ??部分鏡像說明如下:
鏡像名稱 | 是否可選 | 鏡像說明 |
---|---|---|
hyperledger/fabric-tools | 可選 | 包含crytogen蔫饰、configtxgen琅豆、configtxlator我第二次工具的鏡像文件 |
hyperledger/fabric-couchdb | 可選 | CouchDB的數(shù)據(jù)庫鏡像文件、狀態(tài)數(shù)據(jù)庫選擇CouchDB的時候才需要 |
hyperledger/fabric-kafka | 可選 | Kafka的鏡像文件 |
hyperledger/fabric-zookeeper | 可選 | Zookeeper的鏡像文件 |
hyperledger/fabric-peer | 必選 | Peer節(jié)點的鏡像文件 |
hyperledger/fabric-orderer | 必選 | 排序服務節(jié)點的鏡像文件 |
hyperledger/fabric-javaenv | 可選 | java鏈碼的基礎(chǔ)鏡像文件 |
hyperledger/fabric-ccenv | 必選 | Golang鏈碼的基礎(chǔ)鏡像文件 |
hyperledger/fabric-ca | 可選 | fabric-ca的鏡像文件篓吁,用到fabric-ca的時候才需要 |
07 總結(jié)
? ? ??通過上面的介紹茫因,相信大家已經(jīng)了解了bootstrap.sh的作用了。讀者可能會納悶了杖剪,為啥要花這么大篇幅來介紹這個腳本文件冻押??吃飽了撐的嗎盛嘿。這是因為我們通過分析這個文件洛巢,知道了具體內(nèi)部干了什么,知其然也要知其所以然嘛次兆。這樣稿茉,我們也可以不需要這個腳本文件,手動就能把環(huán)境搭建好=嫣俊漓库!
08 參考資料
1.https://hyperledger-fabric.readthedocs.io/en/release-1.2/install.html
2.《深度探索區(qū)塊鏈:Hyperledger技術(shù)與應用》作者: 張增駿 / 董寧 / 朱軒彤 / 陳劍雄 出版社: 機械工業(yè)出版社
- 《區(qū)塊鏈開發(fā)實戰(zhàn) Hyperledger Fabric關(guān)鍵技術(shù)與案例分析》作者: 馮翔 劉濤 吳壽鶴 周廣益 出版社:機械工業(yè)出版社
- 《HyperLedger Fabric開發(fā)實戰(zhàn)快速掌握區(qū)塊鏈技術(shù)》作者:楊毅 出版社: 電子工業(yè)出版社