From 83962dc232043c76dbe9e3a95ea1a7a21703d5fb Mon Sep 17 00:00:00 2001 From: Tom Bombadil Date: Sat, 13 Jun 2020 12:48:09 -0700 Subject: [PATCH 1/5] Initial test of docker container support --- pihole-cloudsync | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/pihole-cloudsync b/pihole-cloudsync index 1ffc1df..ae05e4c 100755 --- a/pihole-cloudsync +++ b/pihole-cloudsync @@ -5,8 +5,8 @@ # Helper script to keep multiple Pi-holes' lists synchronized via Git # Version 4.0 - March 28, 2020 - Steve Jenkins (stevejenkins.com) -version='4.1' -update='April 2, 2020' +version='4.1tb' +update='13 June 2020' # SETUP # Follow the instructions in the README to set up your own private Git @@ -28,9 +28,10 @@ update='April 2, 2020' # 'pihole-cloudsync --pull' will pull (download) your lists from a remote Git repo # Project Home: https://github.com/stevejenkins/pihole-cloudsync + ########################################################################### -# CONSTANTS -pihole_version=4 +# PIHOLE-CLOUDSYNC CONFIGURATION +pihole_version=5 # safe to default to version 5 at this point? personal_git_dir='/usr/local/bin/my-pihole-lists' pihole_dir='/etc/pihole' ad_list='adlists.list' @@ -40,12 +41,14 @@ whitelist_list='whitelist.txt' regex_list='regex.list' gravity_db='gravity.db' custom_list='custom.list' + ########################################################################### # FOR SHARED HOSTS MODE ONLY # RE-INITIALIZE WITH --initpush OR --initpull AFTER ENABLING THIS MODE enable_hosts=off local_hosts='/etc/hosts' shared_hosts='sharedhosts.txt' + ########################################################################### # SHOULDN'T NEED TO EDIT BELOW THIS LINE @@ -55,6 +58,16 @@ if [ "$EUID" -ne 0 ] then SUDO='sudo' fi +# Send commands to container if pihole is running in docker +PIHOLECONTAINER='' +DOCKER='' +if [ -x "$(command -v docker)" ]; then + PIHOLECONTAINER=$(docker ps -f "ancestor=pihole/pihole" --format "{{.Names}}") + if [ -n "${PIHOLECONTAINER}" ] && $(docker inspect -f "{{.State.Running}}" "${PIHOLECONTAINER}"); then + DOCKER='docker exec -i "${PIHOLECONTAINER}"' + fi +fi + # Case-insensitive check for Shared Hosts Mode shared_hosts_mode='' if [ $(echo "$enable_hosts" |tr [:upper:] [:lower:]) == "yes" ] || [ $(echo "$enable_hosts" |tr [:upper:] [:lower:]) == "y" ] || [ $(echo "$enable_hosts" |tr [:upper:] [:lower:]) == "on" ] || [ $(echo "$enable_hosts" |tr [:upper:] [:lower:]) == "true" ] || [ $(echo "$enable_hosts" |tr [:upper:] [:lower:]) == "1" ]; then @@ -90,9 +103,9 @@ pull_initialize () { $SUDO git fetch --all -q $SUDO git reset --hard origin/master -q if [ "$pihole_version" == "5" ]; then - $SUDO service pihole-FTL stop + $SUDO $DOCKER service pihole-FTL stop $SUDO cp $gravity_db $custom_list $pihole_dir - $SUDO service pihole-FTL start + $SUDO $DOCKER service pihole-FTL start else $SUDO cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $pihole_dir fi @@ -104,7 +117,7 @@ pull_initialize () { --in-place=.bak /tmp/hosts $SUDO mv /tmp/hosts $local_hosts fi - $SUDO pihole -g + $SUDO $DOCKER pihole -g echo "Local Pi-hole initialized in Pull mode and first pull successfully completed."; echo "Future pulls can now be perfomed with 'pihole-cloudsync --pull'."; } @@ -146,9 +159,9 @@ pull () { $SUDO git fetch --all -q $SUDO git reset --hard origin/master -q if [ "$pihole_version" == "5" ]; then - $SUDO service pihole-FTL stop + $SUDO $DOCKER service pihole-FTL stop $SUDO cp $gravity_db $custom_list $pihole_dir - $SUDO service pihole-FTL start + $SUDO $DOCKER service pihole-FTL start else $SUDO cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $pihole_dir fi @@ -160,7 +173,7 @@ pull () { --in-place=.bak /tmp/hosts $SUDO mv /tmp/hosts $local_hosts fi - $SUDO pihole -g + $SUDO $DOCKER pihole -g echo 'Done!'; exit 0 else @@ -168,6 +181,9 @@ pull () { exit 0 fi } +docker_test() { + $DOCKER echo "docker test worked" +} ########################################################################### # Check to see whether a command line option was provided if [ -z "$1" ] @@ -178,6 +194,12 @@ fi # Determine which action to perform (InitPush, InitPull, Push, Pull, or Help) for arg in "$@" do + # Test sending commands to docker container + if [ "$arg" == "--dockertest" ] + then + echo "$arg option detected. Testing docker container commands."; + docker_test + exit 0 # Initialize - adds primary Pi-hole's lists to local Git repo before first push/upload if [ "$arg" == "--initpush" ] then From fa3e149ff5eb9a5ca0b8e0c40565e55db7a078a3 Mon Sep 17 00:00:00 2001 From: Tom Bombadil Date: Sat, 13 Jun 2020 12:59:53 -0700 Subject: [PATCH 2/5] small text cleanup --- pihole-cloudsync | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pihole-cloudsync b/pihole-cloudsync index ae05e4c..5823c93 100755 --- a/pihole-cloudsync +++ b/pihole-cloudsync @@ -4,7 +4,9 @@ # pihole-cloudsync # Helper script to keep multiple Pi-holes' lists synchronized via Git -# Version 4.0 - March 28, 2020 - Steve Jenkins (stevejenkins.com) +# Version 4.1 - April 2, 2020 - Steve Jenkins (stevejenkins.com) +# Version 4.1tb - 13 June 2020 - IarwainBen-adar (tom.bombadil@ennorath.network) +# └─TB: Added support for Pi-hole running in a Docker container version='4.1tb' update='13 June 2020' @@ -58,7 +60,7 @@ if [ "$EUID" -ne 0 ] then SUDO='sudo' fi -# Send commands to container if pihole is running in docker +# Send pihole commands via docker exec if pihole is running in docker container PIHOLECONTAINER='' DOCKER='' if [ -x "$(command -v docker)" ]; then From fab72d84569640f30d4824129d6de46c942b8c21 Mon Sep 17 00:00:00 2001 From: Tom Bombadil Date: Sat, 13 Jun 2020 13:07:46 -0700 Subject: [PATCH 3/5] elif facepalm --- pihole-cloudsync | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pihole-cloudsync b/pihole-cloudsync index 5823c93..698d944 100755 --- a/pihole-cloudsync +++ b/pihole-cloudsync @@ -203,7 +203,7 @@ do docker_test exit 0 # Initialize - adds primary Pi-hole's lists to local Git repo before first push/upload - if [ "$arg" == "--initpush" ] + elif [ "$arg" == "--initpush" ] then echo "$arg option detected. Initializing local Git repo for Push/Upload."; push_initialize From f101e0b12bb4f3794a9f8078f1e7736e77eaf3b1 Mon Sep 17 00:00:00 2001 From: Tom Bombadil Date: Sat, 13 Jun 2020 13:55:18 -0700 Subject: [PATCH 4/5] fix for variable substitution error --- pihole-cloudsync | 232 +++++++++++++++++++++++------------------------ 1 file changed, 116 insertions(+), 116 deletions(-) diff --git a/pihole-cloudsync b/pihole-cloudsync index 698d944..90aeb21 100755 --- a/pihole-cloudsync +++ b/pihole-cloudsync @@ -18,12 +18,12 @@ update='13 June 2020' # USAGE: pihole-cloudsync