recipe_gentoo/recipes/common_functions.sh

162 lines
4.9 KiB
Bash

# Function to check if value is well configured in conf file
#
# Parameters
# - 1 : name of service (ex : SSH)
# - 2 : config file (ex : /etc/ssh/sshd_config)
# - 3 : name of param key (ex : ListenAddress)
# - 4 : expected pattern to search without start and end spaces, can be multiple (ex : ${IPV4_ADMIN_LAN_IP} ${IPV6_ADMIN_LAN_IP})
# Note for 4th parameter, if there is a space in one param, you can use regex like [[:space::]]
#
# Return = 0 -> OK value is set
# Return = 1 -> Error (wrong number of param or other)
# Return = 2 -> Unexpected value is set
# Return = 3 -> All expected values are NOT configured
check_value_in_conf_file () {
# Check the number of parameters
if [ ${#} -ne 4 ]
then
echo -e "${RED}ERROR : when call check_value_in_conf_file function, bad parameters number expected : 4.${NC}\n"
return 1
fi
# Check if on param is empty or just contains space(s)
if [[ -z "${1// }" ]] || [[ -z "${2// }" ]] || [[ -z "${3// }" ]] || [[ -z "${4// }" ]]
then
echo -e "${RED}ERROR : at least one parameter is empty, please provide all required params.${NC}\n"
return 1
fi
# Get parameters and delete 'start' en 'end' spaces. For name conf_file
local name=$(echo -e ${1} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | sed -e 's/[[:space:]]/[[:space:]]*/g')
local conf_file=$(echo -e ${2} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | sed -e 's/[[:space:]]/[[:space:]]*/g')
local param_key=$(echo -e ${3} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | sed -e 's/[[:space:]]/[[:space:]]*/g')
local search_ok=$(echo -e ${4} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
# Check if conf file exist
if [[ ! -f "${conf_file}" ]]
then
echo -e "${RED}ERROR : file ${conf_file} NOT FOUND.${NC}\n"
return 1
fi
# Print message to explain this check
echo -e "Check ${BLUE}${name} ${param_key}${NC} config file ${conf_file} ..."
# If multiple expected values
local list_search_ok=(${search_ok})
local search_ok_to_grep="^[[:space:]]*"${param_key}"[[:space:]]*:\?=\?[[:space:]]*"${search_ok}"[[:space:]]*$"
# If there is more than one unexpected values
if [ ${#list_search_ok[@]} -gt 1 ]
then
search_ok_to_grep=""
for var_search_ok in ${list_search_ok[@]}
do
search_ok_to_grep=${search_ok_to_grep}'-e '"^[[:space:]]*${param_key}[[:space:]]*:\?=\?[[:space:]]*${var_search_ok}[[:space:]]*$"' '
done
fi
# Check if unexpected value is set (grep -v) (success if return code = 1)
grep "^[[:space:]]*"${param_key} ${conf_file} | grep -q -v ${search_ok_to_grep}
# Return Code
local RC=${?}
# Unexpected value is set
if [ ${RC} -eq 0 ]
then
echo -e "${RED}Service ${name} has BAD CONFIGURATION for ${param_key} : check KO${NC}\n"
return 2
# Unexpected value is not set
else
search_ok_to_grep="^[[:space:]]*"${param_key}"[[:space:]]*:\?=\?[[:space:]]*"${search_ok}"[[:space:]]*$"
# Boolean for final Return Code, if one return code in the loop -ne 0 -> set finalRC=1
local finalRC=0
# If there is more than one OK values
if [ ${#list_search_ok[@]} -gt 1 ]
then
for var_search_ok in ${list_search_ok[@]}
do
grep -q "^[[:space:]]*"${param_key}"[[:space:]]*:\?=\?[[:space:]]*"${var_search_ok}"[[:space:]]*$" ${conf_file}
RC=${?}
if [ ${RC} -ne 0 ]
then
finalRC=1
fi
done
# Else : do classical check
else
# Check if OK value is set (success if return code = 0)
grep -q "^[[:space:]]*"${param_key}"[[:space:]]*:\?=\?[[:space:]]*"${search_ok}"[[:space:]]*$" ${conf_file}
# Return Code
finalRC=${?}
fi
# All expected values are not configured
if [ ${finalRC} -ne 0 ]
then
echo -e "${RED}Service ${name} has BAD CONFIGURATION for ${param_key} : check KO${NC}\n"
return 3
# OK value is set
else
echo -e "${GREEN}Service ${name} has GOOD CONFIGURATION for ${param_key} : check OK${NC}\n"
return 0
fi
fi
}
# Function to print title
#
# Parameter
# - 1 : title to print (ex : SSH)
#
# Return = 0 -> OK value is set
# Return = 1 -> Error (wrong number of param or other)
print_config_title () {
# Check the number of parameters
if [ ${#} -ne 1 ]
then
echo -e "ERROR : when call print_config_title function, one parameter (only one) expected.\n"
return 1
fi
# Get title param
title=${1}
basic_len=48
# Find number of "-" for title
title_len=$(echo -n " ${title} " | wc -c)
modulo2=$((${title_len} % 2))
# Echo title with 48 chars
echo "------------------------------------------------"
# If title_len <= 48
if [ ${title_len} -lt ${basic_len} ]
then
final_left_len=$(( (${basic_len}-(${title_len}-${modulo2}))/2))
final_right_len=$(( ${final_left_len}-${modulo2} ))
printf %${final_left_len}s | tr " " "-"
echo -n -e " ${BLUE}${title}${NC} "
printf %${final_right_len}s | tr " " "-"
else
echo -e -n " ${BLUE}CHECK ${title} CONFIG${NC}"
fi
echo -e "\n------------------------------------------------\n"
return 0
}