From 6ca47dc3b360a8566d0c505d0c4cf23ed0017d61 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 28 Oct 2017 13:20:02 +0200 Subject: [PATCH 1/6] Add bash / cron based update checker for Pi-hole --- advanced/Scripts/updatecheck.sh | 70 +++++++++++++++++++++++++++++++++ advanced/pihole.cron | 3 ++ pihole | 10 ++++- 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100755 advanced/Scripts/updatecheck.sh diff --git a/advanced/Scripts/updatecheck.sh b/advanced/Scripts/updatecheck.sh new file mode 100755 index 00000000..86f4ba93 --- /dev/null +++ b/advanced/Scripts/updatecheck.sh @@ -0,0 +1,70 @@ +#!/usr/bin/env bash +# Pi-hole: A black hole for Internet advertisements +# (c) 2017 Pi-hole, LLC (https://pi-hole.net) +# Network-wide ad blocking via your own hardware. +# +# Checks for updates via GitHub +# +# This file is copyright under the latest version of the EUPL. +# Please see LICENSE file for your rights under this license. + +# Credit: https://stackoverflow.com/a/46324904 +function json_extract() { + local key=$1 + local json=$2 + + local string_regex='"([^"\]|\\.)*"' + local number_regex='-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?' + local value_regex="${string_regex}|${number_regex}|true|false|null" + local pair_regex="\"${key}\"[[:space:]]*:[[:space:]]*(${value_regex})" + + if [[ ${json} =~ ${pair_regex} ]]; then + echo $(sed 's/^"\|"$//g' <<< "${BASH_REMATCH[1]}") + else + return 1 + fi +} + +GITHUB_CORE_VERSION="$(json_extract tag_name "$(curl -q 'https://api.github.com/repos/pi-hole/pi-hole/releases/latest' 2> /dev/null)")" +GITHUB_WEB_VERSION="$(json_extract tag_name "$(curl -q 'https://api.github.com/repos/pi-hole/AdminLTE/releases/latest' 2> /dev/null)")" +GITHUB_FTL_VERSION="$(json_extract tag_name "$(curl -q 'https://api.github.com/repos/pi-hole/FTL/releases/latest' 2> /dev/null)")" + +echo "${GITHUB_CORE_VERSION} ${GITHUB_WEB_VERSION} ${GITHUB_FTL_VERSION}" > "/etc/pihole/GitHubVersions" + +function get_local_branch() { + # Return active branch + local directory + directory="${1}" + local output + + cd "${directory}" || return 1 + # Store STDERR as STDOUT variable + output=$( { git rev-parse --abbrev-ref HEAD; } 2>&1 ) + echo "$output" + return +} + +CORE_BRANCH="$(get_local_branch /etc/.pihole)" +WEB_BRANCH="$(get_local_branch /var/www/html/admin)" +FTL_BRANCH="$(pihole-FTL tag)" + +echo "${CORE_BRANCH} ${WEB_BRANCH} ${FTL_BRANCH}" > "/etc/pihole/localbranches" + +function get_local_version() { + # Return active branch + local directory + directory="${1}" + local output + + cd "${directory}" || return 1 + # Store STDERR as STDOUT variable + output=$( { git describe --long --dirty --tags; } 2>&1 ) + echo "$output" + return +} + +CORE_VERSION="$(get_local_version /etc/.pihole)" +WEB_VERSION="$(get_local_version /var/www/html/admin)" +FTL_VERSION="$(pihole-FTL version)" + +echo "${CORE_VERSION} ${WEB_VERSION} ${FTL_VERSION}" > "/etc/pihole/localversions" diff --git a/advanced/pihole.cron b/advanced/pihole.cron index f1beb08c..c873b79d 100644 --- a/advanced/pihole.cron +++ b/advanced/pihole.cron @@ -28,3 +28,6 @@ 00 00 * * * root PATH="$PATH:/usr/local/bin/" pihole flush once quiet @reboot root /usr/sbin/logrotate /etc/pihole/logrotate + +# Pi-hole: Grab remote version and branch every 10 minutes +*/10 * * * * root PATH="$PATH:/usr/local/bin/" pihole updatechecker diff --git a/pihole b/pihole index 80cdefe7..601d8d02 100755 --- a/pihole +++ b/pihole @@ -200,7 +200,7 @@ Options: # Scan Wildcards if [[ -e "${wildcardlist}" ]]; then - # Determine all subdomains, domain and TLDs + # Determine all subdomains, domain and TLDs mapfile -t wildcards <<< "$(processWildcards "${domainQuery}")" for match in "${wildcards[@]}"; do @@ -483,7 +483,7 @@ statusFunc() { # Determine if Pi-hole's addn-hosts configs are commented out addnConfigs=$(grep -i "addn-hosts=/" /etc/dnsmasq.d/01-pihole.conf) - + if [[ "${addnConfigs}" =~ "#" ]]; then # A config is commented out case "${1}" in @@ -579,6 +579,11 @@ tricorderFunc() { fi } +updateCheckFunc() { + "${PI_HOLE_SCRIPT_DIR}"/updatecheck.sh "$@" + exit 0 +} + helpFunc() { echo "Usage: pihole [options] Example: 'pihole -w -h' @@ -650,5 +655,6 @@ case "${1}" in "-t" | "tail" ) tailFunc;; "checkout" ) piholeCheckoutFunc "$@";; "tricorder" ) tricorderFunc;; + "updatechecker" ) updateCheckFunc;; * ) helpFunc;; esac From 709851503fb243d0d62dca6c98e2e7c774d71f73 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 28 Oct 2017 13:27:12 +0200 Subject: [PATCH 2/6] Use "pihole-FTL branch" to get FTL's branch Signed-off-by: DL6ER --- advanced/Scripts/updatecheck.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced/Scripts/updatecheck.sh b/advanced/Scripts/updatecheck.sh index 86f4ba93..12c49445 100755 --- a/advanced/Scripts/updatecheck.sh +++ b/advanced/Scripts/updatecheck.sh @@ -46,7 +46,7 @@ function get_local_branch() { CORE_BRANCH="$(get_local_branch /etc/.pihole)" WEB_BRANCH="$(get_local_branch /var/www/html/admin)" -FTL_BRANCH="$(pihole-FTL tag)" +FTL_BRANCH="$(pihole-FTL branch)" echo "${CORE_BRANCH} ${WEB_BRANCH} ${FTL_BRANCH}" > "/etc/pihole/localbranches" From 9be854031fe01dd5ba102878c130950bae4a66d1 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 28 Oct 2017 13:41:24 +0200 Subject: [PATCH 3/6] Don't store FTL branch until the next release of FTL which supports returning the branch in an easy way Signed-off-by: DL6ER --- advanced/Scripts/updatecheck.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/advanced/Scripts/updatecheck.sh b/advanced/Scripts/updatecheck.sh index 12c49445..446fc4ac 100755 --- a/advanced/Scripts/updatecheck.sh +++ b/advanced/Scripts/updatecheck.sh @@ -46,7 +46,10 @@ function get_local_branch() { CORE_BRANCH="$(get_local_branch /etc/.pihole)" WEB_BRANCH="$(get_local_branch /var/www/html/admin)" -FTL_BRANCH="$(pihole-FTL branch)" +#FTL_BRANCH="$(pihole-FTL branch)" +# Don't store FTL branch until the next release of FTL which +# supports returning the branch in an easy way +FTL_BRANCH="XXX" echo "${CORE_BRANCH} ${WEB_BRANCH} ${FTL_BRANCH}" > "/etc/pihole/localbranches" From 7362416afb1a24e252757206dd8bc41f3ca167da Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 28 Oct 2017 15:40:48 +0200 Subject: [PATCH 4/6] Force an update of the updatechecker after update/install Signed-off-by: DL6ER --- automated install/basic-install.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index 6eca6868..8ebd48b2 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -2077,6 +2077,9 @@ main() { # Download and compile the aggregated block list runGravity + # Force an update of the updatechecker + . /opt/pihole/updatecheck.sh + # if [[ "${useUpdateVars}" == false ]]; then displayFinalMessage "${pw}" From 0db76aada0847655b9965b03ee093edbc205e2d4 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 28 Oct 2017 15:44:17 +0200 Subject: [PATCH 5/6] Silence errors when directory to be checked does not exist (system may have been installed without a web interface!) Signed-off-by: DL6ER --- advanced/Scripts/updatecheck.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/advanced/Scripts/updatecheck.sh b/advanced/Scripts/updatecheck.sh index 446fc4ac..26b7d4b0 100755 --- a/advanced/Scripts/updatecheck.sh +++ b/advanced/Scripts/updatecheck.sh @@ -37,7 +37,7 @@ function get_local_branch() { directory="${1}" local output - cd "${directory}" || return 1 + cd "${directory}" 2> /dev/null || return 1 # Store STDERR as STDOUT variable output=$( { git rev-parse --abbrev-ref HEAD; } 2>&1 ) echo "$output" @@ -59,7 +59,7 @@ function get_local_version() { directory="${1}" local output - cd "${directory}" || return 1 + cd "${directory}" 2> /dev/null || return 1 # Store STDERR as STDOUT variable output=$( { git describe --long --dirty --tags; } 2>&1 ) echo "$output" From 90efa3b6101066b898082504661be373eab051c8 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 8 Nov 2017 19:11:41 +0100 Subject: [PATCH 6/6] Simplify git subroutines Signed-off-by: DL6ER --- advanced/Scripts/updatecheck.sh | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/advanced/Scripts/updatecheck.sh b/advanced/Scripts/updatecheck.sh index 26b7d4b0..9b79c4cb 100755 --- a/advanced/Scripts/updatecheck.sh +++ b/advanced/Scripts/updatecheck.sh @@ -33,15 +33,8 @@ echo "${GITHUB_CORE_VERSION} ${GITHUB_WEB_VERSION} ${GITHUB_FTL_VERSION}" > "/et function get_local_branch() { # Return active branch - local directory - directory="${1}" - local output - - cd "${directory}" 2> /dev/null || return 1 - # Store STDERR as STDOUT variable - output=$( { git rev-parse --abbrev-ref HEAD; } 2>&1 ) - echo "$output" - return + cd "${1}" 2> /dev/null || return 1 + git rev-parse --abbrev-ref HEAD || return 1 } CORE_BRANCH="$(get_local_branch /etc/.pihole)" @@ -55,15 +48,8 @@ echo "${CORE_BRANCH} ${WEB_BRANCH} ${FTL_BRANCH}" > "/etc/pihole/localbranches" function get_local_version() { # Return active branch - local directory - directory="${1}" - local output - - cd "${directory}" 2> /dev/null || return 1 - # Store STDERR as STDOUT variable - output=$( { git describe --long --dirty --tags; } 2>&1 ) - echo "$output" - return + cd "${1}" 2> /dev/null || return 1 + git describe --long --dirty --tags || return 1 } CORE_VERSION="$(get_local_version /etc/.pihole)"