mirror of
https://github.com/pi-hole/pi-hole.git
synced 2024-12-25 06:10:20 +00:00
condense repetitive code into functions
This commit is contained in:
parent
d51e0c49b1
commit
2fea5d428d
1 changed files with 66 additions and 117 deletions
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/env bash
|
check_critical_program_versions#!/usr/bin/env bash
|
||||||
# Pi-hole: A black hole for Internet advertisements
|
# Pi-hole: A black hole for Internet advertisements
|
||||||
# (c) 2017 Pi-hole, LLC (https://pi-hole.net)
|
# (c) 2017 Pi-hole, LLC (https://pi-hole.net)
|
||||||
# Network-wide ad blocking via your own hardware.
|
# Network-wide ad blocking via your own hardware.
|
||||||
|
@ -133,48 +133,57 @@ if_directory_exists() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Checks the core version of the Pi-hole codebase
|
compare_local_version_to_git_version() {
|
||||||
check_core_version() {
|
# The git directory to check
|
||||||
echo_current_diagnostic "Pi-hole versions"
|
local git_dir="${1}"
|
||||||
|
# The named component of the project (Core or Web)
|
||||||
|
local pihole_component="${2}"
|
||||||
|
# If we are checking the Core versions,
|
||||||
|
if [[ "${pihole_component}" == "Core" ]]; then
|
||||||
|
# We need to search for "Pi-hole" when using pihole -v
|
||||||
|
local search_term="Pi-hole"
|
||||||
|
elif [[ "${pihole_component}" == "Web" ]]; then
|
||||||
|
local search_term="AdminLTE"
|
||||||
|
fi
|
||||||
|
# Display what we are checking
|
||||||
|
echo_current_diagnostic "${pihole_component} version"
|
||||||
# Store the error message in a variable in case we want to change and/or reuse it
|
# Store the error message in a variable in case we want to change and/or reuse it
|
||||||
local error_msg="git status failed"
|
local error_msg="git status failed"
|
||||||
# If the pihole git directory exists,
|
# If the pihole git directory exists,
|
||||||
if_directory_exists "${PIHOLEGITDIR}" && \
|
if_directory_exists "${git_dir}" && \
|
||||||
# move into it
|
# move into it
|
||||||
cd "${PIHOLEGITDIR}" || \
|
cd "${git_dir}" || \
|
||||||
# if not, report an error
|
# If not, show an error
|
||||||
log_write "pihole repo does not exist"
|
log_write "${COL_LIGHT_RED}Could not cd into ${git_dir}$COL_NC"
|
||||||
# If the git status command completes successfully,
|
|
||||||
# we can assume we can get the information we want
|
|
||||||
if git status &> /dev/null; then
|
if git status &> /dev/null; then
|
||||||
# The current version the user is on
|
# The current version the user is on
|
||||||
PI_HOLE_VERSION=$(git describe --tags --abbrev=0);
|
local remote_version=$(git describe --tags --abbrev=0);
|
||||||
# What branch they are on
|
# What branch they are on
|
||||||
PI_HOLE_BRANCH=$(git rev-parse --abbrev-ref HEAD);
|
local remote_branch=$(git rev-parse --abbrev-ref HEAD);
|
||||||
# The commit they are on
|
# The commit they are on
|
||||||
PI_HOLE_COMMIT=$(git describe --long --dirty --tags --always)
|
local remote_commit=$(git describe --long --dirty --tags --always)
|
||||||
# echo this information out to the user in a nice format
|
# echo this information out to the user in a nice format
|
||||||
# If the current version matches what pihole -v produces, the user is up-to-date
|
# If the current version matches what pihole -v produces, the user is up-to-date
|
||||||
if [[ "${PI_HOLE_VERSION}" == "$(pihole -v | awk '/Pi-hole/ {print $6}' | cut -d ')' -f1)" ]]; then
|
if [[ "${remote_version}" == "$(pihole -v | awk '/${search_term}/ {print $6}' | cut -d ')' -f1)" ]]; then
|
||||||
log_write "${TICK} Core: ${COL_LIGHT_GREEN}${PI_HOLE_VERSION}${COL_NC}"
|
log_write "${TICK} ${pihole_component}: ${COL_LIGHT_GREEN}${remote_version}${COL_NC}"
|
||||||
# If not,
|
# If not,
|
||||||
else
|
else
|
||||||
# echo the current version in yellow, signifying it's something to take a look at, but not a critical error
|
# echo the current version in yellow, signifying it's something to take a look at, but not a critical error
|
||||||
# Also add a URL to an FAQ
|
# Also add a URL to an FAQ
|
||||||
log_write "${INFO} Core: ${COL_YELLOW}${PI_HOLE_VERSION:-Untagged}${COL_NC} (${COL_CYAN}${FAQ_UPDATE_PI_HOLE}${COL_NC})"
|
log_write "${INFO} ${pihole_component}: ${COL_YELLOW}${remote_version:-Untagged}${COL_NC} (${COL_CYAN}${FAQ_UPDATE_PI_HOLE}${COL_NC})"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# If the repo is on the master branch, they are on the stable codebase
|
# If the repo is on the master branch, they are on the stable codebase
|
||||||
if [[ "${PI_HOLE_BRANCH}" == "master" ]]; then
|
if [[ "${remote_branch}" == "master" ]]; then
|
||||||
# so the color of the text is green
|
# so the color of the text is green
|
||||||
log_write "${INFO} Branch: ${COL_LIGHT_GREEN}${PI_HOLE_BRANCH}${COL_NC}"
|
log_write "${INFO} Branch: ${COL_LIGHT_GREEN}${remote_branch}${COL_NC}"
|
||||||
# If it is any other branch, they are in a developement branch
|
# If it is any other branch, they are in a developement branch
|
||||||
else
|
else
|
||||||
# So show that in yellow, signifying it's something to take a look at, but not a critical error
|
# So show that in yellow, signifying it's something to take a look at, but not a critical error
|
||||||
log_write "${INFO} Branch: ${COL_YELLOW}${PI_HOLE_BRANCH:-Detached}${COL_NC} (${COL_CYAN}${FAQ_CHECKOUT_COMMAND}${COL_NC})"
|
log_write "${INFO} Branch: ${COL_YELLOW}${remote_branch:-Detached}${COL_NC} (${COL_CYAN}${FAQ_CHECKOUT_COMMAND}${COL_NC})"
|
||||||
fi
|
fi
|
||||||
# echo the current commit
|
# echo the current commit
|
||||||
log_write "${INFO} Commit: ${PI_HOLE_COMMIT}\n"
|
log_write "${INFO} Commit: ${remote_commit}"
|
||||||
# If git status failed,
|
# If git status failed,
|
||||||
else
|
else
|
||||||
# Return an error message
|
# Return an error message
|
||||||
|
@ -182,123 +191,65 @@ check_core_version() {
|
||||||
# and exit with a non zero code
|
# and exit with a non zero code
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
|
||||||
|
|
||||||
check_web_version() {
|
|
||||||
# Local variable for the error message
|
|
||||||
local error_msg="git status failed"
|
|
||||||
# If the directory exists,
|
|
||||||
if_directory_exists "${ADMINGITDIR}" && \
|
|
||||||
# move into it
|
|
||||||
cd "${ADMINGITDIR}" || \
|
|
||||||
# if not, give an error message
|
|
||||||
log_write "repo does not exist"
|
|
||||||
# If the git status command completes successfully,
|
|
||||||
# we can assume we can get the information we want
|
|
||||||
if git status &> /dev/null; then
|
|
||||||
# The current version the user is on
|
|
||||||
WEB_VERSION=$(git describe --tags --abbrev=0);
|
|
||||||
# What branch they are on
|
|
||||||
WEB_BRANCH=$(git rev-parse --abbrev-ref HEAD);
|
|
||||||
# The commit they are on
|
|
||||||
WEB_COMMIT=$(git describe --long --dirty --tags --always)
|
|
||||||
# If the Web version reported by pihole -v matches the current version
|
|
||||||
if [[ "${WEB_VERSION}" == "$(pihole -v | awk '/AdminLTE/ {print $6}' | cut -d ')' -f1)" ]]; then
|
|
||||||
# echo it in green
|
|
||||||
log_write "${TICK} Web: ${COL_LIGHT_GREEN}${WEB_VERSION}${COL_NC}"
|
|
||||||
# Otherwise,
|
|
||||||
else
|
|
||||||
# Show it in yellow with a link to update Pi-hole
|
|
||||||
log_write "${INFO} Web: ${COL_YELLOW}${WEB_VERSION:-Untagged}${COL_NC} (${COL_CYAN}${FAQ_UPDATE_PI_HOLE}${COL_NC})"
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
# If the repo is on the master branch, they are on the stable codebase
|
|
||||||
if [[ "${WEB_BRANCH}" == "master" ]]; then
|
|
||||||
# so the color of the text is green
|
|
||||||
log_write "${TICK} Branch: ${COL_LIGHT_GREEN}${WEB_BRANCH}${COL_NC}"
|
|
||||||
else
|
|
||||||
# If it is any other branch, they are in a developement branch
|
|
||||||
# So show that in yellow, signifying it's something to take a look at, but not a critical error
|
|
||||||
log_write "${INFO} Branch: ${COL_YELLOW}${WEB_BRANCH:-Detached}${COL_NC} (${COL_CYAN}${FAQ_CHECKOUT_COMMAND}${COL_NC})"
|
|
||||||
fi
|
|
||||||
# echo the current commit
|
|
||||||
log_write "${INFO} Commit: ${WEB_COMMIT}\n"
|
|
||||||
# If git status failed,
|
|
||||||
else
|
|
||||||
# Return an error message
|
|
||||||
log_write "${error_msg}"
|
|
||||||
# and exit with a non zero code
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
check_ftl_version() {
|
check_ftl_version() {
|
||||||
|
local ftl_name="FTL"
|
||||||
|
echo_current_diagnostic "${ftl_name} version"
|
||||||
# Use the built in command to check FTL's version
|
# Use the built in command to check FTL's version
|
||||||
FTL_VERSION=$(pihole-FTL version)
|
FTL_VERSION=$(pihole-FTL version)
|
||||||
# Compare the current FTL version to the remote version
|
# Compare the current FTL version to the remote version
|
||||||
if [[ "${FTL_VERSION}" == "$(pihole -v | awk '/FTL/ {print $6}' | cut -d ')' -f1)" ]]; then
|
if [[ "${FTL_VERSION}" == "$(pihole -v | awk '/FTL/ {print $6}' | cut -d ')' -f1)" ]]; then
|
||||||
# If they are the same, FTL is up-to-date
|
# If they are the same, FTL is up-to-date
|
||||||
log_write "${TICK} FTL: ${COL_LIGHT_GREEN}${FTL_VERSION}${COL_NC}"
|
log_write "${TICK} ${ftl_name}: ${COL_LIGHT_GREEN}${FTL_VERSION}${COL_NC}"
|
||||||
else
|
else
|
||||||
# If not, show it in yellow, signifying there is an update
|
# If not, show it in yellow, signifying there is an update
|
||||||
log_write "${TICK} FTL: ${COL_YELLOW}${FTL_VERSION}${COL_NC}"
|
log_write "${TICK} ${ftl_name}: ${COL_YELLOW}${FTL_VERSION}${COL_NC}"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check the current version of the Web server
|
# Checks the core version of the Pi-hole codebase
|
||||||
check_web_server_version() {
|
check_component_versions() {
|
||||||
# Store the name in a variable in case we ever want to change it
|
# Check the Web version, branch, and commit
|
||||||
WEB_SERVER="lighttpd"
|
compare_local_version_to_git_version "${PIHOLEGITDIR}" "Core"
|
||||||
# Parse out just the version number
|
# Check the Web version, branch, and commit
|
||||||
WEB_SERVER_VERSON="$(lighttpd -v |& head -n1 | cut -d '/' -f2 | cut -d ' ' -f1)"
|
compare_local_version_to_git_version "${ADMINGITDIR}" "Web"
|
||||||
|
# Check the FTL version
|
||||||
|
check_ftl_version
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
get_program_version() {
|
||||||
|
local program_name="${1}"
|
||||||
|
local program_version
|
||||||
|
echo_current_diagnostic "${program_name} version"
|
||||||
|
case "${program_name}" in
|
||||||
|
"lighttpd") program_version="$(${program_name} -v |& head -n1 | cut -d '/' -f2 | cut -d ' ' -f1)"
|
||||||
|
;;
|
||||||
|
"dnsmasq") program_version="$(${program_name} -v |& head -n1 | awk '{print $3}')"
|
||||||
|
;;
|
||||||
|
"php") program_version="$(${program_name} -v |& head -n1 | cut -d '-' -f1 | cut -d ' ' -f2)"
|
||||||
|
;;
|
||||||
|
*) echo "Unrecognized program";
|
||||||
|
esac
|
||||||
# If the Web server does not have a version (the variable is empty)
|
# If the Web server does not have a version (the variable is empty)
|
||||||
if [[ -z "${WEB_SERVER_VERSON}" ]]; then
|
if [[ -z "${program_version}" ]]; then
|
||||||
# Display and error
|
# Display and error
|
||||||
log_write "${CROSS} ${COL_LIGHT_RED}${WEB_SERVER} version could not be detected.${COL_NC}"
|
log_write "${CROSS} ${COL_LIGHT_RED}${program_name} version could not be detected.${COL_NC}"
|
||||||
else
|
else
|
||||||
# Otherwise, display the version
|
# Otherwise, display the version
|
||||||
log_write "${TICK} ${WEB_SERVER}: ${WEB_SERVER_VERSON}"
|
log_write "${TICK} ${program_name}: ${program_version}"
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Check the current version of the DNS server
|
|
||||||
check_resolver_server_version() {
|
|
||||||
# Store the name in a variable in case we ever want to change it
|
|
||||||
RESOLVER="dnsmasq"
|
|
||||||
# Parse out just the version number
|
|
||||||
RESOVLER_VERSON="$(dnsmasq -v |& head -n1 | awk '{print $3}')"
|
|
||||||
# If the DNS server does not have a version (the variable is empty)
|
|
||||||
if [[ -z "${RESOVLER_VERSON}" ]]; then
|
|
||||||
# Display and error
|
|
||||||
log_write "${CROSS} ${COL_LIGHT_RED}${RESOLVER} version could not be detected.${COL_NC}"
|
|
||||||
else
|
|
||||||
# Otherwise, display the version
|
|
||||||
log_write "${TICK} ${RESOLVER}: ${RESOVLER_VERSON}"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
check_php_version() {
|
|
||||||
# Parse out just the version number
|
|
||||||
PHP_VERSION=$(php -v |& head -n1 | cut -d '-' -f1 | cut -d ' ' -f2)
|
|
||||||
# If no version is detected,
|
|
||||||
if [[ -z "${PHP_VERSION}" ]]; then
|
|
||||||
# show an error
|
|
||||||
log_write "${CROSS} ${COL_LIGHT_RED}PHP version could not be detected.${COL_NC}"
|
|
||||||
else
|
|
||||||
# Otherwise, show the version
|
|
||||||
log_write "${TICK} PHP: ${PHP_VERSION}"
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# These are the most critical dependencies of Pi-hole, so we check for them
|
# These are the most critical dependencies of Pi-hole, so we check for them
|
||||||
# and their versions, using the functions above.
|
# and their versions, using the functions above.
|
||||||
check_critical_dependencies() {
|
check_critical_program_versions() {
|
||||||
echo_current_diagnostic "Versions of critical dependencies"
|
|
||||||
# Use the function created earlier and bundle them into one function that checks all the version numbers
|
# Use the function created earlier and bundle them into one function that checks all the version numbers
|
||||||
check_web_server_version
|
get_program_version "dnsmasq"
|
||||||
check_resolver_server_version
|
get_program_version "lighttpd"
|
||||||
check_php_version
|
get_program_version "php"
|
||||||
}
|
}
|
||||||
|
|
||||||
get_distro_attributes() {
|
get_distro_attributes() {
|
||||||
|
@ -877,9 +828,8 @@ upload_to_tricorder() {
|
||||||
# Run through all the functions we made
|
# Run through all the functions we made
|
||||||
make_temporary_log
|
make_temporary_log
|
||||||
initiate_debug
|
initiate_debug
|
||||||
check_core_version
|
check_component_versions
|
||||||
check_web_version
|
check_critical_program_versions
|
||||||
check_ftl_version
|
|
||||||
# setupVars.conf needs to be sourced before the networking so the values are
|
# setupVars.conf needs to be sourced before the networking so the values are
|
||||||
# available to the check_networking function
|
# available to the check_networking function
|
||||||
diagnose_setup_variables
|
diagnose_setup_variables
|
||||||
|
@ -889,7 +839,6 @@ check_networking
|
||||||
check_name_resolution
|
check_name_resolution
|
||||||
process_status
|
process_status
|
||||||
check_x_headers
|
check_x_headers
|
||||||
check_critical_dependencies
|
|
||||||
analyze_gravity_list
|
analyze_gravity_list
|
||||||
check_dnsmasq_d
|
check_dnsmasq_d
|
||||||
check_lighttpd_d
|
check_lighttpd_d
|
||||||
|
|
Loading…
Reference in a new issue