Files
api/plugin/source/dynamix.unraid.net/etc/rc.d/rc.unraid-api
T
Pujit Mehrotra d9ab58eb83 chore: require connect plugin to enable flash backup (#1419)
## Summary by CodeRabbit

- **New Features**
- Added a check to ensure the "unraid-api-plugin-connect" plugin is
enabled before allowing flash backup functionality.
- Introduced a utility to directly verify if specific API plugins are
enabled.

- **Refactor**
- Updated internal logic to use a centralized class and script-based
checks for plugin status and version instead of manual config parsing.
- Improved script command-line interface for easier plugin status and
version checks.

- **Bug Fixes**
- Flash Backup feature and service now only activate when the required
API plugin is enabled, preventing unintended usage.
- Flash Backup UI is conditionally displayed based on the presence of
the API plugin.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---
- To see the specific tasks where the Asana app for GitHub is being
used, see below:
  - https://app.asana.com/0/0/1209357561531351
  - https://app.asana.com/0/0/1210541992642236
2025-06-18 10:20:59 -04:00

159 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
# unraid-api-handler
# shellcheck source=/dev/null
source /etc/profile
flash="/boot/config/plugins/dynamix.my.servers"
[[ ! -d "${flash}" ]] && echo "Please reinstall the Unraid Connect plugin" && exit 1
[[ ! -f "${flash}/env" ]] && echo 'env=production' >"${flash}/env"
unraid_binary_path="/usr/local/bin/unraid-api"
api_base_dir="/usr/local/unraid-api"
scripts_dir="/usr/local/share/dynamix.unraid.net/scripts"
# Ensure script permissions
if [ -d "$scripts_dir" ]; then
chmod +x "$scripts_dir"/*.sh 2>/dev/null
fi
# Placeholder functions for plugin installation/uninstallation
install() {
true
}
uninstall() {
true
}
# Service control functions
start() {
echo "Starting Unraid API service..."
# Restore vendored API plugins if they were installed
if [ -x "$scripts_dir/dependencies.sh" ]; then
"$scripts_dir/dependencies.sh" restore || {
echo "Failed to restore API plugin dependencies! Continuing with start, but API plugins may be unavailable."
}
else
echo "Warning: dependencies.sh script not found or not executable"
fi
# Create log directory if it doesn't exist
mkdir -p /var/log/unraid-api
# Copy env file if needed
if [ -f "${api_base_dir}/.env.production" ] && [ ! -f "${api_base_dir}/.env" ]; then
cp "${api_base_dir}/.env.production" "${api_base_dir}/.env"
fi
# Start the flash backup service if available and connect plugin is enabled
if [ -x "/etc/rc.d/rc.flash_backup" ]; then
# Check if connect plugin is enabled before starting flash backup
if [ -x "$scripts_dir/api_utils.sh" ] && "$scripts_dir/api_utils.sh" is_api_plugin_enabled "unraid-api-plugin-connect"; then
echo "Starting flash backup service..."
/etc/rc.d/rc.flash_backup start
fi
fi
# Start the API service
if [ -x "${unraid_binary_path}" ]; then
"${unraid_binary_path}" start
return $?
else
echo "Error: Unraid API binary not found or not executable at ${unraid_binary_path}"
return 1
fi
}
stop() {
echo "Stopping Unraid API service..."
if [ -x "${unraid_binary_path}" ]; then
"${unraid_binary_path}" stop
return $?
else
echo "Error: Unraid API binary not found or not executable at ${unraid_binary_path}"
return 1
fi
}
restart() {
stop
sleep 2
start
}
status() {
if [ -x "${unraid_binary_path}" ]; then
"${unraid_binary_path}" status
return $?
else
echo "Error: Unraid API binary not found or not executable at ${unraid_binary_path}"
return 1
fi
}
case "$1" in
'start')
start
;;
'stop')
stop
;;
'restart' | 'reload')
restart
;;
'status')
status
;;
'install')
install "$2"
;;
'uninstall')
uninstall
;;
'ensure')
if [ -x "$scripts_dir/dependencies.sh" ]; then
# First clean up old dependencies
"$scripts_dir/dependencies.sh" cleanup
# Then ensure new dependencies are installed
"$scripts_dir/dependencies.sh" ensure "$2"
else
echo "Error: dependencies.sh script not found or not executable"
exit 1
fi
;;
'cleanup-dependencies')
if [ -x "$scripts_dir/dependencies.sh" ]; then
"$scripts_dir/dependencies.sh" cleanup
else
echo "Error: dependencies.sh script not found or not executable"
exit 1
fi
;;
'archive-dependencies')
if [ -x "$scripts_dir/dependencies.sh" ]; then
"$scripts_dir/dependencies.sh" archive
else
echo "Error: dependencies.sh script not found or not executable"
exit 1
fi
;;
'redownload-vendor-archive')
if [ -x "$scripts_dir/dependencies.sh" ]; then
if [ -n "$2" ] && [ -n "$3" ]; then
"$scripts_dir/dependencies.sh" redownload "$2" "$3"
else
echo "Usage: $0 redownload-vendor-archive <archive_path> <version>"
exit 1
fi
else
echo "Error: dependencies.sh script not found or not executable"
exit 1
fi
;;
*)
# Pass all other commands to unraid-api
"${unraid_binary_path}" "$@"
;;
esac