-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy_explorer_fabric.sh
executable file
·223 lines (195 loc) · 7 KB
/
deploy_explorer_fabric.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
#!/bin/bash
# Copyright Tecnalia Research & Innovation (https://www.tecnalia.com)
# Copyright Tecnalia Blockchain LAB
#
# SPDX-License-Identifier: Apache-2.0
#BASH CONFIGURATION
# Enable colored log
export TERM=xterm-256color
function banner(){
echo ""
echo " "
echo " _|_|_|_|_| _| "
echo " _| _|_| _| _|_| _|_|_| _|_|_| _|_| _|_|_| _|_| "
echo " _| _|_|_|_| _| _|_|_|_| _|_| _| _| _| _| _| _|_|_|_| "
echo " _| _| _| _| _|_| _| _| _| _| _| _| "
echo " _| _|_|_| _| _|_|_| _|_|_| _|_|_| _|_| _|_|_| _|_|_| "
echo " _| "
echo " _| "
echo ""
}
# HELPER FUNCTIONS
# Check whether a given container (filtered by name) exists or not
function existsContainer(){
containerName=$1
if [ -n "$(docker ps -aq -f name=$containerName)" ]; then
return 0 #true
else
return 1 #false
fi
}
# HELPER FUNCTIONS
# Check whether a given network (filtered by name) exists or not
function existsNetwork(){
networkName=$1
if [ -n "$(docker network ls -q -f name=$networkName)" ]; then
return 0 #true
else
return 1 #false
fi
}
# Check whether a given network (filtered by name) exists or not
function existsImage(){
imageName=$1
if [ -n "$(docker images -a -q $imageName)" ]; then
return 0 #true
else
return 1 #false
fi
}
# Configure settings of BLOCKCHAIN EXPLORER
function config(){
# BEGIN: GLOBAL VARIABLES OF THE SCRIPT
defaultFabricName="net1"
if [ -z "$1" ]; then
echo "No custom Hyperledger Network configuration supplied. Using default network name: $defaultFabricName"
fabricBlockchainNetworkName=$defaultFabricName
else
fabricBlockchainNetworkName=$1
echo "Using custom Hyperledger Network configuration. Network name: $fabricBlockchainNetworkName"
fi
docker_network_name="fabric-explorer-net"
# Default Telescope Database Credentials.
explorer_db_user="hppoc"
explorer_db_pwd="password"
#configure explorer to connect to specific Blockchain network using given configuration
network_config_file=$(pwd)/examples/$fabricBlockchainNetworkName/config.json
#configure explorer to connect to specific Blockchain network using given crypto materials
network_crypto_base_path=$(pwd)/examples/$fabricBlockchainNetworkName/crypto
# local vnet configuration
# Docker network configuration
# Address: 192.168.10.0 11000000.10101000.00001010. 00000000
# Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
# Wildcard: 0.0.0.255 00000000.00000000.00000000. 11111111
# =>
# Network: 192.168.10.0/24 11000000.10101000.00001010. 00000000
# HostMin: 192.168.10.1 11000000.10101000.00001010. 00000001
# HostMax: 192.168.10.254 11000000.10101000.00001010. 11111110
# Broadcast: 192.168.10.255 11000000.10101000.00001010. 11111111
# Hosts/Net: 254 Class C, Private Internet
subnet=192.168.10.0/24
# database container configuration
fabric_explorer_db_tag="hyperledger-blockchain-explorer-db"
fabric_explorer_db_name="blockchain-explorer-db"
db_ip=192.168.10.11
# fabric explorer configuratio
fabric_explorer_tag="hyperledger-blockchain-explorer"
fabric_explorer_name="blockchain-explorer"
explorer_ip=192.168.10.12
# END: GLOBAL VARIABLES OF THE SCRIPT
}
function deploy_prepare_network(){
if existsNetwork $docker_network_name; then
echo "Removing old configured docker vnet for Telescope"
# to avoid active endpoints
stop_database
stop_explorer
docker network rm $docker_network_name
fi
echo "Creating default Docker vnet for Hyperledger Fabric Explorer"
docker network create --subnet=$subnet $docker_network_name
}
function deploy_build_database(){
echo "Building Hyperledger Fabric Database image from current local version..."
docker build -f postgres-Dockerfile --tag $fabric_explorer_db_tag .
}
function stop_database(){
if existsContainer $fabric_explorer_db_name; then
echo "Stopping previously deployed Hyperledger Fabric Explorer DATABASE instance..."
docker stop $fabric_explorer_db_name && \
docker rm $fabric_explorer_db_name
fi
}
function deploy_run_database(){
stop_database
# deploy database with given user/password configuration
# By default, since docker is used, there are no users created so default available user is
# postgres/password
echo "Deploying Database (POSTGRES) container at $db_ip"
docker run \
-d \
--name $fabric_explorer_db_name \
--net $docker_network_name --ip $db_ip \
-e POSTGRES_PASSWORD=$explorer_db_pwd \
-e PGPASSWORD=$explorer_db_pwd \
$fabric_explorer_db_tag
}
function deploy_load_database(){
echo "Preparing database for Explorer"
echo "Waiting...6s"
sleep 1s
echo "Waiting...5s"
sleep 1s
echo "Waiting...4s"
sleep 1s
echo "Waiting...3s"
sleep 1s
echo "Waiting...2s"
sleep 1s
echo "Waiting...1s"
sleep 1s
echo "Creating Default user..."
docker exec $fabric_explorer_db_name psql -h localhost -U postgres -c "CREATE USER $explorer_db_user WITH PASSWORD '$explorer_db_pwd'"
echo "Creating default database schemas..."
docker exec $fabric_explorer_db_name psql -h localhost -U postgres -a -f /opt/explorerpg.sql
docker exec $fabric_explorer_db_name psql -h localhost -U postgres -a -f /opt/updatepg.sql
}
function deploy_build_explorer(){
echo "Building Hyperledger Fabric explorer image from current local version..."
docker build --tag $fabric_explorer_tag .
echo "Hyperledger Fabric network configuration file is located at $network_config_file"
echo "Hyperledger Fabric network crypto material at $network_crypto_base_path"
}
function stop_explorer(){
if existsContainer $fabric_explorer_name; then
echo "Stopping previously deployed Hyperledger Fabric Explorer instance..."
docker stop $fabric_explorer_name && \
docker rm $fabric_explorer_name
fi
}
function deploy_run_explorer(){
stop_explorer
echo "Deploying Hyperledger Fabric Explorer container at $explorer_ip"
docker run \
-d \
--name $fabric_explorer_name \
--net $docker_network_name --ip $explorer_ip \
-e DATABASE_HOST=$db_ip \
-e DATABASE_USERNAME=$explorer_db_user \
-e DATABASE_PASSWD=$explorer_db_pwd \
-v $network_config_file:/opt/explorer/app/platform/fabric/config.json \
-v $network_crypto_base_path:/tmp/crypto \
-p 8080:8080 \
hyperledger-blockchain-explorer
}
function deploy(){
deploy_prepare_network
echo "Starting explorer in local mode..."
if !(existsImage $fabric_explorer_db_tag); then
deploy_build_database
fi
deploy_run_database
deploy_load_database
if !(existsImage $fabric_explorer_tag); then
deploy_build_explorer
fi
deploy_run_explorer
}
function main(){
banner
#Pass arguments to function exactly as-is
config "$@"
deploy
}
#Pass arguments to function exactly as-is
main "$@"