From 39319abb3be61599601b6242432123705a879df7 Mon Sep 17 00:00:00 2001 From: Harald Pfeiffer Date: Sun, 3 Nov 2019 12:47:17 +0100 Subject: InComm --- README.md | 7 +++ bin/dnfu | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++ etc/sudoers.d/maint | 5 ++ 3 files changed, 150 insertions(+) create mode 100644 README.md create mode 100755 bin/dnfu create mode 100644 etc/sudoers.d/maint diff --git a/README.md b/README.md new file mode 100644 index 0000000..420d70d --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +## Content + +For now, this is just a dump where I throw in scripts I use on Red Hat +derivatives (i.e. Fedora in most of these cases, rarely CentOS). + +Dedicated sections in this readme may follow, so may dedicated repositories on +this server. diff --git a/bin/dnfu b/bin/dnfu new file mode 100755 index 0000000..3cb51bf --- /dev/null +++ b/bin/dnfu @@ -0,0 +1,138 @@ +#!/usr/bin/env bash + +# A script to upgrade a RHEL derivative (Fedora, CentOS, ...). If you have ansible or puppet: +# this is way more simple with an automation framework. This script works with the paradigm +# that you only have a bash available and you want to see what happens without being murdered +# by gazillions of output lines. + +# This script avoids combining options, e.g. dnf -C -q instead of dnf -Cq, since +# dnf is perfectly capable of POSIX standards, but yum fails at combined parameters. +# So we'll leave it this way in case of conversion of this script to yum or extension +# to be capable of both. (tl;dr no combined options because yum sucks at POSIX standards) + +ROK="\033[666D[ \033[32mOK\033[0m ]\033[u\033[K" +RWRN="\033[666D[\033[33mWARN\033[0m]\033[u\033[K" +RERR="\033[666D[\033[31mFAIL\033[0m]\033[u\033[K" +RINF="\033[666D[\033[37mINFO\033[0m]\033[u\033[K" + +UPDAVAIL=1 +SCMD="$(command -v snap 2>/dev/null)" +UCMD="$(command -v dnf 2>/dev/null)" +[ "$?" -ne 0 ]&&UCMD="$(command -v yum 2>/dev/null)" +[ -z "$UCMD" ]&&printf "Neither yum nor dnf found!\n" >&2&&exit 1 +RETVAL=0 + +function radd { + [ -z "$1" ]&&return 1 + printf "\033[666D\033[u\033[K \033[37m(%b)\033[0m" "$1" +} +function rbeg { + [ -z "$1" ]&&return 1 + printf "[....] %b.\033[s.." "$1" +} +function rok { + case "$1" in + "") printf "%b\n" "$ROK";; + *) + printf "%b" "$ROK" + radd "$1" + printf "\n" + ;; + esac +} +function rwrn { + case "$1" in + "") printf "%b\n" "$RWRN";; + *) + printf "%b" "$RWRN" + radd "$1" + printf "\n" + ;; + esac +} +function rerr { + case "$1" in + "") printf "%b\n" "$RERR";; + *) + printf "%b" "$RERR" + radd "$1" + printf "\n" + ;; + esac +} +function rinf { + case "$1" in + "") printf "%b\n" "$RINF";; + *) + printf "%b" "$RINF" + radd "$1" + printf "\n" + ;; + esac +} +function supgrade { + printf "Listing updates...:\n" + sudo "${UCMD}" -C -q check-upgrade + case "$?" in + 0|100) ;; + *) RETVAL=1;return 1;; + esac + rbeg "Upgrading system packages" + sudo "${UCMD}" -q -y upgrade + case "$?" in + 0) rok;; + 1) + rwrn "error occurred but was handled by dnf" + case "RETVAL" in + 0) RETVAL=255;; + esac + ;; + 255) + rwrn "Upgrade done but updates remaining" + case "$RETVAL" in + 0) RETVAL=255;; + esac + ;; + *) + rerr + RETVAL=1;return 1 + ;; + esac + printf "Checking for outdated running services or necessity to reboot...:\n" + sudo "${UCMD}" needs-restarting +} +function snrefresh { + rbeg "Refreshing snapd snaps" + sudo "${SCMD}" refresh >/dev/null 2>&1 + case "$?" in + 0) rok;; + *) rerr;RETVAL=1;return 1;; + esac +} + +rbeg "Updating dnf cache" +sudo "${UCMD}" -q makecache +case "$?" in + 0) rok;; + 100) rwrn;; + *) rerr;exit 1;; +esac +rbeg "Checking for upgrades" +sudo "${UCMD}" -C -q check-upgrade +case "$?" in + 0) + rinf "No updates" + ;; + 100) + rok "Updates found" + UPDAVAIL=0 + ;; +esac + +if [ "$UPDAVAIL" -eq 0 ];then + supgrade +fi + +if [ ! -z "$SCMD" ];then + snrefresh +fi diff --git a/etc/sudoers.d/maint b/etc/sudoers.d/maint new file mode 100644 index 0000000..d02225f --- /dev/null +++ b/etc/sudoers.d/maint @@ -0,0 +1,5 @@ +%maint ALL=(ALL) NOPASSWD:/usr/bin/snap refresh, /usr/bin/needs-restarting, /usr/bin/dnf needs-restarting +# Using -C -q instead of -Cq because yum sucks at this. dnf can cope with it, but in case of +# moving forth to a yum-only system... +%maint ALL=(ALL) NOPASSWD:/usr/bin/dnf -q makecache,/usr/bin/dnf -C -q check-upgrade +%maint ALL=(ALL) NOPASSWD:/usr/bin/dnf -q -y upgrade -- cgit v1.2.3