2016-10-18 11:05:48 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# Pi-hole: A black hole for Internet advertisements
|
|
|
|
# (c) 2015, 2016 by Jacob Salmela
|
|
|
|
# Network-wide ad blocking via your Raspberry Pi
|
|
|
|
# http://pi-hole.net
|
2016-11-01 18:33:04 +00:00
|
|
|
# Check Pi-hole core and admin pages versions and determine what
|
|
|
|
# upgrade (if any) is required. Automatically updates and reinstalls
|
|
|
|
# application if update is detected.
|
2016-10-18 11:05:48 +00:00
|
|
|
#
|
|
|
|
# Pi-hole is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
2016-10-18 11:11:02 +00:00
|
|
|
# Variables
|
2016-10-18 13:19:25 +00:00
|
|
|
|
2016-11-02 05:19:40 +00:00
|
|
|
readonly ADMIN_INTERFACE_GIT_URL="https://github.com/pi-hole/AdminLTE.git"
|
|
|
|
readonly ADMIN_INTERFACE_DIR="/var/www/html/admin"
|
|
|
|
readonly PI_HOLE_GIT_URL="https://github.com/pi-hole/pi-hole.git"
|
|
|
|
readonly PI_HOLE_FILES_DIR="/etc/.pihole"
|
2016-10-18 13:19:25 +00:00
|
|
|
|
|
|
|
is_repo() {
|
2016-11-02 13:41:51 +00:00
|
|
|
# Use git to check if directory is currently under VCS, return the value
|
2016-11-02 12:45:29 +00:00
|
|
|
local directory="${1}"
|
2016-11-02 17:51:09 +00:00
|
|
|
|
2016-11-02 13:41:51 +00:00
|
|
|
git -C "${directory}" status --short &> /dev/null
|
2016-11-02 10:38:35 +00:00
|
|
|
return
|
2016-10-18 13:19:25 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 18:18:13 +00:00
|
|
|
prep_repo() {
|
2016-11-02 12:34:39 +00:00
|
|
|
# Prepare directory for local repository building
|
2016-11-02 17:39:27 +00:00
|
|
|
local directory="${1}"
|
2016-11-02 17:51:09 +00:00
|
|
|
|
2016-11-02 17:39:27 +00:00
|
|
|
rm -rf "${directory}" &> /dev/null
|
2016-11-02 13:41:51 +00:00
|
|
|
return
|
2016-11-02 12:34:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
make_repo() {
|
|
|
|
# Remove the non-repod interface and clone the interface
|
2016-11-02 17:51:09 +00:00
|
|
|
local remoteRepo="${2}"
|
|
|
|
local directory="${1}"
|
2016-11-02 12:45:29 +00:00
|
|
|
|
2016-11-02 18:18:13 +00:00
|
|
|
(prep_repo "${directory}" && git clone -q --depth 1 "${remoteRepo}" "${directory}" > /dev/null)
|
2016-11-02 18:01:50 +00:00
|
|
|
return
|
2016-10-18 13:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
update_repo() {
|
2016-11-02 18:09:53 +00:00
|
|
|
local directory="${1}"
|
|
|
|
local retVal=0
|
2016-11-02 12:49:33 +00:00
|
|
|
# Pull the latest commits
|
2016-11-02 17:39:27 +00:00
|
|
|
|
|
|
|
# Stash all files not tracked for later retrieval
|
2016-11-02 18:09:53 +00:00
|
|
|
git -C "${directory}" stash --all --quiet &> /dev/null || ${retVal}=1
|
2016-11-02 17:39:27 +00:00
|
|
|
# Force a clean working directory for cloning
|
2016-11-02 18:09:53 +00:00
|
|
|
git -C "${directory}" clean --force -d &> /dev/null || ${retVal}=1
|
2016-11-02 17:39:27 +00:00
|
|
|
# Fetch latest changes and apply
|
2016-11-02 18:09:53 +00:00
|
|
|
git -C "${directory}" pull --quiet &> /dev/null || ${retVal}=1
|
|
|
|
return ${retVal}
|
2016-10-18 13:19:25 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 05:19:40 +00:00
|
|
|
getGitFiles() {
|
2016-11-02 14:00:43 +00:00
|
|
|
# Setup git repos for directory and repository passed
|
|
|
|
# as arguments 1 and 2
|
|
|
|
local directory="${1}"
|
2016-11-02 18:01:50 +00:00
|
|
|
local remoteRepo="${2}"
|
2016-11-02 14:00:43 +00:00
|
|
|
echo ":::"
|
|
|
|
echo "::: Checking for existing repository..."
|
|
|
|
if is_repo "${directory}"; then
|
2016-11-02 18:09:53 +00:00
|
|
|
echo -n "::: Updating repository in ${directory}..."
|
2016-11-02 17:39:27 +00:00
|
|
|
update_repo "${directory}" || (echo "*** Error: Could not update local repository. Contact support."; exit 1)
|
2016-11-02 18:09:53 +00:00
|
|
|
echo " done!"
|
2016-11-02 14:00:43 +00:00
|
|
|
else
|
2016-11-02 17:51:09 +00:00
|
|
|
echo -n "::: Cloning ${remoteRepo} into ${directory}..."
|
|
|
|
make_repo "${directory}" "${remoteRepo}" || (echo "Unable to clone repository, please contact support"; exit 1)
|
|
|
|
echo " done!"
|
2016-11-02 14:00:43 +00:00
|
|
|
fi
|
2016-11-02 05:19:40 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 08:12:02 +00:00
|
|
|
main() {
|
2016-11-02 18:01:50 +00:00
|
|
|
local pihole_version_current
|
|
|
|
local pihole_version_latest
|
|
|
|
local web_version_current
|
|
|
|
local web_version_latest
|
2016-11-02 08:12:02 +00:00
|
|
|
|
2016-11-02 19:09:33 +00:00
|
|
|
if ! is_repo "${PI_HOLE_FILES_DIR}" || ! is_repo "${ADMIN_INTERFACE_DIR}" ; then #This is unlikely
|
|
|
|
echo "::: Critical Error: One or more Pi-Hole repos are missing from system!"
|
|
|
|
echo "::: Please re-run install script from https://github.com/pi-hole/pi-hole"
|
|
|
|
exit 1;
|
|
|
|
fi
|
2016-11-02 08:12:02 +00:00
|
|
|
|
|
|
|
echo "::: Checking for updates..."
|
2016-11-02 18:39:22 +00:00
|
|
|
# Checks Pi-hole version string in format vX.X.X
|
|
|
|
pihole_version_current="$(/usr/local/bin/pihole version --pihole --current)"
|
|
|
|
pihole_version_latest="$(/usr/local/bin/pihole version --pihole --latest)"
|
|
|
|
web_version_current="$(/usr/local/bin/pihole version --admin --current)"
|
|
|
|
web_version_latest="$(/usr/local/bin/pihole version --admin --latest)"
|
2016-11-02 08:12:02 +00:00
|
|
|
|
2016-11-02 20:06:21 +00:00
|
|
|
if [[ "${pihole_version_latest}" == "-1" || "${web_version_latest}" == "-1" ]]; then
|
2016-11-02 19:07:59 +00:00
|
|
|
echo "*** Unable to contact GitHub for latest version. Please try again later, contact support if this continues."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2016-11-02 08:12:02 +00:00
|
|
|
# Logic
|
|
|
|
# If latest versions are blank - we've probably hit Github rate limit (stop running `pihole -up so often!):
|
|
|
|
# Update anyway
|
|
|
|
# If Core up to date AND web up to date:
|
|
|
|
# Do nothing
|
|
|
|
# If Core up to date AND web NOT up to date:
|
|
|
|
# Pull web repo
|
|
|
|
# If Core NOT up to date AND web up to date:
|
|
|
|
# pull pihole repo, run install --unattended -- reconfigure
|
|
|
|
# if Core NOT up to date AND web NOT up to date:
|
|
|
|
# pull pihole repo run install --unattended
|
|
|
|
|
|
|
|
if [[ "${pihole_version_current}" == "${pihole_version_latest}" ]] && [[ "${web_version_current}" == "${web_version_latest}" ]]; then
|
2016-11-02 14:29:20 +00:00
|
|
|
echo ":::"
|
|
|
|
echo "::: Pi-hole version is $pihole_version_current"
|
|
|
|
echo "::: Web Admin version is $web_version_current"
|
|
|
|
echo ":::"
|
2016-11-02 08:12:02 +00:00
|
|
|
echo "::: Everything is up to date!"
|
2016-11-02 14:29:20 +00:00
|
|
|
exit 0
|
2016-11-02 08:12:02 +00:00
|
|
|
|
2016-11-02 17:42:05 +00:00
|
|
|
elif [[ "${pihole_version_current}" == "${pihole_version_latest}" ]] && [[ "${web_version_current}" < "${web_version_latest}" ]]; then
|
2016-11-02 14:29:20 +00:00
|
|
|
echo ":::"
|
2016-11-02 08:12:02 +00:00
|
|
|
echo "::: Pi-hole Web Admin files out of date"
|
|
|
|
getGitFiles "${ADMIN_INTERFACE_DIR}" "${ADMIN_INTERFACE_GIT_URL}"
|
2016-11-02 14:29:20 +00:00
|
|
|
|
|
|
|
web_updated=true
|
2016-11-02 13:41:51 +00:00
|
|
|
|
2016-11-02 17:42:05 +00:00
|
|
|
elif [[ "${pihole_version_current}" < "${pihole_version_latest}" ]] && [[ "${web_version_current}" == "${web_version_latest}" ]]; then
|
2016-11-02 08:12:02 +00:00
|
|
|
echo "::: Pi-hole core files out of date"
|
|
|
|
getGitFiles "${PI_HOLE_FILES_DIR}" "${PI_HOLE_GIT_URL}"
|
|
|
|
/etc/.pihole/automated\ install/basic-install.sh --reconfigure --unattended || echo "Unable to complete update, contact Pi-hole" && exit 1
|
2016-11-02 14:29:20 +00:00
|
|
|
core_updated=true
|
2016-11-02 13:41:51 +00:00
|
|
|
|
2016-11-02 17:42:05 +00:00
|
|
|
elif [[ "${pihole_version_current}" < "${pihole_version_latest}" ]] && [[ "${web_version_current}" < "${web_version_latest}" ]]; then
|
2016-11-02 08:12:02 +00:00
|
|
|
echo "::: Updating Everything"
|
|
|
|
getGitFiles "${PI_HOLE_FILES_DIR}" "${PI_HOLE_GIT_URL}"
|
|
|
|
/etc/.pihole/automated\ install/basic-install.sh --unattended || echo "Unable to complete update, contact Pi-hole" && exit 1
|
2016-11-02 14:29:20 +00:00
|
|
|
web_updated=true
|
|
|
|
core_updated=true
|
2016-11-02 19:07:59 +00:00
|
|
|
else
|
|
|
|
echo "*** Update script has malfunctioned, fallthrough reached. Please contact support"
|
|
|
|
exit 1
|
2016-11-02 13:41:51 +00:00
|
|
|
fi
|
2016-11-02 14:29:20 +00:00
|
|
|
|
|
|
|
if [[ "${web_updated}" == true ]]; then
|
2016-11-02 19:07:59 +00:00
|
|
|
web_version_current="$(/usr/local/bin/pihole version --admin --current)"
|
2016-11-02 08:12:02 +00:00
|
|
|
echo ":::"
|
2016-11-02 13:41:51 +00:00
|
|
|
echo "::: Web Admin version is now at ${web_version_current}"
|
|
|
|
echo "::: If you had made any changes in '/var/www/html/admin/', they have been stashed using 'git stash'"
|
2016-11-02 14:29:20 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "${core_updated}" == true ]]; then
|
2016-11-02 19:07:59 +00:00
|
|
|
pihole_version_current="$(/usr/local/bin/pihole version --pihole --current)"
|
2016-11-02 14:29:20 +00:00
|
|
|
echo ":::"
|
|
|
|
echo "::: Pi-hole version is now at ${pihole_version_current}"
|
|
|
|
echo "::: If you had made any changes in '/etc/.pihole/', they have been stashed using 'git stash'"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo ""
|
|
|
|
exit 0
|
|
|
|
|
2016-11-02 08:12:02 +00:00
|
|
|
}
|
2016-11-02 07:51:38 +00:00
|
|
|
|
2016-11-02 08:12:02 +00:00
|
|
|
main
|