Add color back in

We previously removed color because (1) it was outputting the color codes to the
log file, which we do not want and (2) nobody could agree on what colors to use.
I've reintroduced color now fixing (1) by allowing for separate output to the
log file and the console and only sending the color codes to the console and (2)
well, I've changed the blue for info messages to cyan, hopefully everyone's
happy with that, but I'm open to suggestions on the info color.

There are now three more file descriptors for specialized output control, making a total of 5:

fd1 - This sends to stdout and to the log file.
fd2 - This sends to stderr and the log file.
fd3 - This just sends to stdout.
fd4 - This just sends to stderr.
fd5 - This just sends to the logfile.

There are also three additional functions:

msg_format - Intended to be used by infomsg and errmsg, this accepts a variable
name and one or more string args.  If there is just one string after the
variable name then it is copied to the variable verbatim.  If there is more than
one then it is taken to be a format specifier and additional args to be passed
to printf (output going to the variable).

infomsg - This will accept either a single arg which contains a verbatim string
or multiple format / args to be processed by printf.  The output of this will be
sent to the log file and also color coded and sent to stdout.

errmsg - Like infomsg but it uses the error color and sends output to stderr
instead of stdout.
This commit is contained in:
Peter Ajamian 2021-06-21 00:01:02 +12:00
parent c7f8aae697
commit fe473ae20b
1 changed files with 78 additions and 30 deletions

View File

@ -56,16 +56,53 @@ fi
logfile=/var/log/migrate2rocky.log
# Send all output to the logfile as well as stdout.
# After the following we get:
# Output to 1 goes to stdout and the logfile.
# Output to 2 goes to stderr and the logfile.
# Output to 3 just goes to stdout.
# Output to 4 just goes to stderr.
# Output to 5 just goes to the logfile.
truncate -s0 "$logfile"
exec > >(tee -a "$logfile") 2> >(tee -a "$logfile" >&2)
exec \
3>&1 \
4>&2 \
5>> "$logfile" \
> >(tee -a "$logfile") \
2> >(tee -a "$logfile" >&2)
# List nocolor last here so that -x doesn't bork the display.
#errcolor=$(tput setaf 1)
#blue=$(tput setaf 4)
#nocolor=$(tput op)
errcolor=
blue=
nocolor=
errcolor=$(tput setaf 1)
infocolor=$(tput setaf 6)
nocolor=$(tput op)
# Single arg just gets returned verbatim, multi arg gets formatted via printf.
# First arg is the name of a variable to store the results.
msg_format () {
local _var
_var="$1"
shift
if (( $# > 1 )); then
printf -v "$_var" "$@"
else
printf -v "$_var" "%s" "$1"
fi
}
# Send an info message to the log file and stdout (with color)
infomsg () {
local msg
msg_format msg "$@"
printf '%s' "$msg" >&5
printf '%s%s%s' "$infocolor" "$msg" "$nocolor" >&3
}
# Send an error message to the log file and stderr (with color)
errmsg () {
local msg
msg_format msg "$@"
printf '%s' "$msg" >&5
printf '%s%s%s' "$errcolor" "$msg" "$nocolor" >&4
}
export LANG=en_US.UTF-8
shopt -s nullglob
@ -87,18 +124,23 @@ repo_urls=(
unset CDPATH
exit_message() {
printf '%s\n' "$1"
errmsg $'\n'"$1"$'\n\n'
final_message
exit 1
} >&2
}
final_message() {
printf '%s\n' "${errcolor}An error occurred while we were attempting to convert your system to Rocky Linux. Your system may be unstable. Script will now exit to prevent possible damage.$nocolor"
logmessage
errmsg '%s ' \
"An error occurred while we were attempting to convert your system to" \
"Rocky Linux. Your system may be unstable. Script will now exit to" \
"prevent possible damage."$'\n\n'
logmessage
}
logmessage(){
printf '%s\n' "${blue}A log of this installation can be found at $logfile$nocolor"
printf '%s%s%s\n' "$infocolor" \
"A log of this installation can be found at $logfile" \
"$nocolor" >&3
}
# This just grabs a field from os-release and returns it.
@ -314,8 +356,9 @@ collect_system_info () {
# [devel]=quota-devel.$ARCH
PRETTY_NAME=$(os-release PRETTY_NAME)
printf '%s\n' "${blue}Preparing to migrate $PRETTY_NAME to Rocky Linux 8.$nocolor"
printf '\n%s' "${blue}Determining repository names for $PRETTY_NAME$nocolor"
infomsg '%s' \
"Preparing to migrate $PRETTY_NAME to Rocky Linux 8."$'\n\n' \
"Determining repository names for $PRETTY_NAME"
for r in "${!pkg_repo_map[@]}"; do
printf '.'
@ -329,7 +372,7 @@ collect_system_info () {
printf '%s\t%s\n' "${repo_map[$r]}" "$r"
done)
printf '\n%s' "${blue}Getting system package names for $PRETTY_NAME$nocolor."
infomsg $'\n'"Getting system package names for $PRETTY_NAME"
# We don't know what the names of these packages are, we have to discover
# them via various means. The most common means is to look for either a
@ -388,7 +431,7 @@ collect_system_info () {
printf '%s\t%s\n' "${pkg_map[$p]}" "$p"
done)
printf '%s\n' '' "${blue}Getting list of installed system packages$nocolor."
infomsg $'\n'"Getting list of installed system packages."$'\n'
readarray -t installed_packages < <(saferpm -qa --queryformat="%{NAME}\n" "${pkg_map[@]}")
declare -g -A installed_pkg_check installed_pkg_map
@ -419,7 +462,8 @@ collect_system_info () {
# left as a separate excersize to swap out the sig repos.
#sigs_to_swap=()
printf '%s\n' '' "${blue}Getting a list of enabled modules for the system repositories$nocolor."
infomsg '%s' $'\n' \
$'Getting a list of enabled modules for the system repositories.\n'
# Get a list of system enabled modules.
readarray -t enabled_modules < <(
@ -473,9 +517,9 @@ usage() {
generate_rpm_info() {
mkdir /root/convert
printf '%s\n' "${blue}Creating a list of RPMs installed: $1$nocolor"
infomsg "Creating a list of RPMs installed: $1"$'\n'
rpm -qa --qf "%{NAME}|%{VERSION}|%{RELEASE}|%{INSTALLTIME}|%{VENDOR}|%{BUILDTIME}|%{BUILDHOST}|%{SOURCERPM}|%{LICENSE}|%{PACKAGER}\n" | sort > "${convert_info_dir}/$HOSTNAME-rpm-list-$1.log"
printf '%s\n' "${blue}Verifying RPMs installed against RPM database: $1$nocolor" ''
infomsg "Verifying RPMs installed against RPM database: $1"$'\n\n'
rpm -Va | sort -k3 > "${convert_info_dir}/$HOSTNAME-rpm-list-verified-$1.log"
}
@ -516,7 +560,9 @@ EOF
)
if (( ${#check_removed[@]} )); then
printf '%s\n' '' "${blue}Packages found on system that should still be removed. Forcibly removing them with rpm:$nocolor"
infomsg '%s' $'\n' \
"Packages found on system that should still be removed. Forcibly" \
" removing them with rpm:"$'\n'
# Removed packages still found on the system. Forcibly remove them.
for pkg in "${check_removed[@]}"; do
# Extra safety measure, skip if empty string
@ -537,7 +583,9 @@ EOF
} | sort | uniq -u
)
if (( ${#check_installed[@]} )); then
printf '%s\n' '' "${blue}Some required packages were not installed by dnf. Attempting to force with rpm:$nocolor"
infomsg '%s' $'\n' \
"Some required packages were not installed by dnf. Attempting to" \
" force with rpm:"$'\n'
# Get a list of rpm packages to package names
local -A rpm_map
@ -579,9 +627,9 @@ EOF
fi
# Distrosync
printf '%s\n' '' "${blue}Removing dnf cache$nocolor"
infomsg $'\nRemoving dnf cache\n'
rm -rf /var/cache/{yum,dnf}
printf '%s\n' "${blue}Ensuring repos are enabled before the package swap$nocolor"
infomsg $'Ensuring repos are enabled before the package swap\n'
safednf -y config-manager --set-enabled "${!repo_map[@]}" || {
printf '%s\n' 'Repo name missing?'
exit 25
@ -594,23 +642,23 @@ EOF
)
if (( ${#managed_repos[@]} )); then
printf '%s\n' '' "${blue}Disabling subscription managed repos$nocolor."
infomsg $'\nDisabling subscription managed repos\n'
safednf -y config-manager --disable "${managed_repos[@]}"
fi
fi
if (( ${#enabled_modules[@]} )); then
printf '%s\n' "${blue}Enabling modules$nocolor" ''
infomsg $'Enabling modules\n\n'
safednf -y module enable "${enabled_modules[@]}" ||
exit_message "Can't enable modules ${enabled_modules[*]}"
fi
# Make sure that excluded repos are disabled.
printf '%s\n' "${blue}Disabling excluded modules$nocolor" ''
infomsg $'Disabling excluded modules\n\n'
safednf -y module disable "${module_excludes[@]}" ||
exit_message "Can't disable modules ${module_excludes[*]}"
printf '%s\n' '' "${blue}Syncing packages$nocolor" ''
infomsg $'\nSyncing packages\n\n'
dnf -y distro-sync || exit_message "Error during distro-sync."
}
@ -679,7 +727,7 @@ while getopts "hrVR" option; do
verify_all_rpms=true
;;
*)
printf '%s\n' "${errcolor}Invalid switch.$nocolor"
errmsg $'Invalid switch\n'
usage
;;
esac
@ -704,7 +752,7 @@ fi
if [[ $verify_all_rpms && $convert_to_rocky ]]; then
generate_rpm_info finish
printf '%s\n' "${blue}You may review the following files:$nocolor"
infomsg $'You may review the following files:\n'
find /root/convert -type f -name "$HOSTNAME-rpms-*.log"
fi
@ -714,6 +762,6 @@ fi
printf '\n\n\n'
if [[ $convert_to_rocky ]]; then
printf '%s\n' "$blue" "Done, please reboot your system.$nocolor"
infomsg $'\nDone, please reboot your system.\n'
fi
logmessage