-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·443 lines (382 loc) · 13 KB
/
deploy.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/bin/bash
source ./.env
export PATH=${PWD}/bin:${PWD}:$PATH
# FUNCTION TO PRINT HELP
function printhelp(){
echo
echo "Usage:"
echo " deploy.sh [-a] [-h] [-n] [-i] [-c] [-p] [-e]"
echo " -a - DO INITIAL SETUP AND INSTALL CHAINCODE"
echo " -n - NO CHAINCODE INSTALLATION"
echo " -e - START EXPLORER"
echo " -c - CHAINCODE ONLY"
echo " -i - IGNORE ERRORS"
echo " -p - POPULATE DB"
echo " -h - HELP"
echo
}
# FUNCTION TO CHECK FOR ERRORS
function checkResult(){
res=$?
if [ $res -ne 0 ]
then
echo "!!!!! ERROR !!!!!"
if [ "${IGNORE_ERRORS}" != "true" ]; then
exit 1
fi
else
echo "----- SUCCESS -----"
echo
fi
}
if [ "${1}" == "" ]; then
printhelp
exit 1
fi
# DEFAULT OPTS
CHAIN_CODE_ONLY=true
NO_CHAINCODE=true
IGNORE_ERRORS=false
POPULATE_DB=false
START_EXPLORER=false
while getopts "h?ncaipe" opt; do
case "$opt" in
h | \?)
printhelp
;;
n)
CHAIN_CODE_ONLY=false
NO_CHAINCODE=true
;;
c)
CHAIN_CODE_ONLY=true
NO_CHAINCODE=false
;;
a)
CHAIN_CODE_ONLY=false
NO_CHAINCODE=false
;;
i)
IGNORE_ERRORS=true
;;
p)
POPULATE_DB=true
;;
e)
START_EXPLORER=true
;;
esac
done
# Setting The Paths..
CONFIG_ROOT=/root/crypto
#----------------------------------#
ORG1_MSPCONFIGPATH=${CONFIG_ROOT}/peerOrganizations/org1.eapproval.com/users/Admin@org1.eapproval.com/msp
ORG2_MSPCONFIGPATH=${CONFIG_ROOT}/peerOrganizations/org2.eapproval.com/users/Admin@org2.eapproval.com/msp
ORG1_TLS_ROOTCERT_FILE=${CONFIG_ROOT}/peerOrganizations/org1.eapproval.com/peers/peer0.org1.eapproval.com/tls/ca.crt
ORG2_TLS_ROOTCERT_FILE=${CONFIG_ROOT}/peerOrganizations/org2.eapproval.com/peers/peer0.org2.eapproval.com/tls/ca.crt
#----------------------------------#
DEFAULT_ORDERER=orderer.eapproval.com:7050
ORDERER_TLS_ROOTCERT_FILE=${CONFIG_ROOT}/ordererOrganizations/eapproval.com/orderers/orderer.eapproval.com/msp/tlscacerts/tlsca.eapproval.com-cert.pem
#----------------------------------#
PEER0_ORG1="docker exec
-e CORE_PEER_LOCALMSPID=Org1MSP
-e CORE_PEER_ADDRESS=peer0.org1.eapproval.com:7051
-e CORE_PEER_MSPCONFIGPATH=${ORG1_MSPCONFIGPATH}
-e CORE_PEER_TLS_ROOTCERT_FILE=${ORG1_TLS_ROOTCERT_FILE}
cli
peer
--tls=true
--cafile=${ORDERER_TLS_ROOTCERT_FILE}
--orderer=${DEFAULT_ORDERER}
"
PEER0_ORG2="docker exec
-e CORE_PEER_LOCALMSPID=Org2MSP
-e CORE_PEER_ADDRESS=peer0.org2.eapproval.com:9051
-e CORE_PEER_MSPCONFIGPATH=${ORG2_MSPCONFIGPATH}
-e CORE_PEER_TLS_ROOTCERT_FILE=${ORG2_TLS_ROOTCERT_FILE}
cli
peer
--tls=true
--cafile=${ORDERER_TLS_ROOTCERT_FILE}
--orderer=${DEFAULT_ORDERER}
"
if [ "${CHAIN_CODE_ONLY}" != "true" ]; then
echo
echo "################################"
echo "####### CREATING CHANNEL #######"
echo "################################"
${PEER0_ORG1} channel create -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx
checkResult
echo
echo
echo "################################"
echo "####### JOINING CHANNEL ########"
echo "################################"
${PEER0_ORG1} channel join -b $CHANNEL_NAME.block
checkResult
sleep 3
${PEER0_ORG2} channel join -b $CHANNEL_NAME.block
checkResult
sleep 3
echo
echo
echo "######################################"
echo "####### UPDATING ANCHOR PEERS ########"
echo "######################################"
${PEER0_ORG1} channel update -c $CHANNEL_NAME -f ./channel-artifacts/Org1MSPanchors.tx
checkResult
${PEER0_ORG2} channel update -c $CHANNEL_NAME -f ./channel-artifacts/Org2MSPanchors.tx
checkResult
echo
fi
# INSTALLING CHAINCODE
if [ "${NO_CHAINCODE}" != "true" ]; then
# echo "Vendoring Go dependencies ..."
# pushd ./configs/chaincode/fabcar/go
# GO111MODULE=on go mod vendor
# popd
# echo
echo
echo "##############################################################"
echo "####### PACKAGING CHAINCODE ON peer0.org1.eapproval.com ########"
echo "##############################################################"
${PEER0_ORG1} lifecycle chaincode package \
${CC_NAME}.tar.gz \
--path "${CC_SRC_PATH}" \
--lang "${CC_RUNTIME_LANGUAGE}" \
--label ${CC_NAME}v1
checkResult
echo
echo
echo "##############################################################"
echo "####### INSTALLING CHAINCODE ON peer0.org1.eapproval.com #######"
echo "##############################################################"
${PEER0_ORG1} lifecycle chaincode install \
${CC_NAME}.tar.gz
checkResult
echo
REGEX="Package ID: (.*), Label: ${CC_NAME}v1"
if [[ `${PEER0_ORG1} lifecycle chaincode queryinstalled` =~ $REGEX ]]; then
PACKAGE_ID_ORG1=${BASH_REMATCH[1]}
echo $PACKAGE_ID_ORG1
else
echo Could not find package ID for ${CC_NAME}v1 chaincode on peer0.org1.eapproval.com
exit 1
fi
echo
echo "########################################################"
echo "####### APPROVING CHAINCODE DEFINITION FOR ORG1 ########"
echo "########################################################"
${PEER0_ORG1} lifecycle chaincode approveformyorg \
--package-id ${PACKAGE_ID_ORG1} \
--channelID mychannel \
--name ${CC_NAME} \
--version 1.0 \
--signature-policy "AND('Org1MSP.member','Org2MSP.member')" \
--sequence 1 \
--waitForEvent
checkResult
echo
echo
echo "##############################################################"
echo "####### PACKAGING CHAINCODE ON peer0.org2.eapproval.com ########"
echo "##############################################################"
${PEER0_ORG2} lifecycle chaincode package \
${CC_NAME}.tar.gz \
--path "$CC_SRC_PATH" \
--lang "$CC_RUNTIME_LANGUAGE" \
--label ${CC_NAME}v1
checkResult
echo
echo
echo "##############################################################"
echo "####### INSTALLING CHAINCODE ON peer0.org2.eapproval.com #######"
echo "##############################################################"
${PEER0_ORG2} lifecycle chaincode install ${CC_NAME}.tar.gz
checkResult
echo
REGEX="Package ID: (.*), Label: ${CC_NAME}v1"
if [[ `${PEER0_ORG2} lifecycle chaincode queryinstalled` =~ $REGEX ]]; then
PACKAGE_ID_ORG2=${BASH_REMATCH[1]}
echo $PACKAGE_ID_ORG2
else
echo Could not find package ID for ${CC_NAME}v1 chaincode on peer0.org2.eapproval.com
exit 1
fi
echo
echo "########################################################"
echo "####### APPROVING CHAINCODE DEFINITION FOR ORG2 ########"
echo "########################################################"
${PEER0_ORG2} lifecycle chaincode approveformyorg \
--package-id ${PACKAGE_ID_ORG2} \
--channelID mychannel \
--name ${CC_NAME} \
--version 1.0 \
--signature-policy "AND('Org1MSP.member','Org2MSP.member')" \
--sequence 1 \
--waitForEvent
checkResult
echo
echo
echo "################################################"
echo "####### COMMITING CHAINCODE DEFINITION ########"
echo "################################################"
${PEER0_ORG1} lifecycle chaincode commit \
--channelID mychannel \
--name ${CC_NAME} \
--version 1.0 \
--signature-policy "AND('Org1MSP.member','Org2MSP.member')" \
--sequence 1 \
--waitForEvent \
--peerAddresses peer0.org1.eapproval.com:7051 \
--peerAddresses peer0.org2.eapproval.com:9051 \
--tlsRootCertFiles ${ORG1_TLS_ROOTCERT_FILE} \
--tlsRootCertFiles ${ORG2_TLS_ROOTCERT_FILE}
checkResult
echo
echo
echo "######################################################"
echo "####### INVOKING INIT IN CHAINCODE DEFINITION ########"
echo "######################################################"
${PEER0_ORG1} chaincode invoke \
-C mychannel \
-n ${CC_NAME} \
-c '{"function":"initLedger","Args":[]}' \
--waitForEvent \
--waitForEventTimeout 300s \
--peerAddresses peer0.org1.eapproval.com:7051 \
--peerAddresses peer0.org2.eapproval.com:9051 \
--tlsRootCertFiles ${ORG1_TLS_ROOTCERT_FILE} \
--tlsRootCertFiles ${ORG2_TLS_ROOTCERT_FILE}
checkResult
echo
echo
echo "###################################"
echo "####### QUERYING CHAINCODE ########"
echo "###################################"
${PEER0_ORG1} chaincode query \
-C mychannel \
-n ${CC_NAME} \
-c '{"function":"getTotalRequests","Args":[]}' \
--peerAddresses peer0.org1.eapproval.com:7051 \
--tlsRootCertFiles ${ORG1_TLS_ROOTCERT_FILE}
checkResult
echo
fi
if [ "${POPULATE_DB}" == "true" ]; then
local_sampleUserProposalFile='"{\"name\":\"report.txt\",\"data\":\"data:text/plain;base64,c2FtcGxlCg==\"}"'
local_orgs='"[\"ORG1\",\"ORG2\"]"'
local_sampleGoodRemarks='"{\"text\":\"GOOD\",\"file\":{\"data\":\"data:text/plain;base64,c2FtcGxlCg==\",\"name\":\"report.txt\"}}"'
local_sampleBadRemarks='"{\"text\":\"BAD\",\"file\":{\"data\":\"data:text/plain;base64,c2FtcGxlCg==\",\"name\":\"report.txt\"}}"'
echo
echo "#############################################################"
echo "####### INVOKING POPULATE_DB IN CHAINCODE DEFINITION ########"
echo "#############################################################"
${PEER0_ORG1} chaincode invoke \
-C mychannel \
-n ${CC_NAME} \
-c '{"function":"createRequest","Args":["Alpha", "Cell Phone Tower", "Tower Outside of Town.", '$local_sampleUserProposalFile','$local_orgs']}' \
--waitForEvent \
--waitForEventTimeout 300s \
--peerAddresses peer0.org1.eapproval.com:7051 \
--peerAddresses peer0.org2.eapproval.com:9051 \
--tlsRootCertFiles ${ORG1_TLS_ROOTCERT_FILE} \
--tlsRootCertFiles ${ORG2_TLS_ROOTCERT_FILE}
checkResult
echo
${PEER0_ORG1} chaincode invoke \
-C mychannel \
-n ${CC_NAME} \
-c '{"function":"createRequest","Args":["Beta", "Chemical Factory", "Chemical Factory Inside Town.", '$local_sampleUserProposalFile', '$local_orgs']}' \
--waitForEvent \
--waitForEventTimeout 300s \
--peerAddresses peer0.org1.eapproval.com:7051 \
--peerAddresses peer0.org2.eapproval.com:9051 \
--tlsRootCertFiles ${ORG1_TLS_ROOTCERT_FILE} \
--tlsRootCertFiles ${ORG2_TLS_ROOTCERT_FILE}
checkResult
echo
${PEER0_ORG1} chaincode invoke \
-C mychannel \
-n ${CC_NAME} \
-c '{"function":"createRequest","Args":["Gamma", "Ice-cream Factory", "Ice-cream Factory inside Town.", '$local_sampleUserProposalFile', '$local_orgs']}' \
--waitForEvent \
--waitForEventTimeout 300s \
--peerAddresses peer0.org1.eapproval.com:7051 \
--peerAddresses peer0.org2.eapproval.com:9051 \
--tlsRootCertFiles ${ORG1_TLS_ROOTCERT_FILE} \
--tlsRootCertFiles ${ORG2_TLS_ROOTCERT_FILE}
checkResult
echo
${PEER0_ORG1} chaincode invoke \
-C mychannel \
-n ${CC_NAME} \
-c '{"function":"createRequest","Args":["Delta", "Coal mine", "Coal mine inside Town.", '$local_sampleUserProposalFile', '$local_orgs']}' \
--waitForEvent \
--waitForEventTimeout 300s \
--peerAddresses peer0.org1.eapproval.com:7051 \
--peerAddresses peer0.org2.eapproval.com:9051 \
--tlsRootCertFiles ${ORG1_TLS_ROOTCERT_FILE} \
--tlsRootCertFiles ${ORG2_TLS_ROOTCERT_FILE}
checkResult
echo
${PEER0_ORG1} chaincode invoke \
-C mychannel \
-n ${CC_NAME} \
-c '{"function":"approveRequest","Args":["PENDING|Request|1", "ORG1", '$local_sampleGoodRemarks', '$local_sampleGoodRemarks']}' \
--waitForEvent \
--waitForEventTimeout 300s \
--peerAddresses peer0.org1.eapproval.com:7051 \
--peerAddresses peer0.org2.eapproval.com:9051 \
--tlsRootCertFiles ${ORG1_TLS_ROOTCERT_FILE} \
--tlsRootCertFiles ${ORG2_TLS_ROOTCERT_FILE}
checkResult
echo
${PEER0_ORG1} chaincode invoke \
-C mychannel \
-n ${CC_NAME} \
-c '{"function":"approveRequest","Args":["PENDING|Request|1", "ORG2", '$local_sampleGoodRemarks', '$local_sampleGoodRemarks']}' \
--waitForEvent \
--waitForEventTimeout 300s \
--peerAddresses peer0.org1.eapproval.com:7051 \
--peerAddresses peer0.org2.eapproval.com:9051 \
--tlsRootCertFiles ${ORG1_TLS_ROOTCERT_FILE} \
--tlsRootCertFiles ${ORG2_TLS_ROOTCERT_FILE}
checkResult
echo
${PEER0_ORG1} chaincode invoke \
-C mychannel \
-n ${CC_NAME} \
-c '{"function":"declineRequest","Args":["PENDING|Request|2", "ORG1", '$local_sampleBadRemarks', '$local_sampleBadRemarks']}' \
--waitForEvent \
--waitForEventTimeout 300s \
--peerAddresses peer0.org1.eapproval.com:7051 \
--peerAddresses peer0.org2.eapproval.com:9051 \
--tlsRootCertFiles ${ORG1_TLS_ROOTCERT_FILE} \
--tlsRootCertFiles ${ORG2_TLS_ROOTCERT_FILE}
checkResult
echo
echo
echo "###################################"
echo "####### QUERYING CHAINCODE ########"
echo "###################################"
${PEER0_ORG1} chaincode query \
-C mychannel \
-n ${CC_NAME} \
-c '{"function":"getTotalRequests","Args":[]}' \
--peerAddresses peer0.org1.eapproval.com:7051 \
--tlsRootCertFiles ${ORG1_TLS_ROOTCERT_FILE}
checkResult
echo
fi
if [ "${START_EXPLORER}" != "false" ]; then
echo
echo "#######################################"
echo "####### Starting The EXPLORER #########"
echo "#######################################"
docker-compose \
-f ./configs/docker-compose-explorer.yaml \
up -d
checkResult
echo
fi