-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinstall.sh
executable file
·335 lines (295 loc) · 14.1 KB
/
install.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/bin/sh
src_dir="contrib"
near_postfix="near"
network="mainnet"
near_network="mainnet"
silo_config_file=""
chain_id="1313161554"
near_source="nearcore" # nearcore or datalake
migrate_from=""
use_aurora_snapshot=1
use_near_snapshot=1
download_workers=256
trap "echo Exited!; exit 2;" INT TERM
if [ ! -d "./${src_dir}" ]; then
echo "Run ./install.sh from original git repository only!"
exit 1
fi
. ./${src_dir}/bin/common.sh
valid_silo_config() {
if [ ! -f "$1" ]; then
return 1
fi
while read -r config || [ -n "$config" ]; do
if [ -n "$config" ] && [ "$config" != " " ] && ! beginswith "#" "$config"; then
value=$(echo "$config" | cut -d ':' -f2- | awk '{$1=$1};1')
if [ -n "$value" ] && [ "$value" != " " ] && [ -z "${value##*:*}" ]; then
return 1
fi
fi
done < "$1"
}
apply_silo_config() {
silo_network=$(grep "SILO_NETWORK" "$1" | cut -d ':' -f2- | awk '{$1=$1};1')
sed "s/%%SILO_NETWORK%%/${silo_network}/" "${INSTALL_DIR}/config/relayer/relayer.yaml" > "${INSTALL_DIR}/config/relayer/relayer.yaml2" && \
mv "${INSTALL_DIR}/config/relayer/relayer.yaml2" "${INSTALL_DIR}/config/relayer/relayer.yaml"
sed "s/%%SILO_NETWORK%%/${silo_network}/" "${INSTALL_DIR}/docker-compose.yaml" > "${INSTALL_DIR}/docker-compose.yaml2" && \
mv "${INSTALL_DIR}/docker-compose.yaml2" "${INSTALL_DIR}/docker-compose.yaml"
silo_chain_id=$(grep "SILO_CHAIN_ID" "$1" | cut -d ':' -f2- | awk '{$1=$1};1')
sed "s/%%SILO_CHAIN_ID%%/${silo_chain_id}/" "${INSTALL_DIR}/config/refiner/refiner.json" > "${INSTALL_DIR}/config/refiner/refiner.json2" && \
mv "${INSTALL_DIR}/config/refiner/refiner.json2" "${INSTALL_DIR}/config/refiner/refiner.json"
sed "s/%%SILO_CHAIN_ID%%/${silo_chain_id}/" "${INSTALL_DIR}/config/relayer/relayer.yaml" > "${INSTALL_DIR}/config/relayer/relayer.yaml2" && \
mv "${INSTALL_DIR}/config/relayer/relayer.yaml2" "${INSTALL_DIR}/config/relayer/relayer.yaml"
silo_genesis=$(grep "SILO_GENESIS" "$1" | cut -d ':' -f2- | awk '{$1=$1};1')
sed "s/%%SILO_GENESIS%%/${silo_genesis}/" "${INSTALL_DIR}/config/relayer/relayer.yaml" > "${INSTALL_DIR}/config/relayer/relayer.yaml2" && \
mv "${INSTALL_DIR}/config/relayer/relayer.yaml2" "${INSTALL_DIR}/config/relayer/relayer.yaml"
silo_from_block=$(grep "SILO_FROM_BLOCK" "$1" | cut -d ':' -f2- | awk '{$1=$1};1')
sed "s/%%SILO_FROM_BLOCK%%/${silo_from_block}/" "${INSTALL_DIR}/config/relayer/relayer.yaml" > "${INSTALL_DIR}/config/relayer/relayer.yaml2" && \
mv "${INSTALL_DIR}/config/relayer/relayer.yaml2" "${INSTALL_DIR}/config/relayer/relayer.yaml"
sed "s/%%SILO_FROM_BLOCK%%/${silo_from_block}/" "${INSTALL_DIR}/docker-compose.yaml" > "${INSTALL_DIR}/docker-compose.yaml2" && \
mv "${INSTALL_DIR}/docker-compose.yaml2" "${INSTALL_DIR}/docker-compose.yaml"
engine_account=$(grep "SILO_ENGINE_ACCOUNT" "$silo_config_file" | cut -d ':' -f2- | awk '{$1=$1};1')
sed "s/%%SILO_ENGINE_ACCOUNT%%/${engine_account}/" "${INSTALL_DIR}/config/refiner/refiner.json" > "${INSTALL_DIR}/config/refiner/refiner.json2" && \
mv "${INSTALL_DIR}/config/refiner/refiner.json2" "${INSTALL_DIR}/config/refiner/refiner.json"
if [ ${near_source} = "datalake" ]; then
silo_datalake_network=$(to_upper_first "$silo_network")
sed "s/%%SILO_DATALAKE_NETWORK%%/${silo_datalake_network}/" "${INSTALL_DIR}/config/refiner/refiner.json" > "${INSTALL_DIR}/config/refiner/refiner.json2" && \
mv "${INSTALL_DIR}/config/refiner/refiner.json2" "${INSTALL_DIR}/config/refiner/refiner.json"
fi
}
apply_nearcore_config() {
echo "network: $near_network"
if [ "x$migrate_from" != "x" ]; then
ln -s "$migrate_from/near" "${INSTALL_DIR}/near"
ln -s "$migrate_from/engine" "${INSTALL_DIR}/engine"
else
mkdir -p "${INSTALL_DIR}/near" "${INSTALL_DIR}/engine" 2> /dev/null
if [ ! -f "${INSTALL_DIR}/near/config.json" ] || [ ! -f "${INSTALL_DIR}/near/genesis.json" ]; then
echo "Initializing nearcore configuration..."
docker run --rm --name config_init \
-v "$(pwd)/${INSTALL_DIR}"/near:/root/.near:rw \
ghcr.io/aurora-is-near/standalone-rpc/nearcore:latest \
/usr/local/bin/neard --home /root/.near init --chain-id "${near_network}" --download-genesis --download-config rpc
fi
if [ $use_near_snapshot -eq 1 ] && [ ! -f "${INSTALL_DIR}/near/data/CURRENT" ]; then
echo "Downloading near chain snapshot..."
docker run --rm --pull=always \
--init \
-v "$(pwd)/${INSTALL_DIR}/near/data:/data" \
-v "$(pwd)/${src_dir}/bin/download_rclone.sh:/download_rclone.sh" \
--entrypoint=/bin/ash \
rclone/rclone \
-c "trap 'kill -TERM \$pid; exit 1' INT TERM; apk add --no-cache curl && chmod +x /download_rclone.sh && CHAIN_ID=${near_network} SERVICE=near DATA_PATH=/data /download_rclone.sh & pid=\$! && wait \$pid"
echo "Downloaded near chain snapshot"
fi
fi
}
apply_datalake_config() {
if [ "x$AWS_SHARED_CREDENTIALS_FILE" = "x" ]; then
echo "Installation failed, environment variable AWS_SHARED_CREDENTIALS_FILE is needed for datalake config." \
"Please set environment variable AWS_SHARED_CREDENTIALS_FILE for [$USER] user or run installer as 'AWS_SHARED_CREDENTIALS_FILE={path to AWS credentials file} ./install.sh'"
echo "For more details, also see https://docs.aws.amazon.com/sdkref/latest/guide/file-location.html"
exit 1
fi
if [ ! -f "$AWS_SHARED_CREDENTIALS_FILE" ]; then
echo "Installation failed, datalake config requires AWS account." \
"Create [$AWS_SHARED_CREDENTIALS_FILE] file and run install script again!"
exit 1
fi
sed "s|%%AWS%%|${AWS_SHARED_CREDENTIALS_FILE}|" "${INSTALL_DIR}/docker-compose.yaml" > "${INSTALL_DIR}/docker-compose.yaml2"
mv "${INSTALL_DIR}/docker-compose.yaml2" "${INSTALL_DIR}/docker-compose.yaml"
}
install() {
if [ -f "${VERSION_FILE}" ]; then
echo "There is already an Aurora Standalone RPC installation running or an unfinished installation exists"
if confirmed "To continue with installation, you have to first uninstall. Would you like to uninstall?"; then
./uninstall.sh
else
exit 1
fi
fi
echo "Installing" && version | tee "${VERSION_FILE}"
set -e
mkdir -p "${INSTALL_DIR}/data/relayer" \
"${INSTALL_DIR}/data/refiner" \
"${INSTALL_DIR}/config/relayer" \
"${INSTALL_DIR}/config/refiner" \
"${INSTALL_DIR}/engine" \
"${INSTALL_DIR}/config/nginx" 2> /dev/null
if [ ! -f "${INSTALL_DIR}/config/relayer/relayer.yaml" ]; then
cp "./${src_dir}/config/relayer/${network}.yaml" "${INSTALL_DIR}/config/relayer/relayer.yaml"
fi
if [ ! -f "${INSTALL_DIR}/config/relayer/filter.yaml" ]; then
cp "./${src_dir}/config/relayer/filter.yaml" "${INSTALL_DIR}/config/relayer/filter.yaml"
fi
if [ ! -f "${INSTALL_DIR}/config/refiner/refiner.json" ]; then
cp "./${src_dir}/config/refiner/${network}_${near_source}.json" "${INSTALL_DIR}/config/refiner/refiner.json"
fi
if [ ! -f "${INSTALL_DIR}/config/nginx/endpoint.conf" ]; then
cp "./${src_dir}/config/nginx/${network}.conf" "${INSTALL_DIR}/config/nginx/endpoint.conf"
fi
if [ ! -f "${INSTALL_DIR}/docker-compose.yaml" ]; then
cp "./${src_dir}/config/docker/${network}_${near_source}.yaml" "${INSTALL_DIR}/docker-compose.yaml"
fi
if [ "${network}" = "silo" ]; then
apply_silo_config "$silo_config_file"
near_network="$silo_network"
chain_id="$silo_chain_id"
else
near_network="$network"
fi
if [ "${near_source}" = "nearcore" ]; then
apply_nearcore_config
else
apply_datalake_config
fi
if [ $use_aurora_snapshot -eq 1 ] || [ "x$migrate_from" != "x" ]; then
if [ ! -f "${INSTALL_DIR}/data/relayer/.version" ]; then
echo "Downloading snapshot for chain ${chain_id}, block ${latest}"
echo "Fetching, this can take some time..."
docker run --rm --pull=always \
--init \
-v "$(pwd)/${INSTALL_DIR}/data/relayer:/data" \
-v "$(pwd)/${src_dir}/bin/download_rclone.sh:/download_rclone.sh" \
--entrypoint=/bin/ash \
rclone/rclone \
-c "trap 'kill -TERM \$pid; exit 1' INT TERM; apk add --no-cache curl && chmod +x /download_rclone.sh && CHAIN_ID=${chain_id} SERVICE=relayer DATA_PATH=/data /download_rclone.sh & pid=\$! && wait \$pid"
fi
if [ ! -f "${INSTALL_DIR}/engine/.version" ]; then
echo "Downloading state snapshot ${latest}, block ${latest}"
echo "Fetching, this can take some time..."
docker run --rm --pull=always \
--init \
-v "$(pwd)/${INSTALL_DIR}/engine:/data" \
-v "$(pwd)/${src_dir}/bin/download_rclone.sh:/download_rclone.sh" \
--entrypoint=/bin/ash \
rclone/rclone \
-c "trap 'kill -TERM \$pid; exit 1' INT TERM; apk add --no-cache curl && chmod +x /download_rclone.sh && CHAIN_ID=${chain_id} SERVICE=refiner DATA_PATH=/data /download_rclone.sh & pid=\$! && wait \$pid"
fi
fi
if [ ! -f "${INSTALL_DIR}/config/relayer/relayer.json" ]; then
echo "Generating relayer key..."
docker run --pull=always --rm --name near_keygen \
-v "$(pwd)/${INSTALL_DIR}"/config/relayer:/config:rw \
--entrypoint /bin/sh ghcr.io/aurora-is-near/relayer2-public \
-c "/app/app generate-key --output=/config/relayer.json"
fi
account_id=$(grep "account_id" "${INSTALL_DIR}/config/relayer/relayer.json" | cut -d\" -f4)
sed "s/%%SIGNER%%/${account_id}/" "${INSTALL_DIR}/config/relayer/relayer.yaml" > "${INSTALL_DIR}/config/relayer/relayer.yaml2" && \
mv "${INSTALL_DIR}/config/relayer/relayer.yaml2" "${INSTALL_DIR}/config/relayer/relayer.yaml"
if [ $use_aurora_snapshot -eq 0 -a $use_near_snapshot -eq 0 ] \
|| [ $use_near_snapshot -eq 1 -a ${near_source} = "nearcore" -a -f "${INSTALL_DIR}/near/data/CURRENT" -a -f "${INSTALL_DIR}/data/relayer/.version" ] \
|| [ ${near_source} = "datalake" -a -f "${INSTALL_DIR}/data/relayer/.version" ]; then
echo "Setup complete [${network}, ${near_source}]"
fi
set +e
if [ "x$migrate_from" != "x" ]; then
"$migrate_from"/stop.sh
fi
cp "./${src_dir}/bin/start.sh" "${INSTALL_DIR}/start.sh"
cp "./${src_dir}/bin/stop.sh" "${INSTALL_DIR}/stop.sh"
cp "./${src_dir}/bin/common.sh" "${INSTALL_DIR}/common.sh"
cp "./${src_dir}/bin/support.sh" "${INSTALL_DIR}/support.sh"
echo "Starting..."
./"${INSTALL_DIR}"/start.sh
}
version() {
if [ ! -f "${VERSION_FILE}" ]; then
echo "Aurora Standalone RPC"
branch=$(git rev-parse --abbrev-ref HEAD) && echo "branch: $branch" \
&& commit=$(git rev-parse HEAD) && echo "commit: $commit" \
&& tag=$(git describe --exact-match "$commit") 2>/dev/null && echo "tag: $tag"
else
cat "${VERSION_FILE}"
fi
}
usage() {
printf '\nUsage: %s [options]' "$(basename "$0")"
printf '\n\nOptions\n'
printf ' %s\t%s\n\n' "-n {mainnet|testnet|silo}" "network to use, default is mainnet."
printf ' %s\t\t\t%s\n\t\t\t\t%s\n\n' "-f {path}" "for silo networks, this is the path to your silo configuration file." \
"This option is valid only if silo network is used, and '-s' option is ignored if this option is given."
printf ' %s\t\t%s\n\n' "-r {nearcore|datalake}" "near source for indexing, default is nearcore."
printf ' %s\t\t\t%s\n\t\t\t\t%s\n\n' "-m {path}" "use the existing nearcore data at 'path' instead of downloading snapshots from scratch." \
"This option is valid only if nearcore config is used, and '-s' option is ignored if this option is given."
printf ' %s\t\t%s\n\t\t\t\t%s\n\n' "-w {number [1-256]}" "number of workers used for downloading near snapshots, default is 256." \
"NOTE: On some OS and HW configurations, default number of workers may cause high CPU consumption during download."
printf ' %s\t\t\t\t%s\n\t\t\t\t%s\n\t\t\t\t%s\n\n' "-s" "if specified then snapshots are ignored during installation, default downloads and uses snapshots." \
"NOTE: Ignoring snapshots may cause refiner not to index near chain. This can only be a valid option" \
"if near source is selected as datalake otherwise refiner will not be sync with near core from scratch."
printf ' %s\t\t\t\t%s\n\n' "-v" "prints version"
printf ' %s\t\t\t\t%s\n\n' "-h" "prints usage"
printf 'Examples\n'
printf ' %s\t\t-> %s\n\n' "./install.sh -n mainnet -r datalake -s" "use mainnet with near data lake but do not download snapshots"
printf ' %s\t\t-> %s\n\n' "./install.sh -n silo -f ./silo.conf" "use sile network whose config is defined in silo.conf, near source for indexing is nearcore"
}
while getopts ":n:r:m:f:w:svh" opt; do
case "${opt}" in
n)
network="${OPTARG}"
if [ "${network}" = "testnet" ]; then
near_postfix="testnet"
chain_id="1313161555"
elif [ "${network}" != "mainnet" ] && [ "${network}" != "silo" ] ; then
echo "Invalid Value: -${opt} cannot be '${OPTARG}'"
usage
exit 1
fi
;;
r)
near_source="${OPTARG}"
if [ "$near_source" != "nearcore" ] && [ "$near_source" != "datalake" ]; then
echo "Invalid Value: -${opt} cannot be '${OPTARG}'"
usage
exit 1
fi
;;
w)
download_workers="${OPTARG}"
if [ "$download_workers" -lt 1 ] || [ "$download_workers" -gt 256 ]; then
echo "Invalid Value: -${opt} cannot be '${OPTARG}'"
usage
exit 1
fi
;;
m)
migrate_from=$(realpath "${OPTARG}")
if [ ! -d "$migrate_from/near" ] || [ ! -d "$migrate_from/engine" ]; then
echo "Invalid Value: path(s) '${OPTARG}/near' or(and) '${OPTARG}/engine' not exist"
usage
exit 1
fi
;;
s)
use_aurora_snapshot=0
use_near_snapshot=0
;;
f)
silo_config_file="${OPTARG}"
;;
v)
version
exit 0
;;
h)
usage
exit 0
;;
\?)
echo "Invalid Option: -${OPTARG}" 1>&2
usage
exit 1
;;
:)
echo "Invalid Option: -${OPTARG} requires an argument" 1>&2
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ "${network}" = "silo" ] && ! valid_silo_config "$silo_config_file"; then
echo "Invalid silo config, $silo_config_file. For more information, see template config ./${src_dir}/silo/template.conf"
exit 1
fi
install