diff --git a/first_time_install.sh b/first_time_install.sh index e43a7b8b..44eed924 100644 --- a/first_time_install.sh +++ b/first_time_install.sh @@ -15,8 +15,8 @@ on_error() { echo "✗ An error occurred during: $CURRENT_STEP (line $line_no, exit $exit_code)" >&2 if [ -n "${LOG_FILE:-}" ]; then echo "See the log for details: $LOG_FILE" >&2 - echo "-- Last 50 lines from log --" >&2 - tail -n 50 "$LOG_FILE" >&2 || true + echo "-- Last 100 lines from log --" >&2 + tail -n 100 "$LOG_FILE" >&2 || true fi echo "\nCommon fixes:" >&2 echo "- Ensure the Pi is online (try: ping -c1 8.8.8.8)." >&2 @@ -202,8 +202,33 @@ retry() { done } -apt_update() { retry apt update; } -apt_install() { retry apt install -y "$@"; } +# Wait for another apt/dpkg process (commonly unattended-upgrades running +# shortly after first boot) to release its lock before we try apt ourselves. +# Without this, apt_update/apt_install can fail outright in the first couple +# minutes after a fresh Pi OS boot with a generic "Command failed after 3 +# attempts" error. +wait_for_apt_lock() { + command -v flock >/dev/null 2>&1 || return 0 + local lock_file="/var/lib/dpkg/lock-frontend" + local max_wait=180 + local waited=0 + local printed=0 + while ! flock -n "$lock_file" -c true 2>/dev/null; do + if [ "$printed" -eq 0 ]; then + echo "⚠ Waiting for another apt/dpkg process to finish (e.g. unattended-upgrades on first boot)..." + printed=1 + fi + if [ "$waited" -ge "$max_wait" ]; then + echo "⚠ Still waiting after ${max_wait}s; proceeding anyway." + break + fi + sleep 5 + waited=$((waited+5)) + done +} + +apt_update() { wait_for_apt_lock; retry apt update; } +apt_install() { wait_for_apt_lock; retry apt install -y "$@"; } apt_remove() { apt-get remove -y "$@" || true; } check_network() { @@ -222,6 +247,22 @@ check_network() { exit 1 } +check_disk_space() { + command -v df >/dev/null 2>&1 || return 0 + local available_mb + available_mb=$(df -m "$PROJECT_ROOT_DIR" | awk 'NR==2{print $4}') + available_mb=${available_mb:-0} + if [ "$available_mb" -lt 500 ]; then + echo "✗ ERROR: Insufficient disk space: ${available_mb}MB available (need at least 500MB)" + echo " Free up space first, e.g.: sudo apt clean && sudo apt autoremove" + exit 1 + elif [ "$available_mb" -lt 1024 ]; then + echo "⚠ Limited disk space: ${available_mb}MB available (recommend at least 1GB for the rpi-rgb-led-matrix build in Step 6)" + else + echo "✓ Disk space sufficient: ${available_mb}MB available" + fi +} + echo "" echo "This script will perform the following steps:" echo "1. Install system dependencies" @@ -271,8 +312,9 @@ CURRENT_STEP="Install system dependencies" echo "Step 1: Installing system dependencies..." echo "----------------------------------------" -# Ensure network is available before APT operations +# Pre-flight checks before APT operations check_network +check_disk_space # Update package list apt_update @@ -822,14 +864,14 @@ else # Try to initialize submodule if .gitmodules exists if [ -f "$PROJECT_ROOT_DIR/.gitmodules" ] && grep -q "rpi-rgb-led-matrix" "$PROJECT_ROOT_DIR/.gitmodules"; then echo "Initializing rpi-rgb-led-matrix submodule..." - if ! git submodule update --init --recursive rpi-rgb-led-matrix-master 2>&1; then + if ! retry git submodule update --init --recursive rpi-rgb-led-matrix-master; then echo "⚠ Submodule init failed, cloning directly from GitHub..." - git clone https://github.com/hzeller/rpi-rgb-led-matrix.git rpi-rgb-led-matrix-master + retry git clone https://github.com/hzeller/rpi-rgb-led-matrix.git rpi-rgb-led-matrix-master fi else # Fallback: clone directly if submodule not configured echo "Submodule not configured, cloning directly from GitHub..." - git clone https://github.com/hzeller/rpi-rgb-led-matrix.git rpi-rgb-led-matrix-master + retry git clone https://github.com/hzeller/rpi-rgb-led-matrix.git rpi-rgb-led-matrix-master fi fi @@ -841,23 +883,34 @@ else cd "$PROJECT_ROOT_DIR" rm -rf rpi-rgb-led-matrix-master if [ -f "$PROJECT_ROOT_DIR/.gitmodules" ] && grep -q "rpi-rgb-led-matrix" "$PROJECT_ROOT_DIR/.gitmodules"; then - git submodule update --init --recursive rpi-rgb-led-matrix-master + retry git submodule update --init --recursive rpi-rgb-led-matrix-master else - git clone https://github.com/hzeller/rpi-rgb-led-matrix.git rpi-rgb-led-matrix-master + retry git clone https://github.com/hzeller/rpi-rgb-led-matrix.git rpi-rgb-led-matrix-master fi fi - + pushd "$PROJECT_ROOT_DIR/rpi-rgb-led-matrix-master" >/dev/null echo "Installing rpi-rgb-led-matrix Python package (scikit-build-core + cmake)..." echo " Build deps required: python-dev-is-python3 cmake" echo " This compiles C++ — may take 2-5 minutes on Pi 4/5..." - if ! python3 -m pip install --break-system-packages .; then + BUILD_OUTPUT=$(mktemp) + BUILD_SUCCESS=false + if python3 -m pip install --break-system-packages . > "$BUILD_OUTPUT" 2>&1; then + BUILD_SUCCESS=true + fi + cat "$BUILD_OUTPUT" >> "$LOG_FILE" + if [ "$BUILD_SUCCESS" != true ]; then echo "✗ Failed to install rpi-rgb-led-matrix Python package" echo " Ensure build tools are installed:" echo " sudo apt install -y python-dev-is-python3 cmake build-essential" + echo "" + echo "-- Last 50 lines of build output --" + tail -n 50 "$BUILD_OUTPUT" + rm -f "$BUILD_OUTPUT" popd >/dev/null exit 1 fi + rm -f "$BUILD_OUTPUT" popd >/dev/null else echo "✗ rpi-rgb-led-matrix-master directory not found at $PROJECT_ROOT_DIR" @@ -912,7 +965,9 @@ else # Try to install dependencies using the smart installer if available if [ -f "$PROJECT_ROOT_DIR/scripts/install_dependencies_apt.py" ]; then echo "Using smart dependency installer..." - python3 "$PROJECT_ROOT_DIR/scripts/install_dependencies_apt.py" + # -u: unbuffered stdout/stderr so output is captured in $LOG_FILE in + # real time and in order relative to this script's own echo statements + python3 -u "$PROJECT_ROOT_DIR/scripts/install_dependencies_apt.py" else echo "Using pip to install dependencies..." if [ -f "$PROJECT_ROOT_DIR/requirements_web_v2.txt" ]; then diff --git a/scripts/install/one-shot-install.sh b/scripts/install/one-shot-install.sh index ddac1115..472b687f 100755 --- a/scripts/install/one-shot-install.sh +++ b/scripts/install/one-shot-install.sh @@ -340,9 +340,14 @@ main() { echo "" # Execute with proper error handling and non-interactive mode - # Temporarily disable errexit to capture exit code instead of exiting immediately + # Temporarily disable errexit AND the ERR trap to capture exit code instead of + # exiting immediately. `set +e` alone does not suppress the ERR trap, so without + # `trap '' ERR` a non-zero exit from first_time_install.sh would trigger on_error + # here with the generic "Main installation" message instead of the detailed + # if/else handling below. set +e - + trap '' ERR + # Check /tmp permissions - only fix if actually wrong (common in automated scenarios) # When running manually, /tmp usually has correct permissions (1777) TMP_PERMS=$(stat -c '%a' /tmp 2>/dev/null || echo "unknown") @@ -370,6 +375,7 @@ main() { sudo -E env TMPDIR=/tmp LEDMATRIX_ASSUME_YES=1 bash ./first_time_install.sh -y =6.5.0,<7.0.0', @@ -122,47 +178,49 @@ def main(): 'python-socketio>=5.11.0,<6.0.0', 'python-engineio>=4.9.0,<5.0.0' ] - + for package in special_packages: - if not install_via_pip(package): + ok, pip_output = install_via_pip(package) + if not ok: failed_packages.append(package) - + failure_details[package] = pip_output + # Install rgbmatrix module from local source (optional - may already be installed in Step 6) # Check if already installed first if check_package_installed('rgbmatrix'): print("rgbmatrix module already installed, skipping...") else: print("Installing rgbmatrix module from local source...") - try: - # Get project root (parent of scripts directory) - PROJECT_ROOT = Path(__file__).parent.parent - rgbmatrix_path = PROJECT_ROOT / 'rpi-rgb-led-matrix-master' / 'bindings' / 'python' - if rgbmatrix_path.exists(): - # Check if the module has been built (look for setup.py) - setup_py = rgbmatrix_path / 'setup.py' - if setup_py.exists(): - # Try installing - use regular install, not editable mode - # This is optional for web interface and should already be installed in Step 6 - subprocess.check_call([ - sys.executable, '-m', 'pip', 'install', '--break-system-packages', str(rgbmatrix_path) - ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + # Get project root (parent of scripts directory) + PROJECT_ROOT = Path(__file__).parent.parent + rgbmatrix_path = PROJECT_ROOT / 'rpi-rgb-led-matrix-master' / 'bindings' / 'python' + if rgbmatrix_path.exists(): + # Check if the module has been built (look for setup.py) + setup_py = rgbmatrix_path / 'setup.py' + if setup_py.exists(): + # Try installing - use regular install, not editable mode + # This is optional for web interface and should already be installed in Step 6 + ok, output = _run([sys.executable, '-m', 'pip', 'install', '--break-system-packages', str(rgbmatrix_path)]) + if ok: print("rgbmatrix module installed successfully") else: - print("Warning: rgbmatrix setup.py not found, module may need to be built first") - print(" This is normal if Step 6 hasn't completed yet.") + # Don't fail the whole installation - rgbmatrix is optional for web interface + # and should be installed in Step 6 of first_time_install.sh + print("Warning: Failed to install rgbmatrix module:") + for line in output.strip().splitlines()[-ERROR_TAIL_LINES:]: + print(f" {line}") + print(" This is normal if rgbmatrix hasn't been built yet (Step 6).") + print(" The web interface will work without it.") else: - print("Warning: rgbmatrix source not found (this is normal if Step 6 hasn't run yet)") - except subprocess.CalledProcessError as e: - # Don't fail the whole installation - rgbmatrix is optional for web interface - # and should be installed in Step 6 of first_time_install.sh - print(f"Warning: Failed to install rgbmatrix module: {e}") - print(" This is normal if rgbmatrix hasn't been built yet (Step 6).") - print(" The web interface will work without it.") - # Don't add to failed_packages since it's optional - + print("Warning: rgbmatrix setup.py not found, module may need to be built first") + print(" This is normal if Step 6 hasn't completed yet.") + else: + print("Warning: rgbmatrix source not found (this is normal if Step 6 hasn't run yet)") + if failed_packages: print(f"\nFailed to install the following packages: {failed_packages}") print("You may need to install them manually or check your system configuration.") + print_failure_summary(failed_packages, failure_details) return False else: print("\nAll dependencies installed successfully!") diff --git a/web_interface/blueprints/api_v3.py b/web_interface/blueprints/api_v3.py index 4c954b00..9cb849eb 100644 --- a/web_interface/blueprints/api_v3.py +++ b/web_interface/blueprints/api_v3.py @@ -705,7 +705,8 @@ def save_main_config(): display_fields = ['rows', 'cols', 'chain_length', 'parallel', 'brightness', 'hardware_mapping', 'gpio_slowdown', 'rp1_rio', 'scan_mode', 'disable_hardware_pulsing', 'inverse_colors', 'show_refresh_rate', 'pwm_bits', 'pwm_dither_bits', 'pwm_lsb_nanoseconds', 'limit_refresh_rate_hz', 'use_short_date_format', - 'max_dynamic_duration_seconds', 'led_rgb_sequence', 'multiplexing', 'panel_type'] + 'max_dynamic_duration_seconds', 'led_rgb_sequence', 'multiplexing', 'panel_type', + 'row_address_type'] if any(k in data for k in display_fields): if 'display' not in current_config: @@ -736,14 +737,23 @@ def save_main_config(): except (ValueError, TypeError): return jsonify({'status': 'error', 'message': f"Invalid multiplexing value '{data['multiplexing']}'. Must be an integer from 0 to 22."}), 400 + # Validate row_address_type + if 'row_address_type' in data: + try: + rat_val = int(data['row_address_type']) + if rat_val < 0 or rat_val > 4: + return jsonify({'status': 'error', 'message': f"Invalid row_address_type '{data['row_address_type']}'. Must be an integer from 0 to 4."}), 400 + except (ValueError, TypeError): + return jsonify({'status': 'error', 'message': f"Invalid row_address_type '{data['row_address_type']}'. Must be an integer from 0 to 4."}), 400 + # Handle hardware settings for field in ['rows', 'cols', 'chain_length', 'parallel', 'brightness', 'hardware_mapping', 'scan_mode', 'pwm_bits', 'pwm_dither_bits', 'pwm_lsb_nanoseconds', 'limit_refresh_rate_hz', - 'led_rgb_sequence', 'multiplexing', 'panel_type']: + 'led_rgb_sequence', 'multiplexing', 'panel_type', 'row_address_type']: if field in data: if field in ['rows', 'cols', 'chain_length', 'parallel', 'brightness', 'scan_mode', 'pwm_bits', 'pwm_dither_bits', 'pwm_lsb_nanoseconds', 'limit_refresh_rate_hz', - 'multiplexing']: + 'multiplexing', 'row_address_type']: current_config['display']['hardware'][field] = int(data[field]) else: current_config['display']['hardware'][field] = data[field] @@ -1574,6 +1584,66 @@ def execute_system_action(): # Try to restart the web service (assuming it's ledmatrix-web.service) result = subprocess.run(['sudo', 'systemctl', 'restart', 'ledmatrix-web.service'], capture_output=True, text=True, timeout=10) + elif action == 'install_base_requirements': + req_file = PROJECT_ROOT / 'requirements.txt' + if not req_file.exists(): + return jsonify({'status': 'error', 'message': 'No requirements.txt found at project root'}) + result = subprocess.run( + [sys.executable, '-m', 'pip', 'install', '--break-system-packages', '-r', str(req_file)], + capture_output=True, text=True, timeout=120, cwd=str(PROJECT_ROOT) + ) + return jsonify({ + 'status': 'success' if result.returncode == 0 else 'error', + 'message': 'Base requirements installed successfully' if result.returncode == 0 else 'pip install failed', + 'output': (result.stdout + result.stderr).strip() + }) + elif action == 'install_plugin_requirements': + plugins_dir = Path(plugin_manager.plugins_dir) if plugin_manager else PROJECT_ROOT / 'plugin-repos' + results = [] + if plugins_dir.exists(): + for p in sorted(plugins_dir.iterdir()): + req = p / 'requirements.txt' + if p.is_dir() and req.exists(): + r = subprocess.run( + [sys.executable, '-m', 'pip', 'install', '--break-system-packages', '-r', str(req)], + capture_output=True, text=True, timeout=60 + ) + results.append({ + 'plugin': p.name, + 'ok': r.returncode == 0, + 'output': (r.stdout + r.stderr).strip() + }) + ok_count = sum(1 for r in results if r['ok']) + all_ok = all(r['ok'] for r in results) if results else True + return jsonify({ + 'status': 'success' if all_ok else 'error', + 'message': f'Processed {len(results)} plugin(s) — {ok_count} succeeded' if results else 'No plugin requirements.txt files found', + 'details': results + }) + elif action == 'force_git_reset': + project_dir = str(PROJECT_ROOT) + fetch = subprocess.run( + ['git', 'fetch', 'origin'], + capture_output=True, text=True, timeout=30, cwd=project_dir + ) + if fetch.returncode != 0: + return jsonify({'status': 'error', 'message': 'git fetch failed', 'output': fetch.stderr.strip()}) + reset = subprocess.run( + ['git', 'reset', '--hard', 'origin/main'], + capture_output=True, text=True, timeout=30, cwd=project_dir + ) + return jsonify({ + 'status': 'success' if reset.returncode == 0 else 'error', + 'message': 'Reset to origin/main successfully' if reset.returncode == 0 else 'git reset failed', + 'output': (reset.stdout + reset.stderr).strip() + }) + elif action == 'clear_pycache': + cleared = 0 + for d in PROJECT_ROOT.rglob('__pycache__'): + if d.is_dir(): + shutil.rmtree(d, ignore_errors=True) + cleared += 1 + return jsonify({'status': 'success', 'message': f'Cleared {cleared} __pycache__ directories'}) else: return jsonify({'status': 'error', 'message': 'Unknown action'}), 400 @@ -1596,6 +1666,27 @@ def execute_system_action(): logger.error("execute_system_action failed: %s", e, exc_info=True) return jsonify({'status': 'error', 'message': 'Action failed; see logs for details'}), 500 +@api_v3.route('/system/git-info', methods=['GET']) +def get_git_info(): + """Return branch, dirty state, recent commits and remote URL for the Tools tab.""" + d = str(PROJECT_ROOT) + try: + branch = subprocess.run(['git', 'branch', '--show-current'], capture_output=True, text=True, timeout=10, cwd=d) + status = subprocess.run(['git', 'status', '--short', '--untracked-files=no'], capture_output=True, text=True, timeout=15, cwd=d) + log = subprocess.run(['git', 'log', '--oneline', '-5'], capture_output=True, text=True, timeout=10, cwd=d) + remote = subprocess.run(['git', 'remote', 'get-url', 'origin'], capture_output=True, text=True, timeout=10, cwd=d) + return jsonify({ + 'branch': branch.stdout.strip(), + 'dirty': bool(status.stdout.strip()), + 'status': status.stdout.strip(), + 'recent_commits': log.stdout.strip(), + 'remote_url': remote.stdout.strip(), + }) + except Exception as e: + logger.error("get_git_info failed: %s", e, exc_info=True) + return jsonify({'status': 'error', 'message': 'Failed to get git info'}), 500 + + @api_v3.route('/hardware/status', methods=['GET']) def get_hardware_status(): """Return LED matrix hardware initialization status written by display_manager at startup.""" diff --git a/web_interface/blueprints/pages_v3.py b/web_interface/blueprints/pages_v3.py index 73939f52..1086b5f9 100644 --- a/web_interface/blueprints/pages_v3.py +++ b/web_interface/blueprints/pages_v3.py @@ -90,6 +90,8 @@ def load_partial(partial_name): return _load_cache_partial() elif partial_name == 'operation-history': return _load_operation_history_partial() + elif partial_name == 'tools': + return _load_tools_partial() else: return "Partial not found", 404 @@ -448,6 +450,15 @@ def _load_operation_history_partial(): return "Error loading partial", 500 +def _load_tools_partial(): + """Load tools/utilities partial.""" + try: + return render_template('v3/partials/tools.html') + except Exception: + logger.error("Error loading partial", exc_info=True) + return "Error loading partial", 500 + + def _load_plugin_config_partial(plugin_id): """ Load plugin configuration partial - server-side rendered form. diff --git a/web_interface/templates/v3/base.html b/web_interface/templates/v3/base.html index f8a08295..1527fc55 100644 --- a/web_interface/templates/v3/base.html +++ b/web_interface/templates/v3/base.html @@ -1009,6 +1009,11 @@
Inspect the current git state and pull or reset to the latest remote code.
+Pull latest (rebase)
+Stashes local changes, runs git pull --rebase, then restores the stash.
Force reset to origin/main
Runs git fetch origin then git reset --hard origin/main. Discards all local changes.
Re-run pip install to fix missing or broken packages.
Reinstall base requirements
+Installs from requirements.txt in the project root.
Reinstall plugin requirements
+Runs pip install for every installed plugin that has a requirements.txt.
Housekeeping operations that don't affect config or plugins.
+Clear Python cache
+Deletes all __pycache__ directories in the project. Useful after switching branches or debugging import issues.
Quick access to service restarts.
+Restart display service
+Restarts ledmatrix.service.
Restart web interface
+Restarts ledmatrix-web.service. The page will go offline briefly.