From 9b64199a2fa79e193b43f6a8d2bb51ad8ef72722 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 10 Jun 2023 09:55:47 +0200 Subject: [PATCH] refactor: refactor update_system_package_lists Signed-off-by: Dominik Willner --- scripts/crowsnest.sh | 2 +- scripts/klipper.sh | 4 ++-- scripts/moonraker-telegram-bot.sh | 2 +- scripts/moonraker.sh | 2 +- scripts/octoeverywhere.sh | 2 +- scripts/utilities.sh | 21 ++++++++++++--------- 6 files changed, 18 insertions(+), 15 deletions(-) diff --git a/scripts/crowsnest.sh b/scripts/crowsnest.sh index 20b29f5..fb864ab 100644 --- a/scripts/crowsnest.sh +++ b/scripts/crowsnest.sh @@ -212,7 +212,7 @@ function install_crowsnest_dependencies() { update_system_package_lists ### Install required packages - install_system_packages "$log_name" "packages[@]" + install_system_packages "${log_name}" "packages[@]" } function update_crowsnest() { diff --git a/scripts/klipper.sh b/scripts/klipper.sh index 9fb7c00..6ea8c28 100644 --- a/scripts/klipper.sh +++ b/scripts/klipper.sh @@ -325,7 +325,7 @@ function install_klipper_packages() { update_system_package_lists ### Install required packages - install_system_packages "$log_name" "packages[@]" + install_system_packages "${log_name}" "packages[@]" } function create_klipper_service() { @@ -624,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 0ccd931..e88a93f 100644 --- a/scripts/moonraker-telegram-bot.sh +++ b/scripts/moonraker-telegram-bot.sh @@ -125,7 +125,7 @@ function install_telegram_bot_dependencies() { update_system_package_lists ### Install required packages - install_system_packages "$log_name" "packages[@]" + install_system_packages "${log_name}" "packages[@]" } function create_telegram_bot_virtualenv() { diff --git a/scripts/moonraker.sh b/scripts/moonraker.sh index 9a89839..e971878 100644 --- a/scripts/moonraker.sh +++ b/scripts/moonraker.sh @@ -156,7 +156,7 @@ function install_moonraker_dependencies() { update_system_package_lists ### Install required packages - install_system_packages "$log_name" "packages[@]" + install_system_packages "${log_name}" "packages[@]" } function create_moonraker_virtualenv() { diff --git a/scripts/octoeverywhere.sh b/scripts/octoeverywhere.sh index a603b9d..635c443 100644 --- a/scripts/octoeverywhere.sh +++ b/scripts/octoeverywhere.sh @@ -322,7 +322,7 @@ function install_octoeverywhere_dependencies() { update_system_package_lists ### Install required packages - install_system_packages "$log_name" "packages[@]" + install_system_packages "${log_name}" "packages[@]" } #===================================================# diff --git a/scripts/utilities.sh b/scripts/utilities.sh index 4affc3c..7081876 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -307,7 +307,7 @@ function dependency_check() { update_system_package_lists # install required packages - install_system_packages "$log_name" "packages[@]" + install_system_packages "${log_name}" "packages[@]" else ok_msg "Dependencies already met!" @@ -359,8 +359,10 @@ function create_required_folders() { } function update_system_package_lists() { - local cache_mtime update_age silent - if [[ $1 == '--silent' ]]; then silent=1; shift; fi + 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 @@ -371,17 +373,18 @@ function update_system_package_lists() { fi update_age="$(($(date +'%s') - cache_mtime))" + update_interval=$((48*60*60)) # 48hrs - # update if cache is greater than 48 hours old - if [[ $update_age -gt $((48*60*60)) ]]; then - if [[ ! $silent ]]; then status_msg "Updating package lists..."; fi + # 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 ]]; then error_msg "Updating package lists failed!"; fi + if [[ ! ${silent} == "true" ]]; then error_msg "Updating package lists failed!"; fi exit 1 else log_info "Package lists updated successfully" - if [[ ! $silent ]]; then status_msg "Updated package lists."; fi + if [[ ! ${silent} == "true" ]]; then status_msg "Updated package lists."; fi fi else log_info "Package lists updated recently, skipping update..." @@ -425,7 +428,7 @@ function install_system_packages() { ok_msg "${log_name^} packages installed!" else log_error "Failure while installing ${log_name,,} packages" - error_msg "Installing $log_name packages failed!" + error_msg "Installing ${log_name} packages failed!" exit 1 # exit kiauh fi }