1
0
Fork 0
mirror of https://github.com/Luzifer/arch-update.git synced 2024-12-20 11:01:16 +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.timer
)
sha512sums=('16c771a551ceacb25513d5a775f75e5beb690659b3573cefaeee09348c1582f66df0755c7258898929e46cd5ffc5f9d6dfde9e53b017e9244e4a02870992d86e'
sha512sums=('380fa9dd9ce08b9e2dc1452b23f045bb4dc727b1192aa5c46adcb6be4d3e8b0af8dd9be93149b0e4b62132f0783dd04efe30d3fbb7cd28a2dff384e844f96486'
'384a9fc9c7f43bd7e9ec9274b5e930e7e9e3dcea088f524201f0b359f33e470b9b120b6c261b82b2d484d7af937eb67ba2cdf7a0bda2ca424338da03e008a716'
'f9b62fbc31d963525340c408ddacf671c54d9874b4decd1c84286b2a000ef488a85d738f7a3d83db34e9dd98baa84389aff837bdf68531712ba9eaa7a8d762bd')

View file

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