helgeerbe / picframe

Picture frame viewer for raspi, controlled via mqtt and automatticly integrated as mqtt device in homeassistant.
MIT License
106 stars 30 forks source link

Building an installation script for picframe setup #409

Open sapnho opened 1 day ago

sapnho commented 1 day ago

I am building a script to include all the necessary steps to get picframe up and running. (Thanks Helge and Paddy!)

The script would reboot where necessary, remembering the next step in the installation sequence.

Everything works.... except I cannot get picframe -i /home/pi/ to run. That means, it runs, doesn't report any errors but doesn't do what it's supposed to do.

This is the header of script to manage the installation sequence.

#!/bin/bash

# Path to store progress and log file
PROGRESS_FILE="/home/pi/install_progress.txt"
LOG_FILE="/home/pi/install_log.txt"
SERVICE_NAME="install_script_service"

# Function to log messages
log_message() {
    echo "$1" | tee -a "$LOG_FILE"
}

# Function to update progress
update_progress() {
    echo "$1" > "$PROGRESS_FILE"
}

# Function to get the last completed step
get_last_completed_step() {
    if [ -f "$PROGRESS_FILE" ]; then
        cat "$PROGRESS_FILE"
    else
        echo "0"
    fi
}

# Function to add a systemd service to resume the script after reboot
add_systemd_service() {
    local script_path=$(realpath "$0")
    SERVICE_FILE="/etc/systemd/system/$SERVICE_NAME.service"
    sudo tee "$SERVICE_FILE" > /dev/null <<EOL
[Unit]
Description=Resume install script after reboot

[Service]
ExecStart=$script_path
Type=oneshot
RemainAfterExit=true

[Install]
WantedBy=multi-user.target
EOL
    sudo systemctl enable $SERVICE_NAME
    log_message "Added systemd service for reboot resume."
}

# Function to remove the systemd service after completion
remove_systemd_service() {
    sudo systemctl disable $SERVICE_NAME
    sudo rm /etc/systemd/system/$SERVICE_NAME.service
    log_message "Removed systemd service after completion."
}

# Function to reboot and resume
reboot_and_resume() {
    add_systemd_service
    update_progress "$1"
    log_message "Rebooting to complete the installation. The script will continue after reboot."
    sudo reboot
    exit 0
}

Then I have all the various parts like

# Main install script

# Get the last completed step
LAST_COMPLETED_STEP=$(get_last_completed_step)

# Step 1: Update the system (if not already done)
if [ "$LAST_COMPLETED_STEP" -lt 1 ]; then
    log_message "Step 1: Updating system..."
    sudo apt-get update && sudo apt upgrade -y
    reboot_and_resume 1
fi

Samba is Step 2 followed by:

# Step 3: Install additional packages
if [ "$LAST_COMPLETED_STEP" -lt 3 ]; then
    log_message "Step 3: Installing additional packages (xwayland, labwc, wlr-randr, etc.)..."
    sudo apt-get install git libsdl2-dev xwayland labwc wlr-randr -y
    reboot_and_resume 3
fi

# Step 4: Update raspi-config settings and reboot
if [ "$LAST_COMPLETED_STEP" -lt 4 ]; then
    log_message "Step 4: Configuring raspi-config settings for boot..."
    sudo raspi-config nonint do_boot_behaviour B2
    reboot_and_resume 4
fi

# Step 5: Set up virtual environment and install Picframe
if [ "$LAST_COMPLETED_STEP" -lt 5 ]; then
    log_message "Step 5: Setting up virtual environment and installing Picframe..."
    mkdir venv_picframe
    python -m venv /home/pi/venv_picframe
    source venv_picframe/bin/activate
    pip install picframe
    # Initialize Picframe and confirm default directories
    if (echo -e "\n\n\n" | picframe -i /home/pi/); then
        log_message "Picframe initialized with default directories."
    else
        log_message "Error: Failed to initialize Picframe."
        exit 1
    fi
    update_progress 5
    log_message "Picframe installation and initialization completed."
fi

I also added the autostart features in Steps 6-8 and all works fine, except for picframe -i /home/pi/.

I tried so many things but couldn't come up with a solution.

Surely, there are brighter heads here than me.

sapnho commented 1 day ago

Interestingly, it works fine when I put step 5 in a separate script.

sapnho commented 1 hour ago

Problem solved. The script installed as sudo, not a user pi.