From 4160c4aa77daf10e9353ff9fe053ea4eb6283667 Mon Sep 17 00:00:00 2001 From: Cameron Ryder Date: Sun, 4 Jun 2023 23:01:48 -0400 Subject: [PATCH] refactor: dedupe apt update & install functions --- scripts/crowsnest.sh | 22 ++------ scripts/klipper.sh | 22 ++------ scripts/moonraker-telegram-bot.sh | 22 ++------ scripts/moonraker.sh | 22 ++------ scripts/octoeverywhere.sh | 22 ++------ scripts/ui/update_menu.sh | 2 +- scripts/utilities.sh | 86 +++++++++++++++++++------------ 7 files changed, 73 insertions(+), 125 deletions(-) diff --git a/scripts/crowsnest.sh b/scripts/crowsnest.sh index 393f6ab..20b29f5 100644 --- a/scripts/crowsnest.sh +++ b/scripts/crowsnest.sh @@ -197,7 +197,7 @@ function compare_crowsnest_versions() { } function install_crowsnest_dependencies() { - local packages + local packages log_name="Crowsnest" local install_script="${CROWSNEST_DIR}/tools/install.sh" ### read PKGLIST from official install-script @@ -208,25 +208,11 @@ function install_crowsnest_dependencies() { echo "${cyan}${packages}${white}" | tr '[:space:]' '\n' read -r -a packages <<< "${packages}" - ### Update system package info if lists > 3 days old - status_msg "Updating package lists..." - if [[ -z "$(find -H /var/lib/apt/lists -maxdepth 0 -mtime -3)" ]]; then - if ! sudo apt-get update --allow-releaseinfo-change; then - log_error "failure while updating package lists" - error_msg "Updating package lists failed!" - exit 1 - fi - else - status_msg "Package lists updated recently, skipping..." - fi + ### Update system package lists if stale + update_system_package_lists ### Install required packages - status_msg "Installing required packages..." - if ! sudo apt-get install --yes "${packages[@]}"; then - log_error "failure while installing required crowsnest packages" - error_msg "Installing required packages failed!" - exit 1 - fi + install_system_packages "$log_name" "packages[@]" } function update_crowsnest() { diff --git a/scripts/klipper.sh b/scripts/klipper.sh index ca489fb..9fb7c00 100644 --- a/scripts/klipper.sh +++ b/scripts/klipper.sh @@ -295,7 +295,7 @@ function create_klipper_virtualenv() { # @param {string}: python_version - klipper-env python version # function install_klipper_packages() { - local packages python_version="${1}" + local packages log_name="Klipper" python_version="${1}" local install_script="${KLIPPER_DIR}/scripts/install-debian.sh" status_msg "Reading dependencies..." @@ -321,25 +321,11 @@ function install_klipper_packages() { echo "${cyan}${packages}${white}" | tr '[:space:]' '\n' read -r -a packages <<< "${packages}" - ### Update system package info if lists > 3 days old - status_msg "Updating package lists..." - if [[ -z "$(find -H /var/lib/apt/lists -maxdepth 0 -mtime -3)" ]]; then - if ! sudo apt-get update --allow-releaseinfo-change; then - log_error "failure while updating package lists" - error_msg "Updating package lists failed!" - exit 1 - fi - else - status_msg "Package lists updated recently, skipping..." - fi + ### Update system package lists if stale + update_system_package_lists ### Install required packages - status_msg "Installing required packages..." - if ! sudo apt-get install --yes "${packages[@]}"; then - log_error "failure while installing required klipper packages" - error_msg "Installing required packages failed!" - exit 1 - fi + install_system_packages "$log_name" "packages[@]" } function create_klipper_service() { diff --git a/scripts/moonraker-telegram-bot.sh b/scripts/moonraker-telegram-bot.sh index ee0520f..0ccd931 100644 --- a/scripts/moonraker-telegram-bot.sh +++ b/scripts/moonraker-telegram-bot.sh @@ -110,7 +110,7 @@ function telegram_bot_setup_dialog() { } function install_telegram_bot_dependencies() { - local packages + local packages log_name="Telegram Bot" local install_script="${TELEGRAM_BOT_DIR}/scripts/install.sh" ### read PKGLIST from official install-script @@ -121,25 +121,11 @@ function install_telegram_bot_dependencies() { echo "${cyan}${packages}${white}" | tr '[:space:]' '\n' read -r -a packages <<< "${packages}" - ### Update system package info if lists > 3 days old - status_msg "Updating package lists..." - if [[ -z "$(find -H /var/lib/apt/lists -maxdepth 0 -mtime -3)" ]]; then - if ! sudo apt-get update --allow-releaseinfo-change; then - log_error "failure while updating package lists" - error_msg "Updating package lists failed!" - exit 1 - fi - else - status_msg "Package lists updated recently, skipping..." - fi + ### Update system package lists if stale + update_system_package_lists ### Install required packages - status_msg "Installing required packages..." - if ! sudo apt-get install --yes "${packages[@]}"; then - log_error "failure while installing required moonraker-telegram-bot packages" - error_msg "Installing required packages failed!" - exit 1 - fi + install_system_packages "$log_name" "packages[@]" } function create_telegram_bot_virtualenv() { diff --git a/scripts/moonraker.sh b/scripts/moonraker.sh index a2020bf..9a89839 100644 --- a/scripts/moonraker.sh +++ b/scripts/moonraker.sh @@ -141,7 +141,7 @@ function moonraker_setup_dialog() { } function install_moonraker_dependencies() { - local packages + local packages log_name="Moonraker" local install_script="${MOONRAKER_DIR}/scripts/install-moonraker.sh" ### read PKGLIST from official install-script @@ -152,25 +152,11 @@ function install_moonraker_dependencies() { echo "${cyan}${packages}${white}" | tr '[:space:]' '\n' read -r -a packages <<< "${packages}" - ### Update system package info if lists > 3 days old - status_msg "Updating package lists..." - if [[ -z "$(find -H /var/lib/apt/lists -maxdepth 0 -mtime -3)" ]]; then - if ! sudo apt-get update --allow-releaseinfo-change; then - log_error "failure while updating package lists" - error_msg "Updating package lists failed!" - exit 1 - fi - else - status_msg "Package lists updated recently, skipping..." - fi + ### Update system package lists if stale + update_system_package_lists ### Install required packages - status_msg "Installing required packages..." - if ! sudo apt-get install --yes "${packages[@]}"; then - log_error "failure while installing required moonraker packages" - error_msg "Installing required packages failed!" - exit 1 - fi + install_system_packages "$log_name" "packages[@]" } function create_moonraker_virtualenv() { diff --git a/scripts/octoeverywhere.sh b/scripts/octoeverywhere.sh index 18e063f..a603b9d 100644 --- a/scripts/octoeverywhere.sh +++ b/scripts/octoeverywhere.sh @@ -307,7 +307,7 @@ function clone_octoeverywhere() { } function install_octoeverywhere_dependencies() { - local packages + local packages log_name="OctoEverywhere" local install_script="${OCTOEVERYWHERE_DIR}/install.sh" ### read PKGLIST from official install-script @@ -318,25 +318,11 @@ function install_octoeverywhere_dependencies() { echo "${cyan}${packages}${white}" | tr '[:space:]' '\n' read -r -a packages <<< "${packages}" - ### Update system package info if lists > 3 days old - status_msg "Updating package lists..." - if [[ -z "$(find -H /var/lib/apt/lists -maxdepth 0 -mtime -3)" ]]; then - if ! sudo apt-get update --allow-releaseinfo-change; then - log_error "failure while updating package lists" - error_msg "Updating package lists failed!" - exit 1 - fi - else - status_msg "Package lists updated recently, skipping..." - fi + ### Update system package lists if stale + update_system_package_lists ### Install required packages - status_msg "Installing required packages..." - if ! sudo apt-get install --yes "${packages[@]}"; then - log_error "failure while installing required octoeverywhere packages" - error_msg "Installing required packages failed!" - exit 1 - fi + install_system_packages "$log_name" "packages[@]" } #===================================================# diff --git a/scripts/ui/update_menu.sh b/scripts/ui/update_menu.sh index 61e61ee..549a937 100755 --- a/scripts/ui/update_menu.sh +++ b/scripts/ui/update_menu.sh @@ -69,7 +69,7 @@ function update_menu() { 10) do_action "update_crowsnest" "update_ui";; 11) - do_action "update_system" "update_ui";; + do_action "upgrade_system_packages" "update_ui";; a) do_action "update_all" "update_ui";; B|b) diff --git a/scripts/utilities.sh b/scripts/utilities.sh index 269bbdf..3383968 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -285,7 +285,7 @@ function python3_check() { function dependency_check() { local dep=( "${@}" ) - local packages + local packages log_name="dependencies" status_msg "Checking for the following dependencies:" #check if package is installed, if not write its name into array @@ -303,25 +303,12 @@ function dependency_check() { done echo - ### Update system package info if lists > 3 days old - status_msg "Updating package lists..." - if [[ -z "$(find -H /var/lib/apt/lists -maxdepth 0 -mtime -3)" ]]; then - if ! sudo apt-get update --allow-releaseinfo-change; then - log_error "failure while updating package lists" - error_msg "Updating package lists failed!" - exit 1 - fi - else - status_msg "Package lists updated recently, skipping..." - fi - - if sudo apt-get install "${packages[@]}" -y; then - ok_msg "Dependencies installed!" - else - log_error "failure while installing dependencies" - error_msg "Installing dependencies failed!" - return 1 # exit kiauh - fi + # update system package lists if stale + update_system_package_lists + + # install required packages + install_system_packages "$log_name" "packages[@]" + else ok_msg "Dependencies already met!" return @@ -371,8 +358,35 @@ function create_required_folders() { done } +function update_system_package_lists() { + local cache_mtime update_age + if [[ -e /var/lib/apt/periodic/update-success-stamp ]]; then + cache_mtime="$(stat -c $Y /var/lib/apt/periodic/update-success-stamp)" + elif [[ -e /var/lib/apt/lists ]]; then + cache_mtime="$(stat -c $Y /var/lib/apt/lists)" + else + log_error "failure determining package cache age, forcing update" + cache_mtime=0 + fi + + update_age="$(($(date +'%s') - cache_mtime))" + + # update if cache is greater than 48 hours old + if [[ $update_age -gt $((48*60*60)) ]]; then + status_msg "Updating package lists..." + if ! sudo apt-get update --allow-releaseinfo-change; then + log_error "failure while updating package lists" + error_msg "Updating package lists failed!" + exit 1 + fi + else + status_msg "Package lists updated recently, skipping..." + fi +} + function check_system_updates() { local updates_avail info_msg + update_system_package_lists updates_avail=$(apt list --upgradeable 2>/dev/null | sed "1d") if [[ -n ${updates_avail} ]]; then @@ -386,22 +400,26 @@ function check_system_updates() { echo "${info_msg}" } -function update_system() { - status_msg "Updating System ..." - if [[ -z "$(find -H /var/lib/apt/lists -maxdepth 0 -mtime -3)" ]]; then - status_msg "Updating package lists..." - if ! sudo apt-get update --allow-releaseinfo-change; then - log_error "failure while updating package lists" - error_msg "Updating package lists failed!" - exit 1 - fi - else - status_msg "Package lists updated recently, skipping..." - fi +function upgrade_system_packages() { + status_msg "Upgrading System ..." + update_system_package_lists if sudo apt-get upgrade -y; then - print_confirm "Update complete! Check the log above!\n ${yellow}KIAUH will not install any dist-upgrades or\n any packages which have been kept back!${green}" + print_confirm "Upgrade complete! Check the log above!\n ${yellow}KIAUH will not install any dist-upgrades or\n any packages which have been held back!${green}" else - print_error "System update failed! Please look for any errors printed above!" + print_error "System upgrade failed! Please look for any errors printed above!" + fi +} + +function install_system_packages() { + local log_name="$1" + local packages=("${!2}") + status_msg "Installing packages..." + if sudo apt-get install -y "${packages[@]}"; then + ok_msg "${log_name^} packages installed!" + else + log_error "failure while installing ${log_name,,} packages" + error_msg "Installing $log_name packages failed!" + exit 1 # exit kiauh fi }