-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup
271 lines (218 loc) · 6.03 KB
/
setup
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
#!/bin/bash
DOCKER_IMAGE_NAME="inverse_transparency_image"
DOCKER_CONTAINER_LIMIT=99
REVOLORI_DEFAULT_ADDR="http://192.168.3.22:5000"
function ask_for_continue {
if [ $# -ne 1 ]; then
beg="Something"
else
beg=$1
fi
echo
echo "$beg may have failed, check output"
read -p "Do you still want to continue (y/n)? " cont
if [ "$cont" != "y" ] && [ "$cont" != "Y" ]; then
echo "Exiting"
exit 1
fi
}
function append_node_to_compose {
if [ $# -ne 1 ]; then
echo "append_node_to_compose was not given an ID"
echo "Exiting"
exit 1
fi
if [ $1 -lt 10 ]; then
port="0$1"
else
port="$1"
fi
cat >> docker-compose.yml<< EOF
node_$1:
image: $DOCKER_IMAGE_NAME
networks:
- kovacs_network
container_name: inverse_transparency_node_$1
ports:
- "304$port:30401"
- "500$port:40000"
- "510$port:41000"
environment:
BOOTNODE_ENODE: \${BOOTNODE_ENODE}
REVOLORI_TOKEN: \${REVOLORI_TOKEN_$1}
REVOLORI_ADDRESS: \${REVOLORI_ADDRESS}
GETH_KEYSTORE_PATH: "/build/geth/dataDir/keystore/"
EOF
}
function append_network_to_compose {
cat >> docker-compose.yml<< EOF
networks:
kovacs_network:
name: kovacs_network
external: false
EOF
}
function print_headline() {
if [ $# -ne 1 ]; then
echo "print_headline expects 1 argument, got $#"
echo "Exiting"
exit 1
fi
echo
echo "========== $1 =========="
}
function setupEnvAndCompose {
user_number=0
read -p "How many users do you want to create? " user_number
user_number=$((user_number)) # This accepts not only numbers but arithmetic operations as well
if [ $user_number -lt 2 ]; then
echo "Invalid input!"
echo "The entered number must be a valid integer that is bigger than or equal to 2"
echo "Exiting"
exit 1
fi
if [ $user_number -gt $DOCKER_CONTAINER_LIMIT ]; then
echo "This script accepts a maximum of $DOCKER_CONTAINER_LIMIT containers/users"
echo "Exiting"
exit 1
fi
note="IMPORTANT: Replace 127.0.0.1 in the bootnode's enode with use your network IP!"
# check if $BOOTNODE_ENODE
bootnode_enode=""
if [ -z ${BOOTNODE_ENODE+x} ]; then
# Env is not set => read it
read -p "What is the bootnode's enode? $note " bootnode_enode
else
# Env is set
echo "Bootnode enode was found in env"
bootnode_enode="${BOOTNODE_ENODE}"
fi
printf "# Geth\nBOOTNODE_ENODE=\"$bootnode_enode\"\n" > .env
read -p "What is Revolori's IP Address including port and network scheme? Defaults to '$REVOLORI_DEFAULT_ADDR' " revolori_address
if [ "$revolori_address" = "" ]; then
revolori_address="$REVOLORI_DEFAULT_ADDR"
fi
printf "\n# Revolori\nREVOLORI_ADDRESS=\"$revolori_address\"\n" >> .env
print_headline "Creating user accounts"
# create users
printf "Creating $user_number users: "
python3 user-management/createUsers.py "$user_number" "$revolori_address"
if [ $? -ne 0 ]; then
echo
ask_for_continue "Creating users"
else
echo "Done"
fi
printf "Requesting tokens: "
tokens=()
for (( i=0; i < $user_number; i++ )); do
token=$(python3 user-management/getToken.py "$i" "$revolori_address")
if [ $? -ne 0 ]; then
echo "An error occurred when requesting the token for user $i:"
echo "$token"
exit 1
fi
tokens+=($token)
done
if [ "${#tokens[@]}" -ne "$user_number" ]; then
echo
echo "Did not get all reqired tokens"
echo "Expected $user_number, got ${#tokens[@]}"
echo "Exiting"
exit 1
fi
echo "Done"
printf "Writing tokens to .env: "
printf "\n# Tokens\n" >> .env
# Write tokens to env
for (( i=0; i < ${#tokens[@]}; i++ )); do
echo "REVOLORI_TOKEN_$i=\"${tokens[$i]}\"">> .env
done
echo "Done"
print_headline "Docker"
# Create docker-compose.yml
printf "Creating docker-compose.yml: "
echo "version: \"3.7\"" > docker-compose.yml
echo "" >> docker-compose.yml
echo "services:" >> docker-compose.yml
for (( i=0; i < ${#tokens[@]}; i++ )); do
append_node_to_compose $i
done
append_network_to_compose
echo "Done"
}
function startContainers {
printf "Starting containers: "
ret=$(docker-compose up -d --build 2>&1)
if [ $? -ne 0 ]; then
echo "Failure"
echo "$ret"
exit 1
fi
echo "Done"
}
function stopContainers {
printf "Stopping running containers: "
ret=$(docker-compose down --remove-orphans 2>&1)
if [ $? -ne 0 ]; then
echo "Failure"
echo "$ret"
exit 1
fi
echo "Done"
}
function buildImage {
printf "Building the docker image (this may take some time): "
ret=$(docker build . -t "$DOCKER_IMAGE_NAME" 2>&1)
if [ $? -ne 0 ]; then
echo "Failure"
echo "$ret"
exit 1
fi
echo "Done"
}
if [ $# -eq 1 ] && [ "$1" = "rebuild" ]; then
stopContainers
buildImage
startContainers
exit 0
fi
echo "This script assumes that:"
printf "\t- Revolori has been pulled from the latest gabriel/master branch\n"
printf "\t- Revolori is up and running\n"
printf "\t- That a bootnode is running (using the bootnode script located in config/)\n"
printf "\t- That nginx has been configured as a proxi for the bootnode. See config/nginx_bootnode for an example\n"
printf "\t- Go modules are enabled\n"
echo
read -p "Are all these assumptions true for your system (y/n)? " conf
if [ "$conf" = "y" ] || [ "$conf" = "Y" ]; then
echo "Starting setup proccess"
else
echo "See README.md on how to set everythin up and come back once it is done"
exit 1
fi
# Stopping exiting containers
print_headline "Clean up"
stopContainers
# Check if .env and docker-compose.yml exit
print_headline "Setting up .env"
if [ -f ".env" ] || [ -f "docker-compose.yml" ]; then
echo ".env or docker-compose.yml already exists"
read -p "Do you want to create new configuration files (y/n)? " cont
if [ "$cont" = "y" ] || [ "$cont" = "Y" ]; then
setupEnvAndCompose
else
print_headline "Docker"
fi
else
setupEnvAndCompose
fi
buildImage
echo "Setup up successful"
echo
read -p "Do you want to run docker-compose up (y/n)? " cont
if [ "$cont" = "y" ] || [ "$cont" = "Y" ]; then
startContainers
else
echo "Run 'docker-compose up -d --build' when you want to start the containers"
fi