From 5027f9945b451a1c00f453a35d968abef86d03db Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 27 Jun 2025 11:09:19 +0200 Subject: [PATCH] update --- update_firmware.sh | 140 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 update_firmware.sh diff --git a/update_firmware.sh b/update_firmware.sh new file mode 100644 index 0000000..6caa04c --- /dev/null +++ b/update_firmware.sh @@ -0,0 +1,140 @@ +#!/bin/bash + +# moduleair Pro 4G - Comprehensive Update Script +# This script performs a complete system update including git pull, +# config initialization, and service management +# Non-interactive version for WebUI + +#to test: +# sudo chmod +x /var/www/moduleair_pro_4g/update_firmware.sh +# sudo /var/www/moduleair_pro_4g/update_firmware.sh + +echo "======================================" +echo "ModuleAir Pro 4G - Firmware Update" +echo "======================================" +echo "Started at: $(date)" +echo "" + +# Set working directory +cd /var/www/moduleair_pro_4g + +# Ensure this script is executable +chmod +x /var/www/moduleair_pro_4g/update_firmware.sh + +# Function to print status messages +print_status() { + echo "[$(date '+%H:%M:%S')] $1" +} + +# Function to check command success +check_status() { + if [ $? -eq 0 ]; then + print_status "✓ $1 completed successfully" + else + print_status "✗ $1 failed" + return 1 + fi +} + +# Step 1: Git operations +print_status "Step 1: Updating firmware from repository..." + +# Disable filemode to prevent permission issues +git -C /var/www/moduleair_pro_4g config core.fileMode false +check_status "Git fileMode disabled" + +# Fetch latest changes +git fetch origin +check_status "Git fetch" + +# Show current branch +print_status "Current branch: $(git branch --show-current)" + +# Check for local changes +if [ -n "$(git status --porcelain)" ]; then + print_status "Warning: Local changes detected, stashing..." + git stash push -m "Auto-stash before update $(date)" + check_status "Git stash" +fi + +# Pull latest changes +git pull origin $(git branch --show-current) +check_status "Git pull" + +# Step 2: Update database configuration +print_status "" +print_status "Step 2: Updating database configuration..." +/usr/bin/python3 /var/www/moduleair_pro_4g/sqlite/set_config_smart.py +check_status "Database configuration update" + +# Step 3: Check and fix file permissions +print_status "" +print_status "Step 3: Checking file permissions..." +sudo chmod +x /var/www/moduleair_pro_4g/update_firmware.sh +sudo chmod 755 /var/www/moduleair_pro_4g/sqlite/*.py +sudo chmod 755 /var/www/moduleair_pro_4g/NPM/*.py +sudo chmod 755 /var/www/moduleair_pro_4g/BME280/*.py +sudo chmod 755 /var/www/moduleair_pro_4g/SARA/*.py +check_status "File permissions update" + +# Step 4: Restart critical services if they exist +print_status "" +print_status "Step 4: Managing system services..." + +# List of services to check and restart +services=( + "moduleair-npm-data.timer" + "moduleair-sara-data.timer" + "moduleair-bme280-data.timer" +) + +for service in "${services[@]}"; do + if systemctl list-unit-files | grep -q "$service"; then + # Check if service is enabled before restarting + if systemctl is-enabled --quiet "$service" 2>/dev/null; then + print_status "Restarting enabled service: $service" + sudo systemctl restart "$service" + if systemctl is-active --quiet "$service"; then + print_status "✓ $service is running" + else + print_status "⚠ $service failed to start" + fi + else + print_status "ℹ Service $service is disabled, skipping restart" + fi + else + print_status "ℹ Service $service not found (may not be installed)" + fi +done + +# Step 5: System health check +print_status "" +print_status "Step 5: System health check..." + +# Check disk space +disk_usage=$(df / | awk 'NR==2 {print $5}' | sed 's/%//') +if [ "$disk_usage" -gt 90 ]; then + print_status "⚠ Warning: Disk usage is high ($disk_usage%)" +else + print_status "✓ Disk usage is acceptable ($disk_usage%)" +fi + +# Check if database is accessible +if [ -f "/var/www/moduleair_pro_4g/sqlite/sensors.db" ]; then + print_status "✓ Database file exists" +else + print_status "⚠ Warning: Database file not found" +fi + +# Step 6: Final cleanup +print_status "" +print_status "Step 6: Cleaning up..." +sudo find /var/www/moduleair_pro_4g/logs -name "*.log" -size +10M -exec truncate -s 0 {} \; +check_status "Log cleanup" + +print_status "" +print_status "======================================" +print_status "Update completed successfully!" +print_status "======================================" + +exit 0 \ No newline at end of file