-
Notifications
You must be signed in to change notification settings - Fork 0
/
runApp.sh
executable file
·183 lines (167 loc) · 5.18 KB
/
runApp.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
#!/bin/bash -e
##TODO: generate artifacts etc., tedious ?
##TODO: add debug flag to enable/disable for peer ad orderer
## usage message
function usage () {
echo "Usage: "
echo " runApp.sh [-m start|stop|restart] [-t <release-tag>] [-c enable-CouchDB] [-l capture-logs]"
echo " runApp.sh -h|--help (print this message)"
echo " -m <mode> - one of 'start', 'stop', 'restart' " #or 'generate'"
echo " - 'start' - bring up the network with docker-compose up & start the app on port 4000"
echo " - 'up' - same as start"
echo " - 'stop' - stop the network with docker-compose down & clear containers , crypto keys etc.,"
echo " - 'down' - same as stop"
echo " - 'restart' - restarts the network and start the app on port 4000 (Typically stop + start)"
echo " -c enable CouchDB"
echo " -r re-Generate the certs and channel artifacts"
echo " -l capture docker logs before network teardown"
echo " -t <release-tag> - ex: alpha | beta | rc , missing this option will result in using the latest docker images"
echo
echo "Some possible options:"
echo
echo " runApp.sh"
echo " runApp.sh -l"
echo " runApp.sh -r"
echo " runApp.sh -m restart -t beta"
echo " runApp.sh -m start -c"
echo " runApp.sh -m stop"
echo " runApp.sh -m start -t rc1"
echo " runApp.sh -m stop -l"
echo
echo "All defaults:"
echo " runApp.sh"
echo " RESTART the network/app, use latest docker images but TAG, Disable couchdb "
exit 1
}
# Parse commandline args
while getopts "h?m:t:clr" opt; do
case "$opt" in
h|\?)
usage
exit 1
;;
m) MODE=$OPTARG
;;
c) COUCHDB='y'
;;
l) ENABLE_LOGS='y'
;;
r) REGENERATE='y'
;;
t) TAG="$OPTARG"
##TODO: ensure package.json contains right node packages
if [ "$TAG" == "beta" -o "$TAG" == "rc1" ]; then
IMAGE_TAG="`uname -m`-1.0.0-$OPTARG"
elif [ "$TAG" == "1.0.0" ]; then
IMAGE_TAG="`uname -m`-1.0.0"
else
usage
fi
;;
esac
done
: ${MODE:="restart"}
: ${IMAGE_TAG:="latest"}
: ${COUCHDB:="n"}
: ${ENABLE_LOGS:="n"}
export IMAGE_TAG
COMPOSE_FILE=./artifacts/docker-compose.yaml
COMPOSE_FILE_WITH_COUCH=./artifacts/docker-compose-couch.yaml
function dkcl(){
CONTAINERS=$(docker ps -a|wc -l)
if [ "$CONTAINERS" -gt "1" ]; then
docker rm -f $(docker ps -aq)
else
printf "\n========== No containers available for deletion ==========\n"
fi
}
function dkrm(){
DOCKER_IMAGE_IDS=$(docker images | grep "dev\|none\|test-vp\|peer[0-9]-" | awk '{print $3}')
echo
if [ -z "$DOCKER_IMAGE_IDS" -o "$DOCKER_IMAGE_IDS" = " " ]; then
echo "========== No images available for deletion ==========="
else
docker rmi -f $DOCKER_IMAGE_IDS
fi
echo
}
function cleanAndInstall() {
## Make sure cleanup the node_moudles and re-install them again
rm -rf ./node_modules
printf "\n============== Installing node modules =============\n"
npm install
}
function installNodeModules() {
echo
if [ -d node_modules ]; then
npm ls fabric-client fabric-ca-client || cleanAndInstall
else
cleanAndInstall && npm ls fabric-client fabric-ca-client
fi
echo
}
function checkForDockerImages() {
DOCKER_IMAGES=$(docker images | grep "$IMAGE_TAG" | wc -l)
if [ $DOCKER_IMAGES -ne 9 ]; then
printf "\n############# You don't have all fabric images, Let me them pull for you ###########\n"
for IMAGE in peer orderer ca couchdb ccenv javaenv kafka tools zookeeper; do
docker pull hyperledger/fabric-$IMAGE:$IMAGE_TAG
done
fi
}
function startApp() {
if [ "$IMAGE_TAG" = "latest" ]; then
printf "\n ========= Using latest Docker images ===========\n"
else
printf "\n ========= FABRIC IMAGE TAG : $IMAGE_TAG ===========\n"
checkForDockerImages
fi
### Let's not worry about dynamic generation of Org certs and channel artifacts
if [ "$REGENERATE" = "y" ]; then
rm -rf ./artifacts/channel/*.block channel/*.tx ./artifacts/crypto-config
source artifacts/generateArtifacts.sh
fi
#Launch the network
if [ "$COUCHDB" = "y" -o "$COUCHDB" = "Y" ]; then
docker-compose -f $COMPOSE_FILE -f $COMPOSE_FILE_WITH_COUCH up -d
else
docker-compose -f $COMPOSE_FILE up -d
fi
if [ $? -ne 0 ]; then
printf "\n\n!!!!!!!! Unable to pull the start the network, Check your docker-compose !!!!!\n\n"
exit
fi
##Install node modules
installNodeModules
##Start app on port 4000
PORT=4000 node app
}
function shutdownApp() {
printf "\n======================= TEARDOWN NETWORK ====================\n"
if [ "$ENABLE_LOGS" = "y" -o "$ENABLE_LOGS" = "Y" ]; then
source ./artifacts/getContainerLogs.sh
fi
# teardown the network and clean the containers and intermediate images
docker-compose -f $COMPOSE_FILE -f $COMPOSE_FILE_WITH_COUCH down
dkcl
dkrm
# cleanup the material
printf "\n======================= CLEANINGUP ARTIFACTS ====================\n\n"
rm -rf /tmp/hfc-test-kvs_peerOrg* $HOME/.hfc-key-store/ /tmp/fabric-client-kvs_peerOrg*
}
#Launch the network using docker compose
case $MODE in
'start'|'up')
startApp
;;
'stop'|'down')
shutdownApp
;;
'restart')
shutdownApp
startApp
;;
*)
usage
;;
esac