refactor: dedupe apt update & install functions

This commit is contained in:
Cameron Ryder
2023-06-04 23:01:48 -04:00
parent 2d177e0219
commit 4160c4aa77
7 changed files with 73 additions and 125 deletions

View File

@@ -197,7 +197,7 @@ function compare_crowsnest_versions() {
} }
function install_crowsnest_dependencies() { function install_crowsnest_dependencies() {
local packages local packages log_name="Crowsnest"
local install_script="${CROWSNEST_DIR}/tools/install.sh" local install_script="${CROWSNEST_DIR}/tools/install.sh"
### read PKGLIST from official install-script ### read PKGLIST from official install-script
@@ -208,25 +208,11 @@ function install_crowsnest_dependencies() {
echo "${cyan}${packages}${white}" | tr '[:space:]' '\n' echo "${cyan}${packages}${white}" | tr '[:space:]' '\n'
read -r -a packages <<< "${packages}" read -r -a packages <<< "${packages}"
### Update system package info if lists > 3 days old ### Update system package lists if stale
status_msg "Updating package lists..." update_system_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
### Install required packages ### Install required packages
status_msg "Installing required packages..." install_system_packages "$log_name" "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
} }
function update_crowsnest() { function update_crowsnest() {

View File

@@ -295,7 +295,7 @@ function create_klipper_virtualenv() {
# @param {string}: python_version - klipper-env python version # @param {string}: python_version - klipper-env python version
# #
function install_klipper_packages() { 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" local install_script="${KLIPPER_DIR}/scripts/install-debian.sh"
status_msg "Reading dependencies..." status_msg "Reading dependencies..."
@@ -321,25 +321,11 @@ function install_klipper_packages() {
echo "${cyan}${packages}${white}" | tr '[:space:]' '\n' echo "${cyan}${packages}${white}" | tr '[:space:]' '\n'
read -r -a packages <<< "${packages}" read -r -a packages <<< "${packages}"
### Update system package info if lists > 3 days old ### Update system package lists if stale
status_msg "Updating package lists..." update_system_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
### Install required packages ### Install required packages
status_msg "Installing required packages..." install_system_packages "$log_name" "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
} }
function create_klipper_service() { function create_klipper_service() {

View File

@@ -110,7 +110,7 @@ function telegram_bot_setup_dialog() {
} }
function install_telegram_bot_dependencies() { function install_telegram_bot_dependencies() {
local packages local packages log_name="Telegram Bot"
local install_script="${TELEGRAM_BOT_DIR}/scripts/install.sh" local install_script="${TELEGRAM_BOT_DIR}/scripts/install.sh"
### read PKGLIST from official install-script ### read PKGLIST from official install-script
@@ -121,25 +121,11 @@ function install_telegram_bot_dependencies() {
echo "${cyan}${packages}${white}" | tr '[:space:]' '\n' echo "${cyan}${packages}${white}" | tr '[:space:]' '\n'
read -r -a packages <<< "${packages}" read -r -a packages <<< "${packages}"
### Update system package info if lists > 3 days old ### Update system package lists if stale
status_msg "Updating package lists..." update_system_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
### Install required packages ### Install required packages
status_msg "Installing required packages..." install_system_packages "$log_name" "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
} }
function create_telegram_bot_virtualenv() { function create_telegram_bot_virtualenv() {

View File

@@ -141,7 +141,7 @@ function moonraker_setup_dialog() {
} }
function install_moonraker_dependencies() { function install_moonraker_dependencies() {
local packages local packages log_name="Moonraker"
local install_script="${MOONRAKER_DIR}/scripts/install-moonraker.sh" local install_script="${MOONRAKER_DIR}/scripts/install-moonraker.sh"
### read PKGLIST from official install-script ### read PKGLIST from official install-script
@@ -152,25 +152,11 @@ function install_moonraker_dependencies() {
echo "${cyan}${packages}${white}" | tr '[:space:]' '\n' echo "${cyan}${packages}${white}" | tr '[:space:]' '\n'
read -r -a packages <<< "${packages}" read -r -a packages <<< "${packages}"
### Update system package info if lists > 3 days old ### Update system package lists if stale
status_msg "Updating package lists..." update_system_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
### Install required packages ### Install required packages
status_msg "Installing required packages..." install_system_packages "$log_name" "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
} }
function create_moonraker_virtualenv() { function create_moonraker_virtualenv() {

View File

@@ -307,7 +307,7 @@ function clone_octoeverywhere() {
} }
function install_octoeverywhere_dependencies() { function install_octoeverywhere_dependencies() {
local packages local packages log_name="OctoEverywhere"
local install_script="${OCTOEVERYWHERE_DIR}/install.sh" local install_script="${OCTOEVERYWHERE_DIR}/install.sh"
### read PKGLIST from official install-script ### read PKGLIST from official install-script
@@ -318,25 +318,11 @@ function install_octoeverywhere_dependencies() {
echo "${cyan}${packages}${white}" | tr '[:space:]' '\n' echo "${cyan}${packages}${white}" | tr '[:space:]' '\n'
read -r -a packages <<< "${packages}" read -r -a packages <<< "${packages}"
### Update system package info if lists > 3 days old ### Update system package lists if stale
status_msg "Updating package lists..." update_system_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
### Install required packages ### Install required packages
status_msg "Installing required packages..." install_system_packages "$log_name" "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
} }
#===================================================# #===================================================#

View File

@@ -69,7 +69,7 @@ function update_menu() {
10) 10)
do_action "update_crowsnest" "update_ui";; do_action "update_crowsnest" "update_ui";;
11) 11)
do_action "update_system" "update_ui";; do_action "upgrade_system_packages" "update_ui";;
a) a)
do_action "update_all" "update_ui";; do_action "update_all" "update_ui";;
B|b) B|b)

View File

@@ -285,7 +285,7 @@ function python3_check() {
function dependency_check() { function dependency_check() {
local dep=( "${@}" ) local dep=( "${@}" )
local packages local packages log_name="dependencies"
status_msg "Checking for the following dependencies:" status_msg "Checking for the following dependencies:"
#check if package is installed, if not write its name into array #check if package is installed, if not write its name into array
@@ -303,25 +303,12 @@ function dependency_check() {
done done
echo echo
### Update system package info if lists > 3 days old # update system package lists if stale
status_msg "Updating package lists..." update_system_package_lists
if [[ -z "$(find -H /var/lib/apt/lists -maxdepth 0 -mtime -3)" ]]; then
if ! sudo apt-get update --allow-releaseinfo-change; then # install required packages
log_error "failure while updating package lists" install_system_packages "$log_name" "packages[@]"
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
else else
ok_msg "Dependencies already met!" ok_msg "Dependencies already met!"
return return
@@ -371,8 +358,35 @@ function create_required_folders() {
done 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() { function check_system_updates() {
local updates_avail info_msg local updates_avail info_msg
update_system_package_lists
updates_avail=$(apt list --upgradeable 2>/dev/null | sed "1d") updates_avail=$(apt list --upgradeable 2>/dev/null | sed "1d")
if [[ -n ${updates_avail} ]]; then if [[ -n ${updates_avail} ]]; then
@@ -386,22 +400,26 @@ function check_system_updates() {
echo "${info_msg}" echo "${info_msg}"
} }
function update_system() { function upgrade_system_packages() {
status_msg "Updating System ..." status_msg "Upgrading System ..."
if [[ -z "$(find -H /var/lib/apt/lists -maxdepth 0 -mtime -3)" ]]; then update_system_package_lists
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
if sudo apt-get upgrade -y; then 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 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 fi
} }