From 8eb2924832658a970373a906ef67afc3e6a2f07e Mon Sep 17 00:00:00 2001 From: cravl-dev Date: Sat, 17 Jun 2023 12:08:24 -0400 Subject: [PATCH] refactor: update package lists only when stale (#346) Co-authored-by: th33xitus --- .gitignore | 4 +- scripts/crowsnest.sh | 18 ++---- scripts/klipper.sh | 20 ++----- scripts/moonraker-telegram-bot.sh | 18 ++---- scripts/moonraker.sh | 18 ++---- scripts/octoeverywhere.sh | 18 ++---- scripts/ui/install_menu.sh | 3 +- scripts/ui/update_menu.sh | 5 +- scripts/utilities.sh | 97 ++++++++++++++++++++++++------- 9 files changed, 104 insertions(+), 97 deletions(-) diff --git a/.gitignore b/.gitignore index 7084f49..560b8c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -klipper_repos.txt \ No newline at end of file +.vscode +*.code-workspace +klipper_repos.txt diff --git a/scripts/crowsnest.sh b/scripts/crowsnest.sh index cda3820..fb864ab 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,21 +208,11 @@ function install_crowsnest_dependencies() { echo "${cyan}${packages}${white}" | tr '[:space:]' '\n' read -r -a packages <<< "${packages}" - ### Update system package info - 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 + ### 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 66126b4..6ea8c28 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,21 +321,11 @@ function install_klipper_packages() { echo "${cyan}${packages}${white}" | tr '[:space:]' '\n' read -r -a packages <<< "${packages}" - ### Update system package info - 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 + ### 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() { @@ -634,4 +624,4 @@ function get_klipper_python_ver() { local version version=$("${KLIPPY_ENV}"/bin/python --version 2>&1 | cut -d" " -f2 | cut -d"." -f1) echo "${version}" -} \ No newline at end of file +} diff --git a/scripts/moonraker-telegram-bot.sh b/scripts/moonraker-telegram-bot.sh index 7aab1e9..e88a93f 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,21 +121,11 @@ function install_telegram_bot_dependencies() { echo "${cyan}${packages}${white}" | tr '[:space:]' '\n' read -r -a packages <<< "${packages}" - ### Update system package info - 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 + ### 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 655e2d6..e971878 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,21 +152,11 @@ function install_moonraker_dependencies() { echo "${cyan}${packages}${white}" | tr '[:space:]' '\n' read -r -a packages <<< "${packages}" - ### Update system package info - 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 + ### 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 ff953ce..635c443 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,21 +318,11 @@ function install_octoeverywhere_dependencies() { echo "${cyan}${packages}${white}" | tr '[:space:]' '\n' read -r -a packages <<< "${packages}" - ### Update system package info - 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 + ### 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/install_menu.sh b/scripts/ui/install_menu.sh index 2ebcc00..842c367 100755 --- a/scripts/ui/install_menu.sh +++ b/scripts/ui/install_menu.sh @@ -34,7 +34,8 @@ function install_ui() { } function install_menu() { - clear && print_header + clear -x && sudo -v && clear -x # (re)cache sudo credentials so password prompt doesn't bork ui + print_header install_ui ### save all installed webinterface ports to the ini file diff --git a/scripts/ui/update_menu.sh b/scripts/ui/update_menu.sh index 61e61ee..169d9b3 100755 --- a/scripts/ui/update_menu.sh +++ b/scripts/ui/update_menu.sh @@ -11,7 +11,7 @@ set -e -function update_ui() { +function update_ui() { top_border echo -e "| ${green}~~~~~~~~~~~~~~ [ Update Menu ] ~~~~~~~~~~~~~~${white} |" hr @@ -40,6 +40,7 @@ function update_ui() { } function update_menu() { + clear -x && sudo -v && clear -x # (re)cache sudo credentials so password prompt doesn't bork ui do_action "" "update_ui" local action @@ -69,7 +70,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 b3293ff..91ab5ce 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -20,6 +20,8 @@ function check_euid() { echo -e "${red}" top_border echo -e "| !!! THIS SCRIPT MUST NOT RUN AS ROOT !!! |" + echo -e "| |" + echo -e "| It will ask for credentials as needed. |" bottom_border echo -e "${white}" exit 1 @@ -285,7 +287,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,12 +305,12 @@ function dependency_check() { done echo - if sudo apt-get update --allow-releaseinfo-change && sudo apt-get install "${packages[@]}" -y; then - ok_msg "Dependencies installed!" - else - 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 @@ -358,27 +360,78 @@ function create_required_folders() { done } -function check_system_updates() { - local updates_avail info_msg - updates_avail=$(apt list --upgradeable 2>/dev/null | sed "1d") - - if [[ -n ${updates_avail} ]]; then - info_msg="${yellow}System upgrade available!${white}" - # add system to application_updates_available in kiauh.ini - add_to_application_updates "system" +function update_system_package_lists() { + local cache_mtime update_age update_interval silent + + if [[ $1 == '--silent' ]]; then silent="true"; fi + + 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 - info_msg="${green}System up to date! ${white}" + log_warning "Failure determining package cache age, forcing update" + cache_mtime=0 fi - echo "${info_msg}" + update_age="$(($(date +'%s') - cache_mtime))" + update_interval=$((48*60*60)) # 48hrs + + # update if cache is greater than update_interval + if (( update_age > update_interval )); then + if [[ ! ${silent} == "true" ]]; then status_msg "Updating package lists..."; fi + if ! sudo apt-get update --allow-releaseinfo-change &>/dev/null; then + log_error "Failure while updating package lists!" + if [[ ! ${silent} == "true" ]]; then error_msg "Updating package lists failed!"; fi + return 1 + else + log_info "Package lists updated successfully" + if [[ ! ${silent} == "true" ]]; then status_msg "Updated package lists."; fi + fi + else + log_info "Package lists updated recently, skipping update..." + fi } -function update_system() { - status_msg "Updating System ..." - if sudo apt-get update --allow-releaseinfo-change && 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}" +function check_system_updates() { + local updates_avail status + if ! update_system_package_lists --silent; then + status="${red}Update check failed! ${white}" else - print_error "System update failed! Please watch for any errors printed above!" + updates_avail="$(apt list --upgradeable 2>/dev/null | sed "1d")" + + if [[ -n ${updates_avail} ]]; then + status="${yellow}System upgrade available!${white}" + # add system to application_updates_available in kiauh.ini + add_to_application_updates "system" + else + status="${green}System up to date! ${white}" + fi + fi + + echo "${status}" +} + +function upgrade_system_packages() { + status_msg "Upgrading System ..." + update_system_package_lists + if sudo apt-get upgrade -y; then + 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 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 }