-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathstrato
More file actions
executable file
·359 lines (317 loc) · 11.3 KB
/
strato
File metadata and controls
executable file
·359 lines (317 loc) · 11.3 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env bash
set -e
BOOTSTRAP_DOCKER_VERSION=3
Green='\033[0;32m'
Red='\033[0;31m'
Yellow='\033[0;33m'
BYellow='\033[1;33m'
NC='\033[0m'
function checkDocker {
if ! docker ps &> /dev/null
then
echo -e "${Red}Error: docker is required: https://www.docker.com/ . If you have it installed - you may need to run STRATO with sudo user${NC}"
exit 1
fi
if ! docker compose version &> /dev/null && ! docker-compose -v &> /dev/null
then
echo -e "${Red}Error: Docker Compose is required: https://docs.docker.com/compose/install/"
exit 2
else
if ! docker compose version &> /dev/null
then
docker_compose="docker-compose -p strato --log-level ERROR - -f docker-compose.yml"
CNAME_SEP="_"
else
# The `-f docker-compose.yml` is required for the command to work correctly despite it's the default (docker bug?)
docker_compose="docker -l error compose -p strato -f docker-compose.yml"
CNAME_SEP="-"
fi
fi
}
function checkComposeFile {
if [ ! -f docker-compose.yml ]
then
echo -e "${BYellow}docker-compose.yml not found.${NC}"
getCompose
else
echo -e "${Yellow}Using the existing docker-compose.yml${NC}"
fi
if ! grep -q '^[[:space:]]*strato:' docker-compose.yml; then
echo -e "${Red}Wrong docker-compose.yml provided - no 'strato' service found in it.${NC}"
exit 4
fi
# Check bootstrap-docker version compatibility
min_version=$(grep -oP '#bootstrap-docker-min-version:\K.*' docker-compose.yml 2>/dev/null || true)
if [ -n "$min_version" ]; then
if [ "$(printf '%s\n' "$min_version" "$BOOTSTRAP_DOCKER_VERSION" | sort -V | head -1)" != "$min_version" ]; then
echo -e "${Red}This docker-compose.yml requires bootstrap-docker version >= ${min_version}, but this is version ${BOOTSTRAP_DOCKER_VERSION}. Please update the strato bootstrap scripts.${NC}"
exit 3
fi
fi
}
function help {
echo -e "${Yellow}STRATO run script${NC}
Kickstart a STRATO node.
${Yellow}Optional flags:${NC}
The flags are mutually exclusive:
--help|-h - this help.
--down - remove strato containers and leave all volumes intact.
--wipe - stop and remove STRATO containers and wipe out volumes.
--compose - fetch the docker-compose.yml of the latest STRATO release
--pull - pull images used in docker-compose.yml
--get-address - get the address of the running node.
--get-pubkey - get the public key of the running node.
--get-validators - get the list of validating nodes in the network on the running node.
--get-metadata - get the node's metadata in JSON format.
--single - run a single node.
"
}
function wipe {
echo -e "${Yellow}Removing STRATO containers and wiping out volumes${NC}"
${docker_compose} down -v -t 0 --remove-orphans
sudo rm -rf nodedata/
sudo rm -rf logs/
}
function down {
echo -e "${Yellow}Removing STRATO containers${NC}"
${docker_compose} down --remove-orphans
}
function getCompose {
echo -e "${Yellow}Fetching the docker-compose.yml of the latest STRATO release...${NC}"
curl -fLo docker-compose.yml https://github.com/strato-net/strato-platform/releases/download/$(curl -s -L https://github.com/strato-net/strato-platform/releases/latest | grep -om1 '"/strato-net/strato-platform/releases/tag/[^"]*' | grep -oE "[^/]+$")/docker-compose.yml
echo -e "${Yellow}docker-compose.yml downloaded successfully.${NC}"
}
function pullImages {
${docker_compose} pull
}
function getMetadata {
if [[ -n $(docker ps | grep strato${CNAME_SEP}strato${CNAME_SEP}1) ]]; then
METADATA_RESP=$(sudo docker exec strato${CNAME_SEP}nginx${CNAME_SEP}1 curl -s -w $%{http_code} -X GET http://strato:3000/eth/v1.2/metadata | tr '\n' ' ')
METADATA_RESP_STATUS=$(cut -d$ -f2 <<< ${METADATA_RESP})
METADATA_RESP_CONTENT=$(cut -d$ -f1 <<< ${METADATA_RESP})
case "${METADATA_RESP_STATUS}" in
200):
echo "${METADATA_RESP_CONTENT}"
;;
000):
echo -e "${Red}Metadata endpoint is unreachable through strato docker network${NC}"
;;
*):
echo -e "${Red}Error: Unknown response from metadata endpoint${NC}"
exit 24
;;
esac
else
echo -e "${Red}STRATO is not running. Start STRATO to get the node's metadata${NC}"
exit 20
fi
}
function _outputMetadataValue {
metadata=$(getMetadata)
if (which jq > /dev/null); then
echo "${metadata}" | jq -r ".$1"
else
# Using text processing with awk is not the best idea as validators may have any name including the names of properties of json. So just exit if no jq found.
echo "Please install 'jq' library for that feature or use --get-metadata to get a plain response of metadata API endpoint"
exit 27
fi
}
function getAddress {
_outputMetadataValue nodeAddress
}
function getPublicKey {
_outputMetadataValue nodePubKey
}
function getValidators {
_outputMetadataValue validators
}
while [ ${#} -gt 0 ]; do
case "${1}" in
--help|-h)
help
exit 0
;;
--down)
checkDocker
checkComposeFile
down
exit 0
;;
--wipe)
checkDocker
checkComposeFile
wipe
exit 0
;;
--single)
node_type=single
;;
--compose)
getCompose
exit 0
;;
--pull)
checkDocker
checkComposeFile
pullImages
exit 0
;;
--get-address)
checkDocker
getAddress
exit 0
;;
--get-pubkey)
checkDocker
getPublicKey
exit 0
;;
--get-validators)
checkDocker
getValidators
exit 0
;;
--get-metadata)
checkDocker
getMetadata
exit 0
;;
*)
echo -e "${Red}Unknown flag ${1} provided, please check --help. Exit.${NC}"
exit 5
;;
esac
shift 1
done
checkDocker
# Ensure docker-compose.yml exists (download from releases if missing)
checkComposeFile
export NODE_HOST=${NODE_HOST:-localhost}
export OAUTH_DISCOVERY_URL=${OAUTH_DISCOVERY_URL:-'https://keycloak.blockapps.net/auth/realms/mercata/.well-known/openid-configuration'}
export network=${network:-helium}
export VAULT_URL=${VAULT_URL:-'https://vault.blockapps.net:8093'}
export HTTP_PORT=${HTTP_PORT:-80}
export HTTPS_PORT=${HTTPS_PORT:-443}
export ssl=${ssl:-false}
if [ "$ssl" = true ] ; then
http_protocol=https
main_port=${HTTPS_PORT}
else
http_protocol=http
main_port=${HTTP_PORT}
fi
if [[ -z ${OAUTH_CLIENT_ID} || -z ${OAUTH_CLIENT_SECRET} ]] ; then
echo -e "${Red} OAUTH_CLIENT_ID and OAUTH_CLIENT_SECRET are required\nFor additional help see './strato --help'${NC}"
exit 13
fi
echo ""
echo "NODE_HOST: $NODE_HOST"
echo "HTTP_PORT: $HTTP_PORT"
echo "HTTPS_PORT: $HTTPS_PORT"
echo "ssl: $ssl"
echo "OAUTH_DISCOVERY_URL: ${OAUTH_DISCOVERY_URL}"
echo "OAUTH_CLIENT_ID: $OAUTH_CLIENT_ID"
echo "OAUTH_CLIENT_SECRET: $(if [ -z ${OAUTH_CLIENT_SECRET} ]; then echo "not set"; else echo "is set"; fi)"
echo "useCustomGenesis": ${useCustomGenesis:-false(default)}
if [ "${node_type}" == "single" ]
then
echo "" && echo -e "${Yellow}Starting a single node${NC}"
export generateKey=${generateKey:-true}
export networkID=${networkID:-$(($RANDOM * $RANDOM * $RANDOM))}
else
if [[ ${generateKey} = false ]]; then
echo -e "\n${BYellow}WARNING: STRATO was started with generateKey=false. The node will not start until you manually insert a key into the vault using the migrate-nodekey script${NC}"
fi
export generateKey=${generateKey:-true}
if [ -n "$BOOT_NODE_IP" ]
then
export bootnode=${BOOT_NODE_IP}
echo "bootnode: $bootnode"
fi
fi
echo "generateKey: $generateKey"
echo "networkID: ${networkID:-unset - using default}"
echo "bootnode (or BOOT_NODE_IP): ${bootnode:-unset - using default}"
# PARAMETERS VALIDATION
if [ ${HTTP_PORT} = ${HTTPS_PORT} ]; then
echo -e "${Red}Can not bind HTTP and HTTPS listeners to same port (${HTTP_PORT})${NC}"
exit 7
fi
# Make sure NODE_HOST contains port if custom port is provided
if [ ${main_port} != "80" ] && [ ${main_port} != "443" ] && [[ ${NODE_HOST} != *":${main_port}" ]]; then
echo -e "${Red}NODE_HOST should contain the port if custom port is set with HTTP_PORT (for non-ssl) or HTTPS_PORT (for ssl). Expected: NODE_HOST=hostname:${main_port}${NC}"
exit 8
fi
## END OF PARAMETERS VALIDATION
# RUN STRATO-SETUP TO GENERATE NODE CONFIG
if [ ! -f nodedata/.ethereumH/ethconf.yaml ]; then
rm -rf nodedata/
echo -e "${Yellow}Running strato-setup to generate node configuration...${NC}"
# Determine STRATO_IMAGE from docker-compose.yml if not set
if [ -z "${STRATO_IMAGE}" ]; then
STRATO_IMAGE=$(grep -oP 'image:\s*\${STRATO_IMAGE:-\K[^}]+' docker-compose.yml | head -1)
echo "Using STRATO_IMAGE from docker-compose.yml: ${STRATO_IMAGE}"
fi
# Extract REPO_URL from STRATO_IMAGE if not already set
# e.g., "406773134706.dkr.ecr.us-east-1.amazonaws.com/strato/strato:16.7-xxx" -> "406773134706.dkr.ecr.us-east-1.amazonaws.com/strato/"
if [ -z "${REPO_URL}" ] && [ -n "${STRATO_IMAGE}" ]; then
REPO_URL=$(echo "${STRATO_IMAGE}" | sed 's|strato:.*||')
echo "Derived REPO_URL: ${REPO_URL}"
fi
# Create OAuth credentials for strato-setup
mkdir -p "${HOME}/.secrets"
cat > "${HOME}/.secrets/strato_credentials.yaml" << EOF
discoveryUrl: "${OAUTH_DISCOVERY_URL}"
clientId: "${OAUTH_CLIENT_ID}"
clientSecret: "${OAUTH_CLIENT_SECRET}"
EOF
docker run --rm \
--entrypoint strato-setup \
-v "$(pwd)":/node \
-v "${HOME}/.secrets:/root/.secrets:ro" \
${postgres_password:+-e postgres_password="${postgres_password}"} \
"${STRATO_IMAGE}" \
/node/nodedata --dockerMode=allDocker --network="${network}" --vaultUrl="${VAULT_URL}/strato/v2.3" --repoUrl="${REPO_URL:-}" \
--pghost=postgres --kafkahost=kafka --redisHost=redis --apiIPAddress=0.0.0.0 --httpPort="${HTTP_PORT}"
# Fix ownership for non-root containers
sudo chown -R ${DOCKER_UID:-1000}:${DOCKER_GID:-1000} nodedata/ logs/ 2>/dev/null || true
# Move generated docker-compose.yml to current directory (strato-setup creates it in the node dir)
mv nodedata/docker-compose.yml docker-compose.yml
echo -e "${Green}Node configuration generated.${NC}"
fi
# COMPOSE FILE PRE-PROCESSING
function cleanup {
rm docker-compose-temp.yml
}
trap cleanup EXIT
if [ "$ssl" != true ]; then
sed -n '/#TAG_REMOVE_WHEN_NO_SSL/!p' docker-compose.yml > docker-compose-temp.yml
else
if [ ${HTTPS_PORT} != "443" ]; then
sed -n '/#TAG_REMOVE_WHEN_SSL_CUSTOM_HTTPS_PORT/!p' docker-compose.yml > docker-compose-temp.yml
else
cp docker-compose.yml docker-compose-temp.yml
fi
fi
# END OF COMPOSE FILE PRE-PROCESSING
mkdir -p nodedata/
mkdir -p logs/
${docker_compose} -f docker-compose-temp.yml up -d --remove-orphans
if [[ "$useCustomGenesis" = true ]]; then
echo -e "${BYellow}useCustomGenesis is set to 'true' - using custom genesis block. 'strato' container will wait until the file is added to /var/lib/strato/genesis.json within the strato container. See the container log for details: \`sudo docker logs strato${CNAME_SEP}strato${CNAME_SEP}1\`${NC}"
exit 0
fi
# WAIT FOR STRATO TO RUN
started=$(date +%s)
timeout=180
hc_container=$(${docker_compose} ps | grep "\\${CNAME_SEP}nginx${CNAME_SEP}" | awk '{print $1}')
printf "Waiting for STRATO to rise and shine..."
i=0
while ! [[ -n $(docker ps -q -f name=${hc_container} -f health=healthy) ]]; do
if [[ $(date +%s) -ge ${started}+${timeout} ]]; then
echo -e "\n${Red}Node did not start within ${timeout}sec. See 'docker ps' for additional info. Exit.${NC}"
exit 22
fi
done
printf "\n"
echo -e "\n${Green}STRATO has awoken. Check ${http_protocol}://${NODE_HOST}${NC}"