1. 准备阶段

说明

对应官方教程的准备阶段。

为准备阶段编写了自动化脚本并进行了测试。

该脚本会自动下载安装 git,curl,golang,python,node.js docker,docker-compose。

需要注意的是,不同版本的代码对go的要求不一样,2.0版本要求go版本为1.13及以上,1.4要求v1.12,以下安装的是最新版本的go,可以满足所有版本的要求。

自动构建脚本

  • tips
    1. 需要使用虚拟(Virtual)专用(Private)网络(Network)
    2. 在执行过程中会重启多次,每次重启之后接着执行即可,直到输出 恭喜您成功搭建了 fabric 环境! 再停下,需要重启 2 次
    3. 最好在纯净的 ubuntu 16.04 中运行以下脚本,推荐使用英文版的ubuntu,硬盘推荐40GB以上,内存6GB以上
    4. 输入命令 sudo envprepare.sh 执行脚本,执行过程中需要按几次回车
    5. 如果还是不清楚可以看B站视频
    6. 本小节适合任何fabric版本
# filename envprepare.sh
set -e
if [ ! -f "/home/eftales/step1.txt" ] 
then
    # 更换 apt 源为清华源
    sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

    echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse" > sources.list
    echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse" >> sources.list
    echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse" >> sources.list
    echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse" >> sources.list
    echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse" >> sources.list
    echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse" >> sources.list
    echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse" >> sources.list
    echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse" >> sources.list

    sudo mv sources.list /etc/apt/sources.list

    sudo apt-get update

    # 安装 curl
    sudo apt install curl git -y

    # 安装 golang,并配置 gopath 和 goroot
    echo -e "\n" | sudo add-apt-repository ppa:longsleep/golang-backports
    sudo apt-get update
    sudo apt-get install golang-go -y

    sudo mkdir /home/eftales/gopath
    sudo mkdir /home/eftales/gopath/bin
    sudo mkdir /home/eftales/gopath/pkg
    sudo mkdir -p /home/eftales/gopath/src/github.com/hyperledger

    sudo cp /etc/profile  profile
    sudo chmod 777 profile
    echo "# GOPATH" >> profile 
    echo "export GOPATH=/home/eftales/gopath" >> profile 
    echo "export GOROOT=/usr/lib/go" >> profile 
    echo "export GOBIN=\$GOPATH/bin" >> profile 
    echo "export PATH=\$PATH:\$GOBIN " >> profile 
    sudo mv profile /etc/profile



    # 安装 docker 和 docker-compose
    sudo apt-get remove docker docker-engine docker.io containerd runc
    sudo apt-get update
    sudo apt-get install  -y \
        apt-transport-https \
        ca-certificates \
        curl \
        gnupg-agent \
        software-properties-common
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    sudo add-apt-repository \
    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) \
    stable"
    sudo apt-get update
    sudo apt-get install docker-ce docker-ce-cli containerd.io -y

    sudo curl -L "https://github.com/docker/compose/releases/download/1.25.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    sleep 1
    sudo chmod +x /usr/local/bin/docker-compose
    
    sudo systemctl start docker
    sudo systemctl enable docker
    sudo groupadd docker #添加docker用户组
    sudo usermod -a -G docker $USER

    sudo mkdir -p /etc/docker
    echo "{" > daemon.json
    echo "\"registry-mirrors\": [\"https://ofyci9nf.mirror.aliyuncs.com\"]" >> daemon.json
    echo "}" >> daemon.json
    echo "" >> daemon.json

    sudo mv daemon.json  /etc/docker/daemon.json

    sudo systemctl daemon-reload
    sudo systemctl restart docker
    
    sleep 5

    touch step2.txt
    touch step1.txt
    
    echo "第一步已经完成,请重启并重新执行本脚本以完成第二步"

    # sudo gpasswd -a $USER docker #将当前用户添加至docker用户组
    # sudo newgrp docker #更新docker用户组


elif [ -f "/home/eftales/step2.txt" ] 
then
    # 安装 node.js
    sudo apt-get install -y nodejs nodejs-legacy npm 

    npm config set registry https://registry.npm.taobao.org
    npm config list 
    sudo npm install n -g 
    sudo n 8.9.4
    sudo npm install [email protected] -g
    
    node -v
    npm -v  


    rm step2.txt
    touch step3.txt
    echo "开发环境安装成功!"
    echo "重新启动之后还需要执行本文件下载 fabric-sample "


elif [ -f "/home/eftales/step3.txt" ] 
then
    # 下载 fabric-sample 
    sudo mkdir -p $GOPATH/src/github.com/hyperledger
    cd $GOPATH/src/github.com/hyperledger
    sudo rm -rf  $GOPATH/src/github.com/hyperledger/fabric-samples # 如果有的话先删掉
    sudo curl -sS https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh -o bootsrap.sh
    sudo chmod 777  bootsrap.sh
    sudo ./bootsrap.sh 1.4.4 1.4.4 0.4.15 # 这个命令还会下载上面删掉的东西
    sudo chmod 777 -R fabric-samples # 设置文件权限,如果没有这一步之后执行会报错
    sudo cp /etc/profile  profile
    sudo chmod 777 profile
    echo "# fabric-samples-bin" >> profile
    echo "export PATH=\$PATH:\$GOPATH/src/github.com/hyperledger/fabric-samples/bin" >> profile 

    sudo mv profile  /etc/profile



    rm -f step*.txt
    echo "所有步骤都已经完成!"
    
else
    rm -f step*.txt
fi

如果有问题

  1. docker-compose 无法运行
    重新下载安装docker-compose
  2. cryptogen generate --config=./crypto-config.yaml 报错 panic: runtime error: invalid memory address or nil pointer dereference
    执行 cd $GOPATH/src/github.com/hyperledger && sudo chmod 777 -R fabric-samples
  3. 如果有问题基本都是网络问题,重新下载一遍就行。

你可能感兴趣的:(1. 准备阶段)