-
Notifications
You must be signed in to change notification settings - Fork 7
/
operate.sh
executable file
·301 lines (271 loc) · 10.4 KB
/
operate.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
#!/bin/bash
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
export PATH=$GOPATH/src/github.com/hyperledger/fabric/build/bin:${PWD}/../bin:${PWD}:$PATH
export FABRIC_CFG_PATH=${PWD}/artifacts
export VERBOSE=false
# Print the usage message
function printHelp() {
echo "Usage: "
echo " operate.sh <mode> [-y]"
echo " <mode> - One of 'up', 'down', 'restart', 'generate'"
echo " - 'up' - Bring up the network with docker-compose up"
echo " - 'down' - Clear the network with docker-compose down"
echo " - 'restart' - Restart the network"
echo " - 'generate' - Generate required certificates and genesis block"
echo " -y - Automatic yes to prompts"
echo " operate.sh -h (print this message)"
}
# Ask user for confirmation to proceed
function askProceed() {
read -p "Continue? [Y/n] " ans
case $ans in
y | Y | "")
echo "proceeding ..."
;;
n | N)
echo "exiting..."
exit 1
;;
*)
echo "invalid response"
askProceed
;;
esac
}
# Obtain CONTAINER_IDS and remove them
function clearContainers() {
AWK_PATTERN="dev-peer.*.$CC_NAME.*"
CONTAINER_IDS=$(docker ps -a | awk -v PAT="$AWK_PATTERN" '($2 ~ PAT) {print $1}')
if [ -z "$CONTAINER_IDS" -o "$CONTAINER_IDS" == " " ]; then
echo "---- No containers available for deletion ----"
else
docker rm -f $CONTAINER_IDS
fi
}
# Delete any images that were generated as a part of this setup
# function removeUnwantedImages() {
# AWK_PATTERN="dev-peer.*.$CC_NAME.*"
# DOCKER_IMAGE_IDS=$(docker images | awk -v PAT=$AWK_PATTERN '($1 ~ PAT) {print $3}')
# if [ -z "$DOCKER_IMAGE_IDS" -o "$DOCKER_IMAGE_IDS" == " " ]; then
# echo "---- No images available for deletion ----"
# else
# docker rmi -f $DOCKER_IMAGE_IDS
# fi
# }
# Do some basic sanity checking to make sure that the appropriate versions of fabric
# function checkPrereqs() {
# BLACKLISTED_VERSIONS="^1\.0\. ^1\.1\.0-preview ^1\.1\.0-alpha"
# LOCAL_VERSION=$(configtxlator version | sed -ne 's/ Version: //p')
# DOCKER_IMAGE_VERSION=$(docker run --rm hyperledger/fabric-tools:$IMAGETAG peer version | sed -ne 's/ Version: //p' | head -1)
# echo "LOCAL_VERSION=$LOCAL_VERSION"
# echo "DOCKER_IMAGE_VERSION=$DOCKER_IMAGE_VERSION"
# if [ $LOCAL_VERSION != $DOCKER_IMAGE_VERSION ]; then
# echo "=================== WARNING ==================="
# echo " Local fabric binaries and docker images are "
# echo " out of sync. This may cause problems. "
# echo "==============================================="
# fi
# for UNSUPPORTED_VERSION in $BLACKLISTED_VERSIONS; do
# echo $LOCAL_VERSION | grep -q $UNSUPPORTED_VERSION
# if [ $? -eq 0 ]; then
# echo "ERROR! Local Fabric binary version of $LOCAL_VERSION does not match this newer version of BYFN and is unsupported. Either move to a later version of Fabric or checkout an earlier version of fabric-samples."
# exit 1
# fi
# echo $DOCKER_IMAGE_VERSION | grep -q $UNSUPPORTED_VERSION
# if [ $? -eq 0 ]; then
# echo "ERROR! Fabric Docker image version of $DOCKER_IMAGE_VERSION does not match this newer version of BYFN and is unsupported. Either move to a later version of Fabric or checkout an earlier version of fabric-samples."
# exit 1
# fi
# done
# }
# Generate the needed certificates, the genesis block and start the network.
function networkUp() {
echo $PWD
if [ ! -x "scripts/script.sh" -o ! -x "scripts/utils.sh" ]; then
chmod +x scripts/*
fi
# checkPrereqs
if [ ! -d "./artifacts/network/crypto-config" ]; then
generateCerts
generateChannelArtifacts
fi
export MANUFACTURER_CA_PRIVATE_KEY=$(cd ./artifacts/network/crypto-config/peerOrganizations/manufacturer.example.com/ca && ls *_sk)
export MIDDLEMEN_CA_PRIVATE_KEY=$(cd ./artifacts/network/crypto-config/peerOrganizations/middlemen.example.com/ca && ls *_sk)
export CONSUMER_CA_PRIVATE_KEY=$(cd ./artifacts/network/crypto-config/peerOrganizations/consumer.example.com/ca && ls *_sk)
docker-compose -f $COMPOSE_FILE up -d 2>&1
if [ $? -ne 0 ]; then
echo "ERROR !!!! Unable to start network"
exit 1
fi
# now run the end to end script
docker exec cli scripts/script.sh
if [ $? -ne 0 ]; then
echo "ERROR !!!! Test failed"
exit 1
fi
}
# Tear down running network
# function networkDown() {
# export MANUFACTURER_CA_PRIVATE_KEY=$(cd ./artifacts/network/crypto-config/peerOrganizations/manufacturer.example.com/ca && ls *_sk)
# export MIDDLEMEN_CA_PRIVATE_KEY=$(cd ./artifacts/network/crypto-config/peerOrganizations/middlemen.example.com/ca && ls *_sk)
# export CONSUMER_CA_PRIVATE_KEY=$(cd ./artifacts/network/crypto-config/peerOrganizations/consumer.example.com/ca && ls *_sk)
# docker-compose -f $COMPOSE_FILE down --volumes --remove-orphans
# if [ $MODE != "restart" ]; then
# docker run -v $PWD:/tmp/jnu_hlfn --rm hyperledger/fabric-tools:$IMAGETAG rm -rf /tmp/jnu_hlfn/ledgers-backup
# clearContainers
# removeUnwantedImages
# rm -rf ./artifacts/network/*.block ./artifacts/network/*.tx ./artifacts/network/crypto-config/
# fi
# }
# Generates Org certs using cryptogen tool
function generateCerts() {
which cryptogen
if [ $? -ne 0 ]; then
echo "cryptogen tool not found. exiting"
exit 1
fi
echo
echo "######################################################################"
echo "########### Generate certificates using cryptogen tool ###############"
echo "######################################################################"
if [ -d "./artifacts/network/crypto-config" ]; then
rm -rf ./artifacts/network/crypto-config/
fi
set -x
cryptogen generate --config=./artifacts/crypto-config.yaml --output=./artifacts/network/crypto-config
res=$?
set +x
if [ $res -ne 0 ]; then
echo "Failed to generate certificates..."
exit 1
fi
echo
}
function generateChannelArtifacts() {
which configtxgen
if [ $? -ne 0 ]; then
echo "configtxgen tool not found. exiting"
exit 1
fi
echo "######################################################################"
echo "############### Generating Orderer Genesis block ####################"
echo "######################################################################"
echo "CONSENSUS_TYPE="$CONSENSUS_TYPE
set -x
if [ $CONSENSUS_TYPE == "solo" ]; then
configtxgen -profile LoyaltyTokenPlatformGenesis -outputBlock ./artifacts/network/genesis.block
else
set +x
echo "unrecognized CONSESUS_TYPE='$CONSENSUS_TYPE'. exiting"
exit 1
fi
res=$?
set +x
if [ $res -ne 0 ]; then
echo "Failed to generate orderer genesis block..."
exit 1
fi
echo
echo "######################################################################"
echo "##### Generating channel configuration transaction: channel.tx #####"
echo "######################################################################"
set -x
configtxgen -profile LoyaltyTokenOrgsChannel -outputCreateChannelTx ./artifacts/network/channel.tx -channelID $CHANNEL_NAME
res=$?
set +x
if [ $res -ne 0 ]; then
echo "Failed to generate channel configuration transaction..."
exit 1
fi
echo
echo "######################################################################"
echo "######## Generating anchor peer update for ManufacturerMSP ###########"
echo "######################################################################"
set -x
configtxgen -profile LoyaltyTokenOrgsChannel -outputAnchorPeersUpdate ./artifacts/network/ManufacturerMSPanchors.tx -channelID $CHANNEL_NAME -asOrg ManufacturerMSP
res=$?
set +x
if [ $res -ne 0 ]; then
echo "Failed to generate anchor peer update for ManagerMSP..."
exit 1
fi
echo
echo "######################################################################"
echo "######## Generating anchor peer update for MiddleMenMSP ###########"
echo "######################################################################"
set -x
configtxgen -profile LoyaltyTokenOrgsChannel -outputAnchorPeersUpdate ./artifacts/network/MiddleMenMSPanchors.tx -channelID $CHANNEL_NAME -asOrg MiddleMenMSP
res=$?
set +x
if [ $res -ne 0 ]; then
echo "Failed to generate anchor peer update for MiddleMenMSP..."
exit 1
fi
echo
echo
echo "######################################################################"
echo "######## Generating anchor peer update for ConsumerMSP ###########"
echo "######################################################################"
set -x
configtxgen -profile LoyaltyTokenOrgsChannel -outputAnchorPeersUpdate ./artifacts/network/ConsumerMSPanchors.tx -channelID $CHANNEL_NAME -asOrg ConsumerMSP
res=$?
set +x
if [ $res -ne 0 ]; then
echo "Failed to generate anchor peer update for ConsumerMSP..."
exit 1
fi
echo
}
# Obtain the OS and Architecture string that will be used to select the correct
OS_ARCH=$(echo "$(uname -s | tr '[:upper:]' '[:lower:]' | sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')" | awk '{print tolower($0)}')
SYS_CHANNEL="net_p&g_loyalty_hlfn"
CHANNEL_NAME="loyaltyplatformchannel"
CC_NAME="dummycc"
COMPOSE_FILE=./artifacts/docker-compose.yaml
IMAGETAG="1.4.6"
CONSENSUS_TYPE="solo"
PROCEED_ASK="true"
# Parse commandline args
MODE=$1
shift
if [ $MODE == "up" ]; then
EXPMODE="Starting"
elif [ $MODE == "down" ]; then
EXPMODE="Stopping"
elif [ $MODE == "restart" ]; then
EXPMODE="Restarting"
elif [ $MODE == "generate" ]; then
EXPMODE="Generating certs and genesis block"
else
printHelp
exit 1
fi
while getopts "h?y" opt; do
case "$opt" in
h | \?)
printHelp
exit 1
;;
y)
PROCEED_ASK="false"
;;
esac
done
if [ $PROCEED_ASK == "true" ]; then
echo "${EXPMODE} for channel '${CHANNEL_NAME}'"
askProceed
fi
if [ $MODE == "up" ]; then
networkUp
elif [ $MODE == "down" ]; then
networkDown
elif [ $MODE == "restart" ]; then
networkDown
networkUp
elif [ $MODE == "generate" ]; then
generateCerts
generateChannelArtifacts
fi