-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstallfilenode.sh
More file actions
370 lines (301 loc) · 16.2 KB
/
installfilenode.sh
File metadata and controls
370 lines (301 loc) · 16.2 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
360
361
362
363
364
365
366
367
368
369
370
#!/bin/bash
export LC_NUMERIC=C
logging=false
#If you turn logging on, be aware your filenode.log may contain your pool access key!
set -eu -o pipefail # fail on error , debug all lines
LOG_LOCATION=/root/
if [[ $logging == true ]];
then
echo "Logging turned on"
exec > >(tee -i $LOG_LOCATION/filenode.log)
exec 2>&1
fi
# For output readability
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
COLOR_RESET=$(tput sgr0)
shopt -s globstar dotglob
# Get Server IP
function GetServerIP() {
exec 3<>/dev/tcp/ipv4.icanhazip.com/80
echo -e 'GET / HTTP/1.0\r\nhost: ipv4.icanhazip.com\r\n\r' >&3
local serverip=""
while read -r i; do
[ "$i" ] && serverip="$i"
done <&3
echo "$serverip"
}
serverip=$(GetServerIP)
# Install traceroute
sudo apt-get install -y traceroute bc
# Update and upgrade all apps
sudo apt-get update -y && sudo apt-get upgrade -y
# Get the current WAN IP
echo "Checking The IP: $serverip"
GetTracertHops() {
traceroute_output=$(traceroute $serverip)
hop_count=$(echo "$traceroute_output" | tail -n +2 | wc -l)
echo "$hop_count"
}
hops=$(GetTracertHops)
echo "########## Checking for CGNAT ##########"
if [ "$hops" -eq 1 ]; then
echo "${GREEN}You're good. CGNAT not detected${COLOR_RESET}"
else
echo "${RED}[CGNAT DETECTED ERROR] There were $hops hops detected. Possible CGNAT. Seek advice.${COLOR_RESET}"
is_cgnat_ip "$serverip"
fi
echo "########################################"
# Function to check if an IP is in the CGNAT range (100.64.0.0/10)
is_cgnat_ip() {
local ip="$1"
# Convert IP to integer
ip_int=$(printf "%u\n" $(echo "$ip" | awk -F'.' '{print ($1 * 256**3) + ($2 * 256**2) + ($3 * 256) + $4}'))
# CGNAT range: 100.64.0.0 (1681915904) to 100.127.255.255 (1686110207)
cgnat_start=1681915904 # 100.64.0.0
cgnat_end=1686110207 # 100.127.255.255
if [[ "$ip_int" -ge "$cgnat_start" && "$ip_int" -le "$cgnat_end" ]]; then
echo "Your IP $wan_ip is within a typical CGNAT range (100.64.0.0/10)."
fi
}
cat << "MENUEOF"
🌽🌽🌽🌽🌽🌽🌽🌽🌽🌽🌽
██████╗ ██████╗ ██████╗ ██╗
██╔════╝██╔═══██╗██╔══██╗██║
██║ ██║ ██║██████╔╝██║
██║ ██║ ██║██╔═══╝ ██║
╚██████╗╚██████╔╝██║ ██║
╚═════╝ ╚═════╝ ╚═╝ ╚═╝
███████╗██╗██╗ ███████╗ ███╗ ██╗ ██████╗ ██████╗ ███████╗
██╔════╝██║██║ ██╔════╝ ████╗ ██║██╔═══██╗██╔══██╗██╔════╝
█████╗ ██║██║ █████╗ ██╔██╗ ██║██║ ██║██║ ██║█████╗
██╔══╝ ██║██║ ██╔══╝ ██║╚██╗██║██║ ██║██║ ██║██╔══╝
██║ ██║███████╗███████╗ ██║ ╚████║╚██████╔╝██████╔╝███████╗
╚═╝ ╚═╝╚══════╝╚══════╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═════╝ ╚══════╝
███╗ ███╗███████╗███╗ ██╗██╗ ██╗
████╗ ████║██╔════╝████╗ ██║██║ ██║
██╔████╔██║█████╗ ██╔██╗ ██║██║ ██║
██║╚██╔╝██║██╔══╝ ██║╚██╗██║██║ ██║
██║ ╚═╝ ██║███████╗██║ ╚████║╚██████╔╝
╚═╝ ╚═╝╚══════╝╚═╝ ╚═══╝ ╚═════╝
🌽🌽🌽🌽🌽🌽🌽🌽🌽🌽🌽
MENUEOF
PS3='Please choose what type of node you are installing today. File node is only available for now. Please write the number of the menu item and press enter: '
filenode="Install a COPI File Node"
testnet="Other options in the future"
cancelit="Cancel"
options=("$filenode" "$cancelit")
asktorestart=0
select opt in "${options[@]}"
do
case $opt in
"$filenode")
action="filenode"
node_folder="filenode"
echo "You chose a file node install"
sleep 1
break
;;
"$testnet")
echo "You chose an option not live yet"
action="testnet"
sleep 1
break
;;
"$cancelit")
echo "${RED}You chose to cancel${COLOR_RESET}"
action="cancel"
exit 1
break
;;
"Quit")
exit 1
break
;;
*) echo "invalid option $REPLY";;
esac
done
echo "Welcome to the COPI File Node Installer . We will begin to ask you a series of questions. Please have to hand:"
echo "✅ Your SSH Port No (likely to be 22 if you are not sure)"
echo "✅ Your Ubuntu Username. Leave empty to use current user"
echo "✅ Your Pool Access Key From Your Copi Account Online"
echo "✅ Your WAN side Pool Port No If Changing From 8001"
echo "✅ Your LAN side Port No If Changing From 8001"
echo "✅ Think of a name to call your Docker container or leave it empty for COPINode1"
echo "💡 Note: If you need to copy and paste into terminal, you can paste by Ctrl + Shift + V or by using a right click"
read -n 1 -r -s -p $'Press enter to begin...\n'
read -p "What is your ssh port number (Leave empty or put 22 to use the standard ssh port 22)?: " portno
read -p "What is your ubuntu username (Leave it empty to just use $USER. Any typed username will be created if it does not exist) ?: " username
read -p "What is your pool access key? Please enter or paste it in now from your COPI account:" PoolAccessKey
read -p "What is WAN/Internet side pool pool port no? (Leave it empty to accept 8001 if you do not know):" PoolPortNo
read -p "What is your LAN/Internal network side pool pool port no? (Leave it empty to accept 8001 if you do not know):" LANPortNo
read -p "What do you wish to call your docker container? This is useful when you want to refer to it for commands in future. (Leave it empty and we will call it COPINode1):" ContainerName
### SET DEFAULTS FOR EMPTY FIELDS
if [[ $portno == "" ]] || [ -z "$portno" ];
then
portno="22"
fi
if [[ $username == "" ]] || [ -z "$username" ];
then
username=$USER
fi
if [[ $PoolPortNo == "" ]] || [ -z "$PoolPortNo" ];
then
PoolPortNo="8001"
fi
if [[ $LANPortNo == "" ]] || [ -z "$LANPortNo" ];
then
LANPortNo="8001"
fi
if [[ $ContainerName == "" ]] || [ -z "$ContainerName" ];
then
ContainerName="COPINode1"
fi
if [[ $portno == "" ]] || [[ $username == "" ]] || [[ $PoolAccessKey == "" ]] || [[ $PoolPortNo == "" ]];
then
echo "${RED}Some details were not provided. Script is now exiting. Please run again and provide answers to all of the questions${COLOR_RESET}"
exit 1
fi
#########################################
# Create $username user if needed
#########################################
if id "$username" >/dev/null 2>&1; then
echo "user exists"
else
echo "user does not exist...creating"
sudo adduser --gecos "" --disabled-password $username
sudo adduser $username sudo
fi
user_home=$(eval echo "~$username")
serverurl=http://$serverip:$PoolPortNo
healthurl=$serverurl/health
echo "Your health URL will be $healthurl"
sudo apt-get update -y && sudo apt-get upgrade -y
echo "Installing prereqs..."
sudo apt-get update -y && sudo apt-get upgrade -y
#Installing Docker if not found
if ! command -v docker &> /dev/null
then
echo "Docker not found, installing..."
curl -fsSL https://get.docker.com | sudo bash
else
echo "Docker is already installed."
fi
#Installing Docker Compose
if ! command -v docker-compose &> /dev/null
then
echo "Docker Compose not found, installing..."
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose && sudo chmod +x /usr/local/bin/docker-compose
else
echo "Docker Compose is already installed."
fi
docker-compose --version
ubuntuvers=$(lsb_release -rs)
echo "Ubuntu version $ubuntuvers detected"
sudo ufw limit $portno
sudo ufw allow $PoolPortNo/tcp
sudo ufw --force enable
sudo mkdir -p $user_home/$node_folder/
cacheurl="$user_home/$node_folder/cache"
sudo chown -R $username: $user_home/$node_folder/
cd $user_home/$node_folder/
logging_file_name="";
if [[ $action == "filenode" ]];
then
cat <<EOF-SETUP >$user_home/$node_folder/docker-compose.yml
name: cornucopias
services:
pool-server:
image: public.ecr.aws/cornucopias/nodes/pool-server:latest
container_name: $ContainerName
ports:
- "$LANPortNo:8001"
restart: unless-stopped # if you want to manually start the pool server replace “unless-stopped” with “no”
environment:
FILENODES_POOL_ACCESS_KEY: $PoolAccessKey
FILENODES_POOL_PUBLIC_PORT: $PoolPortNo
volumes:
- $cacheurl:/cache
EOF-SETUP
fi
echo "Applying chgrp and chown to docker compose"
sudo chown $username $user_home/$node_folder/docker-compose.yml
sudo chgrp $username $user_home/$node_folder/docker-compose.yml
cat << "DOCKEREOF"
██╗ █████╗ ██╗ ██╗███╗ ██╗ ██████╗██╗ ██╗██╗███╗ ██╗ ██████╗
██║ ██╔══██╗██║ ██║████╗ ██║██╔════╝██║ ██║██║████╗ ██║██╔════╝
██║ ███████║██║ ██║██╔██╗ ██║██║ ███████║██║██╔██╗ ██║██║ ███╗
██║ ██╔══██║██║ ██║██║╚██╗██║██║ ██╔══██║██║██║╚██╗██║██║ ██║
███████╗██║ ██║╚██████╔╝██║ ╚████║╚██████╗██║ ██║██║██║ ╚████║╚██████╔╝
╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝ ╚═════╝
██████╗ ██████╗ ██████╗██╗ ██╗███████╗██████╗
██╔══██╗██╔═══██╗██╔════╝██║ ██╔╝██╔════╝██╔══██╗
██║ ██║██║ ██║██║ █████╔╝ █████╗ ██████╔╝
██║ ██║██║ ██║██║ ██╔═██╗ ██╔══╝ ██╔══██╗
██████╔╝╚██████╔╝╚██████╗██║ ██╗███████╗██║ ██║
╚═════╝ ╚═════╝ ╚═════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
██████╗ ██████╗ ███╗ ███╗██████╗ ██████╗ ███████╗███████╗ ██╗ ██╗██████╗
██╔════╝██╔═══██╗████╗ ████║██╔══██╗██╔═══██╗██╔════╝██╔════╝ ██║ ██║██╔══██╗
██║ ██║ ██║██╔████╔██║██████╔╝██║ ██║███████╗█████╗ ██║ ██║██████╔╝
██║ ██║ ██║██║╚██╔╝██║██╔═══╝ ██║ ██║╚════██║██╔══╝ ██║ ██║██╔═══╝
╚██████╗╚██████╔╝██║ ╚═╝ ██║██║ ╚██████╔╝███████║███████╗ ╚██████╔╝██║
╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═════╝ ╚══════╝╚══════╝ ╚═════╝ ╚═╝
DOCKEREOF
read -n 1 -r -s -p $'Press enter to launch docker compose up -d”...\n'
cd $user_home/$node_folder/
sudo docker compose up -d
echo "Please be patient ... 🌽🌽🌽"
sleep 15
previous_size_bytes=0
previous_time=$(date +%s)
TARGET_SIZE_GB=34.28 # Hardcoded last known size
previous_size_bytes=0
previous_time=$(date +%s)
# This is quite a poor way of monitoring the progress, we will read the docker logs for the next version.
while true; do
current_time=$(date +%s)
file_count=$(find cache -type f | wc -l)
total_size_bytes=$(du -sb cache | awk '{print $1}')
total_size_gb=$(echo "scale=2; $total_size_bytes / 1024 / 1024 / 1024" | bc)
# Calculate progress percentage
if (( $(echo "$total_size_gb >= $TARGET_SIZE_GB" | bc -l) )); then
progress=100
else
progress=$(echo "scale=2; ($total_size_gb / $TARGET_SIZE_GB) * 100" | bc)
fi
# Check if the total size has reached the target
if (( $(echo "$total_size_gb >= $TARGET_SIZE_GB" | bc -l) )); then
echo "✅ Target size reached: $total_size_gb GB."
break # Exit the loop if the size is reached
fi
# Calculate download speed (MB/s)
size_difference=$((total_size_bytes - previous_size_bytes))
time_difference=$((current_time - previous_time))
if [ "$time_difference" -gt 0 ]; then
speed_mb=$(echo "scale=2; $size_difference / 1024 / 1024 / $time_difference" | bc)
else
speed_mb=0
fi
# Display progress and speed
if (( $(echo "$progress >= 100" | bc -l) )); then
echo "$(date): Files in cache: $file_count, Total size: ${total_size_gb}GB [100%] Please wait... | Speed: ${speed_mb} MB/s"
else
printf "$(date): Files in cache: $file_count, Total size: ${total_size_gb}GB [%.2f%%] | Speed: %.2f MB/s\n" "$progress" "$speed_mb"
fi
# Store current values for next iteration
previous_size_bytes=$total_size_bytes
previous_time=$current_time
sleep 15
done
# Get health from check from afar
healthResponse=$(curl -s --interface "$(curl -s ifconfig.me)" "$healthurl")
if [[ $healthResponse == "Ok" ]]; then
echo "${GREEN}Node is OK! You're done! Now why not download uptimerobot app and let it watch KEYWORD Ok at $healthurl so you can be notified when it is offline or failing. If you have 8 or more nodes and wish to attract delegators to your node, be sure to make your node public in your account by changing the private drop down to public.${COLOR_RESET}"
echo "${GREEN}Also, please note it may take a couple of days for your node to show on the public pools page once you make it public.${COLOR_RESET}"
isOK="true"
else
echo "${YELLOW}Checking $IP....Node is currently showing:$healthResponse${COLOR_RESET}"
isOK="false"
fi
echo "End of script. Done"