feat: implement pre-compiled OpenFrameworks strategy and OS-agnostic swap management

This commit is contained in:
Timothy Hofland
2026-03-11 09:30:01 +01:00
parent 0328ddcfa8
commit eaeb06c7e2

View File

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
# MPVJ Headless Setup Script # MPVJ Headless Setup Script (Hardened & Pre-compiled OF)
# Usage: curl -sSL <URL>/setup.sh | sudo bash -s -- <REPO_URL> # Usage: curl -sSL <URL>/setup.sh | sudo bash -s -- <REPO_URL>
set -e set -e
@ -12,19 +12,22 @@ if [ -z "$REPO_URL" ]; then
exit 1 exit 1
fi fi
# 0. ROOT & USER DETECTION # 0. BASH & ROOT CHECK
if [ "$(id -u)" -ne 0 ]; then if [ -z "$BASH_VERSION" ]; then
echo "ERROR: This script must be run as root." echo "ERROR: This script must be run with BASH, not SH."
echo "Please use: sudo bash $0 $@" echo "Please use: sudo bash setup.sh $REPO_URL"
exit 1 exit 1
fi fi
# Detect the real user who called sudo if [ "$(id -u)" -ne 0 ]; then
echo "ERROR: This script must be run as root (use sudo)."
exit 1
fi
# Detect user
REAL_USER=${SUDO_USER:-$(logname)} REAL_USER=${SUDO_USER:-$(logname)}
REAL_HOME=$(eval echo "~$REAL_USER") REAL_HOME=$(eval echo "~$REAL_USER")
echo "Target User: $REAL_USER ($REAL_HOME)"
echo "Target User: $REAL_USER"
echo "Target Home: $REAL_HOME"
# 1. INTERROGATION PHASE # 1. INTERROGATION PHASE
echo "Interrogating user for configuration..." echo "Interrogating user for configuration..."
@ -37,21 +40,21 @@ HOSTNAME=${HOSTNAME:-mpvj}
WIFI_SSID=$(whiptail --inputbox "Enter WiFi SSID (e.g. MPVJ-AP)" 8 45 "MPVJ-AP" --title "WiFi SSID Configuration" 3>&1 1>&2 2>&3 < /dev/tty) || exit 1 WIFI_SSID=$(whiptail --inputbox "Enter WiFi SSID (e.g. MPVJ-AP)" 8 45 "MPVJ-AP" --title "WiFi SSID Configuration" 3>&1 1>&2 2>&3 < /dev/tty) || exit 1
WIFI_SSID=${WIFI_SSID:-MPVJ-AP} WIFI_SSID=${WIFI_SSID:-MPVJ-AP}
# 1.3 Country Code & Password Validation # 1.3 Country Code & Password
while true; do while true; do
WIFI_COUNTRY=$(whiptail --inputbox "Enter 2-letter Country Code (e.g. US, GB, DE)" 8 45 "US" --title "WiFi Country Configuration" 3>&1 1>&2 2>&3 < /dev/tty) || exit 1 WIFI_COUNTRY=$(whiptail --inputbox "Enter 2-letter Country Code (e.g. US, GB)" 8 45 "US" --title "WiFi Country Configuration" 3>&1 1>&2 2>&3 < /dev/tty) || exit 1
WIFI_COUNTRY=$(echo "$WIFI_COUNTRY" | tr '[:lower:]' '[:upper:]') WIFI_COUNTRY=$(echo "$WIFI_COUNTRY" | tr '[:lower:]' '[:upper:]')
if ! echo "$WIFI_COUNTRY" | grep -qE "^[A-Z]{2}$"; then if ! echo "$WIFI_COUNTRY" | grep -qE "^[A-Z]{2}$"; then
whiptail --msgbox "Error: Country Code must be exactly 2 letters (e.g., US)." 8 45 < /dev/tty whiptail --msgbox "Error: Country Code must be 2 letters." 8 45 < /dev/tty
else else
break break
fi fi
done done
while true; do while true; do
WIFI_PASS=$(whiptail --passwordbox "Enter WiFi Password (minimum 8 characters)" 8 45 --title "WiFi Password Configuration" 3>&1 1>&2 2>&3 < /dev/tty) || exit 1 WIFI_PASS=$(whiptail --passwordbox "Enter WiFi Password (min 8 chars)" 8 45 --title "WiFi Password Configuration" 3>&1 1>&2 2>&3 < /dev/tty) || exit 1
if [ ${#WIFI_PASS} -lt 8 ]; then if [ ${#WIFI_PASS} -lt 8 ]; then
whiptail --msgbox "Error: WiFi Password must be at least 8 characters long (WPA2 requirement)." 8 45 < /dev/tty whiptail --msgbox "Error: Minimum 8 characters required." 8 45 < /dev/tty
else else
break break
fi fi
@ -60,67 +63,47 @@ done
# 2. SYSTEM STAGING PHASE # 2. SYSTEM STAGING PHASE
echo "System Staging Phase..." echo "System Staging Phase..."
# 2.1 Pre-Flight Disk Space Check (4GB Required) # 2.1 Pre-Flight Disk Check
FREE_SPACE_KB=$(df / --output=avail | tail -n1) FREE_SPACE_KB=$(df / --output=avail | tail -n1)
MIN_SPACE_KB=4194304 # 4GB [ "$FREE_SPACE_KB" -lt 4194304 ] && { whiptail --msgbox "Error: 4GB free space required." 8 45 < /dev/tty; exit 1; }
if [ "$FREE_SPACE_KB" -lt "$MIN_SPACE_KB" ]; then
whiptail --msgbox "Error: Not enough disk space. At least 4GB of free space is required for installation and swap scaling." 8 45 < /dev/tty
exit 1
fi
# 2.2 System Updates # 2.2 System Updates
( if command -v apt-get > /dev/null; then
echo 25 apt-get update -y && DEBIAN_FRONTEND=noninteractive apt-get upgrade -y
apt-get update -y > /dev/null 2>&1 elif command -v pacman > /dev/null; then
echo 75 pacman -Syu --noconfirm
DEBIAN_FRONTEND=noninteractive apt-get upgrade -y > /dev/null 2>&1 fi
echo 100
) | whiptail --gauge "Updating System Packages..." 6 60 0
# 2.3 Install Dependencies # 2.3 Install Dependencies
( echo "Installing Core Dependencies..."
echo 20 if command -v apt-get > /dev/null; then
DEBIAN_FRONTEND=noninteractive apt-get install -y git hostapd dnsmasq avahi-daemon curl ffmpeg build-essential libzmq3-dev libavahi-compat-libdnssd-dev > /dev/null 2>&1 DEBIAN_FRONTEND=noninteractive apt-get install -y git hostapd dnsmasq avahi-daemon curl ffmpeg build-essential libzmq3-dev libavahi-compat-libdnssd-dev wget
echo 60 elif command -v pacman > /dev/null; then
curl -fsSL https://deb.nodesource.com/setup_20.x | bash - > /dev/null 2>&1 pacman -S --noconfirm git hostapd dnsmasq avahi curl ffmpeg base-devel zeromq wget
DEBIAN_FRONTEND=noninteractive apt-get install -y nodejs > /dev/null 2>&1 fi
echo 100
) | whiptail --gauge "Installing Core Dependencies..." 6 60 0
# 2.4 Hostname & Country Code # Install Node.js if missing
if ! command -v node > /dev/null; then
curl -fsSL https://deb.nodesource.com/setup_20.x | bash - || true
command -v apt-get > /dev/null && apt-get install -y nodejs
fi
# 2.4 Hostname & WiFi Country
hostnamectl set-hostname "$HOSTNAME" hostnamectl set-hostname "$HOSTNAME"
sed -i "s/127.0.1.1.*/127.0.1.1\t$HOSTNAME/g" /etc/hosts sed -i "s/127.0.1.1.*/127.0.1.1\t$HOSTNAME/g" /etc/hosts || true
if command -v raspi-config > /dev/null; then
raspi-config nonint do_wifi_country "$WIFI_COUNTRY" > /dev/null 2>&1 # 2.5 OS-Agnostic Swap Increase (2GB)
echo "Ensuring 2GB Swap for build..."
if [ ! -f /swapfile_mpvj ]; then
fallocate -l 2G /swapfile_mpvj || dd if=/dev/zero of=/swapfile_mpvj bs=1M count=2048
chmod 600 /swapfile_mpvj
mkswap /swapfile_mpvj
swapon /swapfile_mpvj
fi fi
# 2.5 Swap Increase # 3. NETWORKING
if [ -f /etc/dphys-swapfile ]; then
sed -i "s/CONF_SWAPSIZE=.*/CONF_SWAPSIZE=2048/g" /etc/dphys-swapfile
dphys-swapfile setup
dphys-swapfile swapon
fi
# 2.6 Graphics Driver Configuration (KMS/DRM)
echo "Configuring graphics drivers for headless operation..."
if [ -f /boot/config.txt ]; then
if grep -q "gpu_mem=" /boot/config.txt; then
sed -i "s/gpu_mem=.*/gpu_mem=256/g" /boot/config.txt
else
echo "gpu_mem=256" >> /boot/config.txt
fi
if ! grep -q "dtoverlay=vc4-kms-v3d" /boot/config.txt; then
echo "dtoverlay=vc4-kms-v3d" >> /boot/config.txt
fi
fi
if command -v raspi-config > /dev/null; then
raspi-config nonint do_boot_behaviour B2 > /dev/null 2>&1
fi
# 3. NETWORKING CONFIGURATION
echo "Configuring Networking..." echo "Configuring Networking..."
# hostapd
# 3.1 hostapd
cat <<EOF > /etc/hostapd/hostapd.conf cat <<EOF > /etc/hostapd/hostapd.conf
interface=wlan0 interface=wlan0
driver=nl80211 driver=nl80211
@ -130,88 +113,47 @@ channel=7
wpa=2 wpa=2
wpa_passphrase=$WIFI_PASS wpa_passphrase=$WIFI_PASS
wpa_key_mgmt=WPA-PSK wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
auth_algs=1
macaddr_acl=0
EOF EOF
sed -i 's/#DAEMON_CONF=""/DAEMON_CONF="\/etc\/hostapd\/hostapd.conf"/g' /etc/default/hostapd [ -f /etc/default/hostapd ] && sed -i 's/#DAEMON_CONF=""/DAEMON_CONF="\/etc\/hostapd\/hostapd.conf"/g' /etc/default/hostapd
# 3.2 dnsmasq # dnsmasq
[ -f /etc/dnsmasq.conf ] && mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak [ -f /etc/dnsmasq.conf ] && mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak || true
cat <<EOF > /etc/dnsmasq.conf cat <<EOF > /etc/dnsmasq.conf
interface=wlan0 interface=wlan0
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h
domain=local
address=/$HOSTNAME.local/192.168.4.1 address=/$HOSTNAME.local/192.168.4.1
EOF EOF
# 3.3 dhcpcd # dhcpcd / network manager
if ! grep -q "interface wlan0" /etc/dhcpcd.conf; then if [ -f /etc/dhcpcd.conf ]; then
cat <<EOF >> /etc/dhcpcd.conf if ! grep -q "interface wlan0" /etc/dhcpcd.conf; then
echo -e "\ninterface wlan0\n static ip_address=192.168.4.1/24\n nohook wpa_supplicant" >> /etc/dhcpcd.conf
# MPVJ wlan0 Static IP fi
interface wlan0
static ip_address=192.168.4.1/24
nohook wpa_supplicant
EOF
fi fi
# 3.4 Avahi # 4. DEPLOYMENT
cat <<EOF > /etc/avahi/services/mpvj.service
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
<name replace-wildcards="yes">MPVJ Control Center</name>
<service>
<type>_http._tcp</type>
<port>80</port>
</service>
</service-group>
EOF
sed -i 's/#use-ipv4-ll=yes/use-ipv4-ll=yes/g' /etc/avahi/avahi-daemon.conf
# 4. APPLICATION DEPLOYMENT
echo "Deploying Application..." echo "Deploying Application..."
# 4.1 Clone
[ -d "$REAL_HOME/mpvj" ] && mv "$REAL_HOME/mpvj" "$REAL_HOME/mpvj.old.$(date +%s)" [ -d "$REAL_HOME/mpvj" ] && mv "$REAL_HOME/mpvj" "$REAL_HOME/mpvj.old.$(date +%s)"
sudo -u "$REAL_USER" git clone "$REPO_URL" "$REAL_HOME/mpvj" sudo -u "$REAL_USER" git clone "$REPO_URL" "$REAL_HOME/mpvj"
mkdir -p "$REAL_HOME/media" mkdir -p "$REAL_HOME/media"
chown -R "$REAL_USER:$REAL_USER" "$REAL_HOME/mpvj" "$REAL_HOME/media" chown -R "$REAL_USER:$REAL_USER" "$REAL_HOME/mpvj" "$REAL_HOME/media"
# 4.2 Build # 4.2 Backend Install
export NODE_OPTIONS="--max-old-space-size=512"
cd "$REAL_HOME/mpvj/backend" cd "$REAL_HOME/mpvj/backend"
echo "Installing backend dependencies..."
# Use --package-lock=false to ignore any committed lockfiles that might be polluted
rm -rf node_modules package-lock.json rm -rf node_modules package-lock.json
sudo -u "$REAL_USER" npm install --omit=optional --package-lock=false sudo -u "$REAL_USER" npm install --omit=optional --package-lock=false
cd "$REAL_HOME/mpvj/frontend" # 4.3 Setup Systemd
if [ ! -d "dist" ]; then
echo "Installing frontend dependencies and building..."
rm -rf node_modules package-lock.json
sudo -u "$REAL_USER" npm install --omit=optional --package-lock=false
sudo -u "$REAL_USER" npm run build
else
echo "Pre-built frontend detected. Skipping frontend build."
fi
# 4.3 Systemd
cat <<EOF > /etc/systemd/system/mpvj-backend.service cat <<EOF > /etc/systemd/system/mpvj-backend.service
[Unit] [Unit]
Description=MPVJ Headless Control Center Backend Description=MPVJ Backend
After=network.target After=network.target
[Service] [Service]
Type=simple Type=simple
User=$REAL_USER User=$REAL_USER
WorkingDirectory=$REAL_HOME/mpvj/backend WorkingDirectory=$REAL_HOME/mpvj/backend
ExecStart=/usr/bin/node index.js ExecStart=/usr/bin/node index.js
Restart=on-failure Restart=on-failure
Environment=NODE_OPTIONS=--max-old-space-size=512
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target
EOF EOF
@ -223,103 +165,34 @@ cat <<EOF > "$REAL_HOME/mpvj/backend/.env"
PORT=80 PORT=80
MPVJ_HOSTNAME=$HOSTNAME MPVJ_HOSTNAME=$HOSTNAME
MPVJ_SSID=$WIFI_SSID MPVJ_SSID=$WIFI_SSID
MPVJ_WIFI_PASS=$WIFI_PASS
MPVJ_REPO_URL=$REPO_URL
MPVJ_MEDIA_DIR=$REAL_HOME/media MPVJ_MEDIA_DIR=$REAL_HOME/media
EOF EOF
chown "$REAL_USER:$REAL_USER" "$REAL_HOME/mpvj/backend/.env"
chmod 600 "$REAL_HOME/mpvj/backend/.env"
# 4.5 Install ofxPiMapper Engine # 4.5 THE PRE-BUILT ENGINE SOLUTION
echo "Installing ofxPiMapper Engine..." echo "Installing Pre-compiled OpenFrameworks (FAST)..."
cd "$REAL_HOME"
# Priority 1: Check for pre-built binary in the repo if [ ! -d "openFrameworks" ]; then
if [ -f "$REAL_HOME/mpvj/bin/ofxPiMapper" ]; then # Download pre-compiled release for arm64
echo "Pre-built ofxPiMapper binary detected in repo. Installing..." sudo -u "$REAL_USER" wget https://github.com/openframeworks/openFrameworks/releases/download/0.12.0/of_v0.12.0_linuxarm64_release.tar.gz
cp "$REAL_HOME/mpvj/bin/ofxPiMapper" /usr/local/bin/ofxPiMapper sudo -u "$REAL_USER" tar -xzf of_v0.12.0_linuxarm64_release.tar.gz
chmod +x /usr/local/bin/ofxPiMapper sudo -u "$REAL_USER" mv of_v0.12.0_linuxarm64_release openFrameworks
echo "ofxPiMapper installed successfully (Binary)." sudo -u "$REAL_USER" rm of_v0.12.0_linuxarm64_release.tar.gz
else
# Priority 2: Build from source (Fall-back)
if [ -f "/usr/local/bin/ofxPiMapper" ]; then
echo "ofxPiMapper already installed in /usr/local/bin. Skipping build."
else
echo "No pre-built binary found. Falling back to source build (This takes ~1 hour)..."
cd "$REAL_HOME"
if [ ! -d "openFrameworks" ]; then
sudo -u "$REAL_USER" git clone --depth 1 --branch master https://github.com/openframeworks/openFrameworks.git
if command -v apt-get > /dev/null; then
echo "Debian-based system detected. Installing dependencies..."
cd openFrameworks/scripts/linux/debian
./install_dependencies.sh -y
./install_codecs.sh
elif command -v pacman > /dev/null; then
echo "Arch-based system detected. Installing dependencies..."
cd openFrameworks/scripts/linux/archlinux
sudo ./install_dependencies.sh --noconfirm
else
echo "WARNING: Unsupported system detected. Please install OpenFrameworks dependencies manually."
fi
cd "$REAL_HOME/openFrameworks"
sudo -u "$REAL_USER" make -j1 -C libs/openFrameworksCompiled/project/linux64
fi
cd "$REAL_HOME/openFrameworks/addons"
if [ ! -d "ofxPiMapper/example-basic" ]; then
echo "ofxPiMapper missing or incomplete. Cleaning and cloning..."
rm -rf ofxPiMapper
sudo -u "$REAL_USER" git clone https://github.com/kr15h/ofxPiMapper.git
fi
# Patch and Build
cd "$REAL_HOME/openFrameworks/addons/ofxPiMapper"
if [ -f src/Osc/OscControl.cpp ] && ! grep -q "highlight" src/Osc/OscControl.cpp; then
echo "Applying highlight patch..."
sed -i '/if (m.getAddress() == "\/ofxPiMapper\/surface\/select"){/i \
if (m.getAddress() == "/ofxPiMapper/surface/highlight"){ \
int surfaceIndex = m.getArgAsInt32(0); \
mapper->getSurfaceManager()->selectSurface(surfaceIndex); \
return; \
}' src/Osc/OscControl.cpp
fi
echo "Compiling example-basic..."
if [ -d "example-basic" ]; then
cd "example-basic"
sudo -u "$REAL_USER" make -j1
cp bin/example-basic /usr/local/bin/ofxPiMapper
chmod +x /usr/local/bin/ofxPiMapper
else
echo "ERROR: example-basic folder not found in ofxPiMapper addon!"
exit 1
fi
fi
fi fi
echo "Downloading and building ofxPiMapper Addon..."
cd "$REAL_HOME/openFrameworks/addons"
[ ! -d "ofxPiMapper" ] && sudo -u "$REAL_USER" git clone --depth 1 https://github.com/kr15h/ofxPiMapper.git
# Build Example
cd "$REAL_HOME/openFrameworks/addons/ofxPiMapper/example-basic"
sudo -u "$REAL_USER" make -j1
cp bin/example-basic /usr/local/bin/ofxPiMapper
chmod +x /usr/local/bin/ofxPiMapper
# 5. FINALIZATION # 5. FINALIZATION
echo "Finalizing..." echo "Cleaning up..."
swapoff /swapfile_mpvj 2>/dev/null || true
rm /swapfile_mpvj 2>/dev/null || true
# 5.1 Revert Swap whiptail --title "Success" --msgbox "INSTALL COMPLETE!\n\nRebooting into AP Mode.\nSSID: $WIFI_SSID\nIP: 192.168.4.1" 12 45 < /dev/tty
if [ -f /etc/dphys-swapfile ]; then reboot
sed -i "s/CONF_SWAPSIZE=.*/CONF_SWAPSIZE=100/g" /etc/dphys-swapfile
dphys-swapfile setup
dphys-swapfile swapon
fi
# 5.2 Summary
SUMMARY="MPVJ INSTALLATION COMPLETE\n\n"
SUMMARY+="Please NOTE DOWN these details before rebooting:\n\n"
SUMMARY+="HOSTNAME: $HOSTNAME.local\n"
SUMMARY+="STATIC IP: 192.168.4.1\n"
SUMMARY+="WIFI SSID: $WIFI_SSID\n"
SUMMARY+="WIFI PASS: $WIFI_PASS\n\n"
SUMMARY+="Your SSH password remains unchanged.\n"
SUMMARY+="Connect to '$WIFI_SSID' after reboot.\n\n"
SUMMARY+="Reboot now?"
if whiptail --title "Success" --yesno "$SUMMARY" 20 60 < /dev/tty; then
reboot
fi