-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevops.sh
executable file
·740 lines (667 loc) · 20.7 KB
/
devops.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
#!/bin/bash -e
if [ -f "$(dirname "$0")/.env" ]; then
source "$(dirname "$0")/.env"
fi
NAME=""
IP=""
USERNAME=""
PRIVATE_KEY_PATH=""
HOSTS=()
PREV_LINES=0
erase_lines() {
for ((i=0; i<$PREV_LINES; i++)); do
echo -en "\033[1A\033[K"
done
PREV_LINES=0
}
read_hosts() {
HOSTS=()
while IFS= read -r line; do
if [[ $line == Host* ]]; then
host=$(echo $line | cut -d " " -f 2)
HOSTS+=("$host:") # Add colon as a placeholder for IP
elif [[ $line =~ ^[[:space:]]*HostName ]]; then
hostname=$(echo $line | awk '{print $NF}')
# Update the last element with the hostname
last_index=$((${#HOSTS[@]} - 1))
HOSTS[$last_index]="${HOSTS[$last_index]}$hostname"
fi
done < ~/.ssh/config
HOSTS+=("+ Add a new host:")
}
read_host() {
PRIVATE_KEY_PATH=''
USERNAME=`whoami`
in_current=false
while IFS= read -r line; do
if [[ $line == Host* ]]; then
host=$(echo $line | cut -d " " -f 2)
if [ "$host" == "$NAME" ]; then
in_current=true
else
in_current=false
fi
elif [[ $line =~ ^[[:space:]]*User ]]; then
hostname=$(echo $line | awk '{print $NF}')
if [ "$in_current" = true ]; then
USERNAME=$hostname
fi
elif [[ $line =~ ^[[:space:]]*IdentityFile ]]; then
filepath=$(echo $line | awk '{print $NF}')
if [ "$in_current" = true ]; then
PRIVATE_KEY_PATH=$filepath
fi
fi
done < ~/.ssh/config
}
choose_host() {
read_hosts
local selected=0
local key=""
while true; do
erase_lines
echo -e "\n\033[0;31mjole's devops script\033[0m"
echo -e "\033[0;33m\nSelect host:\033[0m\n"
for i in "${!HOSTS[@]}"; do
IFS=':' read -r host ip <<< "${HOSTS[$i]}"
if [ $i -eq $selected ]; then
printf "\033[0;34m> %-30s %s\033[0m\n" "$host" "$ip"
else
printf " %-30s %s\n" "$host" "$ip"
fi
done
echo
PREV_LINES=$(( ${#HOSTS[@]} + 6 ))
read -rsn1 key
case "$key" in
A)
# Up arrow
((selected--))
if [ $selected -lt 0 ]; then
selected=$((${#HOSTS[@]} - 1))
fi
;;
B)
# Down arrow
((selected++))
if [ $selected -ge ${#HOSTS[@]} ]; then
selected=0
fi
;;
q)
break
;;
"")
# Enter key
IFS=':' read -r NAME IP <<< "${HOSTS[$selected]}"
if [[ "${NAME}" = "+ Add a new host" ]]; then
erase_lines
add_to_local_config
read_hosts
else
read_host
display_menu
fi
;;
esac
done
}
display_menu() {
local options=("Connect..."
"Check status"
"Basic setup"
"Install Docker"
"Install Netdata agent"
"Uninstall Netdata agent"
"Install BetterStack (Vector/Docker)"
"Open port..."
"Test ports"
"Add SSH key..."
"Upgrade apt"
"Back")
local selected=0
local key=""
while true; do
host_title
echo -e "\033[0;33mSelect action:\033[0m\n"
for i in "${!options[@]}"; do
if [ $i -eq $selected ]; then
echo -e "\033[0;34m> ${options[$i]}\033[0m"
else
echo " ${options[$i]}"
fi
done
echo
PREV_LINES=$(( $PREV_LINES + 15 ))
read -rsn1 key
case "$key" in
A)
# Up arrow
((selected--))
if [ $selected -lt 0 ]; then
selected=$((${#options[@]} - 1))
fi
;;
B)
# Down arrow
((selected++))
if [ $selected -ge ${#options[@]} ]; then
selected=0
fi
;;
q)
break
;;
"")
# Enter key
case $selected in
0) host_title; display_menu_ssh;;
1) host_title; check_status; finish;;
2) host_title; basic_setup; finish;;
3) host_title; install_docker; finish;;
4) host_title; install_netdata; finish;;
5) host_title; uninstall_netdata; finish;;
6) host_title; install_betterstack; finish;;
7) host_title; display_menu_ports;;
8) host_title; test_ports;;
9) host_title; display_menu_ssh_key;;
10) host_title; upgrade_apt; finish;;
11) break;;
esac
;;
esac
done
}
finish() {
echo -e "\n\033[0;33mPress any key to continue\033[0m"
read -n 1 -s
PREV_LINES=0
}
host_title() {
erase_lines
line_name="$NAME"
line_hostname=""
if [ -n "$IP" ]; then
line_hostname="Hostname: $IP"
fi
line_user="User: $USERNAME"
line_identity=""
if [ -n "$PRIVATE_KEY_PATH" ]; then
line_identity="Identity: $PRIVATE_KEY_PATH"
fi
# get length of each line and find max
max_length=0
for line in "$line_name" "$line_hostname" "$line_user" "$line_identity"; do
if [ ${#line} -gt $max_length ]; then
max_length=${#line}
fi
done
# make each line the same length
line_name=$(printf '%-*s' $max_length "$line_name")
line_hostname=$(printf '%-*s' $max_length "$line_hostname")
line_user=$(printf '%-*s' $max_length "$line_user")
line_identity=$(printf '%-*s' $max_length "$line_identity")
# create line of dashes
dashes=$(printf '%*s' $max_length '')
dashes=${dashes// /-}
echo -e "\n\033[0;31m| $line_name |\033[0m"
echo -e "\033[0;31m|-$dashes-|\033[0m"
if [ -n "$IP" ]; then
echo -e "\033[0;31m| $line_hostname |\033[0m"
PREV_LINES=$(( $PREV_LINES + 1 ))
fi
echo -e "\033[0;31m| $line_user |\033[0m"
if [ -n "$PRIVATE_KEY_PATH" ]; then
echo -e "\033[0;31m| $line_identity |\033[0m"
PREV_LINES=$(( $PREV_LINES + 1 ))
fi
echo
PREV_LINES=$(( $PREV_LINES + 5 ))
}
add_to_local_config() {
echo -en "\033[0;33mName (*): \033[0m"
read NAME
echo -en "\033[0;33mHostname: \033[0m"
read IP
echo -en "\033[0;33mUser (*): \033[0m"
read USERNAME
PRIVATE_KEY_PATH=""
echo -en "\033[0;33mPrivate key: \033[0m"
read PRIVATE_KEY_PATH
local lines=2
if [ -n "$PRIVATE_KEY_PATH" ]; then
if [ ! -f $(eval echo "$PRIVATE_KEY_PATH") ]; then
echo -e "\033[0;31mError: File not found at $PRIVATE_KEY_PATH\033[0m"
NAME=""
IP=""
finish
return
fi
if ! ssh-keygen -l -f $(eval echo "$PRIVATE_KEY_PATH") &>/dev/null; then
echo -e "\033[0;31mError: Invalid SSH private key file\033[0m"
NAME=""
IP=""
finish
return
fi
fi
echo "Host $NAME" >> ~/.ssh/config
if [ -n "$IP" ]; then
echo " HostName $IP" >> ~/.ssh/config
lines+=1
fi
echo " User $USERNAME" >> ~/.ssh/config
if [ -n "$PRIVATE_KEY_PATH" ]; then
echo " IdentityFile $PRIVATE_KEY_PATH" >> ~/.ssh/config
lines+=1
fi
echo -e "\033[0;32m$(tail -n$lines ~/.ssh/config)\033[0m"
finish
display_menu
}
check_status() {
# SSH
echo -e "\033[0;33m⏱ SSH root\033[0m"
if ssh -o StrictHostKeyChecking=accept-new -o BatchMode=yes -o ConnectTimeout=5 root@$NAME "exit" &> /dev/null; then
echo -en "\033[1A\033[K"
echo -e "\033[0;32m✔ SSH root\033[0m"
else
echo -en "\033[1A\033[K"
echo -e "\033[0;31m✘ SSH root\033[0m"
return
fi
echo -e "\033[0;33m⏱ SSH $USERNAME\033[0m"
if ssh -o BatchMode=yes -o ConnectTimeout=5 $NAME "exit" &> /dev/null; then
echo -en "\033[1A\033[K"
echo -e "\033[0;32m✔ SSH $USERNAME\033[0m"
else
echo -en "\033[1A\033[K"
echo -e "\033[0;31m✘ SSH $USERNAME\033[0m"
fi
# UFW
echo -e "\033[0;33m⏱ UFW\033[0m"
ufw_output=$(ssh root@$NAME "ufw status verbose")
if echo "$ufw_output" | grep -q "^Status: active"; then
echo -en "\033[1A\033[K"
echo -en "\033[0;32m✔ UFW\033[0m"
if echo "$ufw_output" | grep -q "^Default: deny (incoming)"; then
echo -e "\033[0;32m ✔ Default: deny incoming\033[0m"
else
echo -e "\033[0;31m ✘ Default: deny incoming\033[0m"
fi
echo "$ufw_output" | sed '1,/^-- /d'
else
echo -en "\033[1A\033[K"
echo -e "\033[0;31m✘ UFW\033[0m"
fi
# Docker
echo -e "\033[0;33m⏱ Docker\033[0m"
docker_output=$(ssh root@$NAME "docker -v" 2>/dev/null) || docker_output=""
if echo "$docker_output" | grep -q "Docker version"; then
echo -en "\033[1A\033[K"
echo -e "\033[0;32m✔ Docker\033[0m"
echo -e "\033[0;33m⏱ ufw-docker\033[0m"
ufwdocker_output=$(ssh root@$NAME "ufw-docker check" 2>/dev/null) || ufwdocker_output=""
if [ -n "$ufwdocker_output" ]; then
echo -en "\033[1A\033[K"
echo -e "\033[0;32m✔ ufw-docker\033[0m"
else
echo -en "\033[1A\033[K"
echo -e "\033[0;31m✘ ufw-docker\033[0m"
fi
else
echo -en "\033[1A\033[K"
echo -e "\033[0;31m✘ Docker\033[0m"
fi
# Netdata
echo -e "\033[0;33m⏱ Netdata\033[0m"
netdata_output=$(ssh root@$NAME "systemctl status netdata" 2>/dev/null) || netdata_output=""
if echo "$netdata_output" | grep -q "Active: active"; then
echo -en "\033[1A\033[K"
echo -e "\033[0;32m✔ Netdata\033[0m"
else
echo -en "\033[1A\033[K"
echo -e "\033[0;31m✘ Netdata\033[0m"
fi
}
basic_setup() {
# Setup UFW: Deny everything by default, allow SSH
echo -e "\033[0;34m\n>>> UFW <<<\n\033[0m"
ssh -o StrictHostKeyChecking=accept-new root@$NAME "ufw allow OpenSSH && \
ufw default deny incoming && \
ufw --force enable && \
ufw status verbose"
# Create user "app" without password, and add authorized keys
echo -e "\033[0;34m>>> CREATE USER $USERNAME <<<\n\033[0m"
ssh root@$NAME "id -u $USERNAME >/dev/null 2>&1 || adduser --gecos '' --disabled-password $USERNAME && \
su $USERNAME -c 'mkdir ~/.ssh; chmod 700 ~/.ssh; touch ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys'"
if [ -z "$PRIVATE_KEY_PATH" ]; then
PRIVATE_KEY_PATH=~/.ssh/id_rsa
fi
scp $(eval echo "$PRIVATE_KEY_PATH").pub root@$NAME:/home/$USERNAME/.ssh/authorized_keys
ssh root@$NAME "chown $USERNAME:$USERNAME /home/$USERNAME/.ssh/authorized_keys && \
su $USERNAME -c 'chmod 600 ~/.ssh/authorized_keys'"
ssh $NAME "echo 'SSH connection successful'"
# Allow low ports for app user, because Docker will run there and might need them
echo -e "\033[0;34m\n>>> ALLOW LOW PORTS FOR USERS <<<\n\033[0m"
ssh root@$NAME "echo 'net.ipv4.ip_unprivileged_port_start=80' >> /etc/sysctl.conf && \
sysctl -p"
}
install_docker() {
# Install Docker
echo -e "\033[0;34m\n>>> DOCKER <<<\n\033[0m"
ssh root@$NAME "DEBIAN_FRONTEND=noninteractive apt-get update -qq && \
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq docker.io > /dev/null && \
usermod -a -G docker $USERNAME"
ssh $NAME "docker -v"
# Install UFW-Docker and allow in on br-+ (docker bridge interfaces created by Kamal)
ssh root@$NAME "wget -O /usr/local/bin/ufw-docker https://raw.githubusercontent.com/chaifeng/ufw-docker/17e6047590e14d3ff1dc6c01f0b4755d115fc078/ufw-docker && \
chmod +x /usr/local/bin/ufw-docker && \
ufw-docker install && \
ufw allow in on br-+ && \
systemctl restart ufw"
}
install_netdata() {
if [ -n "$NETDATA_DESTINATION" ] && [ -n "$NETDATA_API_KEY" ]; then
echo -en "\033[0;33mNetdata destination (\033[0;37m$NETDATA_DESTINATION\033[0;33m): \033[0m"
read NETDATA_DESTINATION_INPUT
NETDATA_DESTINATION=${NETDATA_DESTINATION_INPUT:-$NETDATA_DESTINATION}
echo -en "\033[0;33mNetdata api key (\033[0;37m$NETDATA_API_KEY\033[0;33m): \033[0m"
read NETDATA_API_KEY_INPUT
NETDATA_API_KEY=${NETDATA_API_KEY_INPUT:-$NETDATA_API_KEY}
else
echo -en "\033[0;33mNetdata destination: \033[0m"
read NETDATA_DESTINATION
echo -en "\033[0;33mNetdata api key: \033[0m"
read NETDATA_API_KEY
fi
ssh root@$NAME "curl https://get.netdata.cloud/kickstart.sh > /tmp/netdata-kickstart.sh && \
DEBIAN_FRONTEND=noninteractive sh /tmp/netdata-kickstart.sh --stable-channel && \
cd /etc/netdata 2>/dev/null || cd /opt/netdata/etc/netdata && \
echo -e '[stream]\n enabled = yes\n destination = $NETDATA_DESTINATION:19999\n api key = $NETDATA_API_KEY' > stream.conf && \
cat stream.conf && \
systemctl restart netdata"
echo -e "NETDATA_DESTINATION=$NETDATA_DESTINATION\nNETDATA_API_KEY=$NETDATA_API_KEY\nBETTERSTACK_KEY=$BETTERSTACK_KEY" > "$(dirname "$0")/.env"
}
uninstall_netdata() {
echo -e "\033[0;34m>>> UNINSTALLING NETDATA <<<\n\033[0m"
ssh root@$NAME "systemctl stop netdata || true && \
systemctl disable netdata || true && \
apt-get remove -y --autoremove netdata && \
rm -rf /etc/netdata /var/lib/netdata /var/cache/netdata /var/log/netdata /opt/netdata && \
userdel netdata 2>/dev/null || true && \
find / -name '*netdata*' -type d -exec rm -rf {} + 2>/dev/null || true && \
dpkg-statoverride --list | grep netdata | awk '{print \$4}' | xargs -I {} dpkg-statoverride --remove {}"
}
install_betterstack() {
if [ -n "$BETTERSTACK_KEY" ]; then
echo -en "\033[0;33mBetterStack key (\033[0;37m$BETTERSTACK_KEY\033[0;33m): \033[0m"
read BETTERSTACK_KEY_INPUT
BETTERSTACK_KEY=${BETTERSTACK_KEY_INPUT:-$BETTERSTACK_KEY}
else
echo -en "\033[0;33mBetterStack key: \033[0m"
read BETTERSTACK_KEY
fi
ssh root@$NAME "curl -sSL https://telemetry.betterstack.com/setup-vector/docker/$BETTERSTACK_KEY -o /tmp/setup-vector.sh && \
echo \n | bash /tmp/setup-vector.sh && \
usermod -a -G docker vector || true && \
systemctl restart vector"
echo -e "REBOOT_REQUIRED=\$([ -f /var/run/reboot-required ] && echo 'REBOOT REQUIRED' || (UPDATES=\$(apt-get upgrade -s | grep '^Inst' | cut -d' ' -f2); [ -z "\$UPDATES" ] && echo 'UP TO DATE' || echo 'UPDATES AVAILABLE'))\ncurl -X POST https://in.logs.betterstack.com -H 'Authorization: Bearer $BETTERSTACK_KEY' -H 'Content-Type: application/json' -d \"{\\\"message\\\":\\\"\$REBOOT_REQUIRED\\\",\\\"updates_required\\\":\\\"\$REBOOT_REQUIRED\\\",\\\"docker\\\":{\\\"host\\\":\\\"\$(hostname)\\\"}}\"" > "$(dirname "$0")/reboot_required.sh"
scp "$(dirname "$0")/reboot_required.sh" root@$NAME:/root/reboot_required.sh
rm "$(dirname "$0")/reboot_required.sh"
ssh root@$NAME "chmod +x /root/reboot_required.sh && \
/root/reboot_required.sh && \
(crontab -l 2>/dev/null; echo '0 0 * * 0 /root/reboot_required.sh') | crontab -"
echo -e "NETDATA_DESTINATION=$NETDATA_DESTINATION\nNETDATA_API_KEY=$NETDATA_API_KEY\nBETTERSTACK_KEY=$BETTERSTACK_KEY" > "$(dirname "$0")/.env"
}
test_ports() {
local ports=(22 80 443 3000 5432 6379 19999)
local ip_to_test=$IP
if [ -z "$ip_to_test" ]; then
ip_to_test=$NAME
fi
for port in "${ports[@]}"; do
nc -zG2 $ip_to_test $port &> /dev/null && echo -e "\033[0;32m$port: open\033[0m" || echo -e "\033[0;31m$port: closed\033[0m"
done
while true; do
echo -en "\n\033[0;33mOther port (or press Enter to finish): \033[0m"
read additional_port
if [ -z "$additional_port" ]; then
break
fi
nc -zG2 $IP $additional_port &> /dev/null && echo -e "\033[0;32m$additional_port: open\033[0m" || echo -e "\033[0;31m$additional_port: closed\033[0m"
done
PREV_LINES=0
}
display_menu_ports() {
local options=("On host" "On container" "Back")
local selected=0
local key=""
while true; do
host_title
echo -e "\033[0;33mSelect action:\033[0m\n"
for i in "${!options[@]}"; do
if [ $i -eq $selected ]; then
echo -e "\033[0;34m> ${options[$i]}\033[0m"
else
echo " ${options[$i]}"
fi
done
echo
PREV_LINES=$(( $PREV_LINES + 6 ))
read -rsn1 key
case "$key" in
A)
# Up arrow
((selected--))
if [ $selected -lt 0 ]; then
selected=$((${#options[@]} - 1))
fi
;;
B)
# Down arrow
((selected++))
if [ $selected -ge ${#options[@]} ]; then
selected=0
fi
;;
q)
break
;;
"")
# Enter key
case $selected in
0) host_title; open_host_port; finish;;
1) host_title; open_container_port; finish;;
2) break;;
esac
;;
esac
done
}
open_host_port() {
echo -en "\033[0;33mPort: \033[0m"
read port
ssh root@$NAME "ufw allow $port && ufw status"
}
open_container_port() {
echo -e "\033[0;33m⏱ Listing containers...\033[0m"
local options=()
while IFS= read -r line; do
options+=("$line")
done < <(ssh $NAME "docker ps --format '{{.Names}}'")
local selected=0
local key=""
while true; do
host_title
echo -e "\033[0;33mSelect container:\033[0m\n"
for i in "${!options[@]}"; do
if [ $i -eq $selected ]; then
echo -e "\033[0;34m> ${options[$i]}\033[0m"
else
echo " ${options[$i]}"
fi
done
echo
read -rsn1 key
case "$key" in
A)
# Up arrow
((selected--))
if [ $selected -lt 0 ]; then
selected=$((${#options[@]} - 1))
fi
;;
B)
# Down arrow
((selected++))
if [ $selected -ge ${#options[@]} ]; then
selected=0
fi
;;
"")
# Enter key
container="${options[$selected]}"
echo -en "\033[0;33mPort: \033[0m"
read port
ssh root@$NAME "ufw-docker allow $container $port/tcp && ufw status"
break
;;
esac
done
}
display_menu_ssh(){
local options=("$USERNAME" "root" "Back")
local selected=0
local key=""
while true; do
host_title
echo -e "\033[0;33mSelect user:\033[0m\n"
for i in "${!options[@]}"; do
if [ $i -eq $selected ]; then
echo -e "\033[0;34m> ${options[$i]}\033[0m"
else
echo " ${options[$i]}"
fi
done
echo
PREV_LINES=$(( $PREV_LINES + 6 ))
read -rsn1 key
case "$key" in
A)
# Up arrow
((selected--))
if [ $selected -lt 0 ]; then
selected=$((${#options[@]} - 1))
fi
;;
B)
# Down arrow
((selected++))
if [ $selected -ge ${#options[@]} ]; then
selected=0
fi
;;
q)
break
;;
"")
# Enter key
case $selected in
0) host_title; ssh $NAME; break;;
1) host_title; ssh root@$NAME; break;;
2) break;;
esac
;;
esac
done
}
display_menu_ssh_key(){
local options=("$USERNAME" "root" "Back")
local selected=0
local key=""
while true; do
host_title
echo -e "\033[0;33mSelect user:\033[0m\n"
for i in "${!options[@]}"; do
if [ $i -eq $selected ]; then
echo -e "\033[0;34m> ${options[$i]}\033[0m"
else
echo " ${options[$i]}"
fi
done
echo
PREV_LINES=$(( $PREV_LINES + 6 ))
read -rsn1 key
case "$key" in
A)
# Up arrow
((selected--))
if [ $selected -lt 0 ]; then
selected=$((${#options[@]} - 1))
fi
;;
B)
# Down arrow
((selected++))
if [ $selected -ge ${#options[@]} ]; then
selected=0
fi
;;
q)
break
;;
"")
# Enter key
if [ $selected -eq 2 ]; then
break
fi
echo -en "\033[0;33mPublic key path: \033[0m"
read public_key_path
if [ ! -f "$public_key_path" ]; then
echo -e "\033[0;31mError: File not found at $public_key_path\033[0m"
finish
break
fi
if ! ssh-keygen -l -f "$public_key_path" &>/dev/null; then
echo -e "\033[0;31mError: Invalid SSH public key file\033[0m"
finish
break
fi
case $selected in
0) host_title; ssh-copy-id -i "$public_key_path" $USERNAME@$NAME && ssh -q -i "$public_key_path" $NAME "echo 'SSH connection successful'"; finish; break;;
1) host_title; ssh-copy-id -i "$public_key_path" root@$NAME && ssh -q -i "$public_key_path" root@$NAME "echo 'SSH connection successful'"; finish; break;;
esac
;;
esac
done
}
upgrade_apt() {
echo -e "\033[0;34mRunning containers:\033[0m"
local CONTAINERS=$(ssh -o StrictHostKeyChecking=accept-new root@$NAME "docker ps --format '{{.Names}}'")
echo "$CONTAINERS"
echo -e "\033[0;34mUpgrading...\033[0m"
ssh root@$NAME "DEBIAN_FRONTEND=noninteractive apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get upgrade -q -y"
if ssh root@$NAME "[ -f /var/run/reboot-required ] && reboot"; then
echo -e "\033[0;34mWaiting for server to reboot...\033[0m"
sleep 5
local OK=false
while ! $OK; do
sleep 5
ssh root@$NAME "echo 'OK'" && OK=true
done
else
echo "No reboot needed"
fi
echo -e "\033[0;34mRunning containers after upgrade:\033[0m"
local NEW_CONTAINERS=$(ssh root@$NAME "docker ps --format '{{.Names}}'")
echo "$NEW_CONTAINERS"
if [ "$NEW_CONTAINERS" != "$CONTAINERS" ]; then
echo -e "\033[0;31mWarning: Container list changed after reboot!\033[0m"
echo -e "\033[0;31mBefore:\n$CONTAINERS\033[0m"
echo -e "\033[0;31mAfter:\n$NEW_CONTAINERS\033[0m"
else
echo -e "\n\033[0;32mSuccess\033[0m"
fi
}
choose_host