Refactor install_package

This commit is contained in:
2024-09-03 19:10:59 -04:00
parent f854c12234
commit aface9f1ea

View File

@@ -476,7 +476,7 @@ set_mc_version() {
echo "Using MC version $MC_VERSION from the ${MC_REPO:-$MC_DEFAULT_REPO} repo (determined by $MC_VERSION_SOURCE)" echo "Using MC version $MC_VERSION from the ${MC_REPO:-$MC_DEFAULT_REPO} repo (determined by $MC_VERSION_SOURCE)"
[[ $MC_VERSION_SOURCE == "user input" ]] || echo "To override, use --mcversion" [[ $MC_VERSION_SOURCE == "user input" ]] || echo "To override, use --mcversion"
debug "MC_VERSION=$MC_VERSION, MC_REPO=${MC_REPO:-$MC_DEFAULT_REPO}, MC_PKG=$MC_PKG, MC_RPM=$MC_RPM" return 0
} }
@@ -493,12 +493,11 @@ set_mc_version() {
install_package() { install_package() {
debug "Running: ${FUNCNAME[0]}" "$@" debug "Running: ${FUNCNAME[0]}" "$@"
declare -a pkg_array install_flags local -a pkg_array install_flags
declare -A pkg_aliases local -A pkg_aliases
local long_opts input pkg no_install_check \ local input pkg
allow_downgrades silent refresh no_gpg_check local no_install_check=0 allow_downgrades=0 silent=0 refresh=0 no_gpg_check=0
local long_opts="no-install-check,allow-downgrades,no-gpg-check,refresh,silent"
long_opts="no-install-check,allow-downgrades,no-gpg-check,refresh,silent"
input=$(getopt -o +s -l "$long_opts" -- "$@") || { err "Incorrect options provided"; exit 1; } input=$(getopt -o +s -l "$long_opts" -- "$@") || { err "Incorrect options provided"; exit 1; }
eval set -- "$input" eval set -- "$input"
@@ -515,27 +514,33 @@ install_package() {
shift shift
done done
# Package aliases # Define package aliases based on the distribution
case $ID in case $ID in
debian|ubuntu) debian|ubuntu)
pkg_aliases["rpm-build"]="rpm" pkg_aliases=(
pkg_aliases["createrepo_c"]="createrepo" ["rpm-build"]="rpm"
pkg_aliases["tigervnc-server"]="tigervnc-standalone-server" ["createrepo_c"]="createrepo"
["tigervnc-server"]="tigervnc-standalone-server"
)
;; ;;
esac esac
# Filter installed packages # Filter out already installed packages
for pkg in "$@"; do for pkg in "$@"; do
[[ -v pkg_aliases[$pkg] ]] && { debug "Aliasing $pkg to ${pkg_aliases[$pkg]}"; pkg=${pkg_aliases[$pkg]}; } if [[ -v pkg_aliases[$pkg] ]]; then
if (( no_install_check )) || debug "Aliasing $pkg to ${pkg_aliases[$pkg]}"
! (command -v "$pkg" &>/dev/null || "${PKG_QUERY[@]}" "$pkg" &>/dev/null); then pkg=${pkg_aliases[$pkg]}
fi
if (( no_install_check )) \
|| ! { command -v "$pkg" &>/dev/null \
|| "${PKG_QUERY[@]}" "$pkg" &>/dev/null; }; then
pkg_array+=("$pkg") pkg_array+=("$pkg")
else else
debug "$pkg already installed, skipping installation" debug "$pkg already installed, skipping installation"
fi fi
done done
# Generate distro-specific install flags # Generate installation flags based on the distribution
case $ID in case $ID in
debian|ubuntu) debian|ubuntu)
(( allow_downgrades )) && install_flags+=(--allow-downgrades) (( allow_downgrades )) && install_flags+=(--allow-downgrades)
@@ -550,10 +555,10 @@ install_package() {
;; ;;
esac esac
# Install packages from package array # Install packages if any need installation
if [[ ${#pkg_array[@]} -ge 1 ]]; then if [[ ${#pkg_array[@]} -gt 0 ]]; then
if ! "${PKG_INSTALL[@]}" "${install_flags[@]}" "${pkg_array[@]}"; then if ! "${PKG_INSTALL[@]}" "${install_flags[@]}" "${pkg_array[@]}"; then
(( silent )) || err "Failed to install ${pkg_array[*]}. Attempting to continue" (( silent )) || err "Failed to install ${pkg_array[*]}."
return 1 return 1
fi fi
fi fi
@@ -1034,7 +1039,7 @@ run_createrepo() {
####################################### #######################################
# Symlink certificates if they do not exist in default location # Symlink certificates if they do not exist in default location
####################################### #######################################
symlink_ssl_certs() { link_ssl_certs() {
debug "Running: ${FUNCNAME[0]}" debug "Running: ${FUNCNAME[0]}"
local target_cert f local target_cert f
@@ -1588,54 +1593,15 @@ uninstall() {
####################################### #######################################
# Updates and re-executes this script # Checks for installJRMC update and re-executes, if necessary
####################################### #######################################
update_self() { update_self() {
debug "Running: ${FUNCNAME[0]} $*" debug "Running: ${FUNCNAME[0]} $*"
local script_url="https://git.bryanroessler.com/bryan/installJRMC/raw/master/installJRMC" local script_url="https://git.bryanroessler.com/bryan/installJRMC/raw/master/installJRMC"
local tmp local tmp; tmp=$(mktemp)
tmp=$(mktemp)
# Check if we're in a git directory and if it's the installJRMC repository # Function to extract and normalize version from a script
if git rev-parse --is-inside-work-tree &>/dev/null; then
local git_remote_url
git_remote_url=$(git config --get remote.origin.url)
if [[ "$git_remote_url" == *"bryan/installJRMC"* ]]; then
echo "installJRMC git repository detected. Running git pull..."
# Get the current commit hash of the script's directory
local before_pull_hash
before_pull_hash=$(git rev-parse HEAD)
# Perform git pull
git pull || return 1
# Get the new commit hash after the pull
local after_pull_hash
after_pull_hash=$(git rev-parse HEAD)
# Check if the commit hash has changed
if [[ "$before_pull_hash" != "$after_pull_hash" ]]; then
echo "installJRMC script updated. Restarting script..."
exec "$SCRIPT_PATH" "$@" "--no-self-update"
else
echo "installJRMC script is already up to date."
return 0
fi
fi
fi
# Download the latest version of the script
if command -v curl &>/dev/null; then
curl -s -L -o "$tmp" "$script_url"
elif command -v wget &>/dev/null; then
wget -q -O "$tmp" "$script_url"
else
return 1
fi
# Extract and normalize version from a script
extract_version() { extract_version() {
local version_line local version_line
version_line=$(grep -m 1 'SCRIPT_VERSION=' "$1") version_line=$(grep -m 1 'SCRIPT_VERSION=' "$1")
@@ -1646,13 +1612,45 @@ update_self() {
echo "$version_line" echo "$version_line"
} }
# Compare versions # Check if we're in a git directory and if it's the installJRMC repository
if git -C "$SCRIPT_DIR" rev-parse --is-inside-work-tree &>/dev/null \
&& [[ "$(git -C "$SCRIPT_DIR" config --get remote.origin.url)" == *"bryan/installJRMC"* ]]; then
debug "installJRMC git repository detected. Running git pull..."
# Get the current commit hash
local before_pull_hash
before_pull_hash=$(git -C "$SCRIPT_DIR" rev-parse HEAD)
if git -C "$SCRIPT_DIR" pull | grep -qv "Already up to date"; then
return 0
fi
local after_pull_hash
after_pull_hash=$(git -C "$SCRIPT_DIR" rev-parse HEAD)
if [[ "$before_pull_hash" != "$after_pull_hash" ]]; then
echo "installJRMC repository updated. Restarting script..."
exec "$SCRIPT_PATH" "$@" "--no-self-update"
fi
fi
# Download the latest version of the script
install_package --silent wget
if command -v wget &>/dev/null; then
wget -q -O "$tmp" "$script_url"
elif command -v curl &>/dev/null; then
curl -s -L -o "$tmp" "$script_url"
else
return 1
fi
# Compare versions and update if necessary
local local_version
local_version=$(extract_version "$SCRIPT_PATH") local_version=$(extract_version "$SCRIPT_PATH")
local remote_version
remote_version=$(extract_version "$tmp") remote_version=$(extract_version "$tmp")
[[ -z $remote_version ]] && { rm -f "$tmp"; return 1; } [[ -z $remote_version ]] && { rm -f "$tmp"; return 1; }
# Save and execute the new script if the remote version is newer
if [[ $local_version < $remote_version ]]; then if [[ $local_version < $remote_version ]]; then
echo "Newer version of installJRMC found. Updating..." echo "Newer version of installJRMC found. Updating..."
execute mv "$tmp" "$SCRIPT_PATH" execute mv "$tmp" "$SCRIPT_PATH"
@@ -1660,10 +1658,11 @@ update_self() {
exec "$SCRIPT_PATH" "$@" "--no-self-update" exec "$SCRIPT_PATH" "$@" "--no-self-update"
fi fi
execute rm -f "$tmp" rm -f "$tmp"
} }
main() { main() {
debug "Running: ${FUNCNAME[0]} $*" debug "Running: ${FUNCNAME[0]} $*"
@@ -1726,7 +1725,7 @@ main() {
echo "Installing JRiver Media Center from remote repository" echo "Installing JRiver Media Center from remote repository"
if install_mc_repo; then if install_mc_repo; then
echo "JRiver Media Center installed successfully from remote repository" echo "JRiver Media Center installed successfully from remote repository"
symlink_ssl_certs link_ssl_certs
# migrateLibrary # migrateLibrary
restore_license restore_license
open_firewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp" open_firewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp"
@@ -1756,7 +1755,7 @@ main() {
err "JRiver Media Center local package installation failed" err "JRiver Media Center local package installation failed"
return 1 return 1
fi fi
symlink_ssl_certs link_ssl_certs
# migrateLibrary # migrateLibrary
restore_license restore_license
open_firewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp" open_firewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp"