-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathGUI.sh
More file actions
executable file
·1806 lines (1587 loc) · 60 KB
/
Copy pathGUI.sh
File metadata and controls
executable file
·1806 lines (1587 loc) · 60 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
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# GUI.sh
#
# Interactive menu-driven interface for navigating and executing ProxmoxScripts.
# Supports both local execution and remote execution across multiple nodes.
#
# Usage:
# GUI.sh
# GUI.sh -c
# GUI.sh --clear-logs
# GUI.sh -d
# GUI.sh --debug
# GUI.sh -q
# GUI.sh --quiet
# GUI.sh -h
#
# Options:
# -c, --clear-logs Clear all logs before starting
# -d, --debug Remote scripts show DEBUG level output (very verbose)
# -v, --verbose Remote scripts show INFO level output (default)
# -q, --quiet Remote scripts show only ERROR level output
# -h, --help Show help message
#
# Log Levels:
# DEBUG - All messages including trace and debug information
# INFO - Informational messages, warnings, and errors (default)
# WARN - Warnings and errors only
# ERROR - Errors only
#
# Note: All output is always saved to log files regardless of display level
#
# Requirements:
# - UTILITYPATH must be set (automatically done when running from repository root)
# - For remote execution: sshpass, jq, nodes.json file
#
# Function Index:
# - error_handler
# - show_ascii_art
# - show_top_comments
# - show_script_usage
# - display_path
# - show_common_footer
# - process_common_input
# - __check_remote_dependencies__
# - select_execution_mode
# - configure_single_remote
# - configure_multi_remote
# - configure_multi_saved
# - configure_multi_ip_range
# - configure_multi_vmid_range
# - manage_nodes_menu
# - run_script
# - run_script_local
# - run_script_remote
# - settings_menu
# - update_scripts
# - change_branch
# - view_branches
# - navigate
# - __startup_check__
#
set -euo pipefail
###############################################################################
# ARGUMENT PARSING
###############################################################################
# Parse command-line arguments
CLEAR_LOGS=false
REMOTE_LOG_LEVEL="INFO"
while [[ $# -gt 0 ]]; do
case $1 in
-c | --clear-logs)
CLEAR_LOGS=true
shift
;;
-d | --debug)
REMOTE_LOG_LEVEL="DEBUG"
shift
;;
-v | --verbose)
REMOTE_LOG_LEVEL="INFO"
shift
;;
-q | --quiet)
REMOTE_LOG_LEVEL="ERROR"
shift
;;
--manual)
shift
if [[ -z "${1:-}" ]]; then
echo "Error: --manual requires manual name" >&2
echo "Examples:" >&2
echo " ./GUI.sh --manual getting-started" >&2
echo " ./GUI.sh --manual gui-overview" >&2
exit 1
fi
# Source early to use manual viewer
UTILITYPATH="./Utilities"
source "${UTILITYPATH}/ManualViewer.sh"
__show_manual__ "$1"
exit 0
;;
-h | --help)
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -c, --clear-logs Clear all logs before starting"
echo " -d, --debug Remote scripts show DEBUG level output (very verbose)"
echo " -v, --verbose Remote scripts show INFO level output (default)"
echo " -q, --quiet Remote scripts show only ERROR level output"
echo " --manual <name> Show specific manual"
echo " -h, --help Show this help message"
echo ""
echo "Log Levels:"
echo " DEBUG - All messages including trace and debug information"
echo " INFO - Informational messages, warnings, and errors (default)"
echo " WARN - Warnings and errors only"
echo " ERROR - Errors only"
echo ""
echo "Manuals:"
echo " --manual getting-started Quick start guide"
echo " --manual gui-overview Complete GUI documentation"
echo " --manual execution-modes Execution mode details"
echo " --manual node-management Node configuration guide"
echo " --manual troubleshooting Common issues & solutions"
echo ""
echo " Or press 'h' or '?' from any menu for interactive manual browser"
echo ""
echo "Note: All output is always saved to log files regardless of level"
exit 0
;;
*)
# Ignore unknown args for now
shift
;;
esac
done
# Export REMOTE_LOG_LEVEL so it's available to sourced scripts
export REMOTE_LOG_LEVEL
# Debug: Print the log level (can be removed later)
echo "Debug: REMOTE_LOG_LEVEL is set to: $REMOTE_LOG_LEVEL" >&2
# Clear logs if requested
if [[ "$CLEAR_LOGS" == "true" ]]; then
echo "Clearing logs..."
rm -f /tmp/gui_execution.log /tmp/gui_debug.log /tmp/proxmox_scripts.log
echo "Logs cleared"
sleep 1
fi
###############################################################################
# ERROR HANDLING
###############################################################################
# Global error handler
error_handler() {
__show_error__ "${1:-${LINENO}}" "${2:-$BASH_COMMAND}" "${3:-$?}" "GUI.sh"
echo "Press Enter to continue or Ctrl+C to exit..."
read -r
}
# Set trap for ERR
trap 'error_handler ${LINENO} "$BASH_COMMAND" $?' ERR
###############################################################################
# CONFIG
###############################################################################
BASE_DIR="$(pwd)" # We assume the script is run from the unzipped directory
DISPLAY_PREFIX="cc_pve" # How we display the "root" in the UI
HELP_FLAG="--help" # If your scripts support a help flag, we pass this
LAST_SCRIPT="" # The last script run
LAST_OUTPUT="" # Truncated output of the last script
SHOW_HEADER="false"
CURRENT_BRANCH="main" # Default branch
BRANCH_FILE="$BASE_DIR/.current_branch"
# Configuration managed by ConfigManager.sh utility
###############################################################################
# IMPORT UTILITY FUNCTIONS FOR SCRIPTS AND COLOR GRADIENT LIBRARY
###############################################################################
UTILITYPATH="./Utilities"
source "${UTILITYPATH}/Colors.sh"
source "${UTILITYPATH}/Communication.sh"
source "${UTILITYPATH}/ManualViewer.sh"
source "${UTILITYPATH}/Display.sh"
source "${UTILITYPATH}/ConfigManager.sh"
source "${UTILITYPATH}/RemoteExecutor.sh"
# Initialize configuration
__init_config__
# Load current branch if file exists
if [[ -f "$BRANCH_FILE" ]]; then
CURRENT_BRANCH=$(cat "$BRANCH_FILE")
fi
# Configure logging for GUI
export LOG_FILE="/tmp/gui_execution.log"
export LOG_LEVEL="DEBUG"
export LOG_CONSOLE=0 # Don't spam console, just log to file
###############################################################################
# HEADER MANAGEMENT
###############################################################################
while [[ $# -gt 0 ]]; do
case "$1" in
-h)
SHOW_HEADER="true"
shift
;;
*)
echo "Error: Unknown argument '$1'"
exit 1
;;
esac
done
###############################################################################
# ASCII ART HEADER (uses Display.sh utility)
###############################################################################
show_ascii_art() {
if [[ "$SHOW_HEADER" == "true" ]]; then
__show_ascii_art__ auto
else
__show_ascii_art__ basic
fi
echo
__line_rgb__ "EXECUTION: $EXECUTION_MODE_DISPLAY" 255 200 0
__line_rgb__ "TARGET: $TARGET_DISPLAY" 0 255 200
if [[ "$EXECUTION_MODE" != "local" ]]; then
local log_level_color
case "$REMOTE_LOG_LEVEL" in
DEBUG) log_level_color="100 100 255" ;;
INFO) log_level_color="0 255 0" ;;
WARN) log_level_color="255 200 0" ;;
ERROR) log_level_color="255 100 100" ;;
*) log_level_color="150 150 150" ;;
esac
__line_rgb__ "LOG LEVEL: $REMOTE_LOG_LEVEL" $log_level_color
fi
# Show branch if not main
if [[ "$CURRENT_BRANCH" != "main" ]]; then
__line_rgb__ "BRANCH: $CURRENT_BRANCH" 255 200 100
fi
echo
}
###############################################################################
# UTILITY FUNCTIONS
###############################################################################
show_top_comments() {
local script_path="$1"
clear
show_ascii_art
__show_script_info__ "$script_path" "$(display_path "$script_path")"
__pause__
}
show_script_usage() {
local script_path="$1"
__line_rgb__ "=== Showing usage for: $(display_path "$script_path") ===" 200 200 0
if [[ -x "$script_path" ]]; then
"$script_path" "$HELP_FLAG" 2>&1 || true
else
bash "$script_path" "$HELP_FLAG" 2>&1 || true
fi
echo
__pause__
}
display_path() {
__display_path__ "$1" "$BASE_DIR" "$DISPLAY_PREFIX"
}
###############################################################################
# POLYMORPHIC MENU SYSTEM
###############################################################################
# Show common footer options
# Usage: show_common_footer [back_option] [exit_option]
# back_option: "b" (default) or custom text for back option
# exit_option: "e" (default) or "none" to hide exit option
show_common_footer() {
local back_option="${1:-b}"
local exit_option="${2:-e}"
echo "Type 's' for settings (branch, updates)."
echo "Type 'h' or '?' for manuals and help."
if [[ "$back_option" != "none" ]]; then
echo "Type '$back_option' to go back."
fi
if [[ "$exit_option" != "none" ]]; then
echo "Type '$exit_option' to exit."
fi
}
# Process common menu input
# Returns: 0 if handled, 1 if not handled (caller should process)
# Usage: process_common_input "$choice" [back_option] [exit_option]
# If returns 0, use "continue" to re-show menu
# If returns 1, process choice in caller
process_common_input() {
local choice="$1"
local back_option="${2:-b}"
local exit_option="${3:-e}"
# Settings
if [[ "$choice" == "s" ]]; then
settings_menu
return 0
fi
# Help/Manual
if [[ "$choice" == "h" ]] || [[ "$choice" == "?" ]]; then
__manual_menu__
return 0
fi
# Back
if [[ "$choice" == "$back_option" ]] && [[ "$back_option" != "none" ]]; then
return 0 # Caller should handle return/break
fi
# Exit
if [[ "$choice" == "$exit_option" ]] && [[ "$exit_option" != "none" ]]; then
echo "Exiting..."
exit 0
fi
# Not handled by common input
return 1
}
###############################################################################
# EXECUTION MODE SELECTION
###############################################################################
# Check for required dependencies for remote execution
# Returns: 0 if all dependencies met, 1 if missing dependencies
__check_remote_dependencies__() {
local missing_deps=()
# Check for sshpass (only needed if not using SSH keys)
if ! command -v sshpass &>/dev/null; then
missing_deps+=("sshpass")
fi
# Check for jq (for nodes.json parsing)
if ! command -v jq &>/dev/null; then
missing_deps+=("jq")
fi
if [[ ${#missing_deps[@]} -gt 0 ]]; then
echo
__line_rgb__ "⚠ Missing Dependencies for Remote Execution" 255 200 0
echo
echo "The following tools are required but not installed:"
for dep in "${missing_deps[@]}"; do
echo " - $dep"
done
echo
echo "Installation commands:"
echo " Debian/Ubuntu: sudo apt install ${missing_deps[*]}"
echo " Arch Linux: sudo pacman -S ${missing_deps[*]}"
echo " RHEL/CentOS: sudo yum install ${missing_deps[*]}"
echo
echo "Note: If you have SSH keys configured, sshpass is not required."
echo
read -rp "Press Enter to continue..."
return 1
fi
return 0
}
select_execution_mode() {
while true; do
clear
show_ascii_art
echo "Select execution mode:"
echo "----------------------------------------"
__line_rgb__ "1) Local Execution (this system)" 0 200 200
__line_rgb__ "2) Single Remote Node" 0 200 200
__line_rgb__ "3) Multiple Remote Nodes" 0 200 200
echo
echo "----------------------------------------"
echo
echo "Type 'm' to manage nodes"
show_common_footer "none" "e"
echo
echo "----------------------------------------"
read -rp "Choice: " mode_choice
# Handle common inputs first
if process_common_input "$mode_choice" "none" "e"; then
continue
fi
# Handle menu-specific inputs
case "$mode_choice" in
1)
__set_execution_mode__ "local"
__clear_remote_targets__
return 0
;;
2)
# Check dependencies before proceeding
if ! __check_remote_dependencies__; then
continue
fi
if configure_single_remote; then
return 0
fi
;;
3)
# Check dependencies before proceeding
if ! __check_remote_dependencies__; then
continue
fi
if configure_multi_remote; then
return 0
fi
;;
m)
manage_nodes_menu
;;
*)
echo "Invalid choice!"
sleep 1
;;
esac
done
}
configure_single_remote() {
while true; do
clear
show_ascii_art
echo "Available nodes from nodes.json:"
echo "----------------------------------------"
local node_count
node_count=$(__count_available_nodes__)
if [[ $node_count -eq 0 ]]; then
echo "No nodes found in nodes.json"
echo
read -rp "Enter node IP manually: " manual_ip
read -rp "Enter node name: " manual_name
read -rp "Enter username (default: root): " manual_user
manual_user="${manual_user:-root}"
read -rp "Enter SSH port (default: 22): " manual_port
manual_port="${manual_port:-22}"
# Check for SSH key auth automatically (test since not in nodes.json)
local has_keys=$(__has_ssh_keys__ "$manual_ip" "$manual_user" "" "$manual_port")
if [[ "$has_keys" == "true" ]]; then
echo
__line_rgb__ "SSH key authentication detected" 0 255 0
export USE_SSH_KEYS=true
__clear_remote_targets__
__add_remote_target__ "$manual_name" "$manual_ip" "" "$manual_user" "$manual_port"
__set_execution_mode__ "single-remote"
__success__ "Using SSH key authentication (no password needed)"
sleep 1
return 0
fi
echo
echo "SSH keys not detected, password required."
read -rsp "Enter password: " manual_pass
echo
# Test connection before proceeding
echo "Testing connection..."
if ! __test_remote_connection__ "$manual_ip" "$manual_pass" "$manual_user" "$manual_port"; then
echo
__line_rgb__ "Connection failed! Please check IP, username, port, and password." 255 0 0
[[ -n "${LAST_SSH_ERROR:-}" ]] && echo "Reason: $LAST_SSH_ERROR"
echo "Press Enter to try again..."
read -r
continue
fi
echo "Connection successful!"
sleep 1
__clear_remote_targets__
__add_remote_target__ "$manual_name" "$manual_ip" "$manual_pass" "$manual_user" "$manual_port"
__set_execution_mode__ "single-remote"
return 0
fi
local i=1
declare -A node_menu=()
echo
while IFS= read -r node_name; do
local node_ip
node_ip=$(__get_node_ip__ "$node_name")
local node_username
node_username=$(__get_node_username__ "$node_name")
local node_port
node_port=$(__get_node_port__ "$node_name")
# Check if SSH keys work for this node (use cache)
local key_indicator=""
local has_keys=$(__has_ssh_keys__ "$node_ip" "$node_username" "$node_name" "$node_port")
if [[ "$has_keys" == "true" ]]; then
key_indicator=" [SSH Key]"
else
key_indicator=" [No Key]"
fi
local port_indicator=""
[[ "$node_port" != "22" ]] && port_indicator=":${node_port}"
__line_rgb__ " $i) $node_name ($node_username@$node_ip${port_indicator}) $key_indicator" 0 200 200
node_menu[$i]="$node_name:$node_ip"
((i += 1))
done < <(__get_available_nodes__)
echo
__line_rgb__ "[SSH Key] = SSH keys configured" 0 200 0
__line_rgb__ "[No Key] = No SSH keys configured" 200 200 0
echo
echo "----------------------------------------"
echo
echo "Type 'm' to enter manually"
echo "Type 'scan' to scan/update SSH key status"
show_common_footer "b" "e"
echo
echo "----------------------------------------"
read -rp "Choice: " node_choice
# Handle common inputs first
if process_common_input "$node_choice" "b" "e"; then
if [[ "$node_choice" == "b" ]]; then
return 1
fi
continue
fi
# Handle scan request
if [[ "$node_choice" == "scan" ]]; then
clear
show_ascii_art
__scan_ssh_keys__
sleep 2
continue
fi
# Handle menu-specific inputs
if [[ "$node_choice" == "m" ]]; then
read -rp "Enter node IP: " manual_ip
read -rp "Enter node name: " manual_name
read -rp "Enter username (default: root): " manual_user
manual_user="${manual_user:-root}"
read -rp "Enter SSH port (default: 22): " manual_port
manual_port="${manual_port:-22}"
# Check for SSH key auth automatically (use cache)
local has_keys=$(__has_ssh_keys__ "$manual_ip" "$manual_user" "" "$manual_port")
if [[ "$has_keys" == "true" ]]; then
echo
__line_rgb__ "SSH key authentication detected" 0 255 0
export USE_SSH_KEYS=true
__clear_remote_targets__
__add_remote_target__ "$manual_name" "$manual_ip" "" "$manual_user" "$manual_port"
__set_execution_mode__ "single-remote"
__success__ "Using SSH key authentication (no password needed)"
sleep 1
return 0
fi
echo
echo "SSH keys not detected, password required."
read -rsp "Enter password: " manual_pass
echo
# Test connection before proceeding
echo "Testing connection..."
if ! __test_remote_connection__ "$manual_ip" "$manual_pass" "$manual_user" "$manual_port"; then
echo
__line_rgb__ "Connection failed! Please check IP, username, and password." 255 0 0
[[ -n "${LAST_SSH_ERROR:-}" ]] && echo "Reason: $LAST_SSH_ERROR"
echo "Press Enter to try again..."
read -r
continue
fi
echo "Connection successful!"
sleep 1
__clear_remote_targets__
__add_remote_target__ "$manual_name" "$manual_ip" "$manual_pass" "$manual_user" "$manual_port"
__set_execution_mode__ "single-remote"
return 0
elif [[ -n "$node_choice" && -n "${node_menu[$node_choice]:-}" ]]; then
IFS=':' read -r selected_name selected_ip <<<"${node_menu[$node_choice]}"
local selected_username
selected_username=$(__get_node_username__ "$selected_name")
local selected_port
selected_port=$(__get_node_port__ "$selected_name")
# Check for SSH key auth automatically (use cache)
local has_keys=$(__has_ssh_keys__ "$selected_ip" "$selected_username" "$selected_name" "$selected_port")
if [[ "$has_keys" == "true" ]]; then
echo
__line_rgb__ "SSH key authentication detected" 0 255 0
export USE_SSH_KEYS=true
__clear_remote_targets__
__add_remote_target__ "$selected_name" "$selected_ip" "" "$selected_username" "$selected_port"
__set_execution_mode__ "single-remote"
__success__ "Using SSH key authentication (no password needed)"
sleep 1
return 0
fi
echo
echo "SSH keys not detected, password required."
read -rsp "Enter password for $selected_name: " node_pass
echo
# Test connection before proceeding
echo "Testing connection to $selected_name..."
if ! __test_remote_connection__ "$selected_ip" "$node_pass" "$selected_username" "$selected_port"; then
echo
__line_rgb__ "Connection failed! Please check password." 255 0 0
[[ -n "${LAST_SSH_ERROR:-}" ]] && echo "Reason: $LAST_SSH_ERROR"
echo "Press Enter to try again..."
read -r
continue
fi
echo "Connection successful!"
sleep 1
__clear_remote_targets__
__add_remote_target__ "$selected_name" "$selected_ip" "$node_pass" "$selected_username" "$selected_port"
__set_execution_mode__ "single-remote"
return 0
else
echo "Invalid choice!"
sleep 1
fi
done
}
configure_multi_remote() {
while true; do
clear
show_ascii_art
echo "Multiple Remote Nodes Configuration:"
echo "----------------------------------------"
__line_rgb__ "1) Use saved nodes from nodes.json" 0 200 200
__line_rgb__ "2) IP range (e.g., 192.168.1.100-200)" 0 200 200
__line_rgb__ "3) VMID range (queries Proxmox cluster)" 0 200 200
echo
echo "----------------------------------------"
echo
show_common_footer "b" "e"
echo
echo "----------------------------------------"
read -rp "Choice: " multi_choice
# Handle common inputs first
if process_common_input "$multi_choice" "b" "e"; then
if [[ "$multi_choice" == "b" ]]; then
return 1
fi
continue
fi
case "$multi_choice" in
1)
if configure_multi_saved; then
return 0
fi
;;
2)
if configure_multi_ip_range; then
return 0
fi
;;
3)
if configure_multi_vmid_range; then
return 0
fi
;;
*)
echo "Invalid choice!"
sleep 1
;;
esac
done
}
configure_multi_saved() {
while true; do
clear
show_ascii_art
echo "Available nodes from nodes.json:"
echo "----------------------------------------"
local node_count
node_count=$(__count_available_nodes__)
if [[ $node_count -eq 0 ]]; then
echo "No nodes configured. Add nodes first (option 'm')."
sleep 2
return 1
fi
local i=1
declare -A node_menu=()
echo
while IFS= read -r node_name; do
local node_ip
node_ip=$(__get_node_ip__ "$node_name")
local node_username
node_username=$(__get_node_username__ "$node_name")
# Check if SSH keys work for this node (use cache)
local key_indicator=" [No Key]"
local has_keys=$(__has_ssh_keys__ "$node_ip" "$node_username" "$node_name")
if [[ "$has_keys" == "true" ]]; then
key_indicator=" [SSH Key]"
else
key_indicator=" [No Key]"
fi
__line_rgb__ " $i) $node_name ($node_username@$node_ip) $key_indicator" 0 200 200
node_menu[$i]="$node_name:$node_ip"
((i += 1))
done < <(__get_available_nodes__)
echo
__line_rgb__ "[SSH Key] = SSH keys configured" 0 200 0
__line_rgb__ "[No Key] = No SSH keys configured" 200 200 0
echo
echo "----------------------------------------"
echo
echo "Enter node numbers (comma-separated, e.g., 1,3,5) or:"
echo "Type 'all' to select all nodes"
echo "Type 'scan' to scan/update SSH key status"
show_common_footer "b" "e"
echo
echo "----------------------------------------"
read -rp "Selection: " node_selection
# Handle common inputs first
if process_common_input "$node_selection" "b" "e"; then
if [[ "$node_selection" == "b" ]]; then
return 1
fi
continue
fi
# Handle scan request
if [[ "$node_selection" == "scan" ]]; then
clear
show_ascii_art
__scan_ssh_keys__
sleep 2
continue
fi
# Handle menu-specific inputs
__clear_remote_targets__
if [[ "$node_selection" == "all" ]]; then
while IFS= read -r node_name; do
local node_ip
node_ip=$(__get_node_ip__ "$node_name")
REMOTE_TARGETS+=("$node_name:$node_ip")
done < <(__get_available_nodes__)
else
IFS=',' read -ra selected_nodes <<<"$node_selection"
for num in "${selected_nodes[@]}"; do
num=$(echo "$num" | xargs)
if [[ -n "$num" && -n "${node_menu[$num]:-}" ]]; then
REMOTE_TARGETS+=("${node_menu[$num]}")
fi
done
fi
if [[ ${#REMOTE_TARGETS[@]} -eq 0 ]]; then
echo "No valid nodes selected!"
sleep 2
continue # Redisplay menu
fi
echo
echo "Selected ${#REMOTE_TARGETS[@]} node(s)"
echo
# Check if SSH keys are available (test first node)
# Get username for first node
local first_node_name="${REMOTE_TARGETS[0]%%:*}"
local first_node_username="${NODE_USERNAMES[$first_node_name]:-$DEFAULT_USERNAME}"
local first_node_ip="${REMOTE_TARGETS[0]##*:}"
if ssh -o BatchMode=yes -o ConnectTimeout=2 "${first_node_username}@${first_node_ip}" echo "test" &>/dev/null 2>&1; then
# Verify all selected nodes have SSH keys configured
local all_have_keys=true
for target in "${REMOTE_TARGETS[@]}"; do
IFS=':' read -r node_name node_ip <<<"$target"
local node_username="${NODE_USERNAMES[$node_name]:-$DEFAULT_USERNAME}"
if ! ssh -o BatchMode=yes -o ConnectTimeout=2 "${node_username}@$node_ip" echo "test" &>/dev/null 2>&1; then
all_have_keys=false
break
fi
done
if [[ "$all_have_keys" == "true" ]]; then
echo
__line_rgb__ "SSH key authentication detected for all nodes" 0 255 0
export USE_SSH_KEYS=true
__success__ "Using SSH key authentication (no password needed)"
sleep 1
__set_execution_mode__ "multi-remote"
return 0
else
echo
__line_rgb__ "SSH keys work for some nodes, but not all" 255 165 0
echo
read -rp "Use SSH keys where available and passwords for others? [Y/n]: " use_mixed
if [[ ! "$use_mixed" =~ ^[Nn]$ ]]; then
export USE_SSH_KEYS=true
echo
echo "Nodes needing passwords:"
for target in "${REMOTE_TARGETS[@]}"; do
IFS=':' read -r node_name node_ip <<<"$target"
local node_username="${NODE_USERNAMES[$node_name]:-$DEFAULT_USERNAME}"
if ! ssh -o BatchMode=yes -o ConnectTimeout=2 "${node_username}@$node_ip" echo "test" &>/dev/null 2>&1; then
echo " - $node_name ($node_username@$node_ip)"
fi
done
echo
for target in "${REMOTE_TARGETS[@]}"; do
IFS=':' read -r node_name node_ip <<<"$target"
local node_username="${NODE_USERNAMES[$node_name]:-$DEFAULT_USERNAME}"
if ! ssh -o BatchMode=yes -o ConnectTimeout=2 "${node_username}@$node_ip" echo "test" &>/dev/null 2>&1; then
read -rsp "Enter password for $node_name: " node_pass
echo
# Test connection
local node_port
node_port=$(__get_node_port__ "$node_name")
echo "Testing connection to $node_name..."
if ! __test_remote_connection__ "$node_ip" "$node_pass" "$node_username" "$node_port"; then
echo
__line_rgb__ "Connection to $node_name failed! Skipping this node." 255 0 0
[[ -n "${LAST_SSH_ERROR:-}" ]] && echo "Reason: $LAST_SSH_ERROR"
continue
fi
echo "Connection to $node_name successful!"
NODE_PASSWORDS["$node_name"]="$node_pass"
else
NODE_PASSWORDS["$node_name"]="" # Empty = use SSH keys
fi
done
__set_execution_mode__ "multi-remote"
return 0
fi
fi
fi
# No SSH keys detected, use passwords
read -rp "Same password for all nodes? [y/N]: " same_pass
if [[ "$same_pass" =~ ^[Yy]$ ]]; then
read -rsp "Enter password for all nodes: " shared_pass
echo
# Test password on first node
local first_target="${REMOTE_TARGETS[0]}"
IFS=':' read -r first_name first_ip <<<"$first_target"
local first_username="${NODE_USERNAMES[$first_name]:-$DEFAULT_USERNAME}"
local first_port
first_port=$(__get_node_port__ "$first_name")
echo "Testing password on first node ($first_name)..."
if ! __test_remote_connection__ "$first_ip" "$shared_pass" "$first_username" "$first_port"; then
echo
__line_rgb__ "Password test failed on $first_name!" 255 0 0
[[ -n "${LAST_SSH_ERROR:-}" ]] && echo "Reason: $LAST_SSH_ERROR"
echo "Please try again..."
sleep 2
continue
fi
echo "Password works on $first_name"
sleep 1
for target in "${REMOTE_TARGETS[@]}"; do
IFS=':' read -r node_name node_ip <<<"$target"
NODE_PASSWORDS["$node_name"]="$shared_pass"
done
else
for target in "${REMOTE_TARGETS[@]}"; do
IFS=':' read -r node_name node_ip <<<"$target"
local node_username="${NODE_USERNAMES[$node_name]:-$DEFAULT_USERNAME}"
local node_port
node_port=$(__get_node_port__ "$node_name")
read -rsp "Enter password for $node_name ($node_ip): " node_pass
echo
# Test connection
echo "Testing connection to $node_name..."
if ! __test_remote_connection__ "$node_ip" "$node_pass" "$node_username" "$node_port"; then
echo
__line_rgb__ "Connection to $node_name failed! Please try again." 255 0 0
[[ -n "${LAST_SSH_ERROR:-}" ]] && echo "Reason: $LAST_SSH_ERROR"
# Loop back to retry this specific node
read -rsp "Enter password for $node_name ($node_ip): " node_pass
echo
echo "Testing connection to $node_name..."
if ! __test_remote_connection__ "$node_ip" "$node_pass" "$node_username" "$node_port"; then
echo
__line_rgb__ "Still failed! Skipping $node_name." 255 0 0
[[ -n "${LAST_SSH_ERROR:-}" ]] && echo "Reason: $LAST_SSH_ERROR"
continue
fi
fi
echo "Connection to $node_name successful!"
NODE_PASSWORDS["$node_name"]="$node_pass"
done
fi
__set_execution_mode__ "multi-remote"
return 0
done
}
configure_multi_ip_range() {
clear
show_ascii_art
echo "Temporary IP Range Configuration:"
echo "----------------------------------------"
echo
echo "Enter IP range in format: 192.168.1.100-200"
echo "(Base IP with range of last octet)"
echo
read -rp "IP Range: " ip_range
if [[ -z "$ip_range" ]]; then
return 1
fi
# Parse IP range (e.g., 192.168.1.100-200)
if [[ ! "$ip_range" =~ ^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)([0-9]{1,3})-([0-9]{1,3})$ ]]; then
echo "Error: Invalid IP range format"
echo "Expected format: 192.168.1.100-200"
sleep 2
return 1
fi
local base_ip="${BASH_REMATCH[1]}"
local start_octet="${BASH_REMATCH[2]}"
local end_octet="${BASH_REMATCH[3]}"
if [[ $start_octet -gt $end_octet ]]; then
echo "Error: Start IP must be less than or equal to end IP"
sleep 2
return 1
fi
echo
read -rp "Enter username for all nodes in range (default: root): " shared_user
shared_user="${shared_user:-root}"
read -rp "Enter SSH port for all nodes in range (default: 22): " shared_port
shared_port="${shared_port:-22}"
read -rsp "Enter password for all nodes in range: " shared_pass
echo
# Build targets
__clear_remote_targets__
for ((octet = start_octet; octet <= end_octet; octet++)); do
local ip="${base_ip}${octet}"
local node_name="temp-${ip//\./-}"
REMOTE_TARGETS+=("$node_name:$ip")
NODE_PASSWORDS["$node_name"]="$shared_pass"
NODE_USERNAMES["$node_name"]="$shared_user"
NODE_PORTS["$node_name"]="$shared_port"