Contents

Hyperledger Fabric 1.4.4的环境配置

最近在kcoin小组里参与智能合约的部分,需要用到Hyperledger Fabric,之前在环境配置这里一直卡了好久,所以想把整个过程记录下来,为之后环境重装做一个参考。

1. 前提条件

  • cURL — 最新版
  • Docker — 版本 17.06.2-ce 及以上
  • Docker Compose — 版本1.14.0及以上
  • Golang — 版本1.12.x
  • Nodejs — 8 : 版本8.9.4及以上 ; 10 : 版本10.15.3及以上(实测最新版本13.1.0-2也可以运行)

主要值得一提的是Docker Compose和Nodejs的安装,其下载是不依赖于包管理器的,所以多种发行版的linux都通用。

1.1 Docker Compose的安装

首先下载可执行文件到系统路径下(该步骤可能下载速度比较慢)

1
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

之后赋予该可执行文件执行权限

1
sudo chmod +x /usr/local/bin/docker-compose

1.2 Nodejs安装

根据官方ubuntu一键安装脚本的启发,使用nvm来安装node和npm
执行一键安装脚本

1
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.1/install.sh | bash

加载环境变量,将以下命令直接在终端下运行或者加到~/.bashrc~/.zshrc等配置文件中

1
2
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

安装最新版本的node,npm

1
nvm install

切换不同版本的node,npm

1
nvm use 10

更多使用方法可以参考官方文档https://github.com/nvm-sh/nvm

2. 下载案例、二进制文件以及docker镜像

2.1 一键安装

如果网速足够好可以使用官方的一键安装脚本直接安装,它会下载完所有需要的东西

1
2
3
4
# 删除以前安装的fabric镜像
docker images -a | grep "fabric" | awk '{print $3}' | xargs docker rmi -f
# 将短网址(http://bit.ly/2ysbOFE)改成了完整网址
curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh | bash -s -- 1.4.4 1.4.4 0.4.18

2.2 坎坷安装

2.2.1 准备fabric-samples及二进制文件

我将下载好的fabric-samples以及二进制文件打包成zip文件,下载该zip文件并解压

(或者可以单独clone fabric-samples仓库
下载二进制文件到fabric-samples目录下的bin目录
https://github.com/hyperledger/fabric/releases/download/v1.4.4/hyperledger-fabric-linux-amd64-1.4.4.tar.gz
https://github.com/hyperledger/fabric-ca/releases/download/v1.4.4/hyperledger-fabric-ca-linux-amd64-1.4.4.tar.gz
)

2.2.2 下载docker镜像

之后直接下载docker 镜像即可

将以下脚本保存为bootstrap.sh

  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
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/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=1.4.4
# if ca version not passed in, default to latest released version
CA_VERSION=1.4.4
# current version of thirdparty images (couchdb, kafka and zookeeper) released
THIRDPARTY_IMAGE_VERSION=0.4.18
ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]'|sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')")
MARCH=$(uname -m)

# 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.

dockerPull() {
    image_tag=$1
    shift
    while [[ $# -gt 0 ]]
    do
        image_name="$1"
        echo "====> hyperledger/fabric-$image_name:$image_tag"
        docker pull "hyperledger/fabric-$image_name:$image_tag"
        docker tag "hyperledger/fabric-$image_name:$image_tag" "hyperledger/fabric-$image_name"
        shift
    done
}

pullDockerImages() {
    command -v docker >& /dev/null
    NODOCKER=$?
    if [ "${NODOCKER}" == 0 ]; then
        FABRIC_IMAGES=(peer orderer ccenv tools)
        case "$VERSION" in
        1.*)
            FABRIC_IMAGES+=(javaenv)
            shift
            ;;
        2.*)
            FABRIC_IMAGES+=(nodeenv baseos javaenv)
            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 "===> Pulling thirdparty docker images"
        THIRDPARTY_IMAGES=(zookeeper kafka couchdb)
        dockerPull "${THIRDPARTY_TAG}" "${THIRDPARTY_IMAGES[@]}"
        echo
        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
    VERSION=$1;shift
    if [ -n "$1" ]  && [ "${1:0:1}" != "-" ]; then
        CA_VERSION=$1;shift
        if [ -n  "$1" ] && [ "${1:0:1}" != "-" ]; then
            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
    export FABRIC_TAG=${MARCH}-${VERSION}
    export CA_TAG=${MARCH}-${CA_VERSION}
    export THIRDPARTY_TAG=${MARCH}-${THIRDPARTY_IMAGE_VERSION}
else
    # starting with 1.2.0, multi-arch images will be default
    : "${CA_TAG:="$CA_VERSION"}"
    : "${FABRIC_TAG:="$VERSION"}"
    : "${THIRDPARTY_TAG:="$THIRDPARTY_IMAGE_VERSION"}"
fi


# 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 [ "$DOCKER" == "true" ]; then
    echo
    echo "Pull Hyperledger Fabric docker images"
    echo
    pullDockerImages
fi

将当前用户加入docker组

1
2
3
sudo usermod -aG docker $(whoami)
# 以docker组登陆
newgrp docker 

执行以上脚本

1
2
chmod +x bootstrap.sh
bash bootstrap.sh 1.4.4 1.4.4 0.4.18

pull的docker镜像包括:

  • hyperledger/fabric-peer
  • hyperledger/fabric-orderer
  • hyperledger/fabric-ccenv
  • hyperledger/fabric-ca
  • hyperledger/fabric-zookeeper
  • hyperledger/fabric-kafka
  • hyperledger/fabric-couchdb

3.总结和感想

其实早应该总结出这个文档并发表在博客上的,总是保存成用pdf格式代码拷贝也不方便,而且自己也应该督促自己多动笔,多总结。

在安装环境的过程中两次用到了一键安装的脚本,虽然自己总标榜自己用linux,但是shell脚本编程一点都不懂,自己会的就是简单的linux 命令的堆砌,一点也没有领略到shell脚本的精粹。一定要买那本被广泛推荐的书《linux shell脚本攻略》(我记得以前还在老图漆黑的地下图书室翻出来过,但是一点也没有看!)

还有就是加入kcoin小组到现在虽然一直都没有成果,对整个项目依旧不太熟悉,但是真的感受到了开源的魅力,觉得那句话超酷:

talk is cheap, show me the code

开源社区的氛围真的好吸引人,我可不可以更加积极一点参与讨论交流,贡献代码,收获朋友呢?

文笔好差,以后更要多加练习,学会用更简洁明了的话来叙述清楚一件事,努力改进吧。

参考

  1. docker compose官方安装教程 https://docs.docker.com/compose/install/
  2. nvm官方仓库 https://github.com/creationix/nvm