git fabric 1.0.0 官方源码进行编译,生成 docker images

系统环境:centos 7 64位
docker
docker-compose

不要使用 centos yum 仓库带的 docker,版本过低。
安装docker 官方的repo 里面的版本

$ sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF

yum update

yum install docker-engine

systemctl enable docker

systemctl restart docker

一.添加 fabric 用户和设置存放源码的目录

useradd fabric
# 以 fabric 用户登录,建立如下项目目录.
mkdir /home/fabric/fabric

二.安装 go 的开发环境和必须的组件

以 root 用户安装

wget https://storage.googleapis.com/golang/go1.8.1.linux-amd64.tar.gz

tar -xzvf go1.8.1.linux-amd64.tar.gz

mv ./go  /usr/local

//修改 etc/profile,增加 如下2行内容
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin

source /etc/profile
#安装后,确认 go 1.8 安装成功
go version
go version go1.8 darwin/amd64

#如果无法科学上网,使用如下步骤:
# 1. 以 root安装 gopm
    export GOPATH=/usr/local
    go get -u github.com/gpmgo/gopm
    #确认在/usr/local/bin 目录下有 gopm
# 2. 以 fabric 用户,通过 gopm 安装 goimports
    export GOPATH=/home/fabric/fabric
    gopm get -g -d golang.org/x/tools/cmd/goimports
    #再使用 go install 安装 goimports
    go install golang.org/x/tools/cmd/goimports
    #确认在/home/fabric/fabric/bin 目录下有 goimports

以 fabric 用户 安装 gocov

GOPATH=/home/fabric/fabric
gopm get -g -d golang.org/x/tools/cover
gopm get -g -d github.com/axw/gocov/gocov
go install github.com/axw/gocov/gocov
# 确认 /home/fabric/fabric/bin 目录下有 gocov

安装 gocov-xml

GOPATH=/home/fabric/fabric
gopm get -g -d github.com/AlekSi/gocov-xml
go install github.com/AlekSi/gocov-xml
#确认 /home/fabric/fabric/bin 目录下有 gocov-xml

三. 安装其他必须的组件

以 root 用户安装
```

centos 下需要安装

yum install -y gcc libtool libltdl-dev libtool-ltdl-devel openssl

## 四.设置fabric环境变量

vim ~/.bashrc
export GOPATH=/home/fabric/fabric

使得环境变量生效

source ~/.bashrc
```

五.进入项目目录

cd $GOPATH

六. 建立 go 源码目录结构(一定要建立 src/github.com/hyperledger ,否则无法使用 go 进行编译)

cd $GOPATH
mkdir -p src/github.com/hyperledger

七.拉取 fabric 的源码

cd $GOPATH/src/github.com/hyperledger
git clone https://github.com/hyperledger/fabric.git

八.编译生成 protoc-gen-go

#编译生成 protoc-gen-go 
cd $GOPATH
gopm get -g -d github.com/golang/protobuf/protoc-gen-go
go install github.com/golang/protobuf/protoc-gen-go
#确认在$GOPATH/bin出现protoc-gen-go执行文件

九. 编译 fabric docker 环境

cd $GOPATH/src/github.com/hyperledger/fabric
make docker

如果出现 Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock 这个错误。
解决方案:
1. 把 fabric 用户加入到 sudo 组
a. 以 root 用户登录
b. vim /etc/sudoers
c. 增加 fabric ALL=(ALL) ALL 一行到root ALL=(ALL) ALL下面
2. sudo usermod -a -G docker \(USER
3. newgrp - docker
4. newgrp - `groups \){USER} | cut -d' ' -f1`
参考:
https://techoverflow.net/2017/03/01/solving-docker-permission-denied-while-trying-to-connect-to-the-docker-daemon-socket/

十.如果出现文件或者命令没找到的错误,复制go 的相关文件到 fabric 编译环境

cp $GOPATH/bin/protoc-gen-go $GOPATH/src/github.com/hyperledger/fabric/build/docker/gotools/bin/ 

cp $GOPATH/bin/gocov $GOPATH/src/github.com/hyperledger/fabric/build/docker/gotools/bin/ 

十一.编译 fabric ca 环境

cd /root
git clone https://github.com/hyperledger/fabric-ca.git
cd fabric-ca
make docker

十二. 部署和测试 e2e 应用

一键启动
cd $GOPATH/src/github.com/hyperledger/fabric/examples/e2e_cli
./network_setup.sh up mytestchannel

十三. 部署 Fabric Sample

官方文档:
http://hyperledger-fabric.readthedocs.io/en/latest/samples.html

1.拉取Fabric Sample 的源码

cd ~
git clone https://github.com/hyperledger/fabric-samples.git
cd ~/fabric-samples

2. 下载所需要的执行文件。

官方方法 直接 通过如下命令 :
curl -sSL https://goo.gl/iX9dek | bash

由于不能科学上网。只能通过 浏览器访问

https://goo.gl/iX9dek

或则 访问

https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap-1.0.0.sh

然后把这个 shell 文件保存到 ~/fabric-samples/run.sh 文件内。
然后chmod +xxx ~/fabric-samples/run.sh 。
成功后运行 run.sh。
shell 会自动拉取所有的 fabric 1.0.0 的 image
并且生成 bin目录。目录下有下列文件:

  • cryptogen,
  • configtxgen,
  • configtxlator
  • peer

Finally, the script will download the Hyperledger Fabric docker images from Docker Hub into your local Docker registry and tag them as ‘latest’.

The script lists out the Docker images installed upon conclusion.

Look at the names for each image; these are the components that will ultimately comprise our Hyperledger Fabric network. You will also notice that you have two instances of the same image ID - one tagged as “x86_64-1.0.0” and one tagged as “latest”.

3. 更新环境变量 PATH

export PATH=/home/fabric/fabric-samples/bin:$PATH

4. run it now

cd ~/fabric-samples/first-network
./byfn.sh -m generate

成功后,控制台显示

[fabric@fabirc first-network]$ ./byfn.sh -m generate
Generating certs and genesis block for with channel 'mychannel' and CLI timeout of '10000'
Continue (y/n)? y
proceeding ...
/home/fabric/fabric-samples/bin/cryptogen

##########################################################
##### Generate certificates using cryptogen tool #########
##########################################################
org1.example.com
org2.example.com

/home/fabric/fabric-samples/bin/configtxgen
##########################################################
#########  Generating Orderer Genesis block ##############
##########################################################
2017-07-21 15:05:30.414 CST [common/configtx/tool] main -> INFO 001 Loading configuration
2017-07-21 15:05:30.453 CST [common/configtx/tool] doOutputBlock -> INFO 002 Generating genesis block
2017-07-21 15:05:30.455 CST [common/configtx/tool] doOutputBlock -> INFO 003 Writing genesis block

#################################################################
### Generating channel configuration transaction 'channel.tx' ###
#################################################################
2017-07-21 15:05:30.489 CST [common/configtx/tool] main -> INFO 001 Loading configuration
2017-07-21 15:05:30.496 CST [common/configtx/tool] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx
2017-07-21 15:05:30.497 CST [common/configtx/tool] doOutputChannelCreateTx -> INFO 003 Writing new channel tx

#################################################################
#######    Generating anchor peer update for Org1MSP   ##########
#################################################################
2017-07-21 15:05:30.541 CST [common/configtx/tool] main -> INFO 001 Loading configuration
2017-07-21 15:05:30.545 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update
2017-07-21 15:05:30.546 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update

#################################################################
#######    Generating anchor peer update for Org2MSP   ##########
#################################################################
2017-07-21 15:05:30.590 CST [common/configtx/tool] main -> INFO 001 Loading configuration
2017-07-21 15:05:30.593 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update
2017-07-21 15:05:30.593 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update

[fabric@fabirc first-network]$

5.启动网络

./byfn.sh -m up

如果以前做过测试,特别是通过源码编译,运行过e2e_cli 例子后。这里可能会报告 容器已经存在的错误。

    ERROR: for peer0.org1.example.com  Cannot create container for service peer0.org1.example.com: Conflict. The container name "/peer0.org1.example.com" is already in use by container "f8ab810101f3db4eb5e0a15ef2e06f0fee19259225541031d73c6eaa70a0640e".Creating peer1.org1.example.com ... error
    
    ERROR: for peer1.org1.example.com  Cannot create container for service peer1.org1.example.com: Conflict. The container name "/peer1.org1.example.com" is already in use by container "1517363d7424e01e07f1e5230fcc955d2cc4a1f8778842777ea5a237fedc9b3b".Creating peer0.org2.example.com ... error
    
    ERROR: for peer0.org2.example.com  Cannot create container for service peer0.org2.example.com: Conflict. The container name "/peer0.org2.example.com" is already in use by container "513aeb4b3d8f9b3ec2f90c42d19d6c4a9b2aa058ad39f931de009be8980a192f".Creating peer1.org2.example.com ... error
    
    ERROR: for peer1.org2.example.com  Cannot create container for service peer1.org2.example.com: Conflict. The container name "/peer1.org2.example.com" is already in use by container "c9ac14cc4a6b002dec353bcd2e008752c00f4129d811e250b38c2702c9d83e28".Creating orderer.example.com ... error

可以通过下面命令,清除一下原有的容器,并且删除原来的测试容器用的 images

docker rm -f $(docker ps -aq)
docker rmi  $(docker images -a | grep dev-  | awk '{print $3 }')

控制台出现 :

Starting with channel 'mychannel' and CLI timeout of '10000'
Continue (y/n)?y
proceeding ...
Creating network "net_byfn" with the default driver
Creating peer0.org1.example.com
Creating peer1.org1.example.com
Creating peer0.org2.example.com
Creating orderer.example.com
Creating peer1.org2.example.com
Creating cli


 ____    _____      _      ____    _____
/ ___|  |_   _|    / \    |  _ \  |_   _|
\___ \    | |     / _ \   | |_) |   | |
 ___) |   | |    / ___ \  |  _ <    | |
|____/    |_|   /_/   \_\ |_| \_\   |_|

Channel name : mychannel
Creating channel...

The logs will continue from there. This will launch all of the containers, and then drive a complete end-to-end application scenario. Upon successful completion, it should report the following in your terminal window:

Attempting to Query PEER3 ...3 secs

2017-07-21 07:22:03.903 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2017-07-21 07:22:03.903 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2017-07-21 07:22:03.903 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2017-07-21 07:22:03.903 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2017-07-21 07:22:03.903 UTC [msp/identity] Sign -> DEBU 005 Sign: plaintext: 0A95070A6708031A0C089BD7C6CB0510...6D7963631A0A0A0571756572790A0161
2017-07-21 07:22:03.903 UTC [msp/identity] Sign -> DEBU 006 Sign: digest: 56559226BBF8E0BAC8522F3728F6AC5141868518117FEBE522CC7354DB856494
Query Result: 90
2017-07-21 07:22:18.236 UTC [main] main -> INFO 007 Exiting.....
===================== Query on PEER3 on channel 'mychannel' is successful =====================

========= All GOOD, BYFN execution completed ===========


 _____   _   _   ____
| ____| | \ | | |  _ \
|  _|   |  \| | | | | |
| |___  | |\  | | |_| |
|_____| |_| \_| |____/

6.停止和卸载,删除容器,镜像.

The following will kill your containers, remove the crypto material and four artifacts, and delete the chaincode images from your Docker Registry:

./byfn.sh -m down

你可能感兴趣的:(hyperledger,fabric)