Repository navigation
Expand file tree
/
Copy patharr-stack.sh
More file actions
1223 lines (1068 loc) · 38.6 KB
/
Copy patharr-stack.sh
File metadata and controls
1223 lines (1068 loc) · 38.6 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
#!/usr/bin/env bash
# Copyright (c) 2021-2026 community-scripts ORG
# Author: community-scripts
# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
source <(curl -fsSL ${COMMUNITY_SCRIPTS_CORE_URL:-https://raw.githubusercontent.com/community-scripts/core/main}/core/core.func)
source <(curl -fsSL ${COMMUNITY_SCRIPTS_CORE_URL:-https://raw.githubusercontent.com/community-scripts/core/main}/lib/tools.func)
set -eEo pipefail
color
formatting
icons
set_std_mode
SILENT_LOGFILE="/tmp/arr-stack-$$.log"
silent() { "$@" >>"$SILENT_LOGFILE" 2>&1; }
msg_info() { echo -e "${INFO:-[i]} ${YW}${1}${CL}"; }
msg_ok() { echo -e "${CM:-[ok]} ${GN}${1}${CL}"; }
msg_warn() { echo -e "${YW}[WARN]${CL} ${1}"; }
msg_error() { echo -e "${CROSS:-[x]} ${RD}${1}${CL}"; }
msg_step() { echo -e "${BL}==>${CL} ${1}"; }
cancelled() { msg_warn "Cancelled at $1."; exit 0; }
var_container_storage="${var_container_storage:-}"
var_template_storage="${var_template_storage:-}"
var_bridge="${var_bridge:-}"
var_gateway="${var_gateway:-}"
var_cidr="${var_cidr:-24}"
var_start_ctid="${var_start_ctid:-}"
var_repo="${var_repo:-ProxmoxVE}"
var_qbt_password="${var_qbt_password:-}"
SUMMARY_FILE="${SUMMARY_FILE:-/root/arr-stack-summary.txt}"
QBT_PERMANENT=0
BACKTITLE="Proxmox VE Helper Scripts — arr Stack"
TEMP_DIR=$(mktemp -d)
_on_exit() {
local rc=$?
if (( rc != 0 )); then
if (( ${#INSTALLED_SLUGS[@]} > 0 )); then orphan_report; fi
if [[ -s "$SILENT_LOGFILE" ]]; then
echo
msg_error "Last 20 lines of ${SILENT_LOGFILE}:"
tail -n 20 "$SILENT_LOGFILE"
fi
fi
rm -rf "$TEMP_DIR"
}
trap _on_exit EXIT
declare -A APP
SELECTED_ARRS=""
SELECTED_CLIENTS=""
ORDERED_SLUGS=()
INSTALLED_SLUGS=()
WIRING_RESULTS=()
WIRING_FAILURES=()
SYNC_CATEGORIES_SONARR='[5000,5010,5020,5030,5040,5045,5050]'
SYNC_CATEGORIES_RADARR='[2000,2010,2020,2030,2040,2045,2050,2060]'
SYNC_CATEGORIES_LIDARR='[3000,3010,3020,3030,3040]'
header_info() {
clear
cat <<"EOF"
_ _
__ _ _ __ _ __ ___| |_ __ _ ___| | __
/ _` | '__| '__| ____ / __| __/ _` |/ __| |/ /
| (_| | | | | |____| \__ \ || (_| | (__| <
\__,_|_| |_| |___/\__\__,_|\___|_|\_\
EOF
}
check_root() {
if [[ $EUID -ne 0 ]]; then
msg_error "Run this script as root."
exit 1
fi
}
check_pve_tools() {
local missing=()
for cmd in pct pvesh pvesm; do
command -v "$cmd" >/dev/null 2>&1 || missing+=("$cmd")
done
if (( ${#missing[@]} > 0 )); then
msg_error "Missing Proxmox VE tools: ${missing[*]}. Run this on a PVE node."
exit 1
fi
}
wait_for_port() {
local ip=$1 port=$2 timeout=${3:-60} elapsed=0
while ! (echo > "/dev/tcp/${ip}/${port}") >/dev/null 2>&1; do
sleep 2
elapsed=$((elapsed + 2))
if (( elapsed >= timeout )); then return 1; fi
done
return 0
}
is_valid_ipv4() {
local ip=$1
[[ "$ip" =~ ^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$ ]] || return 1
local a=${BASH_REMATCH[1]} b=${BASH_REMATCH[2]} c=${BASH_REMATCH[3]} d=${BASH_REMATCH[4]}
(( a <= 255 && b <= 255 && c <= 255 && d <= 255 )) || return 1
return 0
}
form_input_program() {
if command -v dialog >/dev/null 2>&1; then
echo "dialog"
elif whiptail --help 2>&1 | grep -q -- '--form'; then
echo "whiptail"
else
echo "none"
fi
}
seed_catalog() {
while IFS='|' read -r slug script port impl apiver kind name contract; do
[[ -z "$slug" ]] && continue
APP[$slug.script]="$script"
APP[$slug.port]="$port"
APP[$slug.impl]="$impl"
APP[$slug.apiver]="$apiver"
APP[$slug.kind]="$kind"
APP[$slug.name]="$name"
APP[$slug.contract]="$contract"
done <<'EOF'
prowlarr|prowlarr.sh|9696||v1|indexer|Prowlarr|
sonarr|sonarr.sh|8989|Sonarr|v3|arr|Sonarr|SonarrSettings
radarr|radarr.sh|7878|Radarr|v3|arr|Radarr|RadarrSettings
lidarr|lidarr.sh|8686|Lidarr|v1|arr|Lidarr|LidarrSettings
seerr|seerr.sh|5055||-|requests|Seerr|
qbittorrent|qbittorrent.sh|8090|QBittorrent|-|client|qBittorrent|QBittorrentSettings
sabnzbd|sabnzbd.sh|7777|Sabnzbd|-|client|SABnzbd|SabnzbdSettings
EOF
}
pick_storage() {
if [[ -n "$var_container_storage" ]]; then
msg_info "Container storage (from env): ${var_container_storage}"
else
local options=() row name type
while IFS= read -r row; do
name=$(awk '{print $1}' <<<"$row")
type=$(awk '{print $2}' <<<"$row")
[[ -z "$name" ]] && continue
options+=("$name" "$type")
done < <(pvesm status -content rootdir 2>/dev/null | awk 'NR>1')
if (( ${#options[@]} == 0 )); then
msg_error "No PVE storage with content 'rootdir' available."
exit 1
fi
if (( ${#options[@]} == 2 )); then
var_container_storage="${options[0]}"
msg_info "Container storage (only option): ${var_container_storage}"
else
var_container_storage=$(whiptail --backtitle "$BACKTITLE" \
--title "Container Storage" \
--menu "Pick a PVE storage for the container rootfs:" 20 70 10 \
"${options[@]}" 3>&1 1>&2 2>&3) || cancelled "storage pick"
fi
fi
if [[ -z "$var_template_storage" ]]; then
var_template_storage=$(pvesm status -content vztmpl 2>/dev/null \
| awk 'NR>1 && $1=="local" {print $1; exit}')
[[ -z "$var_template_storage" ]] && var_template_storage=$(pvesm status -content vztmpl 2>/dev/null \
| awk 'NR>1 {print $1; exit}')
fi
[[ -n "$var_template_storage" ]] && msg_info "Template storage: ${var_template_storage}"
}
pick_network_defaults() {
if [[ -z "$var_bridge" ]]; then
local options=() b
while IFS= read -r b; do
[[ -n "$b" ]] && options+=("$b" "")
done < <(awk '/^iface vmbr/ {print $2}' /etc/network/interfaces 2>/dev/null)
if (( ${#options[@]} == 0 )); then
options=("vmbr0" "")
fi
var_bridge=$(whiptail --backtitle "$BACKTITLE" \
--title "Network Bridge" \
--menu "Pick the Linux bridge for all containers:" 15 60 6 \
"${options[@]}" 3>&1 1>&2 2>&3) || cancelled "bridge pick"
fi
local default_gw
default_gw=$(ip -4 route show default | awk '{print $3}' | head -n1)
while [[ -z "$var_gateway" ]] || ! is_valid_ipv4 "$var_gateway"; do
var_gateway=$(whiptail --backtitle "$BACKTITLE" \
--title "Gateway" \
--inputbox "IPv4 gateway for the container subnet (leave blank for default: $default_gw):" 10 70 \
"${var_gateway:-$default_gw}" 3>&1 1>&2 2>&3) || cancelled "gateway prompt"
if [[ -z "$var_gateway" && -n "$default_gw" ]]; then
var_gateway="$default_gw"
fi
if ! is_valid_ipv4 "$var_gateway"; then
whiptail --backtitle "$BACKTITLE" --title "Invalid" \
--msgbox "Not a valid IPv4 address: ${var_gateway}" 8 60
var_gateway=""
fi
done
while true; do
var_cidr=$(whiptail --backtitle "$BACKTITLE" \
--title "CIDR Mask" \
--inputbox "Network mask (1-32, e.g. 24):" 10 60 \
"${var_cidr:-24}" 3>&1 1>&2 2>&3) || cancelled "CIDR prompt"
if [[ "$var_cidr" =~ ^[0-9]+$ ]] && (( var_cidr >= 1 && var_cidr <= 32 )); then
break
fi
whiptail --backtitle "$BACKTITLE" --title "Invalid" \
--msgbox "CIDR must be an integer between 1 and 32." 8 60
done
msg_info "Bridge ${var_bridge} | gateway ${var_gateway} | mask /${var_cidr}"
}
pick_apps() {
while true; do
local choice
choice=$(whiptail --backtitle "$BACKTITLE" \
--title "Pick *arr Apps" \
--checklist "Prowlarr is always installed. Pick additional apps:" 16 70 6 \
"sonarr" "Sonarr (TV)" ON \
"radarr" "Radarr (Movies)" ON \
"lidarr" "Lidarr (Music)" OFF \
"seerr" "Seerr (Requests)" OFF \
3>&1 1>&2 2>&3) || cancelled "*arr app pick"
SELECTED_ARRS=$(echo "$choice" | tr -d '"')
if [[ -z "$SELECTED_ARRS" ]]; then
if whiptail --backtitle "$BACKTITLE" --title "Confirm" \
--yesno "You picked no *arr apps. Only Prowlarr will be installed and there will be nothing to wire. Continue anyway?" 10 70; then
return
fi
continue
fi
return
done
}
pick_clients() {
local choice
choice=$(whiptail --backtitle "$BACKTITLE" \
--title "Pick Download Clients" \
--checklist "Optional download clients to install + wire:" 14 70 4 \
"qbittorrent" "qBittorrent (Torrents)" ON \
"sabnzbd" "SABnzbd (Usenet)" OFF \
3>&1 1>&2 2>&3) || cancelled "download client pick"
SELECTED_CLIENTS=$(echo "$choice" | tr -d '"')
}
_gen_password() {
local p
p=$(openssl rand -base64 18 2>/dev/null | tr -dc 'A-Za-z0-9' | cut -c1-16)
[[ -z "$p" ]] && p="ChangeMe${RANDOM}${RANDOM}"
printf '%s' "$p"
}
pick_qbittorrent_password() {
[[ " $SELECTED_CLIENTS " == *" qbittorrent "* ]] || return
[[ -n "$var_qbt_password" ]] && { msg_info "qBittorrent password (from env) will be used."; return; }
local choice
choice=$(whiptail --backtitle "$BACKTITLE" \
--title "qBittorrent WebUI Password" \
--menu "Set the qBittorrent admin password:" 15 70 2 \
"generate" "Auto-generate a strong random password (recommended)" \
"manual" "Enter my own password" \
3>&1 1>&2 2>&3) || cancelled "qBittorrent password pick"
if [[ "$choice" == "generate" ]]; then
var_qbt_password=$(_gen_password)
msg_info "A random qBittorrent password was generated (shown in the final summary)."
return
fi
local pw1 pw2
while true; do
pw1=$(whiptail --backtitle "$BACKTITLE" --title "qBittorrent Password" \
--passwordbox "Enter a WebUI password (min 6 chars):" 10 60 \
3>&1 1>&2 2>&3) || cancelled "qBittorrent password entry"
pw2=$(whiptail --backtitle "$BACKTITLE" --title "Confirm Password" \
--passwordbox "Re-enter the password:" 10 60 \
3>&1 1>&2 2>&3) || cancelled "qBittorrent password confirm"
if [[ "$pw1" != "$pw2" ]]; then
whiptail --backtitle "$BACKTITLE" --title "Mismatch" \
--msgbox "Passwords do not match. Try again." 8 50
continue
fi
if (( ${#pw1} < 6 )); then
whiptail --backtitle "$BACKTITLE" --title "Too short" \
--msgbox "Password must be at least 6 characters." 8 50
continue
fi
var_qbt_password="$pw1"
return
done
}
compute_ordered_slugs() {
ORDERED_SLUGS=("prowlarr")
local s
for s in $SELECTED_ARRS; do
[[ "$s" == "seerr" ]] && continue
ORDERED_SLUGS+=("$s")
done
for s in $SELECTED_CLIENTS; do
ORDERED_SLUGS+=("$s")
done
for s in $SELECTED_ARRS; do
[[ "$s" == "seerr" ]] && ORDERED_SLUGS+=("seerr")
done
}
pick_ip_mode_and_ips() {
while true; do
local mode
mode=$(whiptail --backtitle "$BACKTITLE" \
--title "IP Entry Mode" \
--menu "How would you like to enter IP addresses?" 15 75 2 \
"list" "Enter all IPs at once (space- or comma-separated)" \
"form" "Enter each IP in a form" \
3>&1 1>&2 2>&3) || cancelled "IP entry mode pick"
case "$mode" in
list) _collect_ips_list_mode; return ;;
form)
if _collect_ips_one_by_one; then
return
fi
;;
esac
done
}
_collect_ips_list_mode() {
local expected_n=${#ORDERED_SLUGS[@]}
local hint="" s
for s in "${ORDERED_SLUGS[@]}"; do hint+=" ${s}"$'\n'; done
while true; do
local raw
raw=$(whiptail --backtitle "$BACKTITLE" \
--title "Enter ${expected_n} IPv4 addresses" \
--inputbox "Enter ${expected_n} IPs separated by spaces or commas, in this order:"$'\n\n'"${hint}" \
22 78 "" 3>&1 1>&2 2>&3) || cancelled "IP list entry"
local normalized="${raw//,/ }"
local -a ips=()
# shellcheck disable=SC2206
ips=( $normalized )
if (( ${#ips[@]} != expected_n )); then
whiptail --backtitle "$BACKTITLE" --title "Wrong count" \
--msgbox "Expected ${expected_n} IPs, got ${#ips[@]}. Please re-enter." 8 60
continue
fi
local ok=1 i
for i in "${!ips[@]}"; do
if ! is_valid_ipv4 "${ips[$i]}"; then
whiptail --backtitle "$BACKTITLE" --title "Invalid" \
--msgbox "Entry $((i+1)) is not a valid IPv4: ${ips[$i]}" 8 60
ok=0; break
fi
if [[ "${ips[$i]}" == "$var_gateway" ]]; then
whiptail --backtitle "$BACKTITLE" --title "Invalid" \
--msgbox "Entry $((i+1)) collides with the gateway: ${ips[$i]}" 8 60
ok=0; break
fi
done
(( ok == 0 )) && continue
local dup
dup=$(printf '%s\n' "${ips[@]}" | sort | uniq -d | head -n1)
if [[ -n "$dup" ]]; then
whiptail --backtitle "$BACKTITLE" --title "Duplicate IP" \
--msgbox "IP appears more than once: ${dup}" 8 60
continue
fi
for i in "${!ORDERED_SLUGS[@]}"; do
APP[${ORDERED_SLUGS[$i]}.ip]=${ips[$i]}
done
return
done
}
_collect_ips_one_by_one() {
local ui
ui=$(form_input_program)
if [[ "$ui" == "dialog" ]]; then
local expected_n=${#ORDERED_SLUGS[@]}
local -a form_fields=()
local slug
for slug in "${ORDERED_SLUGS[@]}"; do
form_fields+=("$slug" "")
done
while true; do
local raw_values
if ! raw_values=$(dialog --backtitle "$BACKTITLE" \
--title "Container IP Addresses" \
--form "Enter an IPv4 address for each container:" 22 78 0 \
"${form_fields[@]}" 2>&1 >/dev/tty); then
msg_warn "IP form entry cancelled."
return 1
fi
local -a ips=()
mapfile -t ips <<< "$raw_values"
if (( ${#ips[@]} != expected_n )); then
whiptail --backtitle "$BACKTITLE" --title "Wrong count" \
--msgbox "Expected ${expected_n} IPs, got ${#ips[@]}. Please re-enter." 8 60
continue
fi
local ok=1 i
for i in "${!ips[@]}"; do
local ip="${ips[$i]}"
if ! is_valid_ipv4 "$ip"; then
whiptail --backtitle "$BACKTITLE" --title "Invalid" \
--msgbox "Entry $((i+1)) is not a valid IPv4: ${ip}" 8 60
ok=0
break
fi
if [[ "$ip" == "$var_gateway" ]]; then
whiptail --backtitle "$BACKTITLE" --title "Invalid" \
--msgbox "Entry $((i+1)) collides with the gateway: ${ip}" 8 60
ok=0
break
fi
if ping -c 1 -W 1 "$ip" >/dev/null 2>&1; then
whiptail --backtitle "$BACKTITLE" --title "IP In Use" \
--msgbox "Entry $((i+1)) is already in use by another device: ${ip}" 8 60
ok=0
break
fi
done
(( ok == 0 )) && continue
local dup
dup=$(printf '%s\n' "${ips[@]}" | sort | uniq -d | head -n1)
if [[ -n "$dup" ]]; then
whiptail --backtitle "$BACKTITLE" --title "Duplicate IP" \
--msgbox "IP appears more than once: ${dup}" 8 60
continue
fi
for i in "${!ORDERED_SLUGS[@]}"; do
APP[${ORDERED_SLUGS[$i]}.ip]=${ips[$i]}
done
return 0
done
fi
if [[ "$ui" == "whiptail" ]]; then
local expected_n=${#ORDERED_SLUGS[@]}
local -a form_fields=()
local slug
for slug in "${ORDERED_SLUGS[@]}"; do
form_fields+=("$slug" "")
done
while true; do
local raw_values
if ! raw_values=$(whiptail --backtitle "$BACKTITLE" \
--title "Container IP Addresses" \
--separate-output \
--form "Enter an IPv4 address for each container:" 22 78 "$((expected_n + 4))" \
"${form_fields[@]}" 3>&1 1>&2 2>&3); then
msg_warn "IP form entry cancelled."
return 1
fi
local -a ips=()
mapfile -t ips <<< "$raw_values"
if (( ${#ips[@]} != expected_n )); then
whiptail --backtitle "$BACKTITLE" --title "Wrong count" \
--msgbox "Expected ${expected_n} IPs, got ${#ips[@]}. Please re-enter." 8 60
continue
fi
local ok=1 i
for i in "${!ips[@]}"; do
local ip="${ips[$i]}"
if ! is_valid_ipv4 "$ip"; then
whiptail --backtitle "$BACKTITLE" --title "Invalid" \
--msgbox "Entry $((i+1)) is not a valid IPv4: ${ip}" 8 60
ok=0
break
fi
if [[ "$ip" == "$var_gateway" ]]; then
whiptail --backtitle "$BACKTITLE" --title "Invalid" \
--msgbox "Entry $((i+1)) collides with the gateway: ${ip}" 8 60
ok=0
break
fi
if ping -c 1 -W 1 "$ip" >/dev/null 2>&1; then
whiptail --backtitle "$BACKTITLE" --title "IP In Use" \
--msgbox "Entry $((i+1)) is already in use by another device: ${ip}" 8 60
ok=0
break
fi
done
(( ok == 0 )) && continue
local dup
dup=$(printf '%s\n' "${ips[@]}" | sort | uniq -d | head -n1)
if [[ -n "$dup" ]]; then
whiptail --backtitle "$BACKTITLE" --title "Duplicate IP" \
--msgbox "IP appears more than once: ${dup}" 8 60
continue
fi
for i in "${!ORDERED_SLUGS[@]}"; do
APP[${ORDERED_SLUGS[$i]}.ip]=${ips[$i]}
done
return 0
done
fi
local slug ip running="" last_ip="" default_ip=""
for slug in "${ORDERED_SLUGS[@]}"; do
if [[ -n "$last_ip" ]]; then
local prefix="${last_ip%.*}"
local host="${last_ip##*.}"
if [[ "$host" =~ ^[0-9]+$ ]] && (( host < 254 )); then
default_ip="${prefix}.$((host + 1))"
fi
fi
while true; do
local prompt="Enter IPv4 for ${slug}:"
[[ -n "$running" ]] && prompt+=$'\n\nAlready assigned:'"$running"
ip=$(whiptail --backtitle "$BACKTITLE" \
--title "Container IP Addresses" \
--inputbox "$prompt" 16 60 "$default_ip" 3>&1 1>&2 2>&3) || { msg_warn "IP entry cancelled."; return 1; }
if ! is_valid_ipv4 "$ip"; then
whiptail --backtitle "$BACKTITLE" --title "Invalid" \
--msgbox "Not a valid IPv4: ${ip}" 8 60
continue
fi
if [[ "$ip" == "$var_gateway" ]]; then
whiptail --backtitle "$BACKTITLE" --title "Invalid" \
--msgbox "Collides with the gateway: ${ip}" 8 60
continue
fi
local dup=0 other_slug other_ip
for other_slug in "${ORDERED_SLUGS[@]}"; do
[[ "$other_slug" == "$slug" ]] && continue
other_ip="${APP[${other_slug}.ip]:-}"
[[ -n "$other_ip" && "$other_ip" == "$ip" ]] && { dup=1; break; }
done
if (( dup )); then
whiptail --backtitle "$BACKTITLE" --title "Duplicate" \
--msgbox "Already used by another container: ${ip}" 8 60
continue
fi
APP[$slug.ip]=$ip
running+=$'\n '"${slug} -> ${ip}"
last_ip=$ip
break
done
done
return 0
}
pick_start_ctid() {
local default_start
if [[ -n "$var_start_ctid" ]]; then
default_start="$var_start_ctid"
else
default_start=$(pvesh get /cluster/nextid 2>/dev/null || echo "100")
fi
local start
start=$(whiptail --backtitle "$BACKTITLE" \
--title "Starting CTID" \
--inputbox "Starting Container ID (in-use IDs are skipped):" 10 60 \
"$default_start" 3>&1 1>&2 2>&3) || cancelled "starting CTID prompt"
if ! [[ "$start" =~ ^[0-9]+$ ]]; then
msg_error "Invalid CTID: $start"
exit 1
fi
local id=$start s
for s in "${ORDERED_SLUGS[@]}"; do
while pct status "$id" >/dev/null 2>&1; do
id=$((id + 1))
(( id > 999999 )) && { msg_error "Ran out of CTID space."; exit 1; }
done
APP[$s.ctid]=$id
id=$((id + 1))
done
}
confirm_summary() {
local lines="" s
for s in "${ORDERED_SLUGS[@]}"; do
lines+=" $(printf '%-12s ctid=%-5s ip=%-16s port=%s' \
"$s" "${APP[$s.ctid]}" "${APP[$s.ip]}" "${APP[$s.port]}")"$'\n'
done
local body="About to create these containers and wire them together:"$'\n\n'"${lines}"$'\n'"Storage: ${var_container_storage} | Bridge: ${var_bridge} | Gateway: ${var_gateway} | Mask: /${var_cidr}"
whiptail --backtitle "$BACKTITLE" --title "Confirm" \
--yesno "$body" 22 78 || { msg_warn "User cancelled."; exit 0; }
}
orphan_report() {
if (( ${#INSTALLED_SLUGS[@]} == 0 )); then return; fi
msg_error "Containers already created (to clean up, run):"
local s
for s in "${INSTALLED_SLUGS[@]}"; do
echo " pct stop ${APP[$s.ctid]} && pct destroy ${APP[$s.ctid]} # ${s}"
done
}
install_loop() {
local total=${#ORDERED_SLUGS[@]} idx=0
local s script_file ip ctid port
exec 4> >(whiptail --backtitle "$BACKTITLE" --title "Installing Containers" --gauge "Starting installation..." 10 70 0)
for s in "${ORDERED_SLUGS[@]}"; do
idx=$((idx + 1))
ip="${APP[$s.ip]}"
ctid="${APP[$s.ctid]}"
port="${APP[$s.port]}"
script_file="$TEMP_DIR/${s}.sh"
local base_pct=$(( (idx - 1) * 100 / total ))
local half_pct=$(( base_pct + (50 / total) ))
local full_pct=$(( idx * 100 / total ))
echo -e "XXX\n${base_pct}\n[${idx}/${total}] Downloading ct/${s}.sh...\nXXX" >&4
$STD curl -fsSL \
"https://raw.githubusercontent.com/community-scripts/${var_repo}/main/ct/${s}.sh" \
-o "$script_file"
if [[ ! -s "$script_file" ]]; then
exec 4>&-
msg_error "Empty/failed download for ${s}"
exit 1
fi
echo -e "XXX\n${half_pct}\n[${idx}/${total}] Installing ${s} -> ctid=${ctid} ip=${ip}/${var_cidr}\nXXX" >&4
$STD env \
MODE=generated mode=generated PHS_SILENT=1 \
var_ctid="$ctid" \
var_hostname="$s" \
var_brg="$var_bridge" \
var_net="${ip}/${var_cidr}" \
var_gateway="$var_gateway" \
var_container_storage="$var_container_storage" \
var_template_storage="$var_template_storage" \
bash "$script_file"
INSTALLED_SLUGS+=("$s")
if [[ "${APP[$s.kind]}" == "arr" || "${APP[$s.kind]}" == "indexer" ]]; then
echo -e "XXX\n${half_pct}\n[${idx}/${total}] Waiting for ${s} to listen on ${port}...\nXXX" >&4
if ! wait_for_port "$ip" "$port" 90; then
# Handled silently; warning is re-issued during extraction
:
fi
fi
echo -e "XXX\n${full_pct}\n[${idx}/${total}] Installed ${s}\nXXX" >&4
done
exec 4>&-
sleep 0.1
for s in "${ORDERED_SLUGS[@]}"; do
msg_ok "Installed ${s}"
done
}
extract_arr_key() {
local slug=$1 ctid=$2 ip=$3 port=$4
local config_dir="/var/lib/${slug}/config.xml"
msg_info "Waiting for ${slug} on ${ip}:${port}..."
wait_for_port "$ip" "$port" 240 || { msg_error "${slug} never opened ${port}"; return 1; }
local i
for ((i=0; i<60; i++)); do
if pct exec "$ctid" -- test -f "$config_dir" 2>/dev/null; then break; fi
sleep 2
done
local key
key=$(pct exec "$ctid" -- sed -n 's:.*<ApiKey>\([^<]*\)</ApiKey>.*:\1:p' "$config_dir" 2>/dev/null | head -n1 || true)
if [[ -z "$key" ]]; then
msg_error "Failed to extract API key for ${slug} (config: ${config_dir})"
return 1
fi
APP[$slug.apikey]="$key"
msg_ok "${slug} apikey extracted (${key:0:6}…)"
}
extract_qbittorrent_password() {
local ctid=$1 ip=$2
APP[qbittorrent.user]="admin"
msg_info "Waiting for qbittorrent on ${ip}:8090..."
if ! wait_for_port "$ip" 8090 240; then
msg_warn "qbittorrent never opened 8090; assuming legacy default admin/adminadmin."
APP[qbittorrent.pass]="adminadmin"
return 1
fi
# The community-script installs qBittorrent with a hardcoded adminadmin password.
# We skip searching for a temporary password to prevent a 60s hang,
# since we will overwrite the password with the user's custom one anyway.
APP[qbittorrent.pass]="adminadmin"
}
qbt_password_hash() {
# qBittorrent WebUI password format: @ByteArray(<b64 salt>:<b64 key>)
# PBKDF2-HMAC-SHA512, 100000 iterations, 16-byte salt, 64-byte derived key.
# Pure Perl (Digest::SHA + MIME::Base64 are core modules, always present on PVE).
local plain=$1
PW="$plain" perl -e '
use strict; use warnings;
use Digest::SHA qw(hmac_sha512);
use MIME::Base64 qw(encode_base64);
my $pw = $ENV{PW};
open(my $r, "<", "/dev/urandom") or die "urandom: $!";
my $salt; read($r, $salt, 16) == 16 or die "salt read"; close($r);
my $iter = 100000;
# dkLen (64) == hLen (64), so only block T_1 is needed.
my $u = hmac_sha512($salt . pack("N", 1), $pw);
my $t = $u;
for (2 .. $iter) { $u = hmac_sha512($u, $pw); $t ^= $u; }
print "\@ByteArray(" . encode_base64($salt, "") . ":" . encode_base64($t, "") . ")";
' 2>/dev/null
}
set_qbittorrent_permanent_password() {
local ctid=$1 plain=$2
local conf hashval localconf edited meta uid gid perms pushed=1
conf=$(pct exec "$ctid" -- bash -c 'find /root /home /opt /var/lib -name qBittorrent.conf 2>/dev/null | head -n1' 2>/dev/null || true)
if [[ -z "$conf" ]]; then
msg_warn "qBittorrent.conf not found in ctid ${ctid}; keeping temporary password."
return 1
fi
hashval=$(qbt_password_hash "$plain")
if [[ -z "$hashval" ]]; then
msg_warn "Could not compute qBittorrent password hash; keeping temporary password."
return 1
fi
localconf="$TEMP_DIR/qBittorrent.conf"
if ! pct pull "$ctid" "$conf" "$localconf" >/dev/null 2>&1; then
msg_warn "Could not read ${conf} from ctid ${ctid}; keeping temporary password."
return 1
fi
meta=$(pct exec "$ctid" -- stat -c '%u %g %a' "$conf" 2>/dev/null || true)
read -r uid gid perms <<<"$meta"
# qbittorrent-nox rewrites its conf on shutdown, so stop it before editing.
pct exec "$ctid" -- systemctl stop qbittorrent-nox >/dev/null 2>&1 || true
# Insert/replace WebUI\Password_PBKDF2 under [Preferences], preserving the rest.
# Done in Perl so the literal backslash in the key is unambiguous.
edited="$TEMP_DIR/qBittorrent.conf.new"
if ! QBT_HASH="$hashval" QBT_SRC="$localconf" QBT_DST="$edited" perl -e '
use strict; use warnings;
my $key = q{WebUI\Password_PBKDF2};
my $hash = $ENV{QBT_HASH};
open(my $in, "<", $ENV{QBT_SRC}) or die "read: $!";
my @lines = <$in>; close($in);
my @out; my $inprefs = 0; my $done = 0;
for my $ln (@lines) {
if ($ln =~ /^\[/) {
if ($inprefs && !$done) { push @out, qq{$key="$hash"\n}; $done = 1; }
$inprefs = ($ln =~ /^\[Preferences\]\s*$/) ? 1 : 0;
}
if ($inprefs && index($ln, "$key=") == 0) {
if (!$done) { push @out, qq{$key="$hash"\n}; $done = 1; }
next;
}
push @out, $ln;
}
unless ($done) {
push @out, "[Preferences]\n" unless $inprefs;
push @out, qq{$key="$hash"\n};
}
open(my $o, ">", $ENV{QBT_DST}) or die "write: $!";
print $o @out; close($o);
'; then
msg_warn "Failed to edit qBittorrent.conf; restarting service with temporary password."
pct exec "$ctid" -- systemctl start qbittorrent-nox >/dev/null 2>&1 || true
return 1
fi
if [[ -n "$uid" && -n "$gid" && -n "$perms" ]]; then
pct push "$ctid" "$edited" "$conf" --user "$uid" --group "$gid" --perms "$perms" >/dev/null 2>&1 || pushed=0
else
pct push "$ctid" "$edited" "$conf" >/dev/null 2>&1 || pushed=0
fi
pct exec "$ctid" -- systemctl start qbittorrent-nox >/dev/null 2>&1 || true
if (( pushed == 0 )); then
msg_warn "Could not write updated qBittorrent.conf to ctid ${ctid}; keeping temporary password."
return 1
fi
PASS_BY_SLUG[qbittorrent]="$plain"
APP[qbittorrent.pass]="$plain"
QBT_PERMANENT=1
msg_ok "qBittorrent permanent WebUI password set."
}
extract_sabnzbd_key() {
local ctid=$1 ip=$2
msg_info "Waiting for sabnzbd on ${ip}:7777..."
wait_for_port "$ip" 7777 240 || { msg_warn "sabnzbd never opened 7777"; return 1; }
local ini="" candidate
for candidate in /opt/sabnzbd/sabnzbd.ini /root/.sabnzbd/sabnzbd.ini /etc/sabnzbd/sabnzbd.ini; do
if pct exec "$ctid" -- test -f "$candidate" 2>/dev/null; then
ini="$candidate"; break
fi
done
if [[ -z "$ini" ]]; then
msg_warn "Could not locate sabnzbd.ini inside ctid ${ctid}; SABnzbd will need manual setup."
return 1
fi
local key="" i
for ((i=0; i<60; i++)); do
key=$(pct exec "$ctid" -- awk -F' *= *' '/^api_key/ {print $2; exit}' "$ini" 2>/dev/null || true)
[[ -n "$key" ]] && break
sleep 2
done
if [[ -z "$key" ]]; then
msg_warn "sabnzbd api_key not yet written. Open the web wizard once at http://${ip}:7777 and rerun wiring."
return 1
fi
APP[sabnzbd.apikey]="$key"
msg_ok "sabnzbd apikey extracted (${key:0:6}…)"
}
wait_and_extract_keys() {
msg_step "Extracting credentials & API keys"
local s ctid ip port
for s in "${ORDERED_SLUGS[@]}"; do
ctid="${APP[$s.ctid]}"
ip="${APP[$s.ip]}"
port="${APP[$s.port]}"
case "${APP[$s.kind]}" in
indexer|arr)
extract_arr_key "$s" "$ctid" "$ip" "$port" || true
;;
client)
if [[ "$s" == "qbittorrent" ]]; then
extract_qbittorrent_password "$ctid" "$ip" || true
[[ -z "$var_qbt_password" ]] && var_qbt_password=$(_gen_password)
set_qbittorrent_permanent_password "$ctid" "$var_qbt_password" || true
elif [[ "$s" == "sabnzbd" ]]; then
extract_sabnzbd_key "$ctid" "$ip" || true
fi
;;
requests)
msg_warn "Seerr requires the web first-run wizard. URL + keys will be in the summary."
;;
esac
done
}
record_wiring() { WIRING_RESULTS+=("$1"); }
record_failure() { WIRING_FAILURES+=("$1"); }
api_post() {
local url=$1 apikey=$2 payload=$3 label=$4
local resp status=""
resp=$(curl -fsS --max-time 30 --retry 2 \
-H "X-Api-Key: $apikey" \
-H "Content-Type: application/json" \
-X POST "$url" -d "$payload" \
-w '\n__HTTP__%{http_code}' 2>&1) || status="curl_fail"
local code=""
if [[ "$resp" =~ __HTTP__([0-9]+)$ ]]; then
code="${BASH_REMATCH[1]}"
fi
if [[ "$status" == "curl_fail" || -z "$code" || "$code" -ge 400 ]]; then
record_failure "${label} FAIL (http ${code:-?})"
msg_warn "${label} failed (http ${code:-?})"
return 1
fi
record_wiring "${label} OK"
msg_ok "${label}"
}
probe_lidarr_api_version() {
if [[ -z "${APP[lidarr.apikey]:-}" ]]; then return; fi
local ip="${APP[lidarr.ip]}" key="${APP[lidarr.apikey]}"
if curl -fsS --max-time 10 -H "X-Api-Key: $key" \
"http://${ip}:8686/api/v3/system/status" >/dev/null 2>&1; then
APP[lidarr.apiver]="v3"
msg_info "Lidarr supports /api/v3 — using v3 for wiring."
fi
}
wire_arrs_into_prowlarr() {
local prowlarr_ip="${APP[prowlarr.ip]}"
local prowlarr_key="${APP[prowlarr.apikey]:-}"
if [[ -z "$prowlarr_key" ]]; then
msg_warn "Skipping Prowlarr wiring — no Prowlarr API key."
return
fi
local s sync_cats payload
for s in $SELECTED_ARRS; do
[[ "$s" == "seerr" ]] && continue
local key="${APP[$s.apikey]:-}"
if [[ -z "$key" ]]; then
record_failure "Prowlarr -> ${APP[$s.name]} FAIL (no apikey)"
continue
fi
case "$s" in
sonarr) sync_cats="$SYNC_CATEGORIES_SONARR" ;;
radarr) sync_cats="$SYNC_CATEGORIES_RADARR" ;;
lidarr) sync_cats="$SYNC_CATEGORIES_LIDARR" ;;
*) sync_cats='[]' ;;
esac
payload=$(jq -n \
--arg name "${APP[$s.name]}" \
--arg impl "${APP[$s.impl]}" \
--arg contract "${APP[$s.contract]}" \
--arg prowlarr_url "http://${prowlarr_ip}:9696" \
--arg base_url "http://${APP[$s.ip]}:${APP[$s.port]}" \
--arg apikey "$key" \
--argjson sync_cats "$sync_cats" \
'{
name: $name,
syncLevel: "fullSync",
implementation: $impl,
implementationName: $impl,
configContract: $contract,
tags: [],
fields: [
{ name: "prowlarrUrl", value: $prowlarr_url },
{ name: "baseUrl", value: $base_url },
{ name: "apiKey", value: $apikey },
{ name: "syncCategories", value: $sync_cats }
]
}')
api_post "http://${prowlarr_ip}:9696/api/v1/applications" \
"$prowlarr_key" "$payload" \