forked from binary-knight/logwhisperer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·1154 lines (961 loc) · 32 KB
/
install.sh
File metadata and controls
executable file
·1154 lines (961 loc) · 32 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
set -euo pipefail
# Script metadata
readonly SCRIPT_VERSION="1.1.0"
readonly SCRIPT_NAME="LogWhisperer Installer"
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Color codes for output
readonly GREEN='\033[0;32m'
readonly RED='\033[0;31m'
readonly YELLOW='\033[1;33m'
readonly BLUE='\033[0;34m'
readonly NC='\033[0m' # No Color
# Installation defaults
readonly DEFAULT_MODEL="mistral"
readonly DEFAULT_INSTALL_DIR="/opt/logwhisperer"
readonly DEFAULT_CONFIG_DIR="/etc/logwhisperer"
readonly DEFAULT_LOG_DIR="/var/log/logwhisperer"
readonly DEFAULT_BACKUP_DIR="/var/backups/logwhisperer"
readonly SUPPORTED_MODELS=("mistral" "llama2" "codellama" "phi" "gemma" "tinyllama" "dolphin-mixtral")
readonly REQUIRED_COMMANDS=("curl" "systemctl" "grep" "awk" "sed")
readonly PYTHON_REQUIRED="3.7"
# Installation options
INSTALL_MODEL="$DEFAULT_MODEL"
INSTALL_DIR="$DEFAULT_INSTALL_DIR"
CONFIG_DIR="$DEFAULT_CONFIG_DIR"
LOG_DIR="$DEFAULT_LOG_DIR"
INSTALL_SERVICE=false
INSTALL_WEB=false
SKIP_OLLAMA=false
UNINSTALL=false
UPGRADE=false
DRY_RUN=false
VERBOSE=false
FORCE=false
DEV_MODE=false
DEV_KEY=""
# Logging setup
LOGFILE="${SCRIPT_DIR}/install_$(date +%Y%m%d_%H%M%S).log"
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1> >(tee -a "$LOGFILE")
exec 2>&1
# Utility functions
log() {
echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $*"
}
error() {
echo -e "${RED}[ERROR]${NC} $*" >&2
}
warning() {
echo -e "${YELLOW}[WARNING]${NC} $*"
}
info() {
echo -e "${BLUE}[INFO]${NC} $*"
}
verbose() {
if [[ "$VERBOSE" == true ]]; then
echo -e "${BLUE}[VERBOSE]${NC} $*"
fi
}
die() {
error "$@"
exit 1
}
confirm() {
local prompt="${1:-Continue?}"
local response
if [[ "$FORCE" == true ]]; then
return 0
fi
while true; do
read -p "$prompt (y/N): " response
case "${response,,}" in
y|yes) return 0 ;;
n|no|"") return 1 ;;
*) echo "Please answer yes or no." ;;
esac
done
}
# Help function
show_help() {
cat << EOF
$SCRIPT_NAME v$SCRIPT_VERSION
Usage: $0 [OPTIONS]
OPTIONS:
-h, --help Show this help message
-v, --version Show version information
-V, --verbose Enable verbose output
-f, --force Skip confirmation prompts
-n, --dry-run Show what would be done without making changes
--model MODEL LLM model to install (default: $DEFAULT_MODEL)
--install-dir DIR Installation directory (default: $DEFAULT_INSTALL_DIR)
--config-dir DIR Configuration directory (default: $DEFAULT_CONFIG_DIR)
--log-dir DIR Log directory (default: $DEFAULT_LOG_DIR)
--with-service Install and enable systemd service
--with-web Install web dashboard components
--skip-ollama Skip Ollama installation
--upgrade Upgrade existing installation
--uninstall Uninstall LogWhisperer
--dev-mode Enable development mode (bypass license)
--dev-key KEY Development key for dev mode
SUPPORTED MODELS:
${SUPPORTED_MODELS[*]}
EXAMPLES:
# Basic installation
$0
# Install with specific model, service, and web dashboard
$0 --model llama2 --with-service --with-web
# Upgrade existing installation
$0 --upgrade
# Uninstall
$0 --uninstall
EOF
}
# Parse command line arguments
parse_args() {
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
-v|--version)
echo "$SCRIPT_NAME v$SCRIPT_VERSION"
exit 0
;;
-V|--verbose)
VERBOSE=true
shift
;;
-f|--force)
FORCE=true
shift
;;
-n|--dry-run)
DRY_RUN=true
shift
;;
--model)
INSTALL_MODEL="$2"
shift 2
;;
--install-dir)
INSTALL_DIR="$2"
shift 2
;;
--config-dir)
CONFIG_DIR="$2"
shift 2
;;
--log-dir)
LOG_DIR="$2"
shift 2
;;
--with-service)
INSTALL_SERVICE=true
shift
;;
--with-web)
INSTALL_WEB=true
shift
;;
--skip-ollama)
SKIP_OLLAMA=true
shift
;;
--upgrade)
UPGRADE=true
shift
;;
--uninstall)
UNINSTALL=true
shift
;;
--dev-mode)
DEV_MODE=true
shift
;;
--dev-key)
DEV_KEY="$2"
shift 2
;;
*)
error "Unknown option: $1"
show_help
exit 1
;;
esac
done
}
# Check system requirements
check_requirements() {
info "Checking system requirements..."
# Check OS
if [[ "$OSTYPE" != "linux-gnu"* ]]; then
die "This installer only supports Linux systems"
fi
# Check if running as root when needed
if [[ "$INSTALL_SERVICE" == true || "$INSTALL_WEB" == true ]] && [[ "$EUID" -ne 0 ]]; then
die "Service/Web installation requires root privileges. Please run with sudo."
fi
# Check required commands
local missing_commands=()
for cmd in "${REQUIRED_COMMANDS[@]}"; do
if ! command -v "$cmd" &>/dev/null; then
missing_commands+=("$cmd")
fi
done
if [[ ${#missing_commands[@]} -gt 0 ]]; then
die "Missing required commands: ${missing_commands[*]}"
fi
# Check Python if web dashboard is requested
if [[ "$INSTALL_WEB" == true ]]; then
if ! command -v python3 &>/dev/null; then
die "Python 3 is required for web dashboard"
fi
# Check Python version
local python_version=$(python3 -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')
local required_major=$(echo $PYTHON_REQUIRED | cut -d. -f1)
local required_minor=$(echo $PYTHON_REQUIRED | cut -d. -f2)
local actual_major=$(echo $python_version | cut -d. -f1)
local actual_minor=$(echo $python_version | cut -d. -f2)
if [[ $actual_major -lt $required_major ]] || \
[[ $actual_major -eq $required_major && $actual_minor -lt $required_minor ]]; then
die "Python $PYTHON_REQUIRED or higher is required (found $python_version)"
fi
# Check pip
if ! command -v pip3 &>/dev/null; then
die "pip3 is required for web dashboard installation"
fi
fi
# Check disk space
local check_dir="$INSTALL_DIR"
if [[ ! -d "$check_dir" ]]; then
check_dir="$(dirname "$INSTALL_DIR")"
while [[ ! -d "$check_dir" ]] && [[ "$check_dir" != "/" ]]; do
check_dir="$(dirname "$check_dir")"
done
fi
verbose "Checking disk space on: $check_dir"
local free_space
free_space=$(df -BG "$check_dir" 2>/dev/null | awk 'NR==2 {print $4}' | sed 's/G//')
if [[ -n "$free_space" ]] && [[ "$free_space" -lt 1 ]]; then
warning "Less than 1GB free space available in $check_dir"
fi
# Check if model is supported
if [[ ! " ${SUPPORTED_MODELS[*]} " =~ " $INSTALL_MODEL " ]]; then
die "Unsupported model: $INSTALL_MODEL. Supported models: ${SUPPORTED_MODELS[*]}"
fi
verbose "System requirements check passed"
}
# Backup existing installation
backup_existing() {
if [[ -d "$INSTALL_DIR" ]]; then
info "Backing up existing installation..."
local backup_name="logwhisperer_backup_$(date +%Y%m%d_%H%M%S)"
local backup_path="${DEFAULT_BACKUP_DIR}/${backup_name}"
if [[ "$DRY_RUN" == true ]]; then
info "[DRY RUN] Would create backup at $backup_path"
else
mkdir -p "$DEFAULT_BACKUP_DIR"
cp -r "$INSTALL_DIR" "$backup_path"
info "Backup created at $backup_path"
fi
fi
}
# Install Ollama
install_ollama() {
if [[ "$SKIP_OLLAMA" == true ]]; then
info "Skipping Ollama installation (--skip-ollama specified)"
return 0
fi
if command -v ollama &>/dev/null; then
info "Ollama is already installed"
return 0
fi
info "Installing Ollama..."
if [[ "$DRY_RUN" == true ]]; then
info "[DRY RUN] Would install Ollama"
return 0
fi
# Download and verify Ollama installer
local ollama_installer="/tmp/ollama_install.sh"
curl -fsSL https://ollama.com/install.sh -o "$ollama_installer" || die "Failed to download Ollama installer"
# Run installer
bash "$ollama_installer" || die "Failed to install Ollama"
rm -f "$ollama_installer"
# Wait for Ollama to start
info "Waiting for Ollama to start..."
local max_attempts=30
local attempt=0
while [[ $attempt -lt $max_attempts ]]; do
if curl -s http://localhost:11434 >/dev/null 2>&1; then
info "Ollama is running"
break
fi
sleep 1
((attempt++))
done
if [[ $attempt -eq $max_attempts ]]; then
die "Ollama failed to start within 30 seconds"
fi
}
# Pull Ollama models
pull_models() {
if [[ "$SKIP_OLLAMA" == true ]]; then
return 0
fi
info "Pulling Ollama models..."
if [[ "$DRY_RUN" == true ]]; then
info "[DRY RUN] Would pull models: $INSTALL_MODEL, phi"
return 0
fi
# Pull requested model
info "Pulling model: $INSTALL_MODEL"
ollama pull "$INSTALL_MODEL" || warning "Failed to pull model: $INSTALL_MODEL"
# Always pull phi as a lightweight backup model
if [[ "$INSTALL_MODEL" != "phi" ]]; then
info "Pulling backup model: phi"
ollama pull phi || warning "Failed to pull backup model: phi"
fi
}
# Create directory structure
create_directories() {
info "Creating directory structure..."
local dirs=(
"$INSTALL_DIR"
"$INSTALL_DIR/bin"
"$INSTALL_DIR/modules"
"$INSTALL_DIR/reports"
"$CONFIG_DIR"
"$LOG_DIR"
)
# Add web directories if needed
if [[ "$INSTALL_WEB" == true ]]; then
dirs+=(
"$INSTALL_DIR/web"
"$INSTALL_DIR/web/templates"
"$INSTALL_DIR/web/static"
)
fi
for dir in "${dirs[@]}"; do
if [[ "$DRY_RUN" == true ]]; then
info "[DRY RUN] Would create directory: $dir"
else
mkdir -p "$dir"
verbose "Created directory: $dir"
fi
done
}
# Install Python virtual environment for web
install_web_venv() {
if [[ "$INSTALL_WEB" != true ]] || [[ "$DRY_RUN" == true ]]; then
return 0
fi
info "Creating Python virtual environment for web dashboard..."
local venv_dir="$INSTALL_DIR/web-venv"
# Create virtual environment
python3 -m venv "$venv_dir" || die "Failed to create virtual environment"
# Activate and install dependencies
source "$venv_dir/bin/activate"
# Upgrade pip
pip install --upgrade pip >/dev/null 2>&1
# Install web dependencies
local web_deps=(
"flask>=2.0.0"
"flask-socketio>=5.0.0"
"flask-compress>=1.10.0"
"flask-cors>=3.0.0"
"python-socketio[client]>=5.0.0"
"werkzeug>=2.0.0"
"pyyaml>=5.4.0"
"requests>=2.25.0"
"gunicorn>=20.1.0"
"eventlet>=0.30.0"
)
for dep in "${web_deps[@]}"; do
pip install "$dep" || warning "Failed to install $dep"
done
deactivate
info "Web dependencies installed successfully"
}
# Install files
install_files() {
info "Installing LogWhisperer files..."
# Check if required files exist
if [[ ! -f "$SCRIPT_DIR/bin/logwhisperer" ]]; then
die "Required file not found: bin/logwhisperer"
fi
if [[ ! -x "$SCRIPT_DIR/bin/logwhisperer" ]]; then
die "logwhisperer binary is not executable"
fi
# Check for config
local config_source=""
if [[ -f "$SCRIPT_DIR/config/config.yaml.example" ]]; then
config_source="$SCRIPT_DIR/config/config.yaml.example"
elif [[ -f "$SCRIPT_DIR/config/config.yaml" ]]; then
config_source="$SCRIPT_DIR/config/config.yaml"
else
die "Required file not found: config/config.yaml or config/config.yaml.example"
fi
if [[ "$DRY_RUN" == true ]]; then
info "[DRY RUN] Would install files to $INSTALL_DIR"
return 0
fi
# Install binary
cp "$SCRIPT_DIR/bin/logwhisperer" "$INSTALL_DIR/bin/"
chmod +x "$INSTALL_DIR/bin/logwhisperer"
info "Installed: logwhisperer binary"
# Install modules directory
if [[ -d "$SCRIPT_DIR/modules" ]]; then
cp -r "$SCRIPT_DIR/modules" "$INSTALL_DIR/"
info "Installed: modules directory"
fi
# Install web components if requested
if [[ "$INSTALL_WEB" == true ]]; then
# API server
if [[ -f "$SCRIPT_DIR/web/api_server.py" ]]; then
cp "$SCRIPT_DIR/web/api_server.py" "$INSTALL_DIR/web/"
elif [[ -f "$SCRIPT_DIR/api_server.py" ]]; then
cp "$SCRIPT_DIR/api_server.py" "$INSTALL_DIR/web/"
else
warning "api_server.py not found, web dashboard may not function"
fi
# WSGI entry point
if [[ -f "$SCRIPT_DIR/web/wsgi.py" ]]; then
cp "$SCRIPT_DIR/web/wsgi.py" "$INSTALL_DIR/web/"
elif [[ -f "$SCRIPT_DIR/wsgi.py" ]]; then
cp "$SCRIPT_DIR/wsgi.py" "$INSTALL_DIR/web/"
else
# Create wsgi.py if it doesn't exist
cat > "$INSTALL_DIR/web/wsgi.py" << 'EOF'
#!/usr/bin/env python3
"""WSGI entry point for LogWhisperer API Server"""
import os
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
from api_server import create_app
config_path = os.environ.get('LOGWHISPERER_CONFIG', '/etc/logwhisperer/config.yaml')
app, socketio = create_app(config_path)
application = app
EOF
chmod 644 "$INSTALL_DIR/web/wsgi.py"
info "Created wsgi.py entry point"
fi
# Dashboard
if [[ -f "$SCRIPT_DIR/web/dashboard.py" ]]; then
cp "$SCRIPT_DIR/web/dashboard.py" "$INSTALL_DIR/web/"
elif [[ -f "$SCRIPT_DIR/dashboard.py" ]]; then
cp "$SCRIPT_DIR/dashboard.py" "$INSTALL_DIR/web/"
else
warning "dashboard.py not found, web dashboard may not function"
fi
# Templates
if [[ -d "$SCRIPT_DIR/web/templates" ]]; then
cp -r "$SCRIPT_DIR/web/templates/"* "$INSTALL_DIR/web/templates/"
elif [[ -d "$SCRIPT_DIR/templates" ]]; then
cp -r "$SCRIPT_DIR/templates/"* "$INSTALL_DIR/web/templates/"
fi
info "Installed: web dashboard components"
fi
# Install config
if [[ ! -f "$CONFIG_DIR/config.yaml" ]]; then
cp "$config_source" "$CONFIG_DIR/config.yaml"
chmod 644 "$CONFIG_DIR/config.yaml"
info "Installed default configuration"
# Update config with web settings if web is installed
if [[ "$INSTALL_WEB" == true ]]; then
if ! grep -q "^web:" "$CONFIG_DIR/config.yaml"; then
cat >> "$CONFIG_DIR/config.yaml" << 'EOF'
# Web Dashboard Configuration
web:
enabled: true
host: "0.0.0.0"
port: 5123
api_url: "http://localhost:5124"
users:
admin: "changeme" # CHANGE THIS PASSWORD!
session_timeout: 3600
max_log_entries: 1000
update_interval: 1.0
EOF
info "Added web configuration to config.yaml"
fi
fi
warning "Please edit $CONFIG_DIR/config.yaml to configure LogWhisperer"
else
info "Configuration already exists, not overwriting"
fi
}
# Create web templates
create_web_templates() {
if [[ "$INSTALL_WEB" != true ]] || [[ "$DRY_RUN" == true ]]; then
return 0
fi
# Check if templates already exist and have content
if [[ -f "$INSTALL_DIR/web/templates/base.html" ]] && [[ -s "$INSTALL_DIR/web/templates/base.html" ]]; then
verbose "Templates already exist with content"
return 0
fi
info "Creating web dashboard templates..."
cd "$INSTALL_DIR/web"
source "$INSTALL_DIR/web-venv/bin/activate"
# Create templates using dashboard.py
python3 dashboard.py --create-templates || {
warning "Failed to create templates automatically"
deactivate
return 1
}
deactivate
# Verify templates were created
if [[ -f "$INSTALL_DIR/web/templates/base.html" ]] && [[ -s "$INSTALL_DIR/web/templates/base.html" ]]; then
info "Web dashboard templates created successfully"
else
warning "Templates may not have been created properly"
fi
}
# Create wrapper scripts
create_wrappers() {
info "Creating wrapper scripts..."
if [[ "$DRY_RUN" == true ]]; then
info "[DRY RUN] Would create wrapper scripts"
return 0
fi
# Main wrapper
local wrapper_path="/usr/local/bin/logwhisperer"
cat > "$wrapper_path" << EOF
#!/bin/bash
# LogWhisperer wrapper script
# Generated by installer v$SCRIPT_VERSION
export LOGWHISPERER_CONFIG="${CONFIG_DIR}/config.yaml"
EOF
# Add dev mode environment variables if enabled
if [[ "$DEV_MODE" == true ]]; then
cat >> "$wrapper_path" << EOF
# Development mode enabled
export LOGWHISPERER_DEV_MODE="true"
export LOGWHISPERER_DEV_KEY="${DEV_KEY}"
EOF
info "Development mode enabled in wrapper"
fi
cat >> "$wrapper_path" << EOF
exec "$INSTALL_DIR/bin/logwhisperer" "\$@"
EOF
chmod +x "$wrapper_path"
verbose "Created main wrapper at $wrapper_path"
# Web dashboard wrappers if installed
if [[ "$INSTALL_WEB" == true ]]; then
# API server wrapper
mkdir -p "$INSTALL_DIR/bin"
cat > "$INSTALL_DIR/bin/logwhisperer-api" << EOF
#!/bin/bash
# LogWhisperer API Server wrapper - Production
cd "$INSTALL_DIR/web"
source "$INSTALL_DIR/web-venv/bin/activate"
export LOGWHISPERER_CONFIG="${CONFIG_DIR}/config.yaml"
EOF
# Add dev mode to API wrapper too
if [[ "$DEV_MODE" == true ]]; then
cat >> "$INSTALL_DIR/bin/logwhisperer-api" << EOF
export LOGWHISPERER_DEV_MODE="true"
export LOGWHISPERER_DEV_KEY="${DEV_KEY}"
EOF
fi
# Use gunicorn with eventlet for production (removed --keepalive option)
cat >> "$INSTALL_DIR/bin/logwhisperer-api" << 'EOF'
# Production server using gunicorn with eventlet workers
exec gunicorn --worker-class eventlet --workers 1 --bind 0.0.0.0:5124 --timeout 120 --log-level info --access-logfile - --error-logfile - --worker-connections 1000 wsgi:application
EOF
chmod +x "$INSTALL_DIR/bin/logwhisperer-api"
# Web dashboard wrapper
cat > "$INSTALL_DIR/bin/logwhisperer-web" << EOF
#!/bin/bash
# LogWhisperer Web Dashboard wrapper - Production
cd "$INSTALL_DIR/web"
source "$INSTALL_DIR/web-venv/bin/activate"
export LOGWHISPERER_CONFIG="${CONFIG_DIR}/config.yaml"
EOF
# Add dev mode to web wrapper too
if [[ "$DEV_MODE" == true ]]; then
cat >> "$INSTALL_DIR/bin/logwhisperer-web" << EOF
export LOGWHISPERER_DEV_MODE="true"
export LOGWHISPERER_DEV_KEY="${DEV_KEY}"
EOF
fi
# Use gunicorn for production web server (removed --keepalive option)
cat >> "$INSTALL_DIR/bin/logwhisperer-web" << 'EOF'
# Production server using gunicorn
exec gunicorn --workers 2 --bind 0.0.0.0:5123 --timeout 120 --log-level info --access-logfile - --error-logfile - dashboard:app
EOF
chmod +x "$INSTALL_DIR/bin/logwhisperer-web"
verbose "Created web component wrappers"
fi
}
# Install systemd services
install_services() {
if [[ "$INSTALL_SERVICE" != true ]] && [[ "$INSTALL_WEB" != true ]]; then
return 0
fi
if [[ "$DRY_RUN" == true ]]; then
info "[DRY RUN] Would install systemd services"
return 0
fi
# Main service
if [[ "$INSTALL_SERVICE" == true ]]; then
info "Installing LogWhisperer monitoring service..."
cat > "/etc/systemd/system/logwhisperer.service" << EOF
[Unit]
Description=LogWhisperer Monitoring Agent
Documentation=https://github.com/yourusername/logwhisperer
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=/usr/local/bin/logwhisperer monitor
WorkingDirectory=$INSTALL_DIR
Restart=always
RestartSec=10
StandardOutput=append:${LOG_DIR}/logwhisperer.log
StandardError=append:${LOG_DIR}/logwhisperer.error.log
# Security hardening
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=$LOG_DIR $INSTALL_DIR/reports
# Resource limits
LimitNOFILE=65536
MemoryLimit=1G
# Environment
Environment="PYTHONUNBUFFERED=1"
Environment="LOGWHISPERER_CONFIG=$CONFIG_DIR/config.yaml"
EOF
# Add dev mode environment variables to service if enabled
if [[ "$DEV_MODE" == true ]]; then
sed -i '/Environment="LOGWHISPERER_CONFIG=/a Environment="LOGWHISPERER_DEV_MODE=true"\nEnvironment="LOGWHISPERER_DEV_KEY='$DEV_KEY'"' "/etc/systemd/system/logwhisperer.service"
fi
cat >> "/etc/systemd/system/logwhisperer.service" << EOF
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable logwhisperer.service
info "Main monitoring service installed and enabled"
fi
# Web services
if [[ "$INSTALL_WEB" == true ]]; then
info "Installing web dashboard services..."
# API service
cat > "/etc/systemd/system/logwhisperer-api.service" << EOF
[Unit]
Description=LogWhisperer API Server
After=network.target
Before=logwhisperer-web.service
[Service]
Type=simple
User=root
Group=root
WorkingDirectory=$INSTALL_DIR
ExecStart=$INSTALL_DIR/bin/logwhisperer-api
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
SyslogIdentifier=logwhisperer-api
Environment="PYTHONUNBUFFERED=1"
EOF
# Add dev mode to API service
if [[ "$DEV_MODE" == true ]]; then
cat >> "/etc/systemd/system/logwhisperer-api.service" << EOF
Environment="LOGWHISPERER_DEV_MODE=true"
Environment="LOGWHISPERER_DEV_KEY=$DEV_KEY"
EOF
fi
cat >> "/etc/systemd/system/logwhisperer-api.service" << EOF
[Install]
WantedBy=multi-user.target
EOF
# Web service
cat > "/etc/systemd/system/logwhisperer-web.service" << EOF
[Unit]
Description=LogWhisperer Web Dashboard
After=network.target logwhisperer-api.service
Wants=logwhisperer-api.service
[Service]
Type=simple
User=root
Group=root
WorkingDirectory=$INSTALL_DIR/web
ExecStart=$INSTALL_DIR/bin/logwhisperer-web
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
SyslogIdentifier=logwhisperer-web
Environment="PYTHONUNBUFFERED=1"
EOF
# Add dev mode to web service
if [[ "$DEV_MODE" == true ]]; then
cat >> "/etc/systemd/system/logwhisperer-web.service" << EOF
Environment="LOGWHISPERER_DEV_MODE=true"
Environment="LOGWHISPERER_DEV_KEY=$DEV_KEY"
EOF
fi
cat >> "/etc/systemd/system/logwhisperer-web.service" << EOF
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable logwhisperer-api.service
systemctl enable logwhisperer-web.service
info "Web dashboard services installed and enabled"
fi
}
# Start services
start_services() {
if [[ "$DRY_RUN" == true ]]; then
return 0
fi
local services_to_start=()
if [[ "$INSTALL_SERVICE" == true ]]; then
services_to_start+=("logwhisperer")
fi
if [[ "$INSTALL_WEB" == true ]]; then
services_to_start+=("logwhisperer-api" "logwhisperer-web")
fi
if [[ ${#services_to_start[@]} -eq 0 ]]; then
return 0
fi
if confirm "Start services now?"; then
for service in "${services_to_start[@]}"; do
info "Starting $service..."
systemctl start "${service}.service"
sleep 2
if systemctl is-active --quiet "${service}.service"; then
info "$service is running"
else
warning "$service failed to start. Check: journalctl -u $service -n 50"
fi
done
fi
}
# Setup log rotation
setup_log_rotation() {
info "Setting up log rotation..."
if [[ "$DRY_RUN" == true ]]; then
info "[DRY RUN] Would create logrotate config"
return 0
fi
cat > "/etc/logrotate.d/logwhisperer" << EOF
$LOG_DIR/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
create 0644 root root
sharedscripts
postrotate
systemctl reload logwhisperer.service > /dev/null 2>&1 || true
endscript
}
EOF
verbose "Created logrotate configuration"
}
# Verify installation
verify_installation() {
info "Verifying installation..."
local errors=0
# Check main executable
if [[ ! -x "/usr/local/bin/logwhisperer" ]]; then
error "LogWhisperer executable not found"
((errors++))
fi
# Check config
if [[ ! -f "$CONFIG_DIR/config.yaml" ]]; then
error "Configuration file not found"
((errors++))
fi
# Test basic functionality
if ! /usr/local/bin/logwhisperer --version &>/dev/null; then
error "LogWhisperer failed to run"
((errors++))
fi
# Check web components if installed
if [[ "$INSTALL_WEB" == true ]]; then
if [[ ! -f "$INSTALL_DIR/web/api_server.py" ]]; then
warning "API server not found"
fi
if [[ ! -f "$INSTALL_DIR/web/dashboard.py" ]]; then
warning "Dashboard not found"
fi
if [[ ! -d "$INSTALL_DIR/web-venv" ]]; then
warning "Python virtual environment not found"
fi
fi
# Check Ollama
if [[ "$SKIP_OLLAMA" != true ]]; then
if ! curl -s http://localhost:11434 >/dev/null 2>&1; then
warning "Ollama is not accessible"
fi
fi
if [[ $errors -eq 0 ]]; then
info "Installation verified successfully"
return 0
else
error "Installation verification failed with $errors errors"
return 1
fi
}
# Uninstall function
uninstall() {
info "Uninstalling LogWhisperer..."
if ! confirm "Are you sure you want to uninstall LogWhisperer?"; then
info "Uninstall cancelled"
exit 0
fi
# Stop and disable services
local services=("logwhisperer" "logwhisperer-api" "logwhisperer-web")
for service in "${services[@]}"; do
if systemctl is-active --quiet "${service}.service" 2>/dev/null; then
info "Stopping $service..."
systemctl stop "${service}.service"
systemctl disable "${service}.service"
fi
done
# Remove files
local items_to_remove=(
"/usr/local/bin/logwhisperer"
"/etc/systemd/system/logwhisperer.service"
"/etc/systemd/system/logwhisperer-api.service"
"/etc/systemd/system/logwhisperer-web.service"
"/etc/logrotate.d/logwhisperer"
"$INSTALL_DIR"
)
for item in "${items_to_remove[@]}"; do