-
Notifications
You must be signed in to change notification settings - Fork 2
/
configure-environment.sh
executable file
Β·315 lines (273 loc) Β· 12.2 KB
/
configure-environment.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
#!/bin/bash
# Initial variable declarations
DOCKER_NETWORK_PRUNE=false
SETUP_COMPLETE=true
DATA_MARKET_CONTRACT_NUMBER=""
SKIP_CREDENTIAL_UPDATE=false
NO_COLLECTOR=false
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--docker-network-prune)
DOCKER_NETWORK_PRUNE=true
shift
;;
--data-market-contract-number)
DATA_MARKET_CONTRACT_NUMBER=$2
shift 2
;;
--skip-credential-update)
SKIP_CREDENTIAL_UPDATE=true
shift
;;
--no-collector)
NO_COLLECTOR=true
shift
;;
*)
shift
;;
esac
done
# Error handling and cleanup functions
handle_error() {
local exit_code=$?
if [ $exit_code -lt 100 ]; then
echo "Error on line $1: Command exited with status $exit_code"
exit $exit_code
fi
return $exit_code
}
cleanup() {
find . -name "*.backup" -type f -delete
if [ -n "$NAMESPACE" ] && [ -f ".env-${FULL_NAMESPACE}" ] && [ "$SETUP_COMPLETE" = false ]; then
rm -rf ".env-${FULL_NAMESPACE}"
echo "Aborted setup. Deleted .env-${FULL_NAMESPACE} file."
fi
}
trap 'handle_error $LINENO' ERR
trap cleanup EXIT
# Docker daemon check
if ! docker info >/dev/null 2>&1; then
echo "β Docker daemon is not running"
exit 1
fi
# Data market selection
if [ -n "$DATA_MARKET_CONTRACT_NUMBER" ]; then
DATA_MARKET_CONTRACT_CHOICE="$DATA_MARKET_CONTRACT_NUMBER"
else
while true; do
echo "π Select a data market contract: "
echo "1. Aave V3"
echo "2. Uniswap V2"
read DATA_MARKET_CONTRACT_CHOICE
if [[ "$DATA_MARKET_CONTRACT_CHOICE" =~ ^[12]$ ]]; then
break
else
echo "β Invalid choice. Please enter 1 for Aave V3 or 2 for Uniswap V2."
fi
done
fi
if [ "$DATA_MARKET_CONTRACT_CHOICE" = "1" ]; then
echo "Aave V3 selected"
DATA_MARKET_CONTRACT="0xc390a15BcEB89C2d4910b2d3C696BfD21B190F07"
SNAPSHOT_CONFIG_REPO_BRANCH="eth_aavev3_lite_v2"
SNAPSHOTTER_COMPUTE_REPO_BRANCH="eth_aavev3_lite"
NAMESPACE="AAVEV3"
elif [ "$DATA_MARKET_CONTRACT_CHOICE" = "2" ]; then
echo "Uniswap V2 selected"
DATA_MARKET_CONTRACT="0x8023BD7A9e8386B10336E88294985e3Fbc6CF23F"
SNAPSHOT_CONFIG_REPO_BRANCH="eth_uniswapv2-lite_v2"
SNAPSHOTTER_COMPUTE_REPO_BRANCH="eth_uniswapv2_lite_v2"
NAMESPACE="UNISWAPV2"
fi
# Set protocol values
export PROTOCOL_STATE_CONTRACT="0xF68342970beF978697e1104223b2E1B6a1D7764d"
export PROST_RPC_URL="https://rpc-prost1m.powerloom.io"
export PROST_CHAIN_ID=11169
export POWERLOOM_CHAIN=pre-mainnet
export SOURCE_CHAIN=ETH
export FULL_NAMESPACE="${POWERLOOM_CHAIN}-${NAMESPACE}-${SOURCE_CHAIN}"
# Network configuration
export DOCKER_NETWORK_NAME="snapshotter-lite-v2-${SLOT_ID}-${FULL_NAMESPACE}"
# Environment file management
if [ ! -f ".env-${FULL_NAMESPACE}" ]; then
echo "π‘ .env-${FULL_NAMESPACE} file not found, creating one..."
cp env.example ".env-${FULL_NAMESPACE}"
SETUP_COMPLETE=false
# Prompt for required values
read -p "Enter SOURCE_RPC_URL: " SOURCE_RPC_URL
read -p "Enter SIGNER_ACCOUNT_ADDRESS: " SIGNER_ACCOUNT_ADDRESS
read -s -p "Enter SIGNER_ACCOUNT_PRIVATE_KEY: " SIGNER_ACCOUNT_PRIVATE_KEY
echo
read -p "Enter Your SLOT_ID (NFT_ID): " SLOT_ID
read -p "Enter Your TELEGRAM_CHAT_ID (Optional, leave blank to skip.): " TELEGRAM_CHAT_ID
# Update env file
sed -i".backup" "s#<data-market-contract>#$DATA_MARKET_CONTRACT#" ".env-${FULL_NAMESPACE}"
sed -i".backup" "s#<protocol-state-contract>#$PROTOCOL_STATE_CONTRACT#" ".env-${FULL_NAMESPACE}"
sed -i".backup" "s#<snapshot-config-repo-branch>#$SNAPSHOT_CONFIG_REPO_BRANCH#" ".env-${FULL_NAMESPACE}"
sed -i".backup" "s#<snapshotter-compute-repo-branch>#$SNAPSHOTTER_COMPUTE_REPO_BRANCH#" ".env-${FULL_NAMESPACE}"
sed -i".backup" "s#<powerloom-chain>#$POWERLOOM_CHAIN#" ".env-${FULL_NAMESPACE}"
sed -i".backup" "s#<source-chain>#$SOURCE_CHAIN#" ".env-${FULL_NAMESPACE}"
sed -i".backup" "s#<namespace>#${NAMESPACE}#" ".env-${FULL_NAMESPACE}"
sed -i".backup" "s#<full-namespace>#${FULL_NAMESPACE}#" ".env-${FULL_NAMESPACE}"
sed -i".backup" "s#<source-rpc-url>#$SOURCE_RPC_URL#" ".env-${FULL_NAMESPACE}"
sed -i".backup" "s#<signer-account-address>#$SIGNER_ACCOUNT_ADDRESS#" ".env-${FULL_NAMESPACE}"
sed -i".backup" "s#<signer-account-private-key>#$SIGNER_ACCOUNT_PRIVATE_KEY#" ".env-${FULL_NAMESPACE}"
sed -i".backup" "s#<slot-id>#$SLOT_ID#" ".env-${FULL_NAMESPACE}"
sed -i".backup" "s#<telegram-chat-id>#$TELEGRAM_CHAT_ID#" ".env-${FULL_NAMESPACE}"
sed -i".backup" "s#<prost-rpc-url>#$PROST_RPC_URL#" ".env-${FULL_NAMESPACE}"
sed -i".backup" "s#<prost-chain-id>#$PROST_CHAIN_ID#" ".env-${FULL_NAMESPACE}"
sed -i".backup" "s#<docker-network-name>#$DOCKER_NETWORK_NAME#" ".env-${FULL_NAMESPACE}"
echo "π’ .env-${FULL_NAMESPACE} file created successfully."
else
echo "π’ .env-${FULL_NAMESPACE} file found."
if [ "$SKIP_CREDENTIAL_UPDATE" = "true" ]; then
echo "π Skipping credential update prompts due to --skip-credential-update flag"
else
read -p "π«Έ βΆοΈ Would you like to update any of the environment variables? (y/n): " UPDATE_ENV_VARS
if [ "$UPDATE_ENV_VARS" = "y" ]; then
read -p "Enter new SIGNER_ACCOUNT_ADDRESS (press enter to skip): " SIGNER_ACCOUNT_ADDRESS
if [ ! -z "$SIGNER_ACCOUNT_ADDRESS" ]; then
read -s -p "Enter new SIGNER_ACCOUNT_PRIVATE_KEY: " SIGNER_ACCOUNT_PRIVATE_KEY
echo
sed -i".backup" "s#^SIGNER_ACCOUNT_ADDRESS=.*#SIGNER_ACCOUNT_ADDRESS=$SIGNER_ACCOUNT_ADDRESS#" ".env-${FULL_NAMESPACE}"
sed -i".backup" "s#^SIGNER_ACCOUNT_PRIVATE_KEY=.*#SIGNER_ACCOUNT_PRIVATE_KEY=$SIGNER_ACCOUNT_PRIVATE_KEY#" ".env-${FULL_NAMESPACE}"
fi
read -p "Enter new SLOT_ID (NFT_ID) (press enter to skip): " SLOT_ID
if [ ! -z "$SLOT_ID" ]; then
sed -i".backup" "s#^SLOT_ID=.*#SLOT_ID=$SLOT_ID#" ".env-${FULL_NAMESPACE}"
fi
read -p "Enter new SOURCE_RPC_URL (press enter to skip): " SOURCE_RPC_URL
if [ ! -z "$SOURCE_RPC_URL" ]; then
sed -i".backup" "s#^SOURCE_RPC_URL=.*#SOURCE_RPC_URL=$SOURCE_RPC_URL#" ".env-${FULL_NAMESPACE}"
fi
fi
fi
fi
# Source the environment file
source ".env-${FULL_NAMESPACE}"
# Subnet configuration
if [ -z "$SUBNET_THIRD_OCTET" ]; then
SUBNET_THIRD_OCTET=1
echo "π SUBNET_THIRD_OCTET not found in .env, setting to default value ${SUBNET_THIRD_OCTET}"
fi
# Check if network exists and get its subnet
NETWORK_EXISTS=$(docker network ls --format '{{.Name}}' | grep -x "$DOCKER_NETWORK_NAME" || echo "")
EXISTING_NETWORK_SUBNET=""
if [ -n "$NETWORK_EXISTS" ]; then
EXISTING_NETWORK_SUBNET=$(docker network inspect "$DOCKER_NETWORK_NAME" | grep -o '"Subnet": "[^"]*"' | cut -d'"' -f4)
fi
# Check if subnet is in use
SUBNET_IN_USE=$(docker network ls -q | xargs -I {} docker network inspect {} 2>/dev/null | grep -q "172.18.${SUBNET_THIRD_OCTET}" && echo "yes" || echo "no")
if [ "$SUBNET_IN_USE" = "yes" ] || \
([ -n "$NETWORK_EXISTS" ] && [ "$EXISTING_NETWORK_SUBNET" != "172.18.${SUBNET_THIRD_OCTET}.0/24" ]) || \
[ -z "$NETWORK_EXISTS" ]; then
echo "π‘ Warning: Subnet 172.18.${SUBNET_THIRD_OCTET}.0/24 appears to be already in use."
if [ "$DOCKER_NETWORK_PRUNE" = "true" ]; then
read -p "π«Έ βΆοΈ Would you like to prune unused Docker networks? (y/n): " PRUNE_NETWORKS
if [ "$PRUNE_NETWORKS" = "y" ]; then
docker network prune -f
SUBNET_IN_USE=$(docker network ls -q | xargs -I {} docker network inspect {} 2>/dev/null | grep -q "172.18.${SUBNET_THIRD_OCTET}" && echo "yes" || echo "no")
fi
fi
if [ "$SUBNET_IN_USE" = "yes" ]; then
echo "π‘ Subnet 172.18.${SUBNET_THIRD_OCTET}.0/24 is already in use."
echo "β³ Searching for an available subnet..."
FOUND_AVAILABLE_SUBNET=false
for i in $(seq 1 255); do
echo "Checking subnet 172.18.${i}.0/24..."
SUBNET_IN_USE=$(docker network ls -q | xargs -I {} docker network inspect {} 2>/dev/null | grep -q "172.18.${i}" && echo "yes" || echo "no")
if [ "$SUBNET_IN_USE" = "no" ]; then
FOUND_AVAILABLE_SUBNET=true
SUBNET_THIRD_OCTET=$i
break
fi
done
if [ "$FOUND_AVAILABLE_SUBNET" = "false" ]; then
echo "β No available subnets found between 172.18.1.0/24 and 172.18.255.0/24"
echo "π§ Please check your docker networks and/or prune any unused networks."
exit 1
fi
fi
fi
export DOCKER_NETWORK_SUBNET="172.18.${SUBNET_THIRD_OCTET}.0/24"
echo "Selected DOCKER_NETWORK_NAME: ${DOCKER_NETWORK_NAME}"
echo "Selected DOCKER_NETWORK_SUBNET: ${DOCKER_NETWORK_SUBNET}"
# Port configuration
if [ -z "$LOCAL_COLLECTOR_PORT" ]; then
export LOCAL_COLLECTOR_PORT=50051
echo "π LOCAL_COLLECTOR_PORT not found in .env, setting to default value ${LOCAL_COLLECTOR_PORT}"
fi
# UFW firewall configuration
if [ -x "$(command -v ufw)" ]; then
ufw delete allow $LOCAL_COLLECTOR_PORT >> /dev/null
ufw allow from $DOCKER_NETWORK_SUBNET to any port $LOCAL_COLLECTOR_PORT
if [ $? -eq 0 ]; then
echo "β
ufw allow rule added for local collector port ${LOCAL_COLLECTOR_PORT} to allow connections from ${DOCKER_NETWORK_SUBNET}."
else
echo "β ufw firewall allow rule could not be added for local collector port ${LOCAL_COLLECTOR_PORT}."
echo "Please attempt to add it manually with: sudo ufw allow from $DOCKER_NETWORK_SUBNET to any port $LOCAL_COLLECTOR_PORT"
exit 1
fi
else
echo "π‘ ufw command not found, skipping firewall rule addition for local collector port ${LOCAL_COLLECTOR_PORT}."
echo "If you are on a Linux VPS, please ensure that the port is open for connections from ${DOCKER_NETWORK_SUBNET} manually to ${LOCAL_COLLECTOR_PORT}."
fi
# Core API port configuration
if [ -z "$CORE_API_PORT" ]; then
export CORE_API_PORT=8002
echo "π CORE_API_PORT not found in .env, setting to default value ${CORE_API_PORT}"
fi
# Check if port is in use
check_port() {
if command -v lsof >/dev/null 2>&1; then
lsof -i:"$1" >/dev/null 2>&1
else
netstat -tuln | grep -q ":$1 "
fi
}
while check_port $CORE_API_PORT; do
echo "Port ${CORE_API_PORT} is already in use"
CORE_API_PORT=$((CORE_API_PORT + 1))
done
echo "βΉοΈ Using available port: ${CORE_API_PORT}"
sed -i'.backup' "s#^CORE_API_PORT=.*#CORE_API_PORT=$CORE_API_PORT#" ".env-${FULL_NAMESPACE}"
# Set default values for optional environment variables
if [ -z "$MAX_STREAM_POOL_SIZE" ]; then
export MAX_STREAM_POOL_SIZE=2
echo "π MAX_STREAM_POOL_SIZE not found in .env, setting to default value ${MAX_STREAM_POOL_SIZE}"
fi
if [ -z "$STREAM_HEALTH_CHECK_TIMEOUT_MS" ]; then
export STREAM_HEALTH_CHECK_TIMEOUT_MS=5000
echo "π STREAM_HEALTH_CHECK_TIMEOUT_MS not found in .env, setting to default value ${STREAM_HEALTH_CHECK_TIMEOUT_MS}"
fi
if [ -z "$STREAM_WRITE_TIMEOUT_MS" ]; then
export STREAM_WRITE_TIMEOUT_MS=5000
echo "π STREAM_WRITE_TIMEOUT_MS not found in .env, setting to default value ${STREAM_WRITE_TIMEOUT_MS}"
fi
if [ -z "$MAX_WRITE_RETRIES" ]; then
export MAX_WRITE_RETRIES=3
echo "π MAX_WRITE_RETRIES not found in .env, setting to default value ${MAX_WRITE_RETRIES}"
fi
if [ -z "$MAX_CONCURRENT_WRITES" ]; then
export MAX_CONCURRENT_WRITES=4
echo "π MAX_CONCURRENT_WRITES not found in .env, setting to default value ${MAX_CONCURRENT_WRITES}"
fi
# Environment validation
if [ -z "$SOURCE_RPC_URL" ]; then
echo "β RPC URL not found, please set this in your .env!"
exit 1
fi
if [ -z "$SIGNER_ACCOUNT_ADDRESS" ]; then
echo "β SIGNER_ACCOUNT_ADDRESS not found, please set this in your .env!"
exit 1
fi
if [ -z "$SIGNER_ACCOUNT_PRIVATE_KEY" ]; then
echo "β SIGNER_ACCOUNT_PRIVATE_KEY not found, please set this in your .env!"
exit 1
fi
# Export NO_COLLECTOR for use in deploy-services.sh
export NO_COLLECTOR
SETUP_COMPLETE=true
echo "β
Configuration complete. Environment file ready at .env-${FULL_NAMESPACE}"