-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlrops.sh
More file actions
executable file
·6574 lines (5574 loc) · 233 KB
/
lrops.sh
File metadata and controls
executable file
·6574 lines (5574 loc) · 233 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
# lrops.sh - LiveReview Operations Script
# Version: 1.0.0
# Description: One-line installer and management tool for LiveReview
# Repository: https://github.com/HexmosTech/LiveReview
set -euo pipefail # Exit on error, undefined vars, pipe failures
# Ensure the script runs under Bash only
if [[ -z "${BASH_VERSION:-}" ]]; then
echo "This script must be run with Bash." >&2
echo "Try: bash lrops.sh <command> [options]" >&2
exit 1
fi
# =============================================================================
# SCRIPT METADATA AND CONSTANTS
# =============================================================================
SCRIPT_VERSION="1.0.0"
SCRIPT_NAME="lrops.sh"
# Resolve invoking user and home directory robustly (works with sudo)
# Priority: SUDO_UID/SUDO_USER -> tilde expansion -> current $HOME
INVOKING_USER="${SUDO_USER:-${USER:-$(id -un 2>/dev/null || echo "")}}"
if [[ -n "${SUDO_UID:-}" ]]; then
INVOKING_HOME="$(getent passwd "${SUDO_UID}" 2>/dev/null | awk -F: '{print $6}')"
fi
if [[ -z "${INVOKING_HOME:-}" || ! -d "$INVOKING_HOME" ]]; then
if [[ -n "${SUDO_USER:-}" ]]; then
INVOKING_HOME="$(eval echo ~"${SUDO_USER}")"
fi
fi
if [[ -z "${INVOKING_HOME:-}" || ! -d "$INVOKING_HOME" ]]; then
INVOKING_HOME="${HOME}"
fi
# Default install dir: invoking user's home (never root's HOME when run via sudo)
DEFAULT_HOME_DIR="${INVOKING_HOME}"
LIVEREVIEW_INSTALL_DIR="${LIVEREVIEW_INSTALL_DIR:-${DEFAULT_HOME_DIR}/livereview}"
LIVEREVIEW_SCRIPT_PATH="/usr/local/bin/lrops.sh"
GITHUB_REPO="HexmosTech/LiveReview"
GITHUB_API_BASE="https://api.github.com/repos/${GITHUB_REPO}"
DOCKER_REGISTRY="ghcr.io/hexmostech"
DOCKER_IMAGE="livereview"
BACKUP_RETENTION_COUNT="10" # Number of pre-update backups to keep (oldest pruned)
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
BOLD='\033[1m'
GRAY='\033[0;90m'
NC='\033[0m' # No Color
# =============================================================================
# LOGGING AND OUTPUT FUNCTIONS
# =============================================================================
log_info() {
echo -e "${BLUE}ℹ️ INFO:${NC} $*" >&2
}
log_success() {
echo -e "${GREEN}✅ SUCCESS:${NC} $*" >&2
}
log_warning() {
echo -e "${YELLOW}⚠️ WARNING:${NC} $*" >&2
}
log_error() {
echo -e "${RED}❌ ERROR:${NC} $*" >&2
}
log_debug() {
if [[ "${VERBOSE:-false}" == "true" ]]; then
echo -e "${PURPLE}🔍 DEBUG:${NC} $*" >&2
fi
}
progress() {
echo -e "${CYAN}🔄 $*${NC}" >&2
}
section_header() {
echo >&2
echo -e "${BLUE}$(printf '=%.0s' {1..80})${NC}" >&2
echo -e "${BLUE}📋 $*${NC}" >&2
echo -e "${BLUE}$(printf '=%.0s' {1..80})${NC}" >&2
}
# Simple progress countdown with inline updates (one line)
progress_sleep() {
local seconds=${1:-0}
local label=${2:-"Waiting"}
local i
for (( i=1; i<=seconds; i++ )); do
printf "\r%s: %2ds/%2ds" "$label" "$i" "$seconds" >&2
sleep 1
done
echo >&2
}
# =============================================================================
# PORTABLE SED (GNU/BSD) HELPERS
# =============================================================================
# sed -i behaves differently on macOS (BSD sed) vs GNU sed. These helpers
# provide a uniform interface: sed_inplace 's/a/b/' path/to/file
sed_inplace() {
# Usage: sed_inplace 'SED_SCRIPT' FILE
local script="$1"
local file="$2"
case "$(uname -s)" in
Darwin) sed -i '' "$script" "$file" ;;
*) sed -i "$script" "$file" ;;
esac
}
sudo_sed_inplace() {
# Usage: sudo_sed_inplace 'SED_SCRIPT' FILE
local script="$1"
local file="$2"
case "$(uname -s)" in
Darwin) sudo sed -i '' "$script" "$file" ;;
*) sudo sed -i "$script" "$file" ;;
esac
}
# =============================================================================
# ERROR HANDLING AND CLEANUP
# =============================================================================
cleanup() {
local exit_code=$?
if [[ $exit_code -ne 0 ]]; then
log_error "Script failed with exit code $exit_code"
log_info "For troubleshooting help, run: $0 --help"
fi
# Stop sudo keepalive process if running
if [[ -n "${SUDO_REFRESH_PID:-}" ]]; then
kill "${SUDO_REFRESH_PID}" 2>/dev/null || true
fi
exit $exit_code
}
trap cleanup EXIT
error_exit() {
log_error "$1"
exit "${2:-1}"
}
# =============================================================================
# SUDO SESSION AND DOCKER PRIVILEGES
# =============================================================================
# Keep sudo alive during script run to avoid repeated prompts
ensure_sudo_session() {
# Only if not already root and sudo is available
if [[ $EUID -ne 0 ]] && command -v sudo >/dev/null 2>&1; then
log_info "Requesting sudo access upfront (to avoid repeated prompts)..."
if sudo -v; then
# Refresh sudo timestamp in background
(
while true; do
sleep 60
sudo -n true 2>/dev/null || true
done
) &
SUDO_REFRESH_PID=$!
log_debug "Sudo keepalive process started (PID: $SUDO_REFRESH_PID)"
else
log_warning "Could not obtain sudo credentials now; you may be prompted later."
fi
fi
}
# Start sudo keepalive process for self-update operations
start_sudo_keepalive() {
# Only if not already root and sudo is available
if [[ $EUID -ne 0 ]] && command -v sudo >/dev/null 2>&1; then
if sudo -v; then
# Refresh sudo timestamp in background
(
while true; do
sleep 60
sudo -n true 2>/dev/null || true
done
) &
SUDO_REFRESH_PID=$!
log_debug "Sudo keepalive process started (PID: $SUDO_REFRESH_PID)"
else
log_warning "Could not obtain sudo credentials for self-update"
return 1
fi
fi
}
# Stop sudo keepalive process
stop_sudo_keepalive() {
if [[ -n "${SUDO_REFRESH_PID:-}" ]]; then
kill "${SUDO_REFRESH_PID}" 2>/dev/null || true
unset SUDO_REFRESH_PID
log_debug "Sudo keepalive process stopped"
fi
}
# If Docker requires sudo, transparently wrap docker/docker-compose commands
maybe_enable_sudo_for_docker() {
# If docker CLI not present, nothing to do here
command -v docker >/dev/null 2>&1 || return 0
if docker info >/dev/null 2>&1; then
return 0 # No sudo needed
fi
# Try with sudo non-interactively first
if command -v sudo >/dev/null 2>&1 && sudo -n docker info >/dev/null 2>&1; then
:
else
# Fall back to interactive sudo attempt (may prompt once)
if command -v sudo >/dev/null 2>&1 && sudo docker info >/dev/null 2>&1; then
:
else
return 0 # Cannot use sudo either; let the regular checks report errors
fi
fi
# At this point docker works with sudo, set wrappers
log_info "Docker requires sudo; enabling automatic sudo for Docker commands"
USE_SUDO_DOCKER=true
# Define shell function for docker (covers 'docker compose' plugin)
docker() { command sudo docker "$@"; }
}
# =============================================================================
# LIVEREVIEW INSTALLATION DETECTION
# =============================================================================
# Detect LiveReview installation directory automatically
detect_livereview_installation() {
local detected_dir=""
# Method 1: Check default location
local default_dir="${DEFAULT_HOME_DIR}/livereview"
if [[ -f "$default_dir/docker-compose.yml" && -f "$default_dir/.env" ]]; then
detected_dir="$default_dir"
log_debug "Found LiveReview installation at default location: $detected_dir"
fi
# Method 2: Check environment variable override
if [[ -n "${LIVEREVIEW_INSTALL_DIR:-}" && "$LIVEREVIEW_INSTALL_DIR" != "$default_dir" ]]; then
if [[ -f "$LIVEREVIEW_INSTALL_DIR/docker-compose.yml" && -f "$LIVEREVIEW_INSTALL_DIR/.env" ]]; then
detected_dir="$LIVEREVIEW_INSTALL_DIR"
log_debug "Found LiveReview installation at specified location: $detected_dir"
fi
fi
# Method 3: Check other common locations
if [[ -z "$detected_dir" ]]; then
local common_locations=(
"$default_dir"
"./livereview"
"."
)
for location in "${common_locations[@]}"; do
if [[ -f "$location/docker-compose.yml" && -f "$location/.env" ]]; then
# Verify it's actually a LiveReview installation by checking for specific content
if grep -q "livereview-app\|livereview-db" "$location/docker-compose.yml" 2>/dev/null; then
detected_dir="$(realpath "$location")"
log_debug "Found LiveReview installation at: $detected_dir"
break
fi
fi
done
fi
# Method 4: Try to detect from running Docker containers
if [[ -z "$detected_dir" ]] && command -v docker >/dev/null 2>&1; then
log_debug "Attempting to detect installation from running containers..."
# Look for LiveReview containers and try to find their compose file
local container_id
container_id=$(docker ps --filter "name=livereview" --format "{{.ID}}" | head -1)
if [[ -n "$container_id" ]]; then
# Try to get the working directory or volume mounts
local inspect_result
inspect_result=$(docker inspect "$container_id" 2>/dev/null || echo "")
if [[ -n "$inspect_result" ]]; then
# Look for volume mounts that might indicate the installation directory
local possible_dirs
possible_dirs=$(echo "$inspect_result" | grep -oE '"/[^"]*livereview[^"]*"' | tr -d '"' | grep -v '/var/lib/docker' | head -5)
for dir in $possible_dirs; do
# Try parent directories
local parent_dir
parent_dir="$(dirname "$dir")"
if [[ -f "$parent_dir/docker-compose.yml" && -f "$parent_dir/.env" ]]; then
detected_dir="$parent_dir"
log_debug "Detected installation from container volume mount: $detected_dir"
break
fi
done
fi
fi
fi
# Method 5: Search filesystem (last resort, limited scope)
if [[ -z "$detected_dir" ]]; then
log_debug "Searching filesystem for LiveReview installation..."
local search_paths=("${DEFAULT_HOME_DIR}" ".")
for search_path in "${search_paths[@]}"; do
if [[ -d "$search_path" ]]; then
local found_path
found_path=$(find "$search_path" -maxdepth 2 -name "docker-compose.yml" -path "*/livereview/*" 2>/dev/null | head -1)
if [[ -n "$found_path" ]]; then
local candidate_dir
candidate_dir="$(dirname "$found_path")"
if [[ -f "$candidate_dir/.env" ]] && grep -q "livereview-app\|livereview-db" "$found_path" 2>/dev/null; then
detected_dir="$candidate_dir"
log_debug "Found LiveReview installation via filesystem search: $detected_dir"
break
fi
fi
fi
done
fi
# Update the global variable if we found an installation
if [[ -n "$detected_dir" ]]; then
LIVEREVIEW_INSTALL_DIR="$detected_dir"
log_debug "LiveReview installation detected at: $LIVEREVIEW_INSTALL_DIR"
return 0
else
log_debug "No existing LiveReview installation detected, using default: $LIVEREVIEW_INSTALL_DIR"
return 1
fi
}
# =============================================================================
# DOCKER COMPOSE COMPATIBILITY
# =============================================================================
# Global variable to store the correct docker compose command
DOCKER_COMPOSE_CMD=""
# Detect and set the correct docker compose command
detect_docker_compose_cmd() {
if command -v docker-compose >/dev/null 2>&1; then
# Legacy docker-compose is available
if [[ "${USE_SUDO_DOCKER:-false}" == "true" ]]; then
DOCKER_COMPOSE_CMD="sudo docker-compose"
else
DOCKER_COMPOSE_CMD="docker-compose"
fi
log_debug "Using legacy docker-compose command"
elif docker compose version >/dev/null 2>&1; then
# Modern docker compose plugin is available
# 'docker' may already be wrapped to sudo by maybe_enable_sudo_for_docker
DOCKER_COMPOSE_CMD="docker compose"
log_debug "Using modern docker compose plugin"
else
log_error "Neither docker-compose nor docker compose is available"
return 1
fi
return 0
}
# Wrapper function to execute docker compose commands
docker_compose() {
if [[ -z "$DOCKER_COMPOSE_CMD" ]]; then
if ! detect_docker_compose_cmd; then
return 1
fi
fi
# If we have an install directory and docker-compose.yml exists there, use it explicitly
local compose_file=""
if [[ -n "$LIVEREVIEW_INSTALL_DIR" && -f "$LIVEREVIEW_INSTALL_DIR/docker-compose.yml" ]]; then
compose_file="-f $LIVEREVIEW_INSTALL_DIR/docker-compose.yml"
fi
log_debug "Executing: $DOCKER_COMPOSE_CMD $compose_file $*"
$DOCKER_COMPOSE_CMD $compose_file "$@"
}
# =============================================================================
# ARGUMENT PARSING
# =============================================================================
# Default values
EXPRESS_MODE=false
FORCE_INSTALL=false
DRY_RUN=false
VERBOSE=false
DEBUG_MODE=false
LIVEREVIEW_VERSION=""
SHOW_HELP=false
SHOW_VERSION=false
# Test flags (for development)
TEST_GITHUB_API=false
TEST_EXTRACT=false
EXTRACT_TO=""
LIST_EMBEDDED_DATA=false
SHOW_LATEST_VERSION=false
LIST_VERSIONS=false
GENERATE_CONFIG_ONLY=false
INSTALL_TEMPLATES_ONLY=false
OUTPUT_DIR=""
INSTALL_SELF=false
DIAGNOSE=false
BACKUP_TARGET_DIR=""
parse_arguments() {
while [[ $# -gt 0 ]]; do
case $1 in
--express)
EXPRESS_MODE=true
shift
;;
--force)
FORCE_INSTALL=true
shift
;;
--dry-run)
DRY_RUN=true
shift
;;
--verbose|-v)
VERBOSE=true
shift
;;
--debug)
DEBUG_MODE=true
VERBOSE=true
shift
;;
--version)
if [[ -n "${2:-}" && ! "$2" =~ ^-- ]]; then
LIVEREVIEW_VERSION="$2"
shift 2
else
SHOW_VERSION=true
shift
fi
;;
--help|-h)
SHOW_HELP=true
shift
;;
# Skip new commands as they're handled in main()
setup-demo|setup-production)
# These are handled in main() case statement, skip here
shift
;;
# Test and development flags
--test-github-api)
TEST_GITHUB_API=true
shift
;;
--test-extract)
TEST_EXTRACT=true
if [[ -n "${2:-}" && ! "$2" =~ ^-- ]]; then
EXTRACT_TO="$2"
shift 2
else
shift
fi
;;
--extract-to)
EXTRACT_TO="$2"
shift 2
;;
--list-embedded-data)
LIST_EMBEDDED_DATA=true
shift
;;
--show-latest-version)
SHOW_LATEST_VERSION=true
shift
;;
--list-versions)
LIST_VERSIONS=true
shift
;;
--generate-config-only)
GENERATE_CONFIG_ONLY=true
shift
;;
--install-templates-only)
INSTALL_TEMPLATES_ONLY=true
shift
;;
--output-dir)
OUTPUT_DIR="$2"
shift 2
;;
--install-self)
INSTALL_SELF=true
shift
;;
--diagnose)
DIAGNOSE=true
shift
;;
--backup-dir=*)
BACKUP_TARGET_DIR="${1#*=}"
shift
;;
--backup-dir)
BACKUP_TARGET_DIR="$2"
shift 2
;;
--show-plan)
DRY_RUN=true
VERBOSE=true
shift
;;
--*)
log_error "Unknown option: $1"
show_help
exit 1
;;
*)
# Not an option (doesn't start with --), so stop parsing
# This allows commands like 'show-mode' to be handled by main()
break
;;
esac
done
}
# =============================================================================
# HELP AND VERSION DISPLAY
# =============================================================================
show_version() {
echo "LiveReview Operations Script (lrops.sh) v${SCRIPT_VERSION}"
echo "Repository: https://github.com/${GITHUB_REPO}"
echo "Docker Registry: ${DOCKER_REGISTRY}/${DOCKER_IMAGE}"
}
show_help() {
cat << 'EOF'
LiveReview Operations Script (lrops.sh)
USAGE:
# Quick installation (recommended)
curl -fsSL https://raw.githubusercontent.com/HexmosTech/LiveReview/main/lrops.sh | bash -s -- --express
# Two-mode setup commands (new!)
lrops.sh setup-demo # Quick demo mode setup (localhost only)
lrops.sh setup-production # Production mode setup (with reverse proxy)
# Interactive installation
curl -fsSL https://raw.githubusercontent.com/HexmosTech/LiveReview/main/lrops.sh | bash
# Specific version installation
curl -fsSL https://raw.githubusercontent.com/HexmosTech/LiveReview/main/lrops.sh | bash -s -- --version=v1.2.3 --express
# Management commands (after installation)
lrops.sh status # Show installation status
lrops.sh info # Show installation details and file locations
lrops.sh start # Start LiveReview services
lrops.sh stop # Stop LiveReview services
lrops.sh restart # Restart LiveReview services
lrops.sh update [version] # Pull newer image (or specific version) and restart
lrops.sh backup [--backup-dir <path>] [name] # Create manual backup (see detailed options below)
lrops.sh quick-backup # Create quick timestamped backup
lrops.sh list-backups # List all available backups
lrops.sh backup-info <name> # Show detailed information about a backup
lrops.sh delete-backup <name> # Delete a specific backup
lrops.sh restore <id|latest> # Restore a previous backup
lrops.sh set-mode <demo|production> # Switch between demo and production modes
lrops.sh show-mode # Show current deployment mode and configuration
lrops.sh self-update # Update this script to the latest version from GitHub
# Backup options:
(Use --backup-dir as backup subcommand option - see BACKUP OPTIONS below)
lrops.sh uninstall # Safely uninstall (moves directory, keeps backups)
lrops.sh logs [service] # Show container logs
lrops.sh env validate # Validate .env and suggest fixes
lrops.sh help ssl # SSL/TLS setup guidance
lrops.sh help backup # Backup strategies
lrops.sh help nginx # Nginx reverse proxy setup
lrops.sh help caddy # Caddy reverse proxy setup
lrops.sh help apache # Apache reverse proxy setup
INSTALLATION OPTIONS:
--express Use secure defaults, no prompts (demo mode)
--force Overwrite existing installation
--version=v1.2.3 Install specific version (default: latest)
--dry-run Show what would be done without installing
--verbose, -v Enable verbose output
--debug Enable bash debug tracing (set -x, also enables verbose output)
MANAGEMENT OPTIONS:
--help, -h Show this help message
--version Show script version
--diagnose Run diagnostic checks
TWO-MODE DEPLOYMENT SYSTEM:
Demo Mode (default): Perfect for localhost development and testing
- Access: http://localhost:8081/
- Webhooks: Disabled (manual triggers only)
- No external access required
Production Mode: Ready for external access with reverse proxy
- Requires reverse proxy setup
- Webhooks enabled for automatic triggers
- SSL/TLS recommended
TEMPLATE & CONFIGURATION OPTIONS:
--list-embedded-data List all available embedded templates
--test-extract <template> Test extraction of specific template
--extract-to <directory> Extract all templates to directory
DEVELOPMENT & TESTING:
--test-github-api Test GitHub Container Registry API
--show-latest-version Show latest available version
--list-versions List all available versions
SAFETY:
This script will NOT overwrite existing installations unless --force is specified.
All configuration prompts have secure defaults.
Express mode requires no user input and completes in under 5 minutes.
BACKUP OPTIONS:
1. Default backup (to installation directory):
lrops.sh backup # Auto-named: manual-YYYYMMDD_HHMMSS
2. Named backup (to installation directory):
lrops.sh backup my-backup-name # Custom name: my-backup-name-YYYYMMDD_HHMMSS
3. Backup to custom directory:
lrops.sh backup --backup-dir /path/to/backups
lrops.sh backup --backup-dir /path/to/backups custom-name
4. Quick timestamped backup:
lrops.sh quick-backup # Creates: quickbackup-YYYYMMDD_HHMMSS
EXAMPLES:
# Quick demo setup (recommended for first time)
lrops.sh setup-demo
# Production setup with reverse proxy
lrops.sh setup-production
# Force reinstall with specific version
lrops.sh --force --version=v1.2.3 --express
# Preview installation plan
lrops.sh --dry-run --verbose
# Backup examples
lrops.sh backup # Default backup
lrops.sh backup before-upgrade # Named backup
lrops.sh backup --backup-dir ~/my-backups # Custom directory
lrops.sh backup --backup-dir ~/my-backups my-name # Custom directory + name
For more information, visit: https://github.com/HexmosTech/LiveReview
EOF
}
# =============================================================================
# SYSTEM CHECKS AND PREREQUISITES
# =============================================================================
check_system_prerequisites() {
section_header "CHECKING SYSTEM PREREQUISITES"
local errors=0
# Check if running as root (we'll need sudo for some operations)
if [[ $EUID -eq 0 ]] && [[ "${INSTALL_SELF:-false}" != "true" ]]; then
log_warning "Running as root. Consider running as regular user with sudo access."
fi
# Check for required commands
local required_commands=("curl" "docker" "jq")
for cmd in "${required_commands[@]}"; do
if ! command -v "$cmd" &> /dev/null; then
log_error "$cmd is required but not installed"
((errors++))
else
log_success "$cmd is available"
fi
done
# Check Docker daemon
if command -v docker &> /dev/null; then
if ! docker info &> /dev/null; then
log_error "Docker daemon is not running or accessible"
log_info "Try: sudo systemctl start docker"
((errors++))
else
local docker_version=$(docker --version | cut -d' ' -f3 | sed 's/,//')
log_success "Docker daemon is running (version: $docker_version)"
fi
fi
# Check Docker Compose (both legacy and modern)
if detect_docker_compose_cmd; then
local compose_version
if [[ "$DOCKER_COMPOSE_CMD" == "docker-compose" ]]; then
compose_version=$(docker-compose --version | cut -d' ' -f3 | sed 's/,//')
log_success "Docker Compose is available (legacy docker-compose, version: $compose_version)"
else
compose_version=$(docker compose version --short 2>/dev/null || docker compose version | grep -o '[0-9][0-9.]*' | head -1)
log_success "Docker Compose is available (modern docker compose plugin, version: $compose_version)"
fi
else
log_error "Docker Compose is not available (neither docker-compose nor docker compose plugin)"
log_info "Install docker-compose or use Docker Desktop with the compose plugin"
((errors++))
fi
# Check system architecture
local arch=$(uname -m)
case $arch in
x86_64)
log_success "Architecture: amd64 (supported)"
;;
aarch64|arm64)
log_success "Architecture: arm64 (supported)"
;;
*)
log_warning "Architecture: $arch (may not be supported)"
;;
esac
# Check available disk space on target filesystem
local target_fs
target_fs="${LIVEREVIEW_INSTALL_DIR:-${DEFAULT_HOME_DIR}/livereview}"
mkdir -p "$target_fs" 2>/dev/null || true
local available_space
available_space=$(df -P "$target_fs" 2>/dev/null | awk 'NR==2 {print $4}' || echo "0")
if [[ ${available_space:-0} -lt 2097152 ]]; then # 2GB in KB
log_warning "Low disk space for $target_fs. At least 2GB recommended."
else
log_success "Sufficient disk space available"
fi
if [[ $errors -gt 0 ]]; then
error_exit "System prerequisites check failed. Please install missing dependencies."
fi
log_success "All system prerequisites satisfied"
}
check_existing_installation() {
section_header "CHECKING FOR EXISTING INSTALLATION"
local installation_exists=false
# Check for installation directory
if [[ -d "$LIVEREVIEW_INSTALL_DIR" ]]; then
log_warning "Installation directory exists: $LIVEREVIEW_INSTALL_DIR"
installation_exists=true
fi
# Check for running containers
if docker ps --format "table {{.Names}}" | grep -q "livereview"; then
log_warning "LiveReview containers are currently running"
installation_exists=true
fi
# Check for installed script
if [[ -f "$LIVEREVIEW_SCRIPT_PATH" ]]; then
local installed_version=$("$LIVEREVIEW_SCRIPT_PATH" --version 2>/dev/null | head -1 || echo "unknown")
log_info "LiveReview script already installed: $installed_version"
fi
if [[ "$installation_exists" == "true" ]]; then
if [[ "$FORCE_INSTALL" != "true" ]]; then
log_error "Existing LiveReview installation detected"
log_info "Use --force to overwrite existing installation"
log_info "Or run 'lrops.sh status' to check current installation"
error_exit "Installation aborted to prevent data loss" 2
else
log_warning "Proceeding with --force flag (existing installation will be overwritten)"
fi
else
log_success "No existing installation detected"
fi
}
# =============================================================================
# VERSION MANAGEMENT AND GITHUB INTEGRATION
# =============================================================================
# GitHub Container Registry API endpoints
GHCR_TOKEN_URL="https://ghcr.io/token"
GHCR_REGISTRY_URL="https://ghcr.io/v2"
# Semantic version regex pattern
SEMVER_PATTERN="^v?([0-9]+)\.([0-9]+)\.([0-9]+)(-[a-zA-Z0-9\-\.]+)?(\+[a-zA-Z0-9\-\.]+)?$"
get_ghcr_token() {
local repo="$1"
log_debug "Getting anonymous token for repository: $repo"
local token_response
if ! token_response=$(curl -s --fail --connect-timeout 10 \
"${GHCR_TOKEN_URL}?service=ghcr.io&scope=repository:${repo}:pull" 2>/dev/null); then
log_error "Failed to get authentication token from GitHub Container Registry"
return 1
fi
local token
if ! token=$(echo "$token_response" | jq -r '.token' 2>/dev/null); then
log_error "Failed to parse authentication token"
return 1
fi
if [[ "$token" == "null" || -z "$token" ]]; then
log_error "Invalid authentication token received"
return 1
fi
echo "$token"
}
query_ghcr_tags() {
local repo="$1"
log_debug "Querying GHCR tags for repository: $repo"
local token
if ! token=$(get_ghcr_token "$repo"); then
return 1
fi
local tags_response
if ! tags_response=$(curl -s --fail --connect-timeout 10 \
-H "Authorization: Bearer $token" \
"${GHCR_REGISTRY_URL}/${repo}/tags/list" 2>/dev/null); then
log_error "Failed to query container registry for available tags"
return 1
fi
echo "$tags_response"
}
get_available_versions() {
local repo="${1:-hexmostech/livereview}"
log_debug "Getting available versions for $repo"
local response
if ! response=$(query_ghcr_tags "$repo"); then
return 1
fi
# Extract tags from the API response and filter for semantic versions
local tags
if ! tags=$(echo "$response" | jq -r '.tags[]?' 2>/dev/null); then
log_error "Failed to parse tags from container registry response"
return 1
fi
# Filter for semantic versions and sort
echo "$tags" | grep -E "$SEMVER_PATTERN" | sort -V -r || {
log_debug "No semantic version tags found, checking available tags..."
if [[ -n "$tags" ]]; then
log_warning "Available tags (non-semantic versions):"
echo "$tags" | sed 's/^/ - /' >&2
fi
return 1
}
}
get_latest_version() {
local repo="${1:-hexmostech/livereview}"
log_debug "Determining latest semantic version for $repo"
local versions
if ! versions=$(get_available_versions "$repo"); then
return 1
fi
if [[ -z "$versions" ]]; then
log_error "No semantic version tags found for $repo"
log_info "Available tags might use different naming scheme"
return 1
fi
# Return the first (highest) version
echo "$versions" | head -1
}
validate_version_exists() {
local version="$1"
local repo="${2:-hexmostech/livereview}"
log_debug "Validating that version $version exists for $repo"
# Get all tags (not just semantic versions) for validation
local response
if ! response=$(query_ghcr_tags "$repo"); then
return 1
fi
local all_tags
if ! all_tags=$(echo "$response" | jq -r '.tags[]?' 2>/dev/null); then
log_error "Failed to parse tags from container registry response"
return 1
fi
if echo "$all_tags" | grep -q "^${version}$"; then
log_debug "Version $version found in available tags"
return 0
else
log_error "Version $version not found in available tags"
log_info "Available tags:"
echo "$all_tags" | head -10 | sed 's/^/ - /' >&2
return 1
fi
}
is_semantic_version() {
local version="$1"
if [[ "$version" =~ $SEMVER_PATTERN ]]; then
return 0
else
return 1
fi
}
normalize_version_tag() {
local version="$1"
# Remove 'v' prefix if present for Docker image tags
echo "$version" | sed 's/^v//'
}
resolve_version() {
local requested_version="$1"
local repo="${2:-hexmostech/livereview}"
if [[ -z "$requested_version" || "$requested_version" == "latest" ]]; then
log_info "Resolving latest semantic version..."
local latest_version
if ! latest_version=$(get_latest_version "$repo"); then
log_warning "No semantic versions found, falling back to 'latest' tag"
echo "latest"
return 0
fi
log_success "Latest semantic version: $latest_version"
normalize_version_tag "$latest_version"
else
# Validate specific version
log_info "Validating requested version: $requested_version"
if ! validate_version_exists "$requested_version" "$repo"; then
error_exit "Version $requested_version not found"
fi
log_success "Version $requested_version is valid"
normalize_version_tag "$requested_version"
fi
}
# Test functions for development and validation
test_github_api() {
section_header "TESTING GITHUB CONTAINER REGISTRY API"
local repo="hexmostech/livereview"
log_info "Testing GHCR API connectivity..."
local token
if token=$(get_ghcr_token "$repo"); then
log_success "Successfully obtained authentication token"
log_debug "Token: ${token:0:20}..."
else
log_error "Failed to get authentication token"
return 1
fi
log_info "Fetching available tags..."
local response
if response=$(query_ghcr_tags "$repo"); then
log_success "Successfully queried container registry"
echo "$response" | jq '.' 2>/dev/null || echo "$response"
else
log_error "Failed to query container registry"