matiru
Blockchain-Based Supply Chain Transparency for Agricultural Producegit clone https://github.com/maydayv7/matiru
fabric/network/network.sh (8079B)
1#!/usr/bin/env bash
2
3export PATH=${PWD}/../bin:$PATH
4export FABRIC_CFG_PATH=${PWD}/configtx
5export VERBOSE=false
6
7. scripts/utils.sh
8
9function clearContainers() {
10 infoln "Removing remaining containers"
11 docker rm -f $(docker ps -aq --filter label=service=hyperledger-fabric) 2>/dev/null || true
12 docker rm -f $(docker ps -aq --filter name='dev-peer*') 2>/dev/null || true
13}
14
15function removeUnwantedImages() {
16 infoln "Removing generated chaincode docker images"
17 docker image rm -f $(docker images -aq --filter reference='dev-peer*') 2>/dev/null || true
18}
19
20function checkPrereqs() {
21 peer version > /dev/null 2>&1
22 if [[ $? -ne 0 || ! -d "../config" ]]; then
23 errorln "Peer binary and configuration files not found.."
24 exit 1
25 fi
26}
27
28function createOrgs() {
29 if [ -d "organizations/peerOrganizations" ]; then
30 rm -Rf organizations/peerOrganizations && rm -Rf organizations/ordererOrganizations
31 fi
32
33 if [ "$CRYPTO" == "cryptogen" ]; then
34 which cryptogen
35 if [ "$?" -ne 0 ]; then
36 fatalln "cryptogen tool not found. exiting"
37 fi
38 infoln "Generating certificates using cryptogen tool"
39
40 infoln "Creating Peer Org Identities"
41 set -x
42 cryptogen generate --config=./organizations/cryptogen/crypto-config-orgs.yaml --output="organizations"
43 res=$?
44 { set +x; } 2>/dev/null
45 if [ $res -ne 0 ]; then
46 fatalln "Failed to generate certificates..."
47 fi
48
49 infoln "Creating Orderer Org Identities"
50 set -x
51 cryptogen generate --config=./organizations/cryptogen/crypto-config-orderer.yaml --output="organizations"
52 res=$?
53 { set +x; } 2>/dev/null
54 if [ $res -ne 0 ]; then
55 fatalln "Failed to generate certificates..."
56 fi
57
58 fi
59
60 if [ "$CRYPTO" == "Certificate Authorities" ]; then
61 infoln "Generating certificates using Fabric CA"
62 docker-compose -f $COMPOSE_FILE_CA up -d 2>&1
63
64 infoln "Waiting for Fabric CAs to start"
65 while :
66 do
67 if [ ! -f "organizations/fabric-ca/ordererOrg/tls-cert.pem" ]; then
68 sleep 1
69 else
70 break
71 fi
72 done
73
74 . organizations/fabric-ca/registerEnroll.sh
75
76 infoln "Creating Org1 Identities"
77 createOrg1
78 infoln "Creating Org2 Identities"
79 createOrg2
80 infoln "Creating Org3 Identities"
81 createOrg3
82 infoln "Creating Org4 Identities"
83 createOrg4
84 infoln "Creating Orderer Org Identities"
85 createOrderer
86 fi
87
88 infoln "Generating CCP files for all Orgs"
89 ./organizations/ccp-generate.sh
90}
91
92function networkUp() {
93 checkPrereqs
94 if [ ! -d "organizations/peerOrganizations" ]; then
95 createOrgs
96 fi
97
98 COMPOSE_FILES="-f ${COMPOSE_FILE_BASE}"
99 if [ "${DATABASE}" == "couchdb" ]; then
100 COMPOSE_FILES="${COMPOSE_FILES} -f ${COMPOSE_FILE_COUCH}"
101 fi
102
103 docker-compose ${COMPOSE_FILES} up -d 2>&1
104
105 docker ps -a
106 if [ $? -ne 0 ]; then
107 fatalln "Unable to start network"
108 fi
109}
110
111function createChannel() {
112 if [ ! -d "organizations/peerOrganizations" ]; then
113 infoln "Bringing up network"
114 networkUp
115 fi
116 scripts/createChannel.sh $CHANNEL_NAME $CLI_DELAY $MAX_RETRY $VERBOSE
117}
118
119function deployCC() {
120 scripts/deployCC.sh $CHANNEL_NAME $CC_NAME $CC_SRC_PATH $CC_SRC_LANGUAGE $CC_VERSION $CC_SEQUENCE $CC_INIT_FCN $CC_END_POLICY $CC_COLL_CONFIG $CLI_DELAY $MAX_RETRY $VERBOSE
121 if [ $? -ne 0 ]; then
122 fatalln "Deploying chaincode failed"
123 fi
124}
125
126function networkDown() {
127 docker-compose -f $COMPOSE_FILE_BASE -f $COMPOSE_FILE_COUCH -f $COMPOSE_FILE_CA down --volumes --remove-orphans
128 if [ "$MODE" != "restart" ]; then
129 clearContainers
130 removeUnwantedImages
131 docker run --rm -v "$(pwd):/data" busybox sh -c 'cd /data && rm -rf system-genesis-block/*.block organizations/peerOrganizations organizations/ordererOrganizations'
132 docker run --rm -v "$(pwd):/data" busybox sh -c 'cd /data && rm -rf organizations/fabric-ca/org1/msp organizations/fabric-ca/org1/tls-cert.pem organizations/fabric-ca/org1/ca-cert.pem organizations/fabric-ca/org1/IssuerPublicKey organizations/fabric-ca/org1/IssuerRevocationPublicKey organizations/fabric-ca/org1/fabric-ca-server.db'
133 docker run --rm -v "$(pwd):/data" busybox sh -c 'cd /data && rm -rf organizations/fabric-ca/org2/msp organizations/fabric-ca/org2/tls-cert.pem organizations/fabric-ca/org2/ca-cert.pem organizations/fabric-ca/org2/IssuerPublicKey organizations/fabric-ca/org2/IssuerRevocationPublicKey organizations/fabric-ca/org2/fabric-ca-server.db'
134 docker run --rm -v "$(pwd):/data" busybox sh -c 'cd /data && rm -rf organizations/fabric-ca/org3/msp organizations/fabric-ca/org3/tls-cert.pem organizations/fabric-ca/org3/ca-cert.pem organizations/fabric-ca/org3/IssuerPublicKey organizations/fabric-ca/org3/IssuerRevocationPublicKey organizations/fabric-ca/org3/fabric-ca-server.db'
135 docker run --rm -v "$(pwd):/data" busybox sh -c 'cd /data && rm -rf organizations/fabric-ca/org4/msp organizations/fabric-ca/org4/tls-cert.pem organizations/fabric-ca/org4/ca-cert.pem organizations/fabric-ca/org4/IssuerPublicKey organizations/fabric-ca/org4/IssuerRevocationPublicKey organizations/fabric-ca/org4/fabric-ca-server.db'
136 docker run --rm -v "$(pwd):/data" busybox sh -c 'cd /data && rm -rf organizations/fabric-ca/ordererOrg/msp organizations/fabric-ca/ordererOrg/tls-cert.pem organizations/fabric-ca/ordererOrg/ca-cert.pem organizations/fabric-ca/ordererOrg/IssuerPublicKey organizations/fabric-ca/ordererOrg/IssuerRevocationPublicKey organizations/fabric-ca/ordererOrg/fabric-ca-server.db'
137 docker run --rm -v "$(pwd):/data" busybox sh -c 'cd /data && rm -rf channel-artifacts log.txt *.tar.gz'
138 fi
139}
140
141CRYPTO="cryptogen"
142MAX_RETRY=8
143CLI_DELAY=3
144CHANNEL_NAME="mychannel"
145CC_NAME="NA"
146CC_SRC_PATH="NA"
147CC_END_POLICY="NA"
148CC_COLL_CONFIG="NA"
149CC_INIT_FCN="NA"
150COMPOSE_FILE_BASE=docker/docker-compose-network.yaml
151COMPOSE_FILE_COUCH=docker/docker-compose-couch.yaml
152COMPOSE_FILE_CA=docker/docker-compose-ca.yaml
153CC_SRC_LANGUAGE="NA"
154CC_VERSION="1.0"
155CC_SEQUENCE=1
156DATABASE="leveldb"
157
158if [[ $# -lt 1 ]] ; then
159 printHelp
160 exit 0
161else
162 MODE=$1
163 shift
164fi
165
166if [[ $# -ge 1 ]] ; then
167 key="$1"
168 if [[ "$key" == "createChannel" ]]; then
169 export MODE="createChannel"
170 shift
171 fi
172fi
173
174while [[ $# -ge 1 ]] ; do
175 key="$1"
176 case $key in
177 -h )
178 printHelp $MODE
179 exit 0
180 ;;
181 -c )
182 CHANNEL_NAME="$2"
183 shift
184 ;;
185 -ca )
186 CRYPTO="Certificate Authorities"
187 ;;
188 -r )
189 MAX_RETRY="$2"
190 shift
191 ;;
192 -d )
193 CLI_DELAY="$2"
194 shift
195 ;;
196 -s )
197 DATABASE="$2"
198 shift
199 ;;
200 -ccl )
201 CC_SRC_LANGUAGE="$2"
202 shift
203 ;;
204 -ccn )
205 CC_NAME="$2"
206 shift
207 ;;
208 -ccv )
209 CC_VERSION="$2"
210 shift
211 ;;
212 -ccs )
213 CC_SEQUENCE="$2"
214 shift
215 ;;
216 -ccp )
217 CC_SRC_PATH="$2"
218 shift
219 ;;
220 -ccep )
221 CC_END_POLICY="$2"
222 shift
223 ;;
224 -cccg )
225 CC_COLL_CONFIG="$2"
226 shift
227 ;;
228 -cci )
229 CC_INIT_FCN="$2"
230 shift
231 ;;
232 -verbose )
233 VERBOSE=true
234 shift
235 ;;
236 * )
237 errorln "Unknown flag: $key"
238 printHelp
239 exit 1
240 ;;
241 esac
242 shift
243done
244
245if [ ! -d "organizations/peerOrganizations" ]; then
246 CRYPTO_MODE="with crypto from '${CRYPTO}'"
247else
248 CRYPTO_MODE=""
249fi
250
251if [ "$MODE" == "up" ]; then
252 infoln "Starting nodes with CLI timeout of '${MAX_RETRY}' tries and CLI delay of '${CLI_DELAY}' seconds and using database '${DATABASE}' ${CRYPTO_MODE}"
253elif [ "$MODE" == "createChannel" ]; then
254 infoln "Creating channel '${CHANNEL_NAME}'."
255 infoln "If network is not up, starting nodes with CLI timeout of '${MAX_RETRY}' tries and CLI delay of '${CLI_DELAY}' seconds and using database '${DATABASE}' ${CRYPTO_MODE}"
256elif [ "$MODE" == "down" ]; then
257 infoln "Stopping network"
258elif [ "$MODE" == "restart" ]; then
259 infoln "Restarting network"
260elif [ "$MODE" == "deployCC" ]; then
261 infoln "deploying chaincode on channel '${CHANNEL_NAME}'"
262else
263 printHelp
264 exit 1
265fi
266
267if [ "${MODE}" == "up" ]; then
268 networkUp
269elif [ "${MODE}" == "createChannel" ]; then
270 createChannel
271elif [ "${MODE}" == "deployCC" ]; then
272 deployCC
273elif [ "${MODE}" == "down" ]; then
274 networkDown
275else
276 printHelp
277 exit 1
278fi