From 83962dc232043c76dbe9e3a95ea1a7a21703d5fb Mon Sep 17 00:00:00 2001 From: Tom Bombadil <tom.bombadil@ennorath.network> 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 <tom.bombadil@ennorath.network> 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 <tom.bombadil@ennorath.network> 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 <tom.bombadil@ennorath.network> 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 <option> # OPTIONS: -# --initpush Initialize Primary Pi-hole in "Push" mode -# --initpull Initialize Secondary Pi-hole in "Pull" mode -# --push, --upload, --up, -u Push (upload) your Pi-hole lists to a remote Git repo -# --pull, --download, --down, -d Pull (download) your lists from a remote Git repo -# --help, -h, -? Show the current version of pihole-cloudsync -# --version, -v Show version number +# --initpush Initialize Primary Pi-hole in "Push" mode +# --initpull Initialize Secondary Pi-hole in "Pull" mode +# --push, --upload, --up, -u Push (upload) your Pi-hole lists to a remote Git repo +# --pull, --download, --down, -d Pull (download) your lists from a remote Git repo +# --help, -h, -? Show the current version of pihole-cloudsync +# --version, -v Show version number # EXAMPLES: # 'pihole-cloudsync --push' will push (upload) your lists to a remote Git repo @@ -66,116 +66,116 @@ 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}"' + 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 - shared_hosts_mode='1' - echo "Shared Hosts Mode ENABLED."; + shared_hosts_mode='1' + echo "Shared Hosts Mode ENABLED."; else - echo "Shared Hosts Mode DISABLED."; + echo "Shared Hosts Mode DISABLED."; fi # FUNCTIONS push_initialize () { - cd $pihole_dir || exit - if [ "$pihole_version" == "5" ]; then - $SUDO touch $gravity_db $custom_list - $SUDO cp $gravity_db $custom_list $personal_git_dir - else - $SUDO touch $ad_list $black_list $blacklist_list $whitelist_list $regex_list - $SUDO cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $personal_git_dir - fi + cd $pihole_dir || exit + if [ "$pihole_version" == "5" ]; then + $SUDO touch $gravity_db $custom_list + $SUDO cp $gravity_db $custom_list $personal_git_dir + else + $SUDO touch $ad_list $black_list $blacklist_list $whitelist_list $regex_list + $SUDO cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $personal_git_dir + fi - if [ "$shared_hosts_mode" ]; then - $SUDO sed -n '/# SHARED HOSTS - START/,/# SHARED HOSTS - END/p' $local_hosts | $SUDO sed '1d;$d' > /tmp/$shared_hosts - $SUDO cp /tmp/$shared_hosts $personal_git_dir - fi - cd $personal_git_dir || exit - $SUDO git add . - echo "Local Pi-hole initialized in Push mode and local lists were added to local Git repo. Run 'pihole-cloudsync --push' to push to remote Git repo."; + if [ "$shared_hosts_mode" ]; then + $SUDO sed -n '/# SHARED HOSTS - START/,/# SHARED HOSTS - END/p' $local_hosts | $SUDO sed '1d;$d' > /tmp/$shared_hosts + $SUDO cp /tmp/$shared_hosts $personal_git_dir + fi + cd $personal_git_dir || exit + $SUDO git add . + echo "Local Pi-hole initialized in Push mode and local lists were added to local Git repo. Run 'pihole-cloudsync --push' to push to remote Git repo."; } pull_initialize () { - cd $personal_git_dir || exit - $SUDO git remote update > /dev/null - # Remove -q option if you don't want to run in "quiet" mode - $SUDO git fetch --all -q - $SUDO git reset --hard origin/master -q - if [ "$pihole_version" == "5" ]; then - $SUDO $DOCKER service pihole-FTL stop - $SUDO cp $gravity_db $custom_list $pihole_dir - $SUDO $DOCKER service pihole-FTL start - else - $SUDO cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $pihole_dir - fi + cd $personal_git_dir || exit + $SUDO git remote update > /dev/null + # Remove -q option if you don't want to run in "quiet" mode + $SUDO git fetch --all -q + $SUDO git reset --hard origin/master -q + if [ "$pihole_version" == "5" ]; then + $SUDO $DOCKER service pihole-FTL stop + $SUDO cp $gravity_db $custom_list $pihole_dir + $SUDO $DOCKER service pihole-FTL start + else + $SUDO cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $pihole_dir + fi - if [ "$shared_hosts_mode" ]; then - $SUDO cp $local_hosts /tmp/hosts - $SUDO sed -e '/# SHARED HOSTS - START/,/# SHARED HOSTS - END/{/^# SHARED HOSTS/!d}' \ - -e "/# SHARED HOSTS - START.*$/r $shared_hosts" \ - --in-place=.bak /tmp/hosts - $SUDO mv /tmp/hosts $local_hosts - fi - $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'."; + if [ "$shared_hosts_mode" ]; then + $SUDO cp $local_hosts /tmp/hosts + $SUDO sed -e '/# SHARED HOSTS - START/,/# SHARED HOSTS - END/{/^# SHARED HOSTS/!d}' \ + -e "/# SHARED HOSTS - START.*$/r $shared_hosts" \ + --in-place=.bak /tmp/hosts + $SUDO mv /tmp/hosts $local_hosts + fi + $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'."; } push () { - cd $pihole_dir || exit + cd $pihole_dir || exit if [ "$pihole_version" == "5" ]; then $SUDO cp $gravity_db $custom_list $personal_git_dir - else - $SUDO cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $personal_git_dir - fi + else + $SUDO cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $personal_git_dir + fi - if [ "$shared_hosts_mode" ]; then - $SUDO sed -n '/# SHARED HOSTS - START/,/# SHARED HOSTS - END/p' $local_hosts | $SUDO sed '1d;$d' > /tmp/$shared_hosts - $SUDO cp /tmp/$shared_hosts $personal_git_dir - fi - cd $personal_git_dir || exit - $SUDO git remote update > /dev/null + if [ "$shared_hosts_mode" ]; then + $SUDO sed -n '/# SHARED HOSTS - START/,/# SHARED HOSTS - END/p' $local_hosts | $SUDO sed '1d;$d' > /tmp/$shared_hosts + $SUDO cp /tmp/$shared_hosts $personal_git_dir + fi + cd $personal_git_dir || exit + $SUDO git remote update > /dev/null CHANGED=$($SUDO git --work-tree=$personal_git_dir status --porcelain) if [ -n "${CHANGED}" ]; then echo 'Local Pi-hole lists are different than remote Git repo. Updating remote repo...'; - rightnow=$(date +"%B %e, %Y %l:%M%p") - # Remove -q option if you don't want to run in "quiet" mode - $SUDO git commit -a -m "Updated $rightnow" -q - $SUDO git push -q - echo 'Done!'; - exit 0 + rightnow=$(date +"%B %e, %Y %l:%M%p") + # Remove -q option if you don't want to run in "quiet" mode + $SUDO git commit -a -m "Updated $rightnow" -q + $SUDO git push -q + echo 'Done!'; + exit 0 else echo 'Remote Git repo matches local Pi-hole lists. No further action required.'; - exit 0 + exit 0 fi } pull () { - cd $personal_git_dir || exit - $SUDO git remote update > /dev/null - CHANGED=$($SUDO git log HEAD..origin/master --oneline) - if [ -n "${CHANGED}" ]; then + cd $personal_git_dir || exit + $SUDO git remote update > /dev/null + CHANGED=$($SUDO git log HEAD..origin/master --oneline) + if [ -n "${CHANGED}" ]; then echo 'Remote Git repo is different than local Pi-hole lists. Updating local lists...'; # Remove -q option if you don't want to run in "quiet" mode $SUDO git fetch --all -q - $SUDO git reset --hard origin/master -q - if [ "$pihole_version" == "5" ]; then - $SUDO $DOCKER service pihole-FTL stop - $SUDO cp $gravity_db $custom_list $pihole_dir - $SUDO $DOCKER service pihole-FTL start - else - $SUDO cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $pihole_dir - fi + $SUDO git reset --hard origin/master -q + if [ "$pihole_version" == "5" ]; then + $SUDO $DOCKER service pihole-FTL stop + $SUDO cp $gravity_db $custom_list $pihole_dir + $SUDO $DOCKER service pihole-FTL start + else + $SUDO cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $pihole_dir + fi - if [ "$shared_hosts_mode" ]; then - $SUDO cp $local_hosts /tmp/hosts - $SUDO sed -e '/# SHARED HOSTS - START/,/# SHARED HOSTS - END/{/^# SHARED HOSTS/!d}' \ - -e "/# SHARED HOSTS - START.*$/r $shared_hosts" \ - --in-place=.bak /tmp/hosts - $SUDO mv /tmp/hosts $local_hosts - fi - $SUDO $DOCKER pihole -g + if [ "$shared_hosts_mode" ]; then + $SUDO cp $local_hosts /tmp/hosts + $SUDO sed -e '/# SHARED HOSTS - START/,/# SHARED HOSTS - END/{/^# SHARED HOSTS/!d}' \ + -e "/# SHARED HOSTS - START.*$/r $shared_hosts" \ + --in-place=.bak /tmp/hosts + $SUDO mv /tmp/hosts $local_hosts + fi + $SUDO $DOCKER pihole -g echo 'Done!'; exit 0 else @@ -184,7 +184,7 @@ pull () { fi } docker_test() { - $DOCKER echo "docker test worked" + $DOCKER printenv } ########################################################################### # Check to see whether a command line option was provided @@ -198,47 +198,48 @@ 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 + then + echo "$arg option detected. Testing docker container commands with 'printenv'."; + docker_test + exit 0 # Initialize - adds primary Pi-hole's lists to local Git repo before first push/upload elif [ "$arg" == "--initpush" ] - then - echo "$arg option detected. Initializing local Git repo for Push/Upload."; - push_initialize - exit 0 + then + echo "$arg option detected. Initializing local Git repo for Push/Upload."; + push_initialize + exit 0 # Initialize - adds primary Pi-hole's lists to local Git repo before first push/upload elif [ "$arg" == "--initpull" ] - then - echo "$arg option detected. Initializing local Git repo for Pull/Download."; - pull_initialize - exit 0 + then + echo "$arg option detected. Initializing local Git repo for Pull/Download."; + pull_initialize + exit 0 # Push / Upload - Pushes updated local Pi-hole lists to remote Git repo elif [ "$arg" == "--push" ] || [ "$arg" == "--upload" ] || [ "$arg" == "--up" ] || [ "$arg" == "-u" ] then - echo "$arg option detected. Running in Push/Upload mode." - push - exit 0 + echo "$arg option detected. Running in Push/Upload mode." + push + exit 0 # Pull / Download - Pulls updated Pi-hole lists from remote Git repo elif [ "$arg" == "--pull" ] || [ "$arg" == "--download" ] || [ "$arg" == "--down" ]|| [ "$arg" == "-d" ] - then + then echo "$arg option detected. Running in Pull/Download mode." - pull + pull exit 0 # Help - Displays help dialog elif [ "$arg" == "--help" ] || [ "$arg" == "-h" ] || [ "$arg" == "-?" ] - then - cat << EOF + then + cat << EOF Usage: pihole-cloudsync <option> Options: - --push, --upload, --up, -u Push (upload) your Pi-hole lists to a remote Git repo - --pull, --download, --down, -d Pull (download) your lists from a remote Git repo - --initpush Initialize Primary Pi-hole in "Push" mode - --initpull Initialize Secondary Pi-hole in "Pull" mode - --help, -h, -? Show this help dialog - --version, -v Show the current version of pihole-cloudsync + --push, --upload, --up, -u Push (upload) your Pi-hole lists to a remote Git repo + --pull, --download, --down, -d Pull (download) your lists from a remote Git repo + --initpush Initialize Primary Pi-hole in "Push" mode + --initpull Initialize Secondary Pi-hole in "Pull" mode + --help, -h, -? Show this help dialog + --version, -v Show the current version of pihole-cloudsync + --dockertest Validate connection to running Pi-hole docker container Examples: 'pihole-cloudsync --push' will push (upload) your lists to a Git repo @@ -249,13 +250,12 @@ EOF # Version - Displays version number elif [ "$arg" == "--version" ] || [ "$arg" == "-v" ] - then - echo 'pihole-cloudsync v'$version' - Updated '"$update"; - echo 'https://github.com/stevejenkins/pihole-cloudsync'; - + then + echo 'pihole-cloudsync v'$version' - Updated '"$update"; + echo 'https://github.com/stevejenkins/pihole-cloudsync'; # Invalid command line option was passed else - echo "Invalid command line option. Try --push, --pull, or --help." - exit 1 + echo "Invalid command line option. Try --push, --pull, or --help." + exit 1 fi done From 44459d3b7501897f6c1a1cf71726b40e41448cad Mon Sep 17 00:00:00 2001 From: Tom Bombadil <tom.bombadil@ennorath.network> Date: Sat, 13 Jun 2020 16:12:41 -0700 Subject: [PATCH 5/5] moving my-pihole-lists directory under /etc/pihole rather than /usr/local/bin --- pihole-cloudsync | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pihole-cloudsync b/pihole-cloudsync index 90aeb21..099b26d 100755 --- a/pihole-cloudsync +++ b/pihole-cloudsync @@ -34,7 +34,7 @@ update='13 June 2020' ########################################################################### # PIHOLE-CLOUDSYNC CONFIGURATION pihole_version=5 # safe to default to version 5 at this point? -personal_git_dir='/usr/local/bin/my-pihole-lists' +personal_git_dir='/etc/pihole/my-pihole-lists' pihole_dir='/etc/pihole' ad_list='adlists.list' black_list='black.list'