diff --git a/notifiarr-branch-builder.sh b/notifiarr-branch-builder.sh new file mode 100755 index 0000000..765167d --- /dev/null +++ b/notifiarr-branch-builder.sh @@ -0,0 +1,138 @@ +#!/bin/bash + +# Extend the PATH to include the go binary directory +export PATH=$PATH:/usr/local/go/bin + +# Function to display error messages and exit with status 1 +handle_error() { + echo "Error: $1" >&2 + exit 1 +} + +# Function to display usage information +display_help() { + echo "Usage: $0 [options]" + echo "Options:" + echo " -h, --help Display this help message" + echo " --repo-url URL Set the repository URL (default: https://github.com/Notifiarr/notifiarr.git)" + echo " --repo-dir DIR Set the repository directory (default: /opt/notifiarr-repo)" + echo " --bin-path PATH Set the binary path (default: /usr/bin/notifiarr)" + echo " --branch BRANCH Set the branch (default: master)" + echo " --reinstall-apt Reinstall Notifiarr using apt without prompting." + exit 0 +} + +# Function to check and prompt for installation of a required tool +ensure_tool_installed() { + local tool=$1 + local install_cmd=$2 + if ! command -v "$tool" &>/dev/null; then + read -p "$tool is not installed. Do you want to install it? [Y/n] " response + if [[ "$response" =~ ^[Yy] ]]; then + eval "$install_cmd" || handle_error "Failed to install $tool." + else + echo "$tool is required for this script. Exiting." + exit 1 + fi + fi +} + +# Default parameters +repo_url="https://github.com/Notifiarr/notifiarr.git" +repo_dir="/opt/notifiarr-repo" +bin_path="/usr/bin/notifiarr" +branch="master" +apt_reinstall=false + +# Parse command line options +while [[ $# -gt 0 ]]; do + case "$1" in + -h | --help) + display_help + ;; + --repo-url) + repo_url="$2" + shift + ;; + --repo-dir) + repo_dir="$2" + shift + ;; + --bin-path) + bin_path="$2" + shift + ;; + --branch) + branch="$2" + shift + ;; + --reinstall-apt) + apt_reinstall=true + ;; + *) + echo "Invalid option: $1. Use -h for help." + exit 1 + ;; + esac + shift +done + +# Ensure required tools are installed +ensure_tool_installed "make" "sudo apt update && sudo apt install -y make" + +# Reinstallation condition handling +reinstall_notifiarr() { + sudo apt update && sudo apt install --reinstall notifiarr || handle_error "Failed to reinstall Notifiarr using apt." +} + +[[ $apt_reinstall == true ]] && reinstall_notifiarr + +# Repository management +if [[ ! -d "$repo_dir" ]]; then + git clone "$repo_url" "$repo_dir" || handle_error "Failed to clone repository." +else + git -C "$repo_dir" fetch --all --prune || handle_error "Failed to fetch updates from remote." +fi + +# Branch handling and updating +current_branch=$(git -C "$repo_dir" rev-parse --abbrev-ref HEAD) +read -p "Do you want to use the current branch ($current_branch)? [Y/n] " choice +if [[ "$choice" =~ ^[Nn] ]]; then + branches=$(git -C "$repo_dir" branch -r | sed 's/origin\///;s/* //') + echo "Available branches:" + echo "$branches" + while true; do + read -p "Enter the branch name you want to use: " branch + if [[ $branches =~ $branch ]]; then + git -C "$repo_dir" checkout "$branch" || handle_error "Failed to checkout branch $branch." + break + else + echo "Invalid choice. Please select a valid branch." + fi + done +fi + +git -C "$repo_dir" pull || handle_error "Failed to pull latest changes." +make --directory="$repo_dir" || handle_error "Failed to compile." + +# Service management +echo "Stopping notifiarr..." +sudo systemctl stop notifiarr + +if [[ -f "$bin_path" ]]; then + sudo mv "$bin_path" "$repo_dir".old && echo "Old binary moved to $repo_dir.old" +fi + +sudo mv "$repo_dir/notifiarr" "$bin_path" && echo "New binary moved to $bin_path" +sudo chown root:root "$bin_path" + +echo "Starting Notifiarr..." +sudo systemctl start notifiarr + +if sudo systemctl is-active --quiet notifiarr; then + echo "Notifiarr service started and is currently running" +else + handle_error "Failed to start Notifiarr service" +fi + +exit 0 diff --git a/qbm-qbit.sh b/qbm-qbit.sh new file mode 100755 index 0000000..7ea20c7 --- /dev/null +++ b/qbm-qbit.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +# Check if lockfile command exists +if ! command -v lockfile &>/dev/null; then + echo "Error: lockfile command not found. Please install the procmail package." >&2 + exit 1 +fi + +# Load environment variables from .env file if it exists +if [ -f ".env" ]; then + source ".env" +fi + +# Use environment variables with descriptive default values +QBQBM_LOCK=${QBIT_MANAGE_LOCK_FILE_PATH:-/var/lock/qbm-qbit.lock} +QBQBM_PATH_QBM=${QBIT_MANAGE_PATH:-/opt/qbit-manage} +QBQBM_VENV_PATH=${QBIT_MANAGE_VENV_PATH:-/opt/qbit-manage/.venv} +QBQBM_CONFIG_PATH=${QBIT_MANAGE_CONFIG_PATH:-/opt/qbit-manage/config.yml} +QBQBM_QBIT_OPTIONS=${QBIT_MANAGE_OPTIONS:-"-cs -re -cu -tu -ru -sl -r"} +QBQBM_SLEEP_TIME=600 + +# Function to remove the lock file +remove_lock() { + rm -f "$QBQBM_LOCK" +} + +# Function to handle detection of another running instance +another_instance() { + echo "There is another instance running, exiting." + exit 1 +} + +echo "Acquiring Lock" +# Acquire a lock to prevent concurrent execution, with a timeout and lease time +lockfile -r 0 -l "$QBQBM_SLEEP_TIME" "$QBQBM_LOCK" || another_instance + +# Ensure the lock is removed when the script exits +trap remove_lock EXIT + +echo "sleeping for $QBQBM_SLEEP_TIME" +# Pause the script to wait for any pending operations (i.e. Starr Imports) + +sleep $QBQBM_SLEEP_TIME + +# Execute qbit_manage with configurable options +echo "Executing Command" +"$QBQBM_VENV_PATH"/bin/python "$QBQBM_PATH_QBM"/qbit_manage.py "$QBQBM_QBIT_OPTIONS" --config-file "$QBQBM_CONFIG_PATH"