-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupgrade.sh
More file actions
executable file
·1131 lines (948 loc) · 37.4 KB
/
Copy pathupgrade.sh
File metadata and controls
executable file
·1131 lines (948 loc) · 37.4 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
#
# app_rpt__ultra :: the ultimate controller experience for app_rpt
# Copyright (C) 2025 John D. Lewis (AI3I)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
set -euo pipefail
# ==============================================================================
# upgrade.sh - Safe upgrade tool for app_rpt__ultra
# ==============================================================================
#
# This script safely upgrades an existing app_rpt__ultra installation to
# the latest version, handling configuration migration, script updates,
# and validation with automatic rollback on failure.
#
# Version information
readonly SCRIPT_VERSION="2.0.5"
readonly SCRIPT_NAME="upgrade.sh"
readonly REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Installation paths
readonly INSTALL_BASE="/opt/app_rpt"
readonly AST_SOUNDS_RP="/usr/share/asterisk/sounds/rp"
readonly CONFIG_FILE="$INSTALL_BASE/config.ini"
readonly VERSION_FILE="$INSTALL_BASE/VERSION"
readonly BACKUP_BASE="$INSTALL_BASE/backups"
# Colors for output
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly BLUE='\033[0;34m'
readonly CYAN='\033[0;36m'
readonly NC='\033[0m' # No Color
# ==============================================================================
# Logging Functions
# ==============================================================================
log_info() {
echo -e "${GREEN}[INFO]${NC} $*" >&2
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $*" >&2
}
log_error() {
echo -e "${RED}[ERROR]${NC} $*" >&2
}
log_step() {
echo -e "${BLUE}[STEP]${NC} $*" >&2
}
log_success() {
echo -e "${CYAN}[✓]${NC} $*" >&2
}
# ==============================================================================
# Help Text
# ==============================================================================
show_help() {
cat <<'EOF'
app_rpt__ultra Upgrade Tool
Usage: sudo ./upgrade.sh [options]
Options:
--dry-run Show what would change without making changes
--force Skip version check, force upgrade even if same version
--no-backup Skip backup creation (NOT RECOMMENDED)
--auto-yes Assume yes to all prompts (dangerous!)
--help Show this help message
Examples:
sudo ./upgrade.sh # Interactive upgrade with prompts
sudo ./upgrade.sh --dry-run # Preview changes without applying
sudo ./upgrade.sh --force # Force upgrade/reinstall
Description:
This script safely upgrades an existing app_rpt__ultra installation to
the latest version. It will:
1. Create a backup of your current installation
2. Migrate your config.ini (preserving all your settings)
3. Install the new common.sh library
4. Update all 21+ scripts
5. Validate the installation
6. Update the version file
If any step fails, it will automatically rollback to the backup.
Exit Codes:
0 - Upgrade successful
1 - Pre-flight checks failed or user cancelled
2 - Backup failed
3 - Config migration failed
4 - Script installation failed
5 - Validation failed (rollback triggered)
EOF
}
# ==============================================================================
# Pre-flight Checks
# ==============================================================================
check_prerequisites() {
log_step "Running pre-flight checks..."
# Must run as root
if [[ $EUID -ne 0 ]]; then
log_error "This script must be run as root (use sudo)"
exit 1
fi
# Check if installation exists
if [[ ! -d "$INSTALL_BASE" ]]; then
log_error "Installation not found at $INSTALL_BASE"
log_error "Please run install.sh first"
exit 1
fi
# Check config.ini exists
if [[ ! -f "$CONFIG_FILE" ]]; then
log_error "Configuration file not found: $CONFIG_FILE"
log_error "Installation appears incomplete"
exit 1
fi
# Check if asterisk user exists
if ! id asterisk &>/dev/null; then
log_error "asterisk user not found"
log_error "Please ensure Asterisk/ASL3 is installed"
exit 1
fi
# Check required commands
local missing_cmds=()
for cmd in jq rsync awk sed grep dialog fzf; do
if ! command -v "$cmd" &>/dev/null; then
missing_cmds+=("$cmd")
fi
done
if [[ ${#missing_cmds[@]} -gt 0 ]]; then
log_error "Missing required commands: ${missing_cmds[*]}"
log_error "Install with: sudo apt install ${missing_cmds[*]}"
exit 1
fi
# Check Asterisk is installed (optional, might be in custom path)
if ! command -v asterisk &>/dev/null; then
log_warn "asterisk command not found in PATH"
log_warn "Will continue but Asterisk integration may fail"
fi
log_success "Pre-flight checks passed"
}
# ==============================================================================
# Version Management
# ==============================================================================
get_installed_version() {
if [[ -f "$VERSION_FILE" ]]; then
cat "$VERSION_FILE"
else
echo "unknown"
fi
}
get_repo_version() {
if [[ -f "$REPO_DIR/VERSION" ]]; then
cat "$REPO_DIR/VERSION"
else
echo "$SCRIPT_VERSION"
fi
}
escape_sed_replacement() {
# Escape characters that have special meaning in sed replacement strings
# (when using '/' as the delimiter): backslash, forward slash, ampersand
printf '%s' "$1" | sed 's/[\\&/]/\\&/g'
}
compare_versions() {
local current="$1"
local new="$2"
# If current is unknown, always upgrade
if [[ "$current" == "unknown" ]]; then
return 0 # Needs upgrade
fi
# Simple string comparison
if [[ "$current" == "$new" ]]; then
return 1 # Same version
fi
# Different version, needs upgrade
return 0
}
check_version() {
local current_version
local repo_version
current_version=$(get_installed_version)
repo_version=$(get_repo_version)
log_info "Current version: $current_version"
log_info "Repository version: $repo_version"
if compare_versions "$current_version" "$repo_version"; then
log_info "Upgrade available: $current_version → $repo_version"
return 0
else
if [[ "$FORCE_UPGRADE" == true ]]; then
log_warn "Versions are the same, but --force specified"
return 0
else
log_info "System is already up to date (v$repo_version)"
log_info "Use --force to reinstall/repair"
exit 0
fi
fi
}
# ==============================================================================
# Backup Creation
# ==============================================================================
create_backup() {
if [[ "$SKIP_BACKUP" == true ]]; then
log_warn "Skipping backup (--no-backup specified)"
echo ""
return 0
fi
log_step "Creating backup..."
local timestamp
timestamp=$(date +%Y%m%d_%H%M%S)
local backup_dir="$BACKUP_BASE/upgrade_backup_$timestamp"
if [[ "$DRY_RUN" == true ]]; then
log_info "[DRY RUN] Would create backup at: $backup_dir"
echo "$backup_dir"
return 0
fi
mkdir -p "$backup_dir"
# Backup critical files
log_info "Backing up config.ini..."
cp "$CONFIG_FILE" "$backup_dir/config.ini.bkp"
log_info "Backing up bin scripts..."
mkdir -p "$backup_dir/bin"
if [[ -d "$INSTALL_BASE/bin" ]]; then
cp -a "$INSTALL_BASE/bin/"* "$backup_dir/bin/" 2>/dev/null || true
fi
log_info "Backing up lib files..."
mkdir -p "$backup_dir/lib"
if [[ -d "$INSTALL_BASE/lib" ]]; then
cp -a "$INSTALL_BASE/lib/"* "$backup_dir/lib/" 2>/dev/null || true
fi
# Backup Asterisk config
if [[ -f /etc/asterisk/rpt.conf ]]; then
log_info "Backing up rpt.conf..."
cp /etc/asterisk/rpt.conf "$backup_dir/rpt.conf.bkp"
fi
# Backup crontab
if crontab -u asterisk -l &>/dev/null; then
log_info "Backing up crontab..."
crontab -u asterisk -l > "$backup_dir/crontab.bkp" 2>/dev/null || true
fi
# Save version info
echo "$(get_installed_version)" > "$backup_dir/VERSION.old"
echo "$(get_repo_version)" > "$backup_dir/VERSION.new"
log_success "Backup created: $backup_dir"
echo "$backup_dir"
return 0
}
# ==============================================================================
# Platform Detection
# ==============================================================================
detect_platform() {
# Detect AllStarLink appliance type to determine hardware expectations
if dpkg -l 2>/dev/null | grep -q "ii.*asl3-appliance-pi"; then
echo "pi"
elif dpkg -l 2>/dev/null | grep -q "ii.*asl3-appliance-pc"; then
echo "pc"
elif dpkg -l 2>/dev/null | grep -q "ii.*asl3-appliance"; then
echo "generic"
else
echo "unknown"
fi
}
# ==============================================================================
# Network Interface Detection
# ==============================================================================
detect_network_interfaces() {
# Try to detect actual network interfaces
local lan_iface=""
local wlan_iface=""
local vpn_iface=""
local platform
platform=$(detect_platform)
# Find primary ethernet interface
lan_iface=$(ip -o link show | awk -F': ' '{print $2}' | grep -E '^(eth|enp|ens)' | head -1)
[[ -z "$lan_iface" ]] && lan_iface="eth0"
# Find wireless interface (platform-aware)
wlan_iface=$(ip -o link show | awk -F': ' '{print $2}' | grep -E '^(wlan|wlp)' | head -1)
if [[ -z "$wlan_iface" ]]; then
# Only default to wlan0 on Pi platform (which always has wireless)
if [[ "$platform" == "pi" ]]; then
wlan_iface="wlan0"
else
# On PC/generic, leave empty if no wireless found
wlan_iface=""
fi
fi
# VPN interface (usually tun0 or wg0, empty if none found)
vpn_iface=$(ip -o link show | awk -F': ' '{print $2}' | grep -E '^(tun|wg)' | head -1)
# Don't default to tun0 if no VPN interface exists
[[ -z "$vpn_iface" ]] && vpn_iface=""
echo "$lan_iface,$wlan_iface,$vpn_iface"
}
# ==============================================================================
# Configuration Migration
# ==============================================================================
migrate_config() {
log_step "Migrating configuration..."
# Read existing config to extract user values
set +u # Temporarily allow undefined variables
source "$CONFIG_FILE" || {
log_error "Failed to source existing config"
exit 3
}
set -u
# Save critical user values
local user_mynode="${MYNODE:-}"
local user_nwszone="${NWSZONE:-}"
local user_wustation="${WUSTATION:-}"
local user_wuapikey="${WUAPIKEY:-}"
local user_fetchlocal="${FETCHLOCAL:-0}"
local user_fetchpoint="${FETCHPOINT:-localhost}"
local user_retention="${RETENTION:-60}"
# Check if network interface vars already exist
local user_landevice="${landevice:-}"
local user_wlandevice="${wlandevice:-}"
local user_vpndevice="${vpndevice:-}"
# If they don't exist, detect them
if [[ -z "$user_landevice" ]] || [[ -z "$user_wlandevice" ]] || [[ -z "$user_vpndevice" ]]; then
log_info "Detecting network interfaces..."
IFS=',' read -r detected_lan detected_wlan detected_vpn <<< "$(detect_network_interfaces)"
user_landevice="${user_landevice:-$detected_lan}"
user_wlandevice="${user_wlandevice:-$detected_wlan}"
user_vpndevice="${user_vpndevice:-$detected_vpn}"
log_info " LAN interface: $user_landevice"
log_info " WLAN interface: $user_wlandevice"
log_info " VPN interface: $user_vpndevice"
else
log_info "Using existing network interface settings:"
log_info " LAN interface: $user_landevice"
log_info " WLAN interface: $user_wlandevice"
log_info " VPN interface: $user_vpndevice"
fi
# Validate critical values
if [[ -z "$user_mynode" ]] || [[ "$user_mynode" == "%MYNODE%" ]]; then
log_error "MYNODE not set in config.ini"
log_error "Cannot upgrade without valid node number"
exit 3
fi
# Create new config from template
local temp_config="/tmp/config.ini.new.$$"
# Use config.ini.example as template (config.ini no longer in repo as of v2.0.5)
if [[ -f "$REPO_DIR/app_rpt/config.ini.example" ]]; then
cp "$REPO_DIR/app_rpt/config.ini.example" "$temp_config"
elif [[ -f "$REPO_DIR/app_rpt/config.ini" ]]; then
cp "$REPO_DIR/app_rpt/config.ini" "$temp_config"
else
log_error "Could not find config.ini or config.ini.example in repository"
return 3
fi
# Replace placeholders with user values
# Values are escaped to prevent special characters (\, /, &) from breaking sed
sed -i "s/MYNODE=%MYNODE%/MYNODE=$(escape_sed_replacement "$user_mynode")/g" "$temp_config"
sed -i "s/NWSZONE=XXX000/NWSZONE=$(escape_sed_replacement "$user_nwszone")/g" "$temp_config"
sed -i "s/WUSTATION=empty/WUSTATION=$(escape_sed_replacement "$user_wustation")/g" "$temp_config"
sed -i "s/WUAPIKEY=empty/WUAPIKEY=$(escape_sed_replacement "$user_wuapikey")/g" "$temp_config"
sed -i "s/FETCHLOCAL=0/FETCHLOCAL=$(escape_sed_replacement "$user_fetchlocal")/g" "$temp_config"
sed -i "s/FETCHPOINT=localhost/FETCHPOINT=$(escape_sed_replacement "$user_fetchpoint")/g" "$temp_config"
sed -i "s/RETENTION=60/RETENTION=$(escape_sed_replacement "$user_retention")/g" "$temp_config"
# Update network interface variables
sed -i "s/landevice=eth0/landevice=$(escape_sed_replacement "$user_landevice")/g" "$temp_config"
sed -i "s/wlandevice=wlan0/wlandevice=$(escape_sed_replacement "$user_wlandevice")/g" "$temp_config"
sed -i "s/vpndevice=tun0/vpndevice=$(escape_sed_replacement "$user_vpndevice")/g" "$temp_config"
# Replace %%BASEDIR%% placeholder
sed -i "s|%%BASEDIR%%|$INSTALL_BASE|g" "$temp_config"
# Add version marker
sed -i "s|###VERSION=.*|###VERSION=$(get_repo_version)|g" "$temp_config"
# Validate new config can be sourced
if ! bash -c "set -euo pipefail; source '$temp_config'" 2>/dev/null; then
log_error "New config.ini failed validation"
log_error "Aborting upgrade"
rm -f "$temp_config"
exit 3
fi
log_success "Configuration migrated successfully"
echo "$temp_config"
}
# ==============================================================================
# Directory Structure
# ==============================================================================
ensure_directories() {
log_info "Ensuring all required directories exist..."
if [[ "$DRY_RUN" == false ]]; then
# Ensure all sound subdirectories exist (add weather/ for v2.0.5+)
mkdir -p "$INSTALL_BASE"/sounds/{ids,rpt,tails,wx,weather,letters,digits,custom}
mkdir -p "$INSTALL_BASE"/sounds/{_male,_female,_sndfx}
log_success "Directory structure verified"
else
log_info "[DRY RUN] Would ensure directory structure"
fi
}
update_lib_files() {
log_info "Updating library files..."
# Files that should be updated from repo (system-managed)
local update_files=(
"messagetable.txt"
"vocabulary.txt"
"characters.txt"
)
# Files that should be preserved (user-managed)
local preserve_files=(
"autodialers.txt"
"mailboxes.txt"
"emergency_autodialers.txt"
"exemptions.txt"
)
if [[ "$DRY_RUN" == false ]]; then
# Update system-managed files
for file in "${update_files[@]}"; do
if [[ -f "$REPO_DIR/app_rpt/lib/$file" ]]; then
cp "$REPO_DIR/app_rpt/lib/$file" "$INSTALL_BASE/lib/$file"
chmod 644 "$INSTALL_BASE/lib/$file"
chown asterisk:asterisk "$INSTALL_BASE/lib/$file"
log_info "Updated $file"
fi
done
# Create empty output files if they don't exist
touch "$INSTALL_BASE/lib/nwsalerts.out"
touch "$INSTALL_BASE/lib/wunderground.out"
chmod 644 "$INSTALL_BASE/lib"/*.out
chown asterisk:asterisk "$INSTALL_BASE/lib"/*.out
log_success "Library files updated"
else
log_info "[DRY RUN] Would update library files"
fi
}
# ==============================================================================
# Script Installation
# ==============================================================================
install_scripts() {
local temp_config="$1"
log_step "Installing scripts..."
# CRITICAL: Install common.sh FIRST before updating any other scripts
log_info "Installing common.sh (required by all scripts)..."
local temp_common="/tmp/common.sh.new.$$"
cp "$REPO_DIR/app_rpt/bin/common.sh" "$temp_common"
sed -i "s|%%BASEDIR%%|$INSTALL_BASE|g" "$temp_common"
if [[ "$DRY_RUN" == false ]]; then
cp "$temp_common" "$INSTALL_BASE/bin/common.sh"
chmod 755 "$INSTALL_BASE/bin/common.sh"
chown asterisk:asterisk "$INSTALL_BASE/bin/common.sh"
fi
rm -f "$temp_common"
# Test that common.sh can be sourced
if [[ "$DRY_RUN" == false ]]; then
if ! sudo -u asterisk bash -c "source '$INSTALL_BASE/bin/common.sh'" 2>/dev/null; then
log_error "common.sh failed to source correctly"
log_error "Aborting upgrade"
exit 4
fi
log_success "common.sh installed and validated"
else
log_info "[DRY RUN] Would install common.sh"
fi
# Now install all other scripts
local script_count=0
for script in "$REPO_DIR/app_rpt/bin/"*.sh; do
local script_name
script_name=$(basename "$script")
# Skip common.sh (already installed)
[[ "$script_name" == "common.sh" ]] && continue
log_info "Installing $script_name..."
local temp_script="/tmp/$script_name.new.$$"
cp "$script" "$temp_script"
sed -i "s|%%BASEDIR%%|$INSTALL_BASE|g" "$temp_script"
if [[ "$DRY_RUN" == false ]]; then
cp "$temp_script" "$INSTALL_BASE/bin/$script_name"
chmod 755 "$INSTALL_BASE/bin/$script_name"
chown asterisk:asterisk "$INSTALL_BASE/bin/$script_name"
fi
rm -f "$temp_script"
((script_count++))
done
log_success "Installed $script_count scripts"
# Install utility scripts (install, upgrade, repair, uninstall)
log_info "Installing utility scripts..."
local util_count=0
# Create util directory if it doesn't exist
if [[ "$DRY_RUN" == false ]]; then
mkdir -p "$INSTALL_BASE/util"
fi
# Copy utility scripts from repo root to /opt/app_rpt/util/
local util_scripts=("install.sh" "upgrade.sh" "repair.sh" "uninstall.sh")
for util_name in "${util_scripts[@]}"; do
if [[ -f "$REPO_DIR/$util_name" ]]; then
log_info "Installing $util_name..."
local temp_util="/tmp/$util_name.new.$$"
cp "$REPO_DIR/$util_name" "$temp_util"
sed -i "s|%%BASEDIR%%|$INSTALL_BASE|g" "$temp_util"
if [[ "$DRY_RUN" == false ]]; then
cp "$temp_util" "$INSTALL_BASE/util/$util_name"
chmod 755 "$INSTALL_BASE/util/$util_name"
chown asterisk:asterisk "$INSTALL_BASE/util/$util_name"
fi
rm -f "$temp_util"
((util_count++))
else
log_info "Utility script not found: $util_name"
fi
done
if [[ $util_count -gt 0 ]]; then
log_success "Installed $util_count utility scripts"
else
log_info "[DRY RUN] Would install utility scripts"
fi
# Install kerchunkd systemd service
if [[ "$DRY_RUN" == false ]]; then
if [[ -f "$REPO_DIR/kerchunkd.service" ]]; then
log_info "Updating kerchunkd systemd service..."
local kerchunkd_was_active=false
systemctl is-active --quiet kerchunkd 2>/dev/null && kerchunkd_was_active=true
cp "$REPO_DIR/kerchunkd.service" /etc/systemd/system/kerchunkd.service
systemctl daemon-reload
if [[ "$kerchunkd_was_active" == true ]]; then
systemctl restart kerchunkd
log_success "kerchunkd service updated and restarted"
else
log_success "kerchunkd service updated (was not running)"
fi
else
log_info "kerchunkd.service not found in repo — skipping systemd update"
fi
else
log_info "[DRY RUN] Would update kerchunkd.service and reload systemd"
fi
# Install new config.ini
if [[ "$DRY_RUN" == false ]]; then
log_info "Installing new config.ini..."
cp "$temp_config" "$CONFIG_FILE"
chmod 644 "$CONFIG_FILE"
chown asterisk:asterisk "$CONFIG_FILE"
log_success "config.ini updated"
else
log_info "[DRY RUN] Would install new config.ini"
fi
rm -f "$temp_config"
# Ensure AST_SOUNDS_RP exists as a real directory and the convenience symlink
# at INSTALL_BASE/sounds points to it. The secondary /var/lib path is also symlinked.
if [[ "$DRY_RUN" == false ]]; then
mkdir -p "$AST_SOUNDS_RP"/{ids,rpt,tails,wx,weather,letters,digits,custom,_male,_female,_sndfx}
log_success "Real sounds directory: $AST_SOUNDS_RP"
# /opt/app_rpt/sounds — must be a symlink to AST_SOUNDS_RP
if [[ -d "$INSTALL_BASE/sounds" ]] && [[ ! -L "$INSTALL_BASE/sounds" ]]; then
log_warn "sounds/ is a real dir (pre-migration install) — replacing with symlink"
rm -rf "$INSTALL_BASE/sounds"
fi
if [[ ! -e "$INSTALL_BASE/sounds" ]]; then
ln -s "$AST_SOUNDS_RP" "$INSTALL_BASE/sounds"
log_success "Created symlink: $INSTALL_BASE/sounds -> $AST_SOUNDS_RP"
elif [[ -L "$INSTALL_BASE/sounds" ]] && [[ "$(readlink "$INSTALL_BASE/sounds")" == "$AST_SOUNDS_RP" ]]; then
log_info "sounds symlink already correct"
fi
# /var/lib/asterisk/sounds/rp — secondary Asterisk search path
local lib_rp="/var/lib/asterisk/sounds/rp"
if [[ -d "/var/lib/asterisk/sounds" ]]; then
if [[ ! -L "$lib_rp" ]] || [[ "$(readlink "$lib_rp")" != "$AST_SOUNDS_RP" ]]; then
rm -f "$lib_rp"
ln -sf "$AST_SOUNDS_RP" "$lib_rp"
log_success "Symlink: $lib_rp -> $AST_SOUNDS_RP"
else
log_info "Secondary sounds symlink already correct"
fi
fi
else
log_info "[DRY RUN] Would ensure $AST_SOUNDS_RP exists and symlinks are correct"
fi
# defaultlanguage in asterisk.conf covers ALL channel types — including the
# pseudo channels app_rpt creates for sound playback. chan_dahdi.conf only
# covers DAHDI trunk channels and is not sufficient on its own.
local ast_conf="/etc/asterisk/asterisk.conf"
local dahdi_conf="/etc/asterisk/chan_dahdi.conf"
if [[ -f "$ast_conf" ]]; then
if grep -q "^defaultlanguage = rp" "$ast_conf"; then
log_info "asterisk.conf already has defaultlanguage = rp"
else
if [[ "$DRY_RUN" == false ]]; then
if grep -q "^;*defaultlanguage" "$ast_conf"; then
sed -i 's/^;*defaultlanguage\s*=.*/defaultlanguage = rp/' "$ast_conf"
else
sed -i '/^\[options\]/a defaultlanguage = rp' "$ast_conf"
fi
log_success "Set defaultlanguage = rp in asterisk.conf"
else
log_info "[DRY RUN] Would set defaultlanguage = rp in asterisk.conf"
fi
fi
else
log_warn "asterisk.conf not found — set defaultlanguage = rp manually"
fi
if [[ -f "$dahdi_conf" ]]; then
if grep -q "^language=rp" "$dahdi_conf"; then
log_info "chan_dahdi.conf already has language=rp"
else
if [[ "$DRY_RUN" == false ]]; then
if grep -q "^language=" "$dahdi_conf"; then
sed -i 's/^language=.*/language=rp/' "$dahdi_conf"
elif grep -q "^;language=en" "$dahdi_conf"; then
sed -i 's/^;language=en/language=rp/' "$dahdi_conf"
else
sed -i '/^\[general\]/a language=rp' "$dahdi_conf"
fi
log_success "Set language=rp in chan_dahdi.conf"
else
log_info "[DRY RUN] Would set language=rp in chan_dahdi.conf"
fi
fi
else
log_warn "chan_dahdi.conf not found — skipping"
fi
}
# ==============================================================================
# Post-Upgrade Validation
# ==============================================================================
validate_installation() {
log_step "Validating installation..."
local errors=0
# Check all required scripts exist
log_info "Checking script files..."
local required_scripts=(
"common.sh" "asterisk.sh" "cmdparser.sh" "configkeeper.sh"
"ctkeeper.sh" "ctwriter.sh" "datadumper.sh" "datekeeper.sh"
"gpio.sh" "idkeeper.sh" "msgreader.sh" "msgwriter.sh"
"restart.sh" "sayip.sh" "speaktext.sh" "statekeeper.sh"
"system.sh" "tailkeeper.sh" "timekeeper.sh" "weatheralert.sh"
"weatherkeeper.sh" "wireless.sh"
)
for script in "${required_scripts[@]}"; do
if [[ ! -f "$INSTALL_BASE/bin/$script" ]]; then
log_error "Missing: $INSTALL_BASE/bin/$script"
((errors++))
elif [[ ! -x "$INSTALL_BASE/bin/$script" ]]; then
log_error "Not executable: $INSTALL_BASE/bin/$script"
((errors++))
fi
done
# Test common.sh can be sourced
log_info "Testing common.sh..."
if ! sudo -u asterisk bash -c "source '$INSTALL_BASE/bin/common.sh'" 2>/dev/null; then
log_error "common.sh failed to source"
((errors++))
fi
# Test config.ini is valid
log_info "Validating config.ini..."
if ! sudo -u asterisk bash -c "source '$CONFIG_FILE'" 2>/dev/null; then
log_error "config.ini contains errors"
((errors++))
fi
# Check required config variables
log_info "Checking config variables..."
set +u
source "$CONFIG_FILE"
local required_vars=(
"MYNODE" "BASEDIR" "BINDIR" "SOUNDS" "RPTCONF"
"landevice" "vpndevice"
)
for var in "${required_vars[@]}"; do
if [[ -z "${!var:-}" ]]; then
log_error "Missing config variable: $var"
((errors++))
fi
done
# wlandevice is optional (may be empty on non-Pi systems)
if [[ ! -v wlandevice ]]; then
log_error "Missing config variable: wlandevice (must be defined, but can be empty)"
((errors++))
fi
set -u
# Check Asterisk config
if command -v asterisk &>/dev/null; then
log_info "Testing Asterisk configuration..."
if ! asterisk -rx "core show version" &>/dev/null; then
log_warn "Asterisk not running or not responding (not critical)"
fi
fi
if [[ $errors -gt 0 ]]; then
log_error "Validation failed with $errors error(s)"
return 1
fi
log_success "All validation checks passed"
return 0
}
# ==============================================================================
# Version File Update
# ==============================================================================
update_version_file() {
log_step "Updating version information..."
if [[ "$DRY_RUN" == false ]]; then
echo "$(get_repo_version)" > "$VERSION_FILE"
chown asterisk:asterisk "$VERSION_FILE"
log_success "Version set to $(get_repo_version)"
else
log_info "[DRY RUN] Would set version to $(get_repo_version)"
fi
}
# ==============================================================================
# Fix Ownership and Permissions
# ==============================================================================
fix_permissions() {
log_step "Verifying ownership and permissions..."
if [[ "$DRY_RUN" == false ]]; then
# Ensure all of /opt/app_rpt is owned by asterisk:asterisk
chown -R asterisk:asterisk "$INSTALL_BASE"
log_info "Set ownership on $INSTALL_BASE"
# Ensure critical Asterisk directories have correct ownership
local asterisk_dirs=(
"/usr/share/asterisk"
"/var/lib/asterisk"
"/var/log/asterisk"
"/var/spool/asterisk"
"/var/run/asterisk"
)
for dir in "${asterisk_dirs[@]}"; do
if [[ -d "$dir" ]]; then
chown -R asterisk:asterisk "$dir"
fi
done
# Ensure log directory exists and migrate from old /var/log location
mkdir -p /opt/app_rpt/log
chown asterisk:asterisk /opt/app_rpt/log
chmod 775 /opt/app_rpt/log
for _log in app_rpt.log kerchunk_stats.log state_history.log; do
if [[ -f /var/log/$_log ]] && [[ ! -f /opt/app_rpt/log/$_log ]]; then
mv /var/log/$_log /opt/app_rpt/log/$_log
fi
if [[ -f /opt/app_rpt/log/$_log ]]; then
chown asterisk:asterisk /opt/app_rpt/log/$_log
chmod 664 /opt/app_rpt/log/$_log
fi
done
log_success "Ownership and permissions verified"
else
log_info "[DRY RUN] Would verify ownership and permissions"
fi
}
# ==============================================================================
# Rollback Function
# ==============================================================================
rollback_upgrade() {
local backup_dir="$1"
log_error "Upgrade failed, rolling back..."
if [[ ! -d "$backup_dir" ]]; then
log_error "Backup directory not found: $backup_dir"
log_error "Cannot rollback automatically"
return 1
fi
log_info "Restoring from backup: $backup_dir"
# Restore config.ini
if [[ -f "$backup_dir/config.ini.bkp" ]]; then
cp "$backup_dir/config.ini.bkp" "$CONFIG_FILE"
log_info "✓ Restored config.ini"
fi
# Restore bin scripts
if [[ -d "$backup_dir/bin" ]]; then
rm -rf "$INSTALL_BASE/bin/"*
cp -a "$backup_dir/bin/"* "$INSTALL_BASE/bin/"
log_info "✓ Restored bin scripts"
fi
# Restore lib files
if [[ -d "$backup_dir/lib" ]]; then
cp -a "$backup_dir/lib/"* "$INSTALL_BASE/lib/"
log_info "✓ Restored lib files"
fi
# Restore VERSION
if [[ -f "$backup_dir/VERSION.old" ]]; then
cp "$backup_dir/VERSION.old" "$VERSION_FILE"
log_info "✓ Restored version file"
fi
log_info "Rollback complete"
log_info "System restored to pre-upgrade state"
return 0
}
# ==============================================================================
# Upgrade Summary
# ==============================================================================
show_upgrade_summary() {
local current_version="$1"
local new_version="$2"
echo ""
echo "========================================"
echo " app_rpt__ultra Upgrade Summary"
echo "========================================"
echo ""
echo "Current version: $current_version"
echo "New version: $new_version"
echo ""
echo "Changes in v$new_version:"
echo " • NEW: common.sh shared library for all scripts"
echo " • IMPROVED: Better error handling with set -euo pipefail"
echo " • IMPROVED: Consistent logging across all scripts"
echo " • IMPROVED: Better variable quoting and validation"
echo " • FIXED: sayip.sh wlan announcement now says 'wlan' correctly"
echo " • FIXED: Network interface variables now properly defined"
echo " • ADDED: GPL license headers to all scripts"
echo " • ENHANCED: 21 scripts updated with improvements"
echo ""
echo "This upgrade will:"
echo " 1. Create backup of current installation"
echo " 2. Migrate your config.ini (preserving settings)"
echo " 3. Install common.sh (new dependency)"
echo " 4. Update all scripts"
echo " 5. Validate the installation"
echo " 6. Update version to $new_version"
echo ""
echo "Backup location: $BACKUP_BASE/upgrade_backup_*"
echo ""
}
# ==============================================================================
# Command Line Parsing
# ==============================================================================
# Parse command line options
DRY_RUN=false
FORCE_UPGRADE=false
SKIP_BACKUP=false
AUTO_YES=false
while [[ $# -gt 0 ]]; do
case $1 in
--dry-run) DRY_RUN=true; shift ;;
--force) FORCE_UPGRADE=true; shift ;;
--no-backup) SKIP_BACKUP=true; shift ;;
--auto-yes) AUTO_YES=true; shift ;;
--help) show_help; exit 0 ;;
*) echo "Unknown option: $1"; show_help; exit 1 ;;
esac
done
# ==============================================================================
# Main Execution
# ==============================================================================
main() {
local current_version
local repo_version
local backup_dir=""
# Banner
echo ""