-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·262 lines (208 loc) · 5.35 KB
/
setup.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
#!/bin/bash
#### Configuration options #############################################
. ip.cfg
image=quorum
GETH='/usr/local/bin/geth'
BOOTNODE='/usr/local/bin/bootnode'
CONSTELLATION='/usr/local/bin/constellation-node'
########################################################################
nnodes=${#ips[@]}
if [[ $nnodes < 2 ]]
then
echo "ERROR: There must be more than one node IP address."
exit 1
fi
./cleanup.sh
uid=`id -u`
gid=`id -g`
pwd=`pwd`
#### Create directories for each node's configuration ##################
echo '[1] ~~~> Configuring for '$nnodes' nodes.'
n=1
for ip in ${ips[*]}
do
qd=qdata_$n
mkdir -p $qd/{logs,constellation/keys}
mkdir -p $qd/dd/geth
let n++
done
#### Make static-nodes.json and store keys #############################
echo
echo '[2] ~~~> Creating Enodes and static-nodes.json.'
echo "[" > static-nodes.json
n=1
for ip in ${ips[*]}
do
qd=qdata_$n
nodekey_file='/qdata/dd/geth/nodekey'
# Generate the node's Enode and key
enode=`docker run \
-u $uid:$gid \
-v $pwd/$qd:/qdata \
--rm \
$image \
/bin/bash -c "$BOOTNODE -genkey $nodekey_file; \
$BOOTNODE -nodekey $nodekey_file -writeaddress"`
# Add the enode to static-nodes.json
enode_url='enode://'$enode'@'$ip':30303?discport=0&raftport=23000'
sep=`[[ $n < $nnodes ]] && echo ","`
echo ' "'$enode_url'"'$sep >> static-nodes.json
let n++
done
echo "]" >> static-nodes.json
#### Create accounts, keys and genesis.json file #######################
echo
echo '[3] ~~~> Creating Ether accounts and genesis.json.'
cat > genesis.json <<EOF
{
"alloc": {
EOF
n=1
for ip in ${ips[*]}
do
qd=qdata_$n
# Generate an Ether account for the node
touch $qd/passwords.txt
account=`docker run \
-u $uid:$gid \
-v $pwd/$qd:/qdata \
--rm \
$image \
$GETH \
--datadir=/qdata/dd \
--password /qdata/passwords.txt \
account new | cut -c 11-50`
# Add the account to the genesis block so it has some Ether at start-up
sep=`[[ $n < $nnodes ]] && echo ","`
cat >> genesis.json <<EOF
"${account}": {
"balance": "1000000000000000000000000000"
}${sep}
EOF
let n++
done
cat >> genesis.json <<EOF
},
"coinbase": "0x0000000000000000000000000000000000000000",
"config": {
"homesteadBlock": 0
},
"difficulty": "0x0",
"extraData": "0x",
"gasLimit": "0x2FEFD800",
"mixhash": "0x00000000000000000000000000000000000000647572616c65787365646c6578",
"nonce": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": "0x00"
}
EOF
#### Make node list for tm.conf ########################################
nodelist=
n=1
for ip in ${ips[*]}
do
sep=`[[ $ip != ${ips[0]} ]] && echo ","`
nodelist=${nodelist}${sep}'"http://'${ip}':9000/"'
let n++
done
#### Complete each node's configuration ################################
echo
echo '[4] ~~~> Creating Quorum keys and finishing configuration.'
n=1
for ip in ${ips[*]}
do
qd=qdata_$n
cat templates/tm.conf \
| sed s/_NODEIP_/${ips[$((n-1))]}/g \
| sed s%_NODELIST_%$nodelist%g \
> $qd/tm.conf
cp genesis.json $qd/genesis.json
cp static-nodes.json $qd/dd/static-nodes.json
# Generate Quorum-related keys (used by Constellation)
docker run \
-u $uid:$gid \
-v $pwd/$qd:/qdata \
--rm \
$image \
$CONSTELLATION \
--workdir=/qdata/constellation/keys \
--generatekeys=tm < /dev/null > /dev/null
echo 'Node '$n' public key: '`cat $qd/constellation/keys/tm.pub`
cp templates/start.sh $qd/start.sh
chmod 755 $qd/start.sh
let n++
done
rm -rf genesis.json static-nodes.json
#### Create the docker-compose file ####################################
echo
echo '[5] ~~~> Creating docker-compose file.'
cat > docker-compose.yml <<EOF
version: '3'
services:
EOF
n=1
for ip in ${ips[*]}
do
qd=qdata_$n
cat >> docker-compose.yml <<EOF
node_$n:
image: $image
volumes:
- './$qd:/qdata'
networks:
quorum_net:
ipv4_address: '$ip'
ports:
- $((n+22000)):8545
user: '$uid:$gid'
EOF
let n++
done
cat >> docker-compose.yml <<EOF
networks:
quorum_net:
driver: bridge
ipam:
driver: default
config:
- subnet: $subnet
EOF
#### Create pre-populated contracts ####################################
echo
echo '[6] ~~~> Generating sample contract scripts.'
mkdir scripts
# Private contract - insert Node 2 as the recipient
cat templates/contract_pri.js \
| sed s:_NODEKEY_:`cat qdata_2/constellation/keys/tm.pub`:g \
> scripts/contract_pri.js
# Public contract - no change required
cp templates/contract_pub.js scripts/
cp templates/contract_act.js scripts/
### Setup init script ################################################
./geninit.sh
#### Create the k8s yaml file ####################################
echo
echo '[7] ~~~> Creating kubernetes definition yaml.'
n=1
port=31701
for ip in ${ips[*]}
do
qd=qdata_$n
pad=' '
cat $qd/init.sh \
| sed "s/^/$pad/" \
| sed '/^[[:space:]]*$/d' \
> init.sh
echo "$pad/qdata/start.sh" >> init.sh
cat templates/consortium.yaml \
| sed "s;_NODE_ID_;$n;g" \
| sed "s;_NODE_IP_;$ip;g" \
| sed "s;_NODE_PORT_;$port;g" \
| sed '/_NODE_SCRIPT_/r init.sh' \
| sed '/_NODE_SCRIPT_/d' \
>> consortium.yaml
rm init.sh
let n++
let port++
done
cp templates/explorer.yaml explorer.yaml