Last active
March 1, 2024 22:01
-
-
Save lbrooney/a39d1a8a62251a69df09f3af4ff9b373 to your computer and use it in GitHub Desktop.
audiobookshelf install/update script for TrueNAS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Function to display messages in green color | |
green_echo() { | |
echo -e "\033[0;32m$1\033[0m" | |
} | |
# Function to display messages in yellow color | |
yellow_echo() { | |
echo -e "\033[0;33m$1\033[0m" | |
} | |
# Check if required packages are installed, install or update if necessary | |
check_install_update() { | |
pkg update | |
for pkg in "$@"; do | |
if ! pkg info "$pkg" > /dev/null 2>&1; then | |
# Package is not installed, install it | |
green_echo "Installing $pkg..." | |
pkg install -y "$pkg" | |
else | |
# Package is installed, check for updates and update if necessary | |
yellow_echo "Checking for updates to $pkg..." | |
pkg upgrade -q -y "$pkg" | |
fi | |
done | |
} | |
check_install_update "curl" "ffmpeg" "node20" "npm-node20" "nano" | |
# Set the installation directory | |
INSTALL_DIR="/root/audiobookshelf" | |
# Set the configuration path within the installation directory | |
CONFIG_PATH="$INSTALL_DIR/config" | |
# Set the metadata path within the installation directory | |
METADATA_PATH="$INSTALL_DIR/metadata" | |
# Set the backup path within the installation directory | |
BACKUP_PATH="$INSTALL_DIR/backup" | |
# Define the GitHub repository | |
REPO="advplyr/audiobookshelf" | |
# Ensure the installation directory exists | |
mkdir -p $INSTALL_DIR | |
# Ensure configuration, metadata, and backup folders exist | |
mkdir -p "$CONFIG_PATH" | |
mkdir -p "$METADATA_PATH" | |
mkdir -p "$BACKUP_PATH" | |
# Navigate to the installation directory | |
cd $INSTALL_DIR | |
# Check if Audiobookshelf is already installed | |
CURRENT_VERSION=$(cat .version 2>/dev/null) | |
# Fetch the latest release version from GitHub Releases API | |
LATEST_VERSION=$(curl -s "https://api.github.com/repos/$REPO/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') | |
if [ -z "$LATEST_VERSION" ]; then | |
yellow_echo "Failed to retrieve the latest version. Exiting." | |
exit 1 | |
fi | |
if [ "$CURRENT_VERSION" = "$LATEST_VERSION" ]; then | |
green_echo "Audiobookshelf is already up to date. Exiting." | |
exit 0 | |
fi | |
if pm2 list | grep -q "audiobookshelf-$CURRENT_VERSION"; then | |
yellow_echo "Stopping audiobookshelf $CURRENT_VERSION in PM2..." | |
pm2 stop "audiobookshelf-$CURRENT_VERSION" | |
fi | |
if [ -n "$CURRENT_VERSION" ] && [ -d "$CONFIG_PATH" ] && [ -d "$METADATA_PATH" ]; then | |
yellow_echo "Creating a backup of Audiobookshelf folder..." | |
mkdir -p "$BACKUP_PATH/audiobookshelf-$CURRENT_VERSION" | |
cp -r "$CONFIG_PATH" "$BACKUP_PATH/audiobookshelf-$CURRENT_VERSION/config" | |
cp -r "$METADATA_PATH" "$BACKUP_PATH/audiobookshelf-$CURRENT_VERSION/metadata" | |
fi | |
# Set the temporary directory | |
TMP_DIR="$INSTALL_DIR/tmp" | |
# Ensure the temporary directory exists | |
mkdir -p "$TMP_DIR" | |
# Download the latest release into the temporary directory | |
green_echo "Downloading Audiobookshelf version $LATEST_VERSION..." | |
curl -L -o "$TMP_DIR/$LATEST_VERSION.zip" "https://github.com/$REPO/archive/refs/tags/$LATEST_VERSION.zip" | |
# Extract the contents into the install directory | |
unzip -o "$TMP_DIR/$LATEST_VERSION.zip" -d "$INSTALL_DIR" | |
# Remove the temporary directory | |
green_echo "Cleaning up temporary files..." | |
rm -rf "$TMP_DIR" | |
# Enter the Audiobookshelf directory within the install directory | |
cd "$INSTALL_DIR/audiobookshelf-${LATEST_VERSION#?}" | |
# Update npm to the latest version before running npm commands | |
green_echo "Updating npm to the latest version..." | |
npm install -g npm | |
# Install dependencies and build the app | |
yellow_echo "Installing dependencies and building the app..." | |
npm run client | |
npm ci --omit=dev | |
# Update PM2 | |
green_echo "Updating PM2..." | |
npm install -g pm2 | |
pm2 update | |
# Indicate to the user that they may need to run pm2 unstartup and pm2 startup | |
yellow_echo "Note: If you have updated Node versions, consider running the following commands to ensure PM2 compatibility:" | |
echo "pm2 unstartup" | |
echo "pm2 startup" | |
# Ensure the rc.d folder exists (sometimes it doesn't generate and PM2 doesn't account for this) | |
mkdir -p /usr/local/etc/rc.d/ | |
# Run PM2 startup | |
if ! service -e | grep -q "pm2"; then | |
pm2 startup | |
fi | |
# Create a PM2 configuration file with the new version as part of the app name | |
cat <<EOF > pm2config.json | |
{ | |
"apps": [ | |
{ | |
"name": "audiobookshelf-$LATEST_VERSION", | |
"script": "index.js", | |
"env": { | |
"NODE_ENV": "production", | |
"HOST": "0.0.0.0", | |
"CONFIG_PATH": "$CONFIG_PATH", | |
"METADATA_PATH": "$METADATA_PATH", | |
"SOURCE": "FreeBSD" | |
} | |
} | |
] | |
} | |
EOF | |
# Start the Audiobookshelf app with PM2 | |
green_echo "Starting Audiobookshelf with PM2..." | |
pm2 start pm2config.json | |
# Wait for 3 seconds | |
sleep 3 | |
# Check the status of the application | |
STATUS=$(pm2 list | grep "audiobookshelf-$LATEST_VERSION" | awk '{print $18}') | |
# Check if the app is running successfully | |
if [ "$STATUS" = "errored" ]; then | |
yellow_echo "Audiobookshelf $LATEST_VERSION failed to start. Please check logs for details..." | |
yellow_echo "Starting audiobookshelf $CURRENT_VERSION..." | |
pm2 start "audiobookshelf-$CURRENT_VERSION" | |
elif [ "$STATUS" = "online" ]; then | |
# Remove the existing Audiobookshelf app from PM2 if it exists | |
pm2 delete "audiobookshelf-$CURRENT_VERSION" 2>/dev/null | |
green_echo "audiobookshelf $LATEST_VERSION started successfully..." | |
# Save the PM2 configuration | |
green_echo "Saving PM2 configuration..." | |
pm2 save | |
echo "$LATEST_VERSION" > "$INSTALL_DIR/.version" # Save the version number | |
else | |
echo yellow_echo "Something went wrong at the PM2 stage. Please check logs for details..." | |
exit 2 | |
fi | |
# Display PM2 status | |
green_echo "Audiobookshelf updated to version $LATEST_VERSION and running with PM2:" | |
pm2 status |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment