added first_time_install script

This commit is contained in:
Chuck
2025-08-11 14:22:54 -05:00
parent 1704001ef6
commit 643397c939
4 changed files with 579 additions and 1 deletions

199
INSTALLATION_GUIDE.md Normal file
View File

@@ -0,0 +1,199 @@
# LED Matrix Installation Guide
## Quick Start (Recommended for First-Time Installation)
For a complete first-time installation, run:
```bash
sudo ./first_time_install.sh
```
This single script handles everything you need for a new installation.
## Individual Scripts Explained
### **First-Time Installation Scripts**
#### `first_time_install.sh` ⭐ **RECOMMENDED**
- **When to use**: New installations only
- **What it does**: Complete setup including all steps below
- **Usage**: `sudo ./first_time_install.sh`
### **Service Installation Scripts**
#### `install_service.sh`
- **When to use**: Install main LED Matrix display service
- **What it does**:
- Creates systemd service for main display
- Creates systemd service for web interface
- Enables services to start on boot
- **Usage**: `sudo ./install_service.sh`
#### `install_web_service.sh`
- **When to use**: Install only the web interface service (legacy)
- **What it does**: Installs the web interface systemd service
- **Usage**: `sudo ./install_web_service.sh`
- **Note**: `install_service.sh` now handles this automatically
### **Permission Fix Scripts**
#### `fix_cache_permissions.sh`
- **When to use**: When you see cache permission errors
- **What it does**:
- Creates cache directories (`/var/cache/ledmatrix`)
- Sets proper permissions for cache access
- Creates placeholder logo directories
- **Usage**: `sudo ./fix_cache_permissions.sh`
#### `fix_web_permissions.sh`
- **When to use**: When web interface can't access logs or system commands
- **What it does**:
- Adds user to `systemd-journal` group (for log access)
- Adds user to `adm` group (for system access)
- Sets proper file ownership
- **Usage**: `./fix_web_permissions.sh` (run as regular user)
#### `configure_web_sudo.sh`
- **When to use**: When web interface buttons don't work (sudo password errors)
- **What it does**:
- Configures passwordless sudo access for web interface
- Allows web interface to start/stop services without password
- **Usage**: `./configure_web_sudo.sh` (run as regular user)
### **Dependency Installation Scripts**
#### `install_dependencies_apt.py`
- **When to use**: When you want to install packages via apt first, then pip
- **What it does**:
- Tries to install packages via apt (system packages)
- Falls back to pip with `--break-system-packages`
- Handles externally managed Python environments
- **Usage**: `sudo python3 install_dependencies_apt.py`
#### `start_web_v2.py`
- **When to use**: Manual web interface startup
- **What it does**:
- Installs dependencies
- Starts web interface directly
- Includes comprehensive logging
- **Usage**: `python3 start_web_v2.py`
#### `run_web_v2.sh`
- **When to use**: Manual web interface startup (shell script version)
- **What it does**: Same as `start_web_v2.py` but as a shell script
- **Usage**: `./run_web_v2.sh`
### **Utility Scripts**
#### `cleanup_venv.sh`
- **When to use**: Remove virtual environment if you don't want to use it
- **What it does**: Removes `venv_web_v2` directory
- **Usage**: `./cleanup_venv.sh`
#### `start_web_conditionally.py`
- **When to use**: Called by systemd service (don't run manually)
- **What it does**:
- Checks config for `web_display_autostart` setting
- Starts web interface only if enabled
- Used by the systemd service
## Installation Scenarios
### **Scenario 1: Brand New Installation**
```bash
# One command does everything
sudo ./first_time_install.sh
```
### **Scenario 2: Adding Web Interface to Existing Installation**
```bash
# Install web interface dependencies
sudo python3 install_dependencies_apt.py
# Fix permissions
./fix_web_permissions.sh
# Configure sudo access
./configure_web_sudo.sh
# Install services
sudo ./install_service.sh
```
### **Scenario 3: Fixing Permission Issues**
```bash
# Fix cache permissions
sudo ./fix_cache_permissions.sh
# Fix web interface permissions
./fix_web_permissions.sh
# Configure sudo access
./configure_web_sudo.sh
# Log out and back in for group changes to take effect
```
### **Scenario 4: Manual Web Interface Startup**
```bash
# Start web interface manually (for testing)
python3 start_web_v2.py
```
## Post-Installation Steps
### **1. Log Out and Log Back In**
After running permission scripts, you need to log out and back in for group changes to take effect:
```bash
# Or use this command to apply group changes immediately
newgrp systemd-journal
```
### **2. Configure the Web Interface**
Edit `config/config.json` and set:
```json
{
"web_display_autostart": true
}
```
### **3. Access the Web Interface**
Open your browser and go to:
```
http://your-pi-ip:5001
```
### **4. Test Everything**
- Check if services are running: `sudo systemctl status ledmatrix.service`
- Check web interface: `sudo systemctl status ledmatrix-web.service`
- View logs: `journalctl -u ledmatrix.service -f`
## Troubleshooting
### **Web Interface Not Accessible**
1. Check if service is running: `sudo systemctl status ledmatrix-web.service`
2. Check logs: `journalctl -u ledmatrix-web.service -f`
3. Ensure `web_display_autostart` is `true` in config
### **Permission Errors**
1. Run: `./fix_web_permissions.sh`
2. Run: `./configure_web_sudo.sh`
3. Log out and back in
### **Cache Permission Errors**
1. Run: `sudo ./fix_cache_permissions.sh`
### **Sudo Password Prompts**
1. Run: `./configure_web_sudo.sh`
2. Log out and back in
### **Dependency Installation Errors**
1. Run: `sudo python3 install_dependencies_apt.py`
## Summary
For **first-time installations**: Use `first_time_install.sh`
For **existing installations with issues**: Use the individual permission and configuration scripts as needed.
The `first_time_install.sh` script is designed to handle everything automatically, so you typically only need to run individual scripts if you're troubleshooting specific issues.

View File

@@ -246,6 +246,12 @@ sudo nano /boot/firmware/config.txt
sudo reboot sudo reboot
``` ```
9. Run the first_time_install.sh with
```
sudo ./first_time_install.sh
```
to ensure all the permissions are correct.
----------------------------------------------------------------------------------- -----------------------------------------------------------------------------------
## Configuration ## Configuration
@@ -807,7 +813,7 @@ This will start the display cycle but only stays active as long as your ssh sess
The LEDMatrix can be installed as a systemd service to run automatically at boot and be managed easily. The service runs as root to ensure proper hardware timing access for the LED matrix. The LEDMatrix can be installed as a systemd service to run automatically at boot and be managed easily. The service runs as root to ensure proper hardware timing access for the LED matrix.
### Installing the Service ### Installing the Service (this is included in the first_time_install.sh)
1. Make the install script executable: 1. Make the install script executable:
```bash ```bash

268
first_time_install.sh Normal file
View File

@@ -0,0 +1,268 @@
#!/bin/bash
# LED Matrix First-Time Installation Script
# This script handles the complete setup for a new LED Matrix installation
set -e
echo "=========================================="
echo "LED Matrix First-Time Installation Script"
echo "=========================================="
echo ""
# Get the actual user who invoked sudo
if [ -n "$SUDO_USER" ]; then
ACTUAL_USER="$SUDO_USER"
else
ACTUAL_USER=$(whoami)
fi
# Get the home directory of the actual user
USER_HOME=$(eval echo ~$ACTUAL_USER)
# Determine the Project Root Directory (where this script is located)
PROJECT_ROOT_DIR=$(cd "$(dirname "$0")" && pwd)
echo "Detected user: $ACTUAL_USER"
echo "User home directory: $USER_HOME"
echo "Project directory: $PROJECT_ROOT_DIR"
echo ""
# Check if running as root
if [ "$EUID" -eq 0 ]; then
echo "✓ Running as root (required for installation)"
else
echo "✗ This script must be run as root (use sudo)"
echo "Usage: sudo ./first_time_install.sh"
exit 1
fi
echo ""
echo "This script will perform the following steps:"
echo "1. Install system dependencies"
echo "2. Fix cache permissions"
echo "3. Install main LED Matrix service"
echo "4. Install web interface service"
echo "5. Configure web interface permissions"
echo "6. Configure passwordless sudo access"
echo "7. Set up proper file ownership"
echo "8. Test the installation"
echo ""
# Ask for confirmation
read -p "Do you want to proceed with the installation? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Installation cancelled."
exit 0
fi
echo ""
echo "Step 1: Installing system dependencies..."
echo "----------------------------------------"
# Update package list
apt update
# Install required system packages
echo "Installing Python packages and dependencies..."
apt install -y python3-pip python3-venv python3-dev python3-pil python3-pil.imagetk
# Install additional system dependencies that might be needed
echo "Installing additional system dependencies..."
apt install -y git curl wget unzip
echo "✓ System dependencies installed"
echo ""
echo "Step 2: Fixing cache permissions..."
echo "----------------------------------"
# Run the cache permissions fix
if [ -f "$PROJECT_ROOT_DIR/fix_cache_permissions.sh" ]; then
echo "Running cache permissions fix..."
bash "$PROJECT_ROOT_DIR/fix_cache_permissions.sh"
echo "✓ Cache permissions fixed"
else
echo "⚠ Cache permissions script not found, creating cache directories manually..."
mkdir -p /var/cache/ledmatrix
chown "$ACTUAL_USER:$ACTUAL_USER" /var/cache/ledmatrix
chmod 777 /var/cache/ledmatrix
echo "✓ Cache directories created manually"
fi
echo ""
echo "Step 3: Installing main LED Matrix service..."
echo "---------------------------------------------"
# Run the main service installation
if [ -f "$PROJECT_ROOT_DIR/install_service.sh" ]; then
echo "Running main service installation..."
bash "$PROJECT_ROOT_DIR/install_service.sh"
echo "✓ Main LED Matrix service installed"
else
echo "✗ Main service installation script not found"
exit 1
fi
echo ""
echo "Step 4: Installing web interface dependencies..."
echo "------------------------------------------------"
# Install web interface dependencies
echo "Installing Python dependencies for web interface..."
cd "$PROJECT_ROOT_DIR"
# Try to install dependencies using the smart installer if available
if [ -f "$PROJECT_ROOT_DIR/install_dependencies_apt.py" ]; then
echo "Using smart dependency installer..."
python3 "$PROJECT_ROOT_DIR/install_dependencies_apt.py"
else
echo "Using pip to install dependencies..."
python3 -m pip install --break-system-packages -r requirements_web_v2.txt
# Install rgbmatrix module from local source
echo "Installing rgbmatrix module..."
python3 -m pip install --break-system-packages -e rpi-rgb-led-matrix-master/bindings/python
fi
echo "✓ Web interface dependencies installed"
echo ""
echo "Step 5: Configuring web interface permissions..."
echo "------------------------------------------------"
# Add user to required groups
echo "Adding user to systemd-journal group..."
usermod -a -G systemd-journal "$ACTUAL_USER"
echo "Adding user to adm group..."
usermod -a -G adm "$ACTUAL_USER"
echo "✓ User added to required groups"
echo ""
echo "Step 6: Configuring passwordless sudo access..."
echo "------------------------------------------------"
# Create sudoers configuration for the web interface
echo "Creating sudoers configuration..."
SUDOERS_FILE="/etc/sudoers.d/ledmatrix_web"
# Get command paths
PYTHON_PATH=$(which python3)
SYSTEMCTL_PATH=$(which systemctl)
REBOOT_PATH=$(which reboot)
POWEROFF_PATH=$(which poweroff)
BASH_PATH=$(which bash)
# Create sudoers content
cat > /tmp/ledmatrix_web_sudoers << EOF
# LED Matrix Web Interface passwordless sudo configuration
# This allows the web interface user to run specific commands without a password
# Allow $ACTUAL_USER to run specific commands without a password for the LED Matrix web interface
$ACTUAL_USER ALL=(ALL) NOPASSWD: $REBOOT_PATH
$ACTUAL_USER ALL=(ALL) NOPASSWD: $POWEROFF_PATH
$ACTUAL_USER ALL=(ALL) NOPASSWD: $SYSTEMCTL_PATH start ledmatrix.service
$ACTUAL_USER ALL=(ALL) NOPASSWD: $SYSTEMCTL_PATH stop ledmatrix.service
$ACTUAL_USER ALL=(ALL) NOPASSWD: $SYSTEMCTL_PATH restart ledmatrix.service
$ACTUAL_USER ALL=(ALL) NOPASSWD: $SYSTEMCTL_PATH enable ledmatrix.service
$ACTUAL_USER ALL=(ALL) NOPASSWD: $SYSTEMCTL_PATH disable ledmatrix.service
$ACTUAL_USER ALL=(ALL) NOPASSWD: $SYSTEMCTL_PATH status ledmatrix.service
$ACTUAL_USER ALL=(ALL) NOPASSWD: $PYTHON_PATH $PROJECT_ROOT_DIR/display_controller.py
$ACTUAL_USER ALL=(ALL) NOPASSWD: $BASH_PATH $PROJECT_ROOT_DIR/start_display.sh
$ACTUAL_USER ALL=(ALL) NOPASSWD: $BASH_PATH $PROJECT_ROOT_DIR/stop_display.sh
EOF
# Install the sudoers file
cp /tmp/ledmatrix_web_sudoers "$SUDOERS_FILE"
chmod 440 "$SUDOERS_FILE"
rm /tmp/ledmatrix_web_sudoers
echo "✓ Passwordless sudo access configured"
echo ""
echo "Step 7: Setting proper file ownership..."
echo "----------------------------------------"
# Set ownership of project files to the user
echo "Setting project file ownership..."
chown -R "$ACTUAL_USER:$ACTUAL_USER" "$PROJECT_ROOT_DIR"
# Set proper permissions for config files
if [ -f "$PROJECT_ROOT_DIR/config/config.json" ]; then
chmod 644 "$PROJECT_ROOT_DIR/config/config.json"
echo "✓ Config file permissions set"
fi
echo "✓ File ownership configured"
echo ""
echo "Step 8: Testing the installation..."
echo "----------------------------------"
# Test sudo access
echo "Testing sudo access..."
if sudo -u "$ACTUAL_USER" sudo -n systemctl status ledmatrix.service > /dev/null 2>&1; then
echo "✓ Sudo access test passed"
else
echo "⚠ Sudo access test failed - may need to log out and back in"
fi
# Test journal access
echo "Testing journal access..."
if sudo -u "$ACTUAL_USER" journalctl --no-pager --lines=1 > /dev/null 2>&1; then
echo "✓ Journal access test passed"
else
echo "⚠ Journal access test failed - may need to log out and back in"
fi
# Check service status
echo "Checking service status..."
if systemctl is-active --quiet ledmatrix.service; then
echo "✓ Main LED Matrix service is running"
else
echo "⚠ Main LED Matrix service is not running"
fi
if systemctl is-active --quiet ledmatrix-web.service; then
echo "✓ Web interface service is running"
else
echo "⚠ Web interface service is not running"
fi
echo ""
echo "=========================================="
echo "Installation Complete!"
echo "=========================================="
echo ""
echo "IMPORTANT: For group changes to take effect, you need to:"
echo "1. Log out and log back in to your SSH session, OR"
echo "2. Run: newgrp systemd-journal"
echo ""
echo "After logging back in, you can:"
echo ""
echo "Access the web interface at:"
echo " http://your-pi-ip:5001"
echo ""
echo "Check service status:"
echo " sudo systemctl status ledmatrix.service"
echo " sudo systemctl status ledmatrix-web.service"
echo ""
echo "View logs:"
echo " journalctl -u ledmatrix.service -f"
echo " journalctl -u ledmatrix-web.service -f"
echo ""
echo "Control the display:"
echo " sudo systemctl start ledmatrix.service"
echo " sudo systemctl stop ledmatrix.service"
echo ""
echo "Enable/disable web interface autostart:"
echo " Edit config/config.json and set 'web_display_autostart': true"
echo ""
echo "Configuration files:"
echo " Main config: config/config.json"
echo " Secrets: config/config_secrets.json (create from template if needed)"
echo ""
echo "Enjoy your LED Matrix display!"

105
fix_web_permissions.sh Normal file
View File

@@ -0,0 +1,105 @@
#!/bin/bash
# LED Matrix Web Interface Permissions Fix Script
# This script fixes permissions for the web interface to access logs and system commands
set -e
echo "Fixing LED Matrix Web Interface permissions..."
# Get the current user (should be the user running the web interface)
WEB_USER=$(whoami)
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo "Detected web interface user: $WEB_USER"
echo "Project directory: $PROJECT_DIR"
# Check if running as root
if [ "$EUID" -eq 0 ]; then
echo "Error: This script should not be run as root."
echo "Run it as the user that will be running the web interface."
exit 1
fi
echo ""
echo "This script will:"
echo "1. Add the web user to the 'systemd-journal' group for log access"
echo "2. Add the web user to the 'adm' group for additional system access"
echo "3. Configure sudoers for passwordless access to system commands"
echo "4. Set proper file permissions"
echo ""
# Ask for confirmation
read -p "Do you want to proceed? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Permission fix cancelled."
exit 0
fi
echo ""
echo "Step 1: Adding user to systemd-journal group..."
if sudo usermod -a -G systemd-journal "$WEB_USER"; then
echo "✓ Added $WEB_USER to systemd-journal group"
else
echo "✗ Failed to add user to systemd-journal group"
fi
echo ""
echo "Step 2: Adding user to adm group..."
if sudo usermod -a -G adm "$WEB_USER"; then
echo "✓ Added $WEB_USER to adm group"
else
echo "✗ Failed to add user to adm group"
fi
echo ""
echo "Step 3: Setting proper file permissions..."
# Set ownership of project files to the web user
if sudo chown -R "$WEB_USER:$WEB_USER" "$PROJECT_DIR"; then
echo "✓ Set project ownership to $WEB_USER"
else
echo "✗ Failed to set project ownership"
fi
# Set proper permissions for config files
if sudo chmod 644 "$PROJECT_DIR/config/config.json" 2>/dev/null; then
echo "✓ Set config file permissions"
else
echo "⚠ Config file permissions not set (file may not exist)"
fi
echo ""
echo "Step 4: Testing journal access..."
# Test if the user can now access journal logs
if journalctl --user-unit=ledmatrix.service --no-pager --lines=1 > /dev/null 2>&1; then
echo "✓ Journal access test passed"
elif sudo -u "$WEB_USER" journalctl --no-pager --lines=1 > /dev/null 2>&1; then
echo "✓ Journal access test passed (with sudo)"
else
echo "⚠ Journal access test failed - you may need to log out and back in"
fi
echo ""
echo "Step 5: Testing sudo access..."
# Test sudo access for system commands
if sudo -n systemctl status ledmatrix.service > /dev/null 2>&1; then
echo "✓ Sudo access test passed"
else
echo "⚠ Sudo access test failed - you may need to run configure_web_sudo.sh"
fi
echo ""
echo "Permission fix completed!"
echo ""
echo "IMPORTANT: For group changes to take effect, you need to:"
echo "1. Log out and log back in, OR"
echo "2. Run: newgrp systemd-journal"
echo "3. Restart the web interface service:"
echo " sudo systemctl restart ledmatrix-web.service"
echo ""
echo "After logging back in, test journal access with:"
echo " journalctl --no-pager --lines=5"
echo ""
echo "If you still have sudo issues, run:"
echo " ./configure_web_sudo.sh"