1
0
Fork 0
mirror of https://github.com/Luzifer/arch-update.git synced 2024-10-18 06:44:23 +00:00

Use nocolor option of checkupdates

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2024-02-02 10:06:33 +01:00
parent fa498355eb
commit 87c4eeb88a
Signed by: luzifer
SSH key fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E
2 changed files with 115 additions and 115 deletions

View file

@ -13,7 +13,7 @@ source=(
arch-update.service arch-update.service
arch-update.timer arch-update.timer
) )
sha512sums=('16c771a551ceacb25513d5a775f75e5beb690659b3573cefaeee09348c1582f66df0755c7258898929e46cd5ffc5f9d6dfde9e53b017e9244e4a02870992d86e' sha512sums=('380fa9dd9ce08b9e2dc1452b23f045bb4dc727b1192aa5c46adcb6be4d3e8b0af8dd9be93149b0e4b62132f0783dd04efe30d3fbb7cd28a2dff384e844f96486'
'384a9fc9c7f43bd7e9ec9274b5e930e7e9e3dcea088f524201f0b359f33e470b9b120b6c261b82b2d484d7af937eb67ba2cdf7a0bda2ca424338da03e008a716' '384a9fc9c7f43bd7e9ec9274b5e930e7e9e3dcea088f524201f0b359f33e470b9b120b6c261b82b2d484d7af937eb67ba2cdf7a0bda2ca424338da03e008a716'
'f9b62fbc31d963525340c408ddacf671c54d9874b4decd1c84286b2a000ef488a85d738f7a3d83db34e9dd98baa84389aff837bdf68531712ba9eaa7a8d762bd') 'f9b62fbc31d963525340c408ddacf671c54d9874b4decd1c84286b2a000ef488a85d738f7a3d83db34e9dd98baa84389aff837bdf68531712ba9eaa7a8d762bd')

View file

@ -15,172 +15,172 @@ COLOR_PLAIN="\033[0m"
# Check we do have root access # Check we do have root access
if [ $(id -u) -ne 0 ]; then if [ $(id -u) -ne 0 ]; then
echo "Escalating to root..." echo "Escalating to root..."
exec sudo $0 "$@" exec sudo $0 "$@"
fi fi
# Load defaults from defaults file # Load defaults from defaults file
[ -f /etc/default/arch_update ] && source /etc/default/arch_update [ -f /etc/default/arch_update ] && source /etc/default/arch_update
function checktool() { # ( command ) function checktool() { # ( command )
which "$1" >/dev/null 2>&1 && return 0 || return 1 which "$1" >/dev/null 2>&1 && return 0 || return 1
} }
function error() { # ( message ) function error() { # ( message )
local msg="$@" local msg="$@"
log "$msg" "${COLOR_RED}" log "$msg" "${COLOR_RED}"
} }
function feature() { # ( name, enabled ) function feature() { # ( name, enabled )
local v local v
[ $2 -eq 1 ] && v="${COLOR_GREEN}enabled${COLOR_PLAIN}" || v="${COLOR_YELLOW}disabled${COLOR_PLAIN}" [ $2 -eq 1 ] && v="${COLOR_GREEN}enabled${COLOR_PLAIN}" || v="${COLOR_YELLOW}disabled${COLOR_PLAIN}"
echo "$1 $v" echo "$1 $v"
} }
function inArray() { # ( keyOrValue, arrayKeysOrValues ) function inArray() { # ( keyOrValue, arrayKeysOrValues )
local e local e
for e in "${@:2}"; do for e in "${@:2}"; do
[[ $e == "$1" ]] && return 0 [[ $e == "$1" ]] && return 0
done done
return 1 return 1
} }
function info() { # ( message ) function info() { # ( message )
local msg="$@" local msg="$@"
log "$msg" "${COLOR_CYAN}" log "$msg" "${COLOR_CYAN}"
} }
function log() { # ( message, color ) function log() { # ( message, color )
local s local s
local color="${2:-${COLOR_PLAIN}}" local color="${2:-${COLOR_PLAIN}}"
s="[$(date)] $1" s="[$(date)] $1"
echo -e "${color}${s}${COLOR_PLAIN}" >&2 echo -e "${color}${s}${COLOR_PLAIN}" >&2
LOGLINES+=("$(echo "$s" | sed -E "s/\\\033\[[^m]*m//g")") LOGLINES+=("$(echo "$s" | sed -E "s/\\\033\[[^m]*m//g")")
} }
function main() { # ( ) function main() { # ( )
log "Starting arch_update on $(hostname): $(feature "dry-run" $DRYRUN), $(feature "reboot" $REBOOT), $(feature "service-restart" $RESSVC)" log "Starting arch_update on $(hostname): $(feature "dry-run" $DRYRUN), $(feature "reboot" $REBOOT), $(feature "service-restart" $RESSVC)"
checktool checkupdates || { checktool checkupdates || {
error "Missing tool 'checkupdates': Need to install package pacman-contrib" error "Missing tool 'checkupdates': Need to install package pacman-contrib"
exit 1 exit 1
} }
# Collect packages to be updated # Collect packages to be updated
log "Collecting packages..." log "Collecting packages..."
packages=($(checkupdates | awk '{print $1}' || true)) packages=($(checkupdates --nocolor | awk '{print $1}' || true))
if [ ${#packages[@]} -eq 0 ]; then if [ ${#packages[@]} -eq 0 ]; then
success "Nothing to do, exiting now." success "Nothing to do, exiting now."
exit 0 exit 0
fi fi
info "${#packages[@]} packages to update: ${packages[@]}" info "${#packages[@]} packages to update: ${packages[@]}"
# Collect services to be restarted # Collect services to be restarted
log "Collecting affected services..." log "Collecting affected services..."
services=() services=()
for package in "${packages[@]}"; do for package in "${packages[@]}"; do
if (systemctl is-active "${package}.service" >/dev/null); then if (systemctl is-active "${package}.service" >/dev/null); then
services+=("${package}.service") services+=("${package}.service")
fi fi
done done
if [ ${#services[@]} -gt 0 ]; then if [ ${#services[@]} -gt 0 ]; then
info "${#services[@]} services to restart: ${services[@]}" info "${#services[@]} services to restart: ${services[@]}"
else else
info "No services need to be restarted" info "No services need to be restarted"
fi fi
# Check whether system should be rebooted # Check whether system should be rebooted
log "Checking whether reboot is adviced..." log "Checking whether reboot is adviced..."
needs_reboot=() needs_reboot=()
for pkg in "${REQ_REBOOT[@]}"; do for pkg in "${REQ_REBOOT[@]}"; do
if (inArray "${pkg}" "${packages[@]}"); then if (inArray "${pkg}" "${packages[@]}"); then
needs_reboot+=("${pkg}") needs_reboot+=("${pkg}")
fi fi
done done
if [ ${#needs_reboot[@]} -gt 0 ]; then if [ ${#needs_reboot[@]} -gt 0 ]; then
warn "Reboot is adviced for: ${needs_reboot[@]}" warn "Reboot is adviced for: ${needs_reboot[@]}"
fi fi
# Ensure dry-run is met # Ensure dry-run is met
[ ${DRYRUN} -eq 0 ] || { [ ${DRYRUN} -eq 0 ] || {
success "Dry-Run enabled, not taking action!" success "Dry-Run enabled, not taking action!"
exit 1 exit 1
} }
# Execute sync of repos # Execute sync of repos
log "Syncing repos..." log "Syncing repos..."
pacman -Syy --noconfirm pacman -Syy --noconfirm
# Update packages # Update packages
log "Upgrading packages..." log "Upgrading packages..."
pacman -S --noconfirm "${packages[@]}" pacman -S --noconfirm "${packages[@]}"
# If enabled and required do a reboot # If enabled and required do a reboot
if [ ${#needs_reboot[@]} -gt 0 ]; then if [ ${#needs_reboot[@]} -gt 0 ]; then
if [ $REBOOT -eq 1 ]; then if [ $REBOOT -eq 1 ]; then
info "Reboot will be executed in 1 minute" info "Reboot will be executed in 1 minute"
shutdown -r +1 # Give the script enough time to finish and queue reboot shutdown -r +1 # Give the script enough time to finish and queue reboot
info "Reboot scheduled, ending script now" info "Reboot scheduled, ending script now"
exit 0 # Do not execute more of this script exit 0 # Do not execute more of this script
else else
warn "Reboot is strongly suggested but auto-reboot is disabled" warn "Reboot is strongly suggested but auto-reboot is disabled"
date +%s >/tmp/arch_update_needs_reboot date +%s >/tmp/arch_update_needs_reboot
fi fi
fi fi
# Restart affected services # Restart affected services
if [ $RESSVC -eq 1 ]; then if [ $RESSVC -eq 1 ]; then
for svc in "${services[@]}"; do for svc in "${services[@]}"; do
log "Restarting service ${svc}" log "Restarting service ${svc}"
systemctl restart "${svc}" || error "Restart of ${svc} failed and needs attention" systemctl restart "${svc}" || error "Restart of ${svc} failed and needs attention"
done done
else else
warn "Not restarting services as requested" warn "Not restarting services as requested"
fi fi
success "Everything finished" success "Everything finished"
} }
function sendResult() { # ( ) function sendResult() { # ( )
IFS=$'\n' IFS=$'\n'
echo "${LOGLINES[*]}" >>/var/log/arch_update.log echo "${LOGLINES[*]}" >>/var/log/arch_update.log
} }
function success() { # ( message ) function success() { # ( message )
local msg="$@" local msg="$@"
log "$msg" "${COLOR_GREEN}" log "$msg" "${COLOR_GREEN}"
} }
function warn() { # ( message ) function warn() { # ( message )
local msg="$@" local msg="$@"
log "$msg" "${COLOR_YELLOW}" log "$msg" "${COLOR_YELLOW}"
} }
# Argument parsing # Argument parsing
while getopts ":nrs" o; do while getopts ":nrs" o; do
case "${o}" in case "${o}" in
n) n)
DRYRUN=1 DRYRUN=1
;; ;;
r) r)
REBOOT=1 REBOOT=1
;; ;;
s) s)
RESSVC=1 RESSVC=1
;; ;;
*) *)
echo "Usage: $0 [-nrs]" >&2 echo "Usage: $0 [-nrs]" >&2
echo " -n Dry-Run: Do nothing except looking for updates" >&2 echo " -n Dry-Run: Do nothing except looking for updates" >&2
echo " -r Reboot: In case packages flagged for reboot are updated, reboot after update" >&2 echo " -r Reboot: In case packages flagged for reboot are updated, reboot after update" >&2
echo " -s Services Restart: Restart all systemd services matching updated package names" >&2 echo " -s Services Restart: Restart all systemd services matching updated package names" >&2
exit 1 exit 1
;; ;;
esac esac
done done
shift $((OPTIND - 1)) shift $((OPTIND - 1))