refactor: update package lists only when stale (#346)

Co-authored-by: th33xitus <th33xitus@googlemail.com>
This commit is contained in:
cravl-dev
2023-06-17 12:08:24 -04:00
committed by GitHub
parent 5d7debd65e
commit 8eb2924832
9 changed files with 104 additions and 97 deletions

2
.gitignore vendored
View File

@@ -1 +1,3 @@
.vscode
*.code-workspace
klipper_repos.txt

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -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[@]"
}
#===================================================#

View File

@@ -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

View File

@@ -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)

View File

@@ -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 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
log_warning "Failure determining package cache age, forcing update"
cache_mtime=0
fi
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 check_system_updates() {
local updates_avail info_msg
updates_avail=$(apt list --upgradeable 2>/dev/null | sed "1d")
local updates_avail status
if ! update_system_package_lists --silent; then
status="${red}Update check failed! ${white}"
else
updates_avail="$(apt list --upgradeable 2>/dev/null | sed "1d")"
if [[ -n ${updates_avail} ]]; then
info_msg="${yellow}System upgrade available!${white}"
status="${yellow}System upgrade available!${white}"
# add system to application_updates_available in kiauh.ini
add_to_application_updates "system"
else
info_msg="${green}System up to date! ${white}"
status="${green}System up to date! ${white}"
fi
fi
echo "${info_msg}"
echo "${status}"
}
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 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 update failed! Please watch 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
}