Skip to content

Commit

Permalink
harmonize log output
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvinSchiller committed Nov 17, 2023
1 parent b872745 commit aec3f4a
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions installation/includes/02_helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ check_os_type() {
set_optional_service_enablement() {
local service="$1"
local enablement="$2"
local option="$3" # optional, dont't quote in next call!
local option="${3:+$3 }" # optional, dont't quote in next call!

local local_sudo=
if [[ ! $option =~ "--user" ]]; then
if [[ ! ${option} =~ "--user" ]]; then
local_sudo=sudo
fi

if (systemctl $option -all list-unit-files $service | grep -w "$service") ;then
$local_sudo systemctl $option $enablement $service
if (systemctl -all list-unit-files ${option}${service} | grep -w "${service}") ;then
${local_sudo} systemctl ${option}${enablement} ${service}
fi
}

Expand All @@ -82,7 +82,7 @@ verify_files_exist() {

for file in $files
do
test ! -f ${file} && exit_on_error "ERROR: ${file} does not exists or is not a file!"
test ! -f ${file} && exit_on_error "ERROR: '${file}' does not exists or is not a file!"
done
echo "CHECK"
}
Expand All @@ -94,7 +94,7 @@ verify_dirs_exist() {

for dir in $dirs
do
test ! -d ${dir} && exit_on_error "ERROR: ${dir} does not exists or is not a dir!"
test ! -d ${dir} && exit_on_error "ERROR: '${dir}' does not exists or is not a dir!"
done
echo "CHECK"
}
Expand All @@ -110,14 +110,14 @@ verify_files_chmod_chown() {

for file in $files
do
test ! -f ${file} && exit_on_error "ERROR: ${file} does not exists or is not a file!"
test ! -f ${file} && exit_on_error "ERROR: '${file}' does not exists or is not a file!"

mod_actual=$(stat --format '%a' "${file}")
user_actual=$(stat -c '%U' "${file}")
group_actual=$(stat -c '%G' "${file}")
test ! "${mod_expected}" -eq "${mod_actual}" && exit_on_error "ERROR: ${file} actual mod (${mod_actual}) differs from expected (${mod_expected})!"
test ! "${user_expected}" == "${user_actual}" && exit_on_error "ERROR: ${file} actual owner (${user_actual}) differs from expected (${user_expected})!"
test ! "${group_expected}" == "${group_actual}" && exit_on_error "ERROR: ${file} actual group (${group_actual}) differs from expected (${group_expected})!"
test ! "${mod_expected}" -eq "${mod_actual}" && exit_on_error "ERROR: '${file}' actual mod '${mod_actual}' differs from expected '${mod_expected}'!"
test ! "${user_expected}" == "${user_actual}" && exit_on_error "ERROR: '${file}' actual owner '${user_actual}' differs from expected '${user_expected}'!"
test ! "${group_expected}" == "${group_actual}" && exit_on_error "ERROR: '${file}' actual group '${group_actual}' differs from expected '${group_expected}'!"
done
echo "CHECK"
}
Expand All @@ -132,82 +132,82 @@ verify_dirs_chmod_chown() {

for dir in $dirs
do
test ! -d ${dir} && exit_on_error "ERROR: ${dir} does not exists or is not a dir!"
test ! -d ${dir} && exit_on_error "ERROR: '${dir}' does not exists or is not a dir!"

mod_actual=$(stat --format '%a' "${dir}")
user_actual=$(stat -c '%U' "${dir}")
group_actual=$(stat -c '%G' "${dir}")
test ! "${mod_expected}" -eq "${mod_actual}" && exit_on_error "ERROR: ${dir} actual mod (${mod_actual}) differs from expected (${mod_expected})!"
test ! "${user_expected}" == "${user_actual}" && exit_on_error "ERROR: ${dir} actual owner (${user_actual}) differs from expected (${user_expected})!"
test ! "${group_expected}" == "${group_actual}" && exit_on_error "ERROR: ${dir} actual group (${group_actual}) differs from expected (${group_expected})!"
test ! "${mod_expected}" -eq "${mod_actual}" && exit_on_error "ERROR: '${dir}' actual mod '${mod_actual}' differs from expected '${mod_expected}'!"
test ! "${user_expected}" == "${user_actual}" && exit_on_error "ERROR: '${dir}' actual owner '${user_actual}' differs from expected '${user_expected}'!"
test ! "${group_expected}" == "${group_actual}" && exit_on_error "ERROR: '${dir}' actual group '${group_actual}' differs from expected '${group_expected}'!"
done
echo "CHECK"
}

verify_file_contains_string() {
local string="$1"
local file="$2"
echo "Verify '${string}' found in ${file}"
echo "Verify '${string}' found in '${file}'"

if [[ ! $(grep -iw "${string}" "${file}") ]]; then
exit_on_error "ERROR: '${string}' not found in ${file}"
exit_on_error "ERROR: '${string}' not found in '${file}'"
fi
echo "CHECK"
}

verify_file_contains_string_once() {
local string="$1"
local file="$2"
echo "Verify '${string}' found in ${file}"
echo "Verify '${string}' found in '${file}'"

local file_contains_string_count=$(grep -oiw "${string}" "${file}" | wc -l)
if [ "$file_contains_string_count" -lt 1 ]; then
exit_on_error "ERROR: '${string}' not found in ${file}"
exit_on_error "ERROR: '${string}' not found in '${file}'"
elif [ "$file_contains_string_count" -gt 1 ]; then
exit_on_error "ERROR: '${string}' found more than once in ${file}"
exit_on_error "ERROR: '${string}' found more than once in '${file}'"
fi
echo "CHECK"
}

check_service_state() {
local service="$1"
local desired_state="$2"
local option="$3" # optional, dont't quote in next call!
echo "Verify service ${option} ${service} is ${desired_state}"
local option="${3:+$3 }" # optional, dont't quote in next call!
echo "Verify service '${option}${service}' is '${desired_state}'"

local actual_state=$(systemctl is-active ${option} ${service})
local actual_state=$(systemctl is-active ${option}${service})
if [[ ! "${actual_state}" == "${desired_state}" ]]; then
exit_on_error "ERROR: service ${option} ${service} is not ${desired_state} (state: ${actual_state})."
exit_on_error "ERROR: service '${option}${service}' is not '${desired_state}' (state: '${actual_state}')."
fi
echo "CHECK"
}

check_service_enablement() {
local service="$1"
local desired_enablement="$2"
local option="$3" # optional, dont't quote in next call!
echo "Verify service ${option} ${service} is ${desired_enablement}"
local option="${3:+$3 }" # optional, dont't quote in next call!
echo "Verify service ${option}${service} is ${desired_enablement}"

local actual_enablement=$(systemctl is-enabled ${option} ${service})
local actual_enablement=$(systemctl is-enabled ${option}${service})
if [[ ! "${actual_enablement}" == "${desired_enablement}" ]]; then
exit_on_error "ERROR: service ${option} ${service} is not ${desired_enablement} (state: ${actual_enablement})."
exit_on_error "ERROR: service ${option}${service} is not ${desired_enablement} (state: ${actual_enablement})."
fi
echo "CHECK"
}

check_optional_service_enablement() {
local service="$1"
local desired_enablement="$2"
local option="$3" # optional, dont't quote in next call!
echo "Verify service ${option} ${service} is ${desired_enablement}"
local option="${3:+$3 }" # optional, dont't quote in next call!
echo "Verify service ${option}${service} is ${desired_enablement}"

local actual_enablement=$(systemctl is-enabled ${option} ${service}) 2>/dev/null
local actual_enablement=$(systemctl is-enabled ${option}${service}) 2>/dev/null
if [[ -z "${actual_enablement}" ]]; then
echo "INFO: optional service ${service} is not installed."
echo "INFO: optional service ${option}${service} is not installed."
elif [[ "${actual_enablement}" == "static" ]]; then
echo "INFO: optional service ${service} is set static."
echo "INFO: optional service ${option}${service} is set static."
elif [[ ! "${actual_enablement}" == "${desired_enablement}" ]]; then
exit_on_error "ERROR: service ${service} is not ${desired_enablement} (state: ${actual_enablement})."
exit_on_error "ERROR: service ${option}${service} is not ${desired_enablement} (state: ${actual_enablement})."
fi
echo "CHECK"
}
Expand Down

0 comments on commit aec3f4a

Please sign in to comment.