#!/bin/bash set -euo pipefail # Enhanced Start Script for Hexo Blog Container v0.0.4 # Features: Supervisor integration, enhanced monitoring, automatic recovery readonly SCRIPT_VERSION="0.0.4-enhanced" readonly LOG_FILE="/var/log/container/startup.log" readonly CONFIG_DIR="/etc/container/templates" # Colors for output readonly RED='\033[0;31m' readonly GREEN='\033[0;32m' readonly YELLOW='\033[1;33m' readonly BLUE='\033[0;34m' readonly NC='\033[0m' # No Color # Logging function log() { local level="$1" shift local message="$*" local timestamp=$(date '+%Y-%m-%d %H:%M:%S') echo -e "${timestamp} [${level}] ${message}" # Write to log file if possible if [[ -w "/var/log/container" ]] || [[ -w "$LOG_FILE" ]]; then echo "${timestamp} [${level}] ${message}" >> "$LOG_FILE" fi # Send to syslog logger -t "hexo-start" "${level}: ${message}" } log_info() { log "${GREEN}INFO${NC}" "$@"; } log_warn() { log "${YELLOW}WARN${NC}" "$@"; } log_error() { log "${RED}ERROR${NC}" "$@"; } log_debug() { log "${BLUE}DEBUG${NC}" "$@"; } # Error handler handle_error() { local line_number=$1 log_error "Script failed at line ${line_number}" log_error "Attempting graceful shutdown..." cleanup exit 1 } trap 'handle_error ${LINENO}' ERR # Cleanup function cleanup() { log_info "Performing cleanup..." # Kill any background processes if needed jobs -p | xargs -r kill 2>/dev/null || true } trap cleanup EXIT # Main startup function main() { log_info "Starting Hexo Blog Container ${SCRIPT_VERSION}" log_info "================================================" # System information log_info "System: $(uname -a)" log_info "Memory: $(free -h | awk '/^Mem:/ {print $2}')" log_info "Disk: $(df -h / | awk 'NR==2 {print $4 " available"}')" # Environment setup setup_environment # User management setup_users # Configure services configure_ssh configure_nginx configure_git configure_monitoring # Pre-flight checks preflight_checks # Start services if [[ "${SUPERVISOR_ENABLED:-true}" == "true" ]]; then start_with_supervisor else start_traditional fi } setup_environment() { log_info "Setting up environment..." # Create necessary directories mkdir -p /var/log/container mkdir -p /var/run/sshd mkdir -p /backup/auto mkdir -p /home/www/hexo mkdir -p /home/hexo/.ssh # Set timezone if not set if [[ -n "${TZ:-}" ]]; then ln -sf "/usr/share/zoneinfo/${TZ}" /etc/localtime echo "${TZ}" > /etc/timezone log_info "Timezone set to: ${TZ}" fi # Setup locale if [[ -n "${LANG:-}" ]]; then locale-gen "${LANG}" 2>/dev/null || true update-locale "LANG=${LANG}" 2>/dev/null || true log_info "Locale set to: ${LANG}" fi } setup_users() { log_info "Setting up users..." # Update hexo user UID/GID if specified if [[ -n "${PUID:-}" ]] && [[ "${PUID}" != "1000" ]]; then usermod -u "${PUID}" hexo log_info "Updated hexo user UID to: ${PUID}" fi if [[ -n "${PGID:-}" ]] && [[ "${PGID}" != "1000" ]]; then groupmod -g "${PGID}" hexo log_info "Updated hexo group GID to: ${PGID}" fi # Fix ownership after potential UID/GID changes chown -R hexo:hexo /home/hexo /home/www/hexo /var/log/container /backup log_info "Updated file ownership for hexo user" } configure_ssh() { log_info "Configuring SSH server..." # Process SSH configuration template if [[ -f "${CONFIG_DIR}/sshd_config.template" ]]; then envsubst < "${CONFIG_DIR}/sshd_config.template" > /etc/ssh/sshd_config log_info "SSH configuration applied from template" else log_warn "SSH template not found, using default configuration" fi # Setup SSH banner if [[ -f "${CONFIG_DIR}/banner.txt" ]]; then cp "${CONFIG_DIR}/banner.txt" /etc/ssh/banner.txt log_info "SSH banner configured" fi # Generate host keys if they don't exist if [[ ! -f /etc/ssh/ssh_host_rsa_key ]]; then ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N '' -q log_info "Generated SSH RSA host key" fi if [[ ! -f /etc/ssh/ssh_host_ed25519_key ]]; then ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N '' -q log_info "Generated SSH Ed25519 host key" fi # Test SSH configuration if sshd -t; then log_info "SSH configuration is valid" else log_error "SSH configuration is invalid" return 1 fi } configure_nginx() { log_info "Configuring Nginx server..." # Process Nginx configuration template if [[ -f "${CONFIG_DIR}/nginx.conf.template" ]]; then envsubst < "${CONFIG_DIR}/nginx.conf.template" > /etc/nginx/nginx.conf log_info "Nginx configuration applied from template" else log_warn "Nginx template not found, using default configuration" fi # Create nginx user if it doesn't exist if ! id nginx >/dev/null 2>&1; then log_info "Creating nginx user..." useradd -r -s /bin/false nginx fi # Test Nginx configuration if nginx -t; then log_info "Nginx configuration is valid" else log_error "Nginx configuration is invalid" return 1 fi # Create default index if it doesn't exist if [[ ! -f /home/www/hexo/index.html ]]; then log_info "Creating default index page..." cat > /home/www/hexo/index.html << 'EOF'
✅ Ready for deployment
Upload your Hexo blog content via Git push to get started!
Version: 0.0.4-enhanced