From 8e3462aaf2aa8fabba38709d7e145b26600f8677 Mon Sep 17 00:00:00 2001 From: Oier Bravo Urtasun Date: Thu, 25 Sep 2025 15:34:30 +0200 Subject: [PATCH] more install things --- ENV_SETUP.md | 135 +++++++++++++++++++++++++++++++++++++++++ install.sh | 37 +++++++++-- templates/env.template | 25 ++++++++ uninstall.sh | 18 +++++- usb_automount.sh | 9 ++- 5 files changed, 214 insertions(+), 10 deletions(-) create mode 100644 ENV_SETUP.md create mode 100644 templates/env.template diff --git a/ENV_SETUP.md b/ENV_SETUP.md new file mode 100644 index 0000000..0b5cbed --- /dev/null +++ b/ENV_SETUP.md @@ -0,0 +1,135 @@ +# Environment Configuration Setup + +This document explains how to configure the Video Player system using environment variables. + +## Environment File Setup + +The system now supports reading configuration from a `.env` file. This allows you to customize the user, group, and other settings without modifying the installation scripts. + +### Creating the Environment File + +1. Copy the template file: + ```bash + cp templates/env.template .env + ``` + +2. Edit the `.env` file with your preferred settings: + ```bash + nano .env + ``` + +### Available Configuration Variables + +| Variable | Default | Description | +|----------|---------|-------------| +| `USER` | `pi` | System user to run the video player service | +| `GROUP` | `pi` | System group for the user | +| `VIDEO_FOLDER` | `/home/pi/Videos` | Directory containing video files | +| `SERVICE_NAME` | `video-player` | Name of the systemd service | +| `INSTALL_DIR` | `/opt/video_player` | Installation directory | +| `CONFIG_DIR` | `/etc/video_player` | Configuration directory | +| `GPIO_GROUP` | `gpio` | Group for GPIO access | +| `LOG_FILE` | `/var/log/video_player.log` | Log file path | +| `LOG_LEVEL` | `INFO` | Logging level | +| `DISPLAY` | `:0` | X11 display | +| `XAUTHORITY` | `/home/pi/.Xauthority` | X11 authority file | + +### Example .env File + +```bash +# System User Configuration +USER=myuser +GROUP=myuser + +# Video Configuration +VIDEO_FOLDER=/home/myuser/Videos + +# Service Configuration +SERVICE_NAME=video-player +INSTALL_DIR=/opt/video_player +CONFIG_DIR=/etc/video_player + +# GPIO Configuration +GPIO_GROUP=gpio + +# Logging Configuration +LOG_FILE=/var/log/video_player.log +LOG_LEVEL=INFO + +# Display Configuration +DISPLAY=:0 +XAUTHORITY=/home/myuser/.Xauthority +``` + +## Installation Process + +1. **Create your .env file** (as described above) + +2. **Run the installation script**: + ```bash + sudo ./install.sh + ``` + + The script will: + - Load configuration from your `.env` file + - Create the specified user and group if they don't exist + - Set up the service with the correct user/group + - Configure file permissions appropriately + +3. **Verify the installation**: + ```bash + video-player-test + ``` + +## Troubleshooting + +### User/Group Issues + +If you encounter permission errors: + +1. Check that the user exists: + ```bash + id $USER + ``` + +2. Check that the user is in the GPIO group: + ```bash + groups $USER + ``` + +3. If needed, manually add the user to the GPIO group: + ```bash + sudo usermod -a -G gpio $USER + ``` + +### Service Issues + +If the service fails to start: + +1. Check the service status: + ```bash + sudo systemctl status video-player + ``` + +2. Check the logs: + ```bash + sudo journalctl -u video-player -f + ``` + +3. Verify the service file has the correct user/group: + ```bash + sudo systemctl cat video-player + ``` + +## Uninstallation + +To uninstall the system: + +1. **Create your .env file** (if not already done) + +2. **Run the uninstall script**: + ```bash + sudo ./uninstall.sh + ``` + +The uninstall script will use the same configuration from your `.env` file to properly clean up the installation. diff --git a/install.sh b/install.sh index 4b759ea..5763150 100755 --- a/install.sh +++ b/install.sh @@ -16,8 +16,6 @@ NC='\033[0m' # No Color INSTALL_DIR="/opt/video_player" CONFIG_DIR="/etc/video_player" SERVICE_NAME="video-player" -USER="pi" -VIDEO_FOLDER="${VIDEO_FOLDER:-/home/pi/Videos}" # Function to print colored output print_status() { @@ -36,6 +34,25 @@ print_error() { echo -e "${RED}[ERROR]${NC} $1" } +# Load environment variables from .env file if it exists +if [[ -f ".env" ]]; then + print_status "Loading configuration from .env file..." + source .env +else + print_warning "No .env file found, using default values" + # Create .env file from template if it doesn't exist + if [[ -f "templates/env.template" ]]; then + cp templates/env.template .env + print_status "Created .env file from template. Please review and modify as needed." + fi +fi + +# Set defaults if not defined in .env +USER="${USER:-pi}" +GROUP="${GROUP:-pi}" +VIDEO_FOLDER="${VIDEO_FOLDER:-/home/pi/Videos}" +GPIO_GROUP="${GPIO_GROUP:-gpio}" + # Function to check if running as root check_root() { if [[ $EUID -ne 0 ]]; then @@ -182,10 +199,18 @@ copy_files() { cp usb_automount.sh "$INSTALL_DIR/" chmod +x "$INSTALL_DIR/usb_automount.sh" - # Copy service files + # Copy service files and customize them cp video-player.service /etc/systemd/system/ cp usb-automount.service /etc/systemd/system/ + # Update service files with correct user/group and environment variables + sed -i "s/User=root/User=$USER/g" /etc/systemd/system/video-player.service + sed -i "s/Group=root/Group=$GROUP/g" /etc/systemd/system/video-player.service + sed -i "s/SupplementaryGroups=gpio/SupplementaryGroups=$GPIO_GROUP/g" /etc/systemd/system/video-player.service + sed -i "s|Environment=XAUTHORITY=/home/pi/.Xauthority|Environment=XAUTHORITY=/home/$USER/.Xauthority|g" /etc/systemd/system/video-player.service + sed -i "s/User=root/User=$USER/g" /etc/systemd/system/usb-automount.service + sed -i "s/Group=root/Group=$GROUP/g" /etc/systemd/system/usb-automount.service + # Copy configuration templates mkdir -p "$CONFIG_DIR/templates" cp templates/* "$CONFIG_DIR/templates/" 2>/dev/null || true @@ -368,12 +393,12 @@ setup_gpio_permissions() { print_status "Setting up GPIO permissions..." # Add user to gpio group - usermod -a -G gpio "$USER" + usermod -a -G "$GPIO_GROUP" "$USER" # Create udev rule for GPIO access cat > /etc/udev/rules.d/99-gpio.rules << EOF -SUBSYSTEM=="gpio", GROUP="gpio", MODE="0664" -SUBSYSTEM=="gpio*", PROGRAM="/bin/sh -c 'chown -R root:gpio /sys/class/gpio && chmod -R 775 /sys/class/gpio; chown -R root:gpio /sys/devices/virtual/gpio && chmod -R 775 /sys/devices/virtual/gpio'" +SUBSYSTEM=="gpio", GROUP="$GPIO_GROUP", MODE="0664" +SUBSYSTEM=="gpio*", PROGRAM="/bin/sh -c 'chown -R root:$GPIO_GROUP /sys/class/gpio && chmod -R 775 /sys/class/gpio; chown -R root:$GPIO_GROUP /sys/devices/virtual/gpio && chmod -R 775 /sys/devices/virtual/gpio'" EOF # Reload udev rules diff --git a/templates/env.template b/templates/env.template new file mode 100644 index 0000000..a729c2b --- /dev/null +++ b/templates/env.template @@ -0,0 +1,25 @@ +# Environment Configuration for Video Player System +# Copy this file to .env and modify the values as needed + +# System User Configuration +USER=pi +GROUP=pi + +# Video Configuration +VIDEO_FOLDER=/home/pi/Videos + +# Service Configuration +SERVICE_NAME=video-player +INSTALL_DIR=/opt/video_player +CONFIG_DIR=/etc/video_player + +# GPIO Configuration +GPIO_GROUP=gpio + +# Logging Configuration +LOG_FILE=/var/log/video_player.log +LOG_LEVEL=INFO + +# Display Configuration +DISPLAY=:0 +XAUTHORITY=/home/pi/.Xauthority diff --git a/uninstall.sh b/uninstall.sh index 1e7e554..5ce9472 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -16,7 +16,6 @@ NC='\033[0m' # No Color INSTALL_DIR="/opt/video_player" CONFIG_DIR="/etc/video_player" SERVICE_NAME="video-player" -USER="pi" # Function to print colored output print_status() { @@ -35,6 +34,19 @@ print_error() { echo -e "${RED}[ERROR]${NC} $1" } +# Load environment variables from .env file if it exists +if [[ -f ".env" ]]; then + print_status "Loading configuration from .env file..." + source .env +else + print_warning "No .env file found, using default values" +fi + +# Set defaults if not defined in .env +USER="${USER:-pi}" +GROUP="${GROUP:-pi}" +GPIO_GROUP="${GPIO_GROUP:-gpio}" + # Function to check if running as root check_root() { if [[ $EUID -ne 0 ]]; then @@ -175,8 +187,8 @@ remove_gpio_rules() { remove_gpio_group() { print_status "Removing user from gpio group..." - if groups "$USER" | grep -q "gpio"; then - gpasswd -d "$USER" gpio + if groups "$USER" | grep -q "$GPIO_GROUP"; then + gpasswd -d "$USER" "$GPIO_GROUP" print_success "User removed from gpio group" else print_warning "User not in gpio group" diff --git a/usb_automount.sh b/usb_automount.sh index ceb52fc..cd234a8 100755 --- a/usb_automount.sh +++ b/usb_automount.sh @@ -6,7 +6,14 @@ # Configuration MOUNT_BASE="/media/usb" LOG_FILE="/var/log/usb_automount.log" -USER="pi" + +# Load environment variables from .env file if it exists +if [[ -f ".env" ]]; then + source .env +fi + +# Set defaults if not defined in .env +USER="${USER:-pi}" # Function to log messages log_message() {