1. 程式人生 > >fabric v1.1 自動化部署指令碼

fabric v1.1 自動化部署指令碼

自動部署如下結構:

IP 節點 域名
10.254.186.164 orderer orderer.example.com
10.254.186.164 peer peer0.org1.example.com
10.254.247.165 peer peer1.org1.example.com
10.254.207.154 peer
peer0.org2.example.com

前置環境:

  • go v1.9+
  • fabric v1.1的bin檔案已經放到$PATH
  • 寫好的chaincode
  • docker
  • /etc/hosts 已經配置好上面的域名對映
  • 機器間開通免密登入

啟動環境:

清空環境:

相關檔案:

deploy.sh

path=`pwd`
mychannel=$1

if [ $# -ne 1 ]; then
        echo
"請新增channelID" echo $mychannel exit else echo "start deploying" fi #建立資料夾 if [ ! -d ~/peer ];then mkdir ~/peer else echo dir exist fi if [ ! -d ~/orderer ];then mkdir ~/orderer else echo dir exist fi echo "##########################################################" echo "##### Generate certificates using cryptogen tool #########"
echo "##########################################################" cryptogen generate --config=crypto-config.yaml --output ./certs mkdir orderer.example.com cp -rf certs/ordererOrganizations/example.com/orderers/orderer.example.com/* orderer.example.com/ cp orderer.yaml orderer.example.com cp orderer_start.sh orderer.example.com/start.sh mkdir orderer.example.com/data mkdir peer0.org1.example.com cp -rf certs/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/* peer0.org1.example.com/ cp core.yaml peer0.org1.example.com mkdir peer0.org1.example.com/data cp -rf peer0.org1.example.com/ peer1.org1.example.com/ rm -rf peer1.org1.example.com/msp/ rm -rf peer1.org1.example.com/tls/ cp -rf certs/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/* peer1.org1.example.com/ sed -i "s/peer0.org1.example.com/peer1.org1.example.com/g" peer1.org1.example.com/core.yaml cp -rf peer0.org1.example.com/ peer0.org2.example.com/ rm -rf peer0.org2.example.com/msp/ rm -rf peer0.org2.example.com/tls/ cp -rf certs/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/* peer0.org2.example.com/ sed -i "s/peer0.org1.example.com/peer0.org2.example.com/g" peer0.org2.example.com/core.yaml sed -i "s/Org1MSP/Org2MSP/g" peer0.org2.example.com/core.yaml #傳輸peer/orderer cp peer_start.sh peer0.org1.example.com/start.sh cp peer_start.sh peer1.org1.example.com/start.sh cp peer_start.sh peer0.org2.example.com/start.sh echo "##########################################################" echo "######### Generating Orderer Genesis block ##############" echo "##########################################################" #生成創世區塊 configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./genesisblock cp genesisblock ~/orderer #遠端部署peer cd $path ssh [email protected] "mkdir -p ~/peer; rm -rf ~/peer/*" ssh [email protected] "mkdir -p ~/peer; rm -rf ~/peer/*" scp -r orderer.example.com/* ~/orderer/ scp -r peer0.org1.example.com/* ~/peer/ scp -r peer1.org1.example.com/* 10.254.247.165:~/peer/ scp -r peer0.org2.example.com/* 10.254.207.154:~/peer/ echo "##########################################################" echo "############ start orderer && peers ####################" echo "##########################################################" #啟動本地的peer/orderer cd ~/orderer echo "strating orderer ..." sh start.sh if [ $? != 0 ]; then echo "orderer start fail" exit 1 else echo "orderer start success" fi cd ~/peer echo "starting peer ..." sh start.sh if [ $? != 0 ]; then echo "peer start fail" exit 1 else echo "peer start success" fi #啟動遠端peer ssh [email protected] "cd ~/peer; sh start.sh > /dev/null 2>&1 &" ssh [email protected] "cd ~/peer; sh start.sh > /dev/null 2>&1 &" echo "##########################################################" echo "######### Create Adimn && Users in each ORG #############" echo "##########################################################" #建立使用者 #org1 Admin(peer0) cd $path mkdir Admin_1 cp -rf certs/peerOrganizations/org1.example.com/users/[email protected]/* Admin_1/ cp peer0.org1.example.com/core.yaml Admin_1/ cp peer.sh Admin_1/ cd Admin_1 echo `./peer.sh node status` #org1 user1 (peer1) cd $path cp -rf Admin_1/ User_1/ rm -rf User_1/msp rm -rf User_1/tls cp -rf certs/peerOrganizations/org1.example.com/users/[email protected]/* User_1/ sed -i "s/peer0.org1.example/peer1.org1.example/g" User_1/peer.sh #org2 Admin cp -rf Admin_1/ Admin_2/ rm -rf Admin_2/msp/ rm -rf Admin_2/tls/ cp -rf certs/peerOrganizations/org2.example.com/users/[email protected]/* Admin_2/ sed -i "s/peer0.org1.example/peer0.org2.example/g" Admin_2/peer.sh sed -i "s/Org1MSP/Org2MSP/g" Admin_2/peer.sh sed -i "s/peer0.org1.example.com/peer0\.org2\.example.com/g" Admin_2/core.yaml sed -i "s/Org1MSP/Org2MSP/g" Admin_2/core.yaml #複製根證書 #把使用org1的另一個身份控制peer1.org1.example.com cp -rf Admin_1/ Admin_1_2/ sed -i "s/peer0.org1.example/peer1.org1.example/g" Admin_1_2/peer.sh cp certs/ordererOrganizations/example.com/tlsca/tlsca.example.com-cert.pem Admin_1/ cp certs/ordererOrganizations/example.com/tlsca/tlsca.example.com-cert.pem User_1/ cp certs/ordererOrganizations/example.com/tlsca/tlsca.example.com-cert.pem Admin_2/ cp certs/ordererOrganizations/example.com/tlsca/tlsca.example.com-cert.pem Admin_1_2/ echo "##########################################################" echo "######### Create Channel && Anchor Peer #################" echo "##########################################################" #建立channel configtxgen -profile TwoOrgsChannel -outputCreateChannelTx $mychannel.tx -channelID $mychannel configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate Org1MSPanchors.tx -channelID $mychannel -asOrg Org1MSP configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate Org2MSPanchors.tx -channelID $mychannel -asOrg Org2MSP cd Admin_1 ./peer.sh channel create -o orderer.example.com:7050 -c $mychannel -f ../$mychannel.tx --tls true --cafile tlsca.example.com-cert.pem cp $mychannel.block ../Admin_2 cp $mychannel.block ../User_1 cp $mychannel.block ../Admin_1_2 #peer加入channel ./peer.sh channel join -b $mychannel.block if [ $? != 0 ]; then echo "peer0.org1.example join $mychannel fail" exit 1 else echo "------peer0.org1.example join $mychannel success !!!" fi cd ../Admin_1_2/ ./peer.sh channel join -b $mychannel.block if [ $? != 0 ]; then echo "peer1.org1.example join $mychannel fail" exit 1 else echo "------peer1.org1.example join $mychannel success !!!" fi cd ../Admin_2/ ./peer.sh channel join -b $mychannel.block if [ $? != 0 ]; then echo "peer0.org2.example join $mychannel fail" exit 1 else echo "------peer0.org2.example join $mychannel success !!!" fi cd ../Admin_1/ ./peer.sh channel update -o orderer.example.com:7050 -c $mychannel -f ../Org1MSPanchors.tx --tls true --cafile ./tlsca.example.com-cert.pem cd ../Admin_2/ ./peer.sh channel update -o orderer.example.com:7050 -c $mychannel -f ../Org2MSPanchors.tx --tls true --cafile ./tlsca.example.com-cert.pem echo "##########################################################" echo "#################### ChainCode install ###################" echo "##########################################################" #安裝合約 go get github.com/introclass/hyperledger-fabric-chaincodes/demo cd $path cd Admin_1 ./peer.sh chaincode package demo-pack.out -n demo -v 0.0.1 -s -S -p github.com/introclass/hyperledger-fabric-chaincodes/demo ./peer.sh chaincode signpackage demo-pack.out signed-demo-pack.out cp signed-demo-pack.out ../Admin_1_2/ cp signed-demo-pack.out ../Admin_2/ ./peer.sh chaincode install ./signed-demo-pack.out if [ $? != 0 ]; then echo "peer0.org1.example.com install $mychannel fail" exit 1 else echo "------peer0.org1.example.com $mychannel success !!!" fi cd ../Admin_1_2 ./peer.sh chaincode install ./signed-demo-pack.out if [ $? != 0 ]; then echo "peer1.org1.example.com install $mychannel fail" exit 1 else echo "------peer1.org1.example.com $mychannel success !!!" fi cd ../Admin_2 ./peer.sh chaincode install ./signed-demo-pack.out if [ $? != 0 ]; then echo "peer0.org2.example.com install $mychannel fail" exit 1 else echo "------peer0.org2.example.com $mychannel success !!!" fi echo "##########################################################" echo "#################### ChainCode invoke ###################" echo "##########################################################" cd ../Admin_1 echo "Admin_1 instantiate" ./peer.sh chaincode instantiate -o orderer.example.com:7050 --tls true --cafile ./tlsca.example.com-cert.pem -C $mychannel -n demo -v 0.0.1 -c '{"Args":["init"]}' -P "OR('Org1MSP.member','Org2MSP.member')" sleep 10 echo "Admin_1 invoke" ./peer.sh chaincode invoke -o orderer.example.com:7050 --tls true --cafile ./tlsca.example.com-cert.pem -C $mychannel -n demo -c '{"Args":["write","key1","key1valueisabc"]}' sleep 2 cd ../Admin_2 echo "Admin_2 query" ./peer.sh chaincode query -C $mychannel -n demo -c '{"Args":["query","key1"]}' sleep 2 cd ../User_1 echo "User_1 query" ./peer.sh chaincode query -C $mychannel -n demo -c '{"Args":["query","key1"]}' echo "deploy finished !!!"

clear.sh

killall -9 orderer
killall -9 peer
rm -rf ~/peer
rm -rf ~/orderer
rm -rf Admin\@org1.example.com/
rm -rf certs/
rm -rf genesisblock
rm -rf orderer.example.com/
rm -rf peer0.org*
rm -rf peer1.org1.example.com/
rm -rf Admin_1
rm -rf Admin_2
rm -rf User_1
rm -rf Admin_1_2
rm -rf *.tx
ssh [email protected] "killall -9 peer;rm -rf ~/peer/*"
ssh [email protected] "killall -9 peer;rm -rf ~/peer/*"

peer.sh

#!/bin/bash
export FABRIC_CFG_PATH=`pwd`

export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_TLS_CERT_FILE=./tls/client.crt
export CORE_PEER_TLS_KEY_FILE=./tls/client.key

export CORE_PEER_MSPCONFIGPATH=./msp
export CORE_PEER_ADDRESS=peer0.org1.example.com:7051
export CORE_PEER_LOCALMSPID=Org1MSP
export CORE_PEER_TLS_ROOTCERT_FILE=./tls/ca.crt
export CORE_PEER_ID=cli
export CORE_LOGGING_LEVEL=INFO

peer $*

orderer_start.sh

nohup orderer start 2>&1 &

peer_start.sh

nohup peer node start 2>&1 &

cryptoconfig.yaml

OrdererOrgs:
  - Name: Orderer
    Domain: example.com
    Specs:
      - Hostname: orderer
PeerOrgs:
  - Name: Org1
    Domain: org1.example.com
    Template:
      Count: 2
    Users:
      Count: 1
  - Name: Org2
    Domain: org2.example.com
    Template:
      Count: 1
    Users:
      Count: 1

configtx.yaml

OrdererOrgs:
  - Name: Orderer
    Domain: example.com
    Specs:
      - Hostname: orderer
PeerOrgs:
  - Name: Org1
    Domain: org1.example.com
    Template:
      Count: 2
    Users:
      Count: 1
  - Name: Org2
    Domain: org2.example.com
    Template:
      Count: 1
    Users:
      Count: 1
bogon:fabric_deploy didi$ cat configtx.yaml
Profiles:
    TwoOrgsOrdererGenesis:
        Orderer:
            <<: *OrdererDefaults
            Organizations:
                - *OrdererOrg
        Consortiums:
            SampleConsortium:
                Organizations:
                    - *Org1
                    - *Org2
    TwoOrgsChannel:
        Consortium: SampleConsortium
        Application:
            <<: *ApplicationDefaults
            Organizations:
                - *Org1
                - *Org2
Organizations:
    - &OrdererOrg
        Name: OrdererOrg
        ID: OrdererMSP
        MSPDir: ./certs/ordererOrganizations/example.com/msp
    - &Org1
        Name: Org1MSP
        ID: Org1MSP
        MSPDir: ./certs/peerOrganizations/org1.example.com/msp
        AnchorPeers:
            - Host: peer0.org1.example.com
              Port: 7051
    - &Org2
        Name: Org2MSP
        ID: Org2MSP
        MSPDir: ./certs/peerOrganizations/org2.example.com/msp
        AnchorPeers:
            - Host: peer0.org2.example.com
              Port: 7051
Orderer: &OrdererDefaults
    OrdererType: solo
    Addresses:
        - orderer.example.com:7050
    BatchTimeout: 2s
    BatchSize:
        MaxMessageCount: 10
        AbsoluteMaxBytes: 99 MB
        PreferredMaxBytes: 512 KB
    Kafka:
        Brokers:
            - 127.0.0.1:9092
    Organizations:
Application: &ApplicationDefaults
    Organizations:

core.yaml

logging:
    peer:       warning
    cauthdsl:   warning
    gossip:     warning
    ledger:     info
    msp:        warning
    policies:   warning
    grpc:       error
    format: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
peer:
    id: peer0.org1.example.com
    networkId: dev
    listenAddress: 0.0.0.0:7051
    address: 0.0.0.0:7051
    addressAutoDetect: false
    gomaxprocs: -1
    gossip:
        bootstrap: 127.0.0.1:7051
        bootstrap: peer0.org1.example.com:7051
        useLeaderElection: true
        orgLeader: false
        endpoint:
        maxBlockCountToStore: 100
        maxPropagationBurstLatency: 10ms
        maxPropagationBurstSize: 10
        propagateIterations: 1
        propagatePeerNum: 3
        pullInterval: 4s
        pullPeerNum: 3
        requestStateInfoInterval: 4s
        publishStateInfoInterval: 4s
        stateInfoRetentionInterval:
        publishCertPeriod: 10s
        skipBlockVerification: false
        dialTimeout: 3s
        connTimeout: 2s
        recvBuffSize: 20
        sendBuffSize: 200
        digestWaitTime: 1s
        requestWaitTime: 1s
        responseWaitTime: 2s
        aliveTimeInterval: 5s
        aliveExpirationTimeout: 25s
        reconnectInterval: 25s
        externalEndpoint: peer0.org1.example.com:7051
        election:
            startupGracePeriod: 15s
            membershipSampleInterval: 1s
            leaderAliveThreshold: 10s
            leaderElectionDuration: 5s
    events:
        address: 0.0.0.0:7053
        buffersize: 100
        timeout: 10ms
    tls:
        enabled: true
        cert:
            file: ./tls/server.crt
        key:
            file: ./tls/server.key
        rootcert:
            file: ./tls/ca.crt
        serverhostoverride:
    fileSystemPath: ./data
    BCCSP:
        Default: SW
        SW:
            Hash: SHA2
            Security: 256
            FileKeyStore:
                KeyStore:
    mspConfigPath: msp
    localMspId: Org1MSP
    profile:
        enabled:    true
        listenAddress: 0.0.0.0:6060
vm:
    endpoint: unix:///var/run/docker.sock
    docker:
        tls:
            enabled: false
            ca:
                file: docker/ca.crt
            cert:
                file: docker/tls.crt
            key:
                file: docker/tls.key
        attachStdout: false
        hostConfig:
            NetworkMode: host
            Dns:
               # - 192.168.0.1
            LogConfig:
                Type: json-file
                Config:
                    max-size: "50m"
                    max-file: "5"
            Memory: 2147483648
chaincode:
    peerAddress:
    id:
        path:
        name:
    builder: $(DOCKER_NS)/fabric-ccenv:$(ARCH)-$(PROJECT_VERSION)
    golang:
        runtime: $(BASE_DOCKER_NS)/fabric-baseos:$(ARCH)-$(BASE_VERSION)
    car:
        runtime: $(BASE_DOCKER_NS)/fabric-baseos:$(ARCH)-$(BASE_VERSION)
    java:
        Dockerfile:  |
            from $(DOCKER_NS)/fabric-javaenv:$(ARCH)-$(PROJECT_VERSION)
    startuptimeout: 300s
    executetimeout: 30s
    mode: net
    keepalive: 0
    system:
        cscc: enable
        lscc: enable
        escc: enable
        vscc: enable
        qscc: enable
    logging:
      level:  info
      shim:   warning
      format: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
ledger:
  blockchain:
  state:
    stateDatabase: goleveldb
    couchDBConfig:
       couchDBAddress: 127.0.0.1:5984
       username:
       password:
       maxRetries: 3
       maxRetriesOnStartup: 10
       requestTimeout: 35s
       queryLimit: 10000
  history:
    enableHistoryDatabase: true
bogon:fabric_deploy didi$
bogon:fabric_deploy didi$
bogon:fabric_deploy didi$ cat core.yaml
logging:
    peer:       warning
    cauthdsl:   warning
    gossip:     warning
    ledger:     info
    msp:        warning
    policies:   warning
    grpc:       error
    format: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
peer:
    id: peer0.org1.example.com
    networkId: dev
    listenAddress: 0.0.0.0:7051
    address: 0.0.0.0:7051
    addressAutoDetect: false
    gomaxprocs: -1
    gossip:
        bootstrap: 127.0.0.1:7051
        bootstrap: peer0.org1.example.com:7051
        useLeaderElection: true
        orgLeader: false
        endpoint:
        maxBlockCountToStore: 100
        maxPropagationBurstLatency: 10ms
        maxPropagationBurstSize: 10
        propagateIterations: 1
        propagatePeerNum: 3
        pullInterval: 4s
        pullPeerNum: 3
        requestStateInfoInterval: 4s
        publishStateInfoInterval: 4s
        stateInfoRetentionInterval:
        publishCertPeriod: 10s
        skipBlockVerification: false
        dialTimeout: 3s
        connTimeout: 2s
        recvBuffSize: 20
        sendBuffSize: 200
        digestWaitTime: 1s
        requestWaitTime: 1s
        responseWaitTime: 2s
        aliveTimeInterval: 5s
        aliveExpirationTimeout: 25s
        reconnectInterval: 25s
        externalEndpoint: peer0.org1.example.com:7051
        election:
            startupGracePeriod: 15s
            membershipSampleInterval: 1s
            leaderAliveThreshold: 10s
            leaderElectionDuration: 5s
    events:
        address: 0.0.0.0:7053
        buffersize: 100
        timeout: 10ms
    tls:
        enabled: true
        cert:
            file: ./tls/server.crt
        key:
            file: ./tls/server.key
        rootcert:
            file: ./tls/ca.crt
        serverhostoverride:
    fileSystemPath: ./data
    BCCSP:
        Default: SW
        SW:
            Hash: SHA2
            Security: 256
            FileKeyStore:
                KeyStore:
    mspConfigPath: msp
    localMspId: Org1MSP
    profile:
        enabled:    true
        listenAddress: 0.0.0.0:6060
vm:
    endpoint: unix:///var/run/docker.sock
    docker:
        tls:
            enabled: false