From 0276c72fe2bf9ed80cfbe923437d3f282c310bb4 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 24 Dec 2016 15:05:57 +0100 Subject: [PATCH 01/11] replace 'git -C' with long version (see #1009) --- advanced/Scripts/update.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) mode change 100644 => 100755 advanced/Scripts/update.sh diff --git a/advanced/Scripts/update.sh b/advanced/Scripts/update.sh old mode 100644 new mode 100755 index 10728cd8..929d8a25 --- a/advanced/Scripts/update.sh +++ b/advanced/Scripts/update.sh @@ -23,8 +23,8 @@ is_repo() { # Use git to check if directory is currently under VCS, return the value local directory="${1}" - git -C "${directory}" status --short &> /dev/null - return + curdir=$PWD; cd $directory; git status --short &> /dev/null; rc=$?; cd $curdir + return $rc } prep_repo() { From b020010f0daa191b419ae4765c03be851dd017f4 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 24 Dec 2016 15:08:30 +0100 Subject: [PATCH 02/11] Removed some of the &> /dev/null --- advanced/Scripts/update.sh | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/advanced/Scripts/update.sh b/advanced/Scripts/update.sh index 929d8a25..e5fd1526 100755 --- a/advanced/Scripts/update.sh +++ b/advanced/Scripts/update.sh @@ -40,22 +40,19 @@ make_repo() { local remoteRepo="${2}" 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 } update_repo() { local directory="${1}" - local retVal=0 # Pull the latest commits - # Stash all files not tracked for later retrieval - git -C "${directory}" stash --all --quiet &> /dev/null || ${retVal}=1 + git -C "${directory}" stash --all --quiet # Force a clean working directory for cloning - git -C "${directory}" clean --force -d &> /dev/null || ${retVal}=1 + git -C "${directory}" clean --force -d # Fetch latest changes and apply - git -C "${directory}" pull --quiet &> /dev/null || ${retVal}=1 - return ${retVal} + git -C "${directory}" pull --quiet } getGitFiles() { From c449a1c0e0db0e4d630285fc77e126815fded680 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 24 Dec 2016 15:19:39 +0100 Subject: [PATCH 03/11] Added GitCheckUpdateAvail() --- advanced/Scripts/update.sh | 39 +++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/advanced/Scripts/update.sh b/advanced/Scripts/update.sh index e5fd1526..8b289227 100755 --- a/advanced/Scripts/update.sh +++ b/advanced/Scripts/update.sh @@ -23,7 +23,11 @@ is_repo() { # Use git to check if directory is currently under VCS, return the value local directory="${1}" - curdir=$PWD; cd $directory; git status --short &> /dev/null; rc=$?; cd $curdir + curdir=$PWD; + cd $directory; + git status --short &> /dev/null; + rc=$?; + cd $curdir return $rc } @@ -73,29 +77,42 @@ getGitFiles() { fi } +GitCheckUpdateAvail() { + local directory="${1}" + curdir=$PWD; + cd "${directory}" + + # Fetch latest changes in this repo + git fetch origin + status="$(git status -sb)" + + cd "${curdir}" + + if [[ $status == *"behind"* ]]; then + # Local branch is behind remote branch -> Update + return 1 + else + # Local branch is up-to-date + return 0 + fi +} + main() { local pihole_version_current local pihole_version_latest 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 "::: Please re-run install script from https://github.com/pi-hole/pi-hole" exit 1; fi 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 - echo "*** Unable to contact GitHub for latest version. Please try again later, contact support if this continues." - exit 1 - fi + # Logic # If latest versions are blank - we've probably hit Github rate limit (stop running `pihole -up so often!): From 65c35a5530468c604ea9f212aa928fb96fb8e935 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 24 Dec 2016 15:32:25 +0100 Subject: [PATCH 04/11] Use new subroutine to determine if updates are available --- advanced/Scripts/update.sh | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/advanced/Scripts/update.sh b/advanced/Scripts/update.sh index 8b289227..14ca74de 100755 --- a/advanced/Scripts/update.sh +++ b/advanced/Scripts/update.sh @@ -86,14 +86,17 @@ GitCheckUpdateAvail() { git fetch origin status="$(git status -sb)" + # Change back to original directory cd "${curdir}" if [[ $status == *"behind"* ]]; then # Local branch is behind remote branch -> Update - return 1 - else - # Local branch is up-to-date 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 } @@ -112,7 +115,18 @@ main() { echo "::: Checking for updates..." + if GitCheckUpdateAvail "${PI_HOLE_FILES_DIR}" ; then + core_update=true + echo "Pi-hole Core update available" + fi + if GitCheckUpdateAvail "${ADMIN_INTERFACE_DIR}" ; then + web_update=true + echo "Web Interface update available" + fi + + + exit # Logic # If latest versions are blank - we've probably hit Github rate limit (stop running `pihole -up so often!): From 4632b0f797e3cab7d24ce62e07bdf202e3e25cca Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 24 Dec 2016 15:40:31 +0100 Subject: [PATCH 05/11] Updated updater logic --- advanced/Scripts/update.sh | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/advanced/Scripts/update.sh b/advanced/Scripts/update.sh index 14ca74de..3cbc1d65 100755 --- a/advanced/Scripts/update.sh +++ b/advanced/Scripts/update.sh @@ -117,20 +117,21 @@ main() { if GitCheckUpdateAvail "${PI_HOLE_FILES_DIR}" ; then core_update=true - echo "Pi-hole Core update available" + 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" + echo "::: Web Interface: update available" + else + web_update=false + echo "::: Web Interface: up to date" fi - - exit - # 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: @@ -140,46 +141,40 @@ main() { # 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 - echo ":::" - echo "::: Pi-hole version is $pihole_version_current" - echo "::: Web Admin version is $web_version_current" + if ! ${core_update} && ! ${web_update} ; then echo ":::" echo "::: Everything is up to date!" exit 0 - elif [[ "${pihole_version_current}" == "${pihole_version_latest}" ]] && [[ "${web_version_current}" < "${web_version_latest}" ]]; then + elif ! ${core_update} && ${web_update} ; then echo ":::" echo "::: Pi-hole Web Admin files out of date" getGitFiles "${ADMIN_INTERFACE_DIR}" "${ADMIN_INTERFACE_GIT_URL}" - web_updated=true - - elif [[ "${pihole_version_current}" < "${pihole_version_latest}" ]] && [[ "${web_version_current}" == "${web_version_latest}" ]]; then + elif ${core_update} && ! ${web_update} ; then + echo ":::" 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 - 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" 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 - web_updated=true - core_updated=true else echo "*** Update script has malfunctioned, fallthrough reached. Please contact support" exit 1 fi - if [[ "${web_updated}" == true ]]; then + if [[ "${web_update}" == true ]]; then web_version_current="$(/usr/local/bin/pihole version --admin --current)" echo ":::" 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'" fi - if [[ "${core_updated}" == true ]]; then + if [[ "${core_update}" == true ]]; then pihole_version_current="$(/usr/local/bin/pihole version --pihole --current)" echo ":::" echo "::: Pi-hole version is now at ${pihole_version_current}" From 15db1ffdd51760ab44f5a52faefd301ef42fd53c Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 24 Dec 2016 15:48:43 +0100 Subject: [PATCH 06/11] Remove variables not needed any more --- advanced/Scripts/update.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/advanced/Scripts/update.sh b/advanced/Scripts/update.sh index 3cbc1d65..bd8d6b26 100755 --- a/advanced/Scripts/update.sh +++ b/advanced/Scripts/update.sh @@ -102,9 +102,7 @@ GitCheckUpdateAvail() { main() { local pihole_version_current - local pihole_version_latest local web_version_current - local web_version_latest #This is unlikely if ! is_repo "${PI_HOLE_FILES_DIR}" || ! is_repo "${ADMIN_INTERFACE_DIR}" ; then From e647efd47174ee474224d400f7c27d842726b73c Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Sat, 24 Dec 2016 11:24:20 -0800 Subject: [PATCH 07/11] Shell script, not javascript Signed-off-by: Dan Schaper --- advanced/Scripts/update.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/advanced/Scripts/update.sh b/advanced/Scripts/update.sh index bd8d6b26..ae73caa4 100755 --- a/advanced/Scripts/update.sh +++ b/advanced/Scripts/update.sh @@ -23,10 +23,10 @@ is_repo() { # Use git to check if directory is currently under VCS, return the value local directory="${1}" - curdir=$PWD; - cd $directory; - git status --short &> /dev/null; - rc=$?; + curdir=$PWD + cd $directory + git status --short &> /dev/null + rc=$? cd $curdir return $rc } From 68c17b26dca9bc3afc3255652bc050ce82146838 Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Sat, 24 Dec 2016 11:41:42 -0800 Subject: [PATCH 08/11] Bashisms Signed-off-by: Dan Schaper --- advanced/Scripts/update.sh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/advanced/Scripts/update.sh b/advanced/Scripts/update.sh index ae73caa4..43210a28 100755 --- a/advanced/Scripts/update.sh +++ b/advanced/Scripts/update.sh @@ -22,13 +22,15 @@ readonly PI_HOLE_FILES_DIR="/etc/.pihole" is_repo() { # Use git to check if directory is currently under VCS, return the value local directory="${1}" + local curdir + local rc - curdir=$PWD - cd $directory + curdir="${PWD}" + cd "${directory}" || { echo "Unable to change to ${directory}, exiting."; exit 1; } git status --short &> /dev/null rc=$? - cd $curdir - return $rc + cd "${curdir}" || { echo "Unable to change to ${curdir}, exiting."; exit 1; } + return "${rc}" } prep_repo() { From 3365ef7aaa60595039d85db2ee40e3e9e4b9c535 Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Sat, 24 Dec 2016 11:54:04 -0800 Subject: [PATCH 09/11] `is_repo()` returns values to caller, silence function. Signed-off-by: Dan Schaper --- advanced/Scripts/update.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/advanced/Scripts/update.sh b/advanced/Scripts/update.sh index 43210a28..9f9ea320 100755 --- a/advanced/Scripts/update.sh +++ b/advanced/Scripts/update.sh @@ -26,10 +26,10 @@ is_repo() { local rc curdir="${PWD}" - cd "${directory}" || { echo "Unable to change to ${directory}, exiting."; exit 1; } + cd "${directory}" &> /dev/null || return 1 git status --short &> /dev/null rc=$? - cd "${curdir}" || { echo "Unable to change to ${curdir}, exiting."; exit 1; } + cd "${curdir}" &> /dev/null || return 1 return "${rc}" } From 5e28e6b9ac66f85f1310b582755eb3ba51973a92 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 25 Dec 2016 01:17:46 +0100 Subject: [PATCH 10/11] Removed remaining git -C --- advanced/Scripts/update.sh | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/advanced/Scripts/update.sh b/advanced/Scripts/update.sh index 9f9ea320..2abbca22 100755 --- a/advanced/Scripts/update.sh +++ b/advanced/Scripts/update.sh @@ -52,13 +52,18 @@ make_repo() { update_repo() { local directory="${1}" + local curdir + + curdir="${PWD}" + cd "${directory}" &> /dev/null || return 1 # Pull the latest commits # Stash all files not tracked for later retrieval - git -C "${directory}" stash --all --quiet + git stash --all --quiet # Force a clean working directory for cloning - git -C "${directory}" clean --force -d + git clean --force -d # Fetch latest changes and apply - git -C "${directory}" pull --quiet + git pull --quiet + cd "${curdir}" &> /dev/null || return 1 } getGitFiles() { From e15548cbf5b792ea13cfb7db3aaf62fb28b62fa4 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 25 Dec 2016 01:18:58 +0100 Subject: [PATCH 11/11] Add --quiet to git fetch in GitCheckUpdateAvail() --- advanced/Scripts/update.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced/Scripts/update.sh b/advanced/Scripts/update.sh index 2abbca22..a2a5e8dc 100755 --- a/advanced/Scripts/update.sh +++ b/advanced/Scripts/update.sh @@ -90,7 +90,7 @@ GitCheckUpdateAvail() { cd "${directory}" # Fetch latest changes in this repo - git fetch origin + git fetch --quiet origin status="$(git status -sb)" # Change back to original directory