Files
nas/lib/logging.sh
Sebastian Palencsár cb91166616 feat: Major v2.0.0 rewrite - Enterprise-grade NAS setup script
🚀 BREAKING CHANGE: Complete rewrite to enterprise-grade standards

###  New Features:
- Enhanced input validation (IP, port, username, path)
- Automatic rollback mechanism on failures
- Comprehensive unit testing framework (50+ tests)
- Advanced logging with timestamps and levels
- Interactive configuration system with persistence
- Performance optimization suite (kernel, Docker, Samba)
- Advanced firewall configuration with monitoring
- System health monitoring and maintenance tools
- Multi-distribution support with version validation

### 🛡️ Security Enhancements:
- SSH hardening with security policies
- Rate limiting for critical services
- IP blocking/unblocking tools
- Intrusion detection capabilities
- Firewall monitoring with alerts
- Secure input sanitization

### 🔧 Architecture Improvements:
- Modular library structure
- Centralized configuration management
- Common functions separation
- Professional error handling with set -euo pipefail
- Signal handling for graceful shutdowns
- Resource cleanup mechanisms

### 📚 Documentation:
- Professional README with comprehensive guides
- Enhanced CONTRIBUTING.md with development standards
- Complete CHANGELOG.md with version history
- Troubleshooting guides and best practices

### 🧪 Testing & Quality:
- Unit tests for all critical functions
- Performance regression testing
- Multi-distribution integration testing
- Input validation testing
- Error scenario testing

This release transforms the script from a basic tool to a production-ready,
enterprise-grade NAS setup solution suitable for professional environments.
2025-06-17 10:57:46 +02:00

92 lines
2.3 KiB
Bash

#!/bin/bash
# Enhanced logging with timestamps and levels
log_with_timestamp() {
local level=$1
local message=$2
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
local color_start=$3
local color_end=${NC}
echo -e "${color_start}[$timestamp] [$level] $message${color_end}"
echo "[$timestamp] [$level] $message" >> "${LOG_FILE}" 2>/dev/null || true
}
log_info() {
log_with_timestamp "INFO" "$1" "${GREEN}"
}
log_warning() {
log_with_timestamp "WARNING" "$1" "${YELLOW}"
}
log_error() {
log_with_timestamp "ERROR" "$1" "${RED}" >&2
}
log_debug() {
if [[ "${DEBUG}" == "true" ]]; then
log_with_timestamp "DEBUG" "$1" "${NC}"
fi
}
log_success() {
log_with_timestamp "SUCCESS" "$1" "${GREEN}"
}
# Progress tracking
show_progress() {
local current=$1
local total=$2
local message=${3:-"Processing"}
local percentage=$((current * 100 / total))
local bar_length=50
local filled_length=$((percentage * bar_length / 100))
printf "\r${GREEN}[${message}] ["
printf "%${filled_length}s" | tr ' ' '='
printf "%$((bar_length - filled_length))s" | tr ' ' '-'
printf "] %d%% (%d/%d)${NC}" $percentage $current $total
if [[ $current -eq $total ]]; then
echo ""
log_success "$message completed"
fi
}
backup_config() {
local config_file=$1
if [ -f "$config_file" ]; then
local backup_file="${config_file}.$(date +%F-%T).bak"
if sudo cp "$config_file" "$backup_file" 2>/dev/null; then
log_info "Backup of $config_file created at $backup_file"
return 0
else
log_error "Failed to create backup of $config_file"
return 1
fi
else
log_warning "Config file $config_file does not exist, skipping backup"
return 0
fi
}
# Rollback functionality
add_rollback_action() {
local action="$1"
echo "$action" >> "${ROLLBACK_FILE}"
log_debug "Added rollback action: $action"
}
execute_rollback() {
if [[ -f "${ROLLBACK_FILE}" ]]; then
log_warning "Executing rollback actions..."
while IFS= read -r action; do
log_info "Rollback: $action"
eval "$action" || log_error "Failed to execute rollback action: $action"
done < <(tac "${ROLLBACK_FILE}")
rm -f "${ROLLBACK_FILE}"
log_info "Rollback completed"
fi
}