mirror of
https://github.com/pi-hole/pi-hole.git
synced 2024-11-15 10:43:55 +00:00
Merge pull request #1033 from pi-hole/updater_rewrite
update.sh code refactoring
This commit is contained in:
commit
fcdd58ac94
1 changed files with 53 additions and 35 deletions
|
@ -30,7 +30,7 @@ is_repo() {
|
||||||
git status --short &> /dev/null
|
git status --short &> /dev/null
|
||||||
rc=$?
|
rc=$?
|
||||||
cd "${curdir}" &> /dev/null || return 1
|
cd "${curdir}" &> /dev/null || return 1
|
||||||
return $rc
|
return "${rc}"
|
||||||
}
|
}
|
||||||
|
|
||||||
prep_repo() {
|
prep_repo() {
|
||||||
|
@ -46,26 +46,24 @@ make_repo() {
|
||||||
local remoteRepo="${2}"
|
local remoteRepo="${2}"
|
||||||
local directory="${1}"
|
local directory="${1}"
|
||||||
|
|
||||||
(prep_repo "${directory}" && git clone -q --depth 1 "${remoteRepo}" "${directory}" > /dev/null)
|
(prep_repo "${directory}" && git clone -q --depth 1 "${remoteRepo}" "${directory}")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
update_repo() {
|
update_repo() {
|
||||||
local directory="${1}"
|
local directory="${1}"
|
||||||
local curdir
|
local curdir
|
||||||
# Pull the latest commits
|
|
||||||
|
|
||||||
curdir="${PWD}"
|
curdir="${PWD}"
|
||||||
cd "${directory}" &> /dev/null || return 1
|
cd "${directory}" &> /dev/null || return 1
|
||||||
|
# Pull the latest commits
|
||||||
# Stash all files not tracked for later retrieval
|
# Stash all files not tracked for later retrieval
|
||||||
git stash --all --quiet &> /dev/null
|
git stash --all --quiet
|
||||||
# Force a clean working directory for cloning
|
# Force a clean working directory for cloning
|
||||||
git clean --force -d &> /dev/null
|
git clean --force -d
|
||||||
# Fetch latest changes and apply
|
# Fetch latest changes and apply
|
||||||
git pull --quiet &> /dev/null
|
git pull --quiet
|
||||||
cd "${curdir}" &> /dev/null || return 1
|
cd "${curdir}" &> /dev/null || return 1
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getGitFiles() {
|
getGitFiles() {
|
||||||
|
@ -86,33 +84,59 @@ getGitFiles() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GitCheckUpdateAvail() {
|
||||||
|
local directory="${1}"
|
||||||
|
curdir=$PWD;
|
||||||
|
cd "${directory}"
|
||||||
|
|
||||||
|
# Fetch latest changes in this repo
|
||||||
|
git fetch --quiet origin
|
||||||
|
status="$(git status -sb)"
|
||||||
|
|
||||||
|
# Change back to original directory
|
||||||
|
cd "${curdir}"
|
||||||
|
|
||||||
|
if [[ $status == *"behind"* ]]; then
|
||||||
|
# Local branch is behind remote branch -> Update
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
# Local branch is up-to-date or in a situation
|
||||||
|
# where this updater cannot be used (like on a
|
||||||
|
# branch that exists only locally)
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
local pihole_version_current
|
local pihole_version_current
|
||||||
local pihole_version_latest
|
|
||||||
local web_version_current
|
local web_version_current
|
||||||
local web_version_latest
|
|
||||||
|
|
||||||
if ! is_repo "${PI_HOLE_FILES_DIR}" || ! is_repo "${ADMIN_INTERFACE_DIR}" ; then #This is unlikely
|
#This is unlikely
|
||||||
|
if ! is_repo "${PI_HOLE_FILES_DIR}" || ! is_repo "${ADMIN_INTERFACE_DIR}" ; then
|
||||||
echo "::: Critical Error: One or more Pi-Hole repos are missing from system!"
|
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"
|
echo "::: Please re-run install script from https://github.com/pi-hole/pi-hole"
|
||||||
exit 1;
|
exit 1;
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "::: Checking for updates..."
|
echo "::: Checking for updates..."
|
||||||
# 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)"
|
|
||||||
|
|
||||||
if [[ "${pihole_version_latest}" == "-1" || "${web_version_latest}" == "-1" ]]; then
|
if GitCheckUpdateAvail "${PI_HOLE_FILES_DIR}" ; then
|
||||||
echo "*** Unable to contact GitHub for latest version. Please try again later, contact support if this continues."
|
core_update=true
|
||||||
exit 1
|
echo "::: Pi-hole Core: update available"
|
||||||
|
else
|
||||||
|
core_update=false
|
||||||
|
echo "::: Pi-hole Core: up to date"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if GitCheckUpdateAvail "${ADMIN_INTERFACE_DIR}" ; then
|
||||||
|
web_update=true
|
||||||
|
echo "::: Web Interface: update available"
|
||||||
|
else
|
||||||
|
web_update=false
|
||||||
|
echo "::: Web Interface: up to date"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Logic
|
# 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:
|
# If Core up to date AND web up to date:
|
||||||
# Do nothing
|
# Do nothing
|
||||||
# If Core up to date AND web NOT up to date:
|
# If Core up to date AND web NOT up to date:
|
||||||
|
@ -122,46 +146,40 @@ main() {
|
||||||
# if Core NOT up to date AND web NOT up to date:
|
# if Core NOT up to date AND web NOT up to date:
|
||||||
# pull pihole repo run install --unattended
|
# pull pihole repo run install --unattended
|
||||||
|
|
||||||
if [[ "${pihole_version_current}" == "${pihole_version_latest}" ]] && [[ "${web_version_current}" == "${web_version_latest}" ]]; then
|
if ! ${core_update} && ! ${web_update} ; then
|
||||||
echo ":::"
|
|
||||||
echo "::: Pi-hole version is $pihole_version_current"
|
|
||||||
echo "::: Web Admin version is $web_version_current"
|
|
||||||
echo ":::"
|
echo ":::"
|
||||||
echo "::: Everything is up to date!"
|
echo "::: Everything is up to date!"
|
||||||
exit 0
|
exit 0
|
||||||
|
|
||||||
elif [[ "${pihole_version_current}" == "${pihole_version_latest}" ]] && [[ "${web_version_current}" < "${web_version_latest}" ]]; then
|
elif ! ${core_update} && ${web_update} ; then
|
||||||
echo ":::"
|
echo ":::"
|
||||||
echo "::: Pi-hole Web Admin files out of date"
|
echo "::: Pi-hole Web Admin files out of date"
|
||||||
getGitFiles "${ADMIN_INTERFACE_DIR}" "${ADMIN_INTERFACE_GIT_URL}"
|
getGitFiles "${ADMIN_INTERFACE_DIR}" "${ADMIN_INTERFACE_GIT_URL}"
|
||||||
|
|
||||||
web_updated=true
|
elif ${core_update} && ! ${web_update} ; then
|
||||||
|
echo ":::"
|
||||||
elif [[ "${pihole_version_current}" < "${pihole_version_latest}" ]] && [[ "${web_version_current}" == "${web_version_latest}" ]]; then
|
|
||||||
echo "::: Pi-hole core files out of date"
|
echo "::: Pi-hole core files out of date"
|
||||||
getGitFiles "${PI_HOLE_FILES_DIR}" "${PI_HOLE_GIT_URL}"
|
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
|
/etc/.pihole/automated\ install/basic-install.sh --reconfigure --unattended || echo "Unable to complete update, contact Pi-hole" && exit 1
|
||||||
core_updated=true
|
|
||||||
|
|
||||||
elif [[ "${pihole_version_current}" < "${pihole_version_latest}" ]] && [[ "${web_version_current}" < "${web_version_latest}" ]]; then
|
elif ${core_update} && ${web_update} ; then
|
||||||
|
echo ":::"
|
||||||
echo "::: Updating Everything"
|
echo "::: Updating Everything"
|
||||||
getGitFiles "${PI_HOLE_FILES_DIR}" "${PI_HOLE_GIT_URL}"
|
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
|
/etc/.pihole/automated\ install/basic-install.sh --unattended || echo "Unable to complete update, contact Pi-hole" && exit 1
|
||||||
web_updated=true
|
|
||||||
core_updated=true
|
|
||||||
else
|
else
|
||||||
echo "*** Update script has malfunctioned, fallthrough reached. Please contact support"
|
echo "*** Update script has malfunctioned, fallthrough reached. Please contact support"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "${web_updated}" == true ]]; then
|
if [[ "${web_update}" == true ]]; then
|
||||||
web_version_current="$(/usr/local/bin/pihole version --admin --current)"
|
web_version_current="$(/usr/local/bin/pihole version --admin --current)"
|
||||||
echo ":::"
|
echo ":::"
|
||||||
echo "::: Web Admin version is now at ${web_version_current}"
|
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'"
|
echo "::: If you had made any changes in '/var/www/html/admin/', they have been stashed using 'git stash'"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "${core_updated}" == true ]]; then
|
if [[ "${core_update}" == true ]]; then
|
||||||
pihole_version_current="$(/usr/local/bin/pihole version --pihole --current)"
|
pihole_version_current="$(/usr/local/bin/pihole version --pihole --current)"
|
||||||
echo ":::"
|
echo ":::"
|
||||||
echo "::: Pi-hole version is now at ${pihole_version_current}"
|
echo "::: Pi-hole version is now at ${pihole_version_current}"
|
||||||
|
|
Loading…
Reference in a new issue