diff --git a/advanced/Scripts/speedtestmod/mod.sh b/advanced/Scripts/speedtestmod/mod.sh index 926dec3ad2..09235fa72e 100755 --- a/advanced/Scripts/speedtestmod/mod.sh +++ b/advanced/Scripts/speedtestmod/mod.sh @@ -1,16 +1,5 @@ #!/bin/bash - -getLocalVersion() { - local src=${1:-} - - if [ -z "$src" ]; then - echo "" - else - local localVersion=$(pihole -v | grep "$src" | cut -d ' ' -f 6) - [ "$localVersion" != "HEAD" ] || localVersion=$(pihole -v | grep "$src" | cut -d ' ' -f 7) - echo "$localVersion" - fi -} +aborted=0 download() { local path=$1 @@ -19,29 +8,39 @@ download() { local localVersion=${4:-} local branch="${5:-master}" local dest=$path/$name - local tags=$(git ls-remote --tags "$url" | awk -F/ '{print $3}' | grep '^v[0-9]' | grep -v '\^{}' | sort -V) [ -d "$dest" ] && [ ! -d "$dest/.git" ] && mv -f "$dest" "$dest.old" || : [ -d "$dest" ] || git clone --depth=1 -b "$branch" "$url" "$dest" -q cd "$dest" git config --global --add safe.directory "$dest" - if [ "$url" != "old" ]; then + if [ "$aborted" == "0" ]; then git remote -v | grep -q "old" || git remote rename origin old git remote -v | grep -q "origin" && git remote remove origin git remote add -t "$branch" origin "$url" elif [ -d .git/refs/remotes/old ]; then git remote remove origin git remote rename old origin - git clean -ffdx + url=$(git remote get-url origin) + fi + + local tags=$(git ls-remote --tags "$url" | awk -F/ '{print $3}' | grep '^v[0-9]' | grep -v '\^{}' | sort -V) + local latestTag=$(tail -n1 <<<"$tags") + local localTag=$latestTag + + if [[ "$localVersion" == *.* ]]; then + latestTag=$localVersion + localTag=$latestTag + elif [ ! -z "$localVersion" ]; then + localTag=$(pihole -v | grep "$localVersion" | cut -d ' ' -f 6) + [ "$localTag" != "HEAD" ] || localTag=$(pihole -v | grep "$localVersion" | cut -d ' ' -f 7) fi git fetch origin --depth=1 $branch:refs/remotes/origin/$branch -q git reset --hard origin/"$branch" -q git checkout -B "$branch" -q - [[ "$localVersion" == *.* ]] && local latestTag=$localVersion || local latestTag=$(getLocalVersion "$localVersion") - [ "$url" != "old" ] && [[ "$url" != *"arevindh"* ]] && ! git remote -v | grep -q "old.*ipitio" && [[ "$localVersion" < "$latestTag" ]] && latestTag=$(awk -v lv="$localVersion" '$1 <= lv' <<< "$tags" | tail -n1) || latestTag=$(tail -n1 <<< "$tags") - [ "$branch" == "master" ] && [[ "$url" != *"ipitio"* ]] && [ "$(git rev-parse HEAD)" != "$(git rev-parse $latestTag 2>/dev/null)" ] && git fetch origin tag $latestTag --depth=1 -q && git -c advice.detachedHead=false checkout "$latestTag" -q + [ "$aborted" == "0" ] && { [[ "$url" != *"arevindh"* ]] && [[ "$url" != *"ipitio"* ]] && ! git remote -v | grep -q "old.*ipitio" && [[ "$localTag" < "$latestTag" ]] && latestTag=$(awk -v lv="$localTag" '$1 <= lv' <<<"$tags" | tail -n1) || :; } || latestTag=$localTag + [ "$branch" == "master" ] && [[ "$url" != *"ipitio"* ]] && [ "$(git rev-parse HEAD)" != "$(git rev-parse $latestTag 2>/dev/null)" ] && git fetch origin tag $latestTag --depth=1 -q && git -c advice.detachedHead=false checkout "$latestTag" -q || : cd .. } @@ -60,7 +59,6 @@ notInstalled() { # allow to source the above helper functions without running the whole script if [[ "${SKIP_MOD:-}" != true ]]; then - aborted=0 html_dir=/var/www/html core_dir=/etc/.pihole opt_dir=/opt/pihole @@ -82,31 +80,7 @@ if [[ "${SKIP_MOD:-}" != true ]]; then isEmpty() { db=$1 - [ -f $db ] && sqlite3 "$db" "select * from $db_table limit 1;" >/dev/null 2>&1 && [ ! -z "$(sqlite3 "$db" "select * from $db_table limit 1;")" ] && return 1 || return 0 - } - - manageHistory() { - if [ "${1:-}" == "db" ]; then - if [ -f $curr_db ] && ! isEmpty $curr_db; then - echo "Flushing Database..." - mv -f $curr_db $last_db - [ ! -f $etc_dir/last_speedtest ] || mv -f $etc_dir/last_speedtest $etc_dir/last_speedtest.old - - if [ -f /var/log/pihole/speedtest.log ]; then - mv -f /var/log/pihole/speedtest.log /var/log/pihole/speedtest.log.old - rm -f $etc_dir/speedtest.log - fi - elif [ -f $last_db ]; then - echo "Restoring Database..." - mv -f $last_db $curr_db - [ ! -f $etc_dir/last_speedtest.old ] || mv -f $etc_dir/last_speedtest.old $etc_dir/last_speedtest - - if [ -f /var/log/pihole/speedtest.log.old ]; then - mv -f /var/log/pihole/speedtest.log.old /var/log/pihole/speedtest.log - cp -af /var/log/pihole/speedtest.log $etc_dir/speedtest.log - fi - fi - fi + [ -f $db ] && sqlite3 "$db" "select * from $db_table limit 1;" &>/dev/null && [ ! -z "$(sqlite3 "$db" "select * from $db_table limit 1;")" ] && return 1 || return 0 } swapScripts() { @@ -117,112 +91,32 @@ if [[ "${SKIP_MOD:-}" != true ]]; then set -u } - installMod() { - echo "Installing Mod..." - - if [ ! -f /usr/local/bin/pihole ]; then - echo "Installing Pi-hole..." - curl -sSL https://install.pi-hole.net | sudo bash - fi - - local PHP_VERSION=$(php -v | head -n 1 | awk '{print $2}' | cut -d "." -f 1,2) - local PKG_MANAGER=$(command -v apt-get || command -v dnf || command -v yum) - local PKGS=(bc sqlite3 jq tmux wget "php$PHP_VERSION-sqlite3") - local missingPkgs=() - - for pkg in "${PKGS[@]}"; do - notInstalled "$pkg" && missingPkgs+=("$pkg") || : - done - - if [ ${#missingPkgs[@]} -gt 0 ]; then - [[ "$PKG_MANAGER" != *"apt-get"* ]] || apt-get update > /dev/null - $PKG_MANAGER install -y "${missingPkgs[@]}" > /dev/null - fi - - download /etc .pihole https://github.com/arevindh/pi-hole Pi-hole - swapScripts - cp -af $core_dir/advanced/Scripts/speedtestmod/. $opt_dir/speedtestmod/ - pihole -a -s - download $html_dir admin https://github.com/arevindh/AdminLTE web - download $etc_dir speedtest https://github.com/arevindh/pihole-speedtest - pihole updatechecker - } - - uninstall() { - if [ -f $curr_wp ] && cat $curr_wp | grep -q SpeedTest; then - echo "Restoring Pi-hole..." - pihole -a -s -1 - download $html_dir admin https://github.com/pi-hole/AdminLTE web - download /etc .pihole https://github.com/pi-hole/pi-hole Pi-hole - st_ver=$(getLocalVersion "speedtest") - rm -rf $etc_dir/speedtest - swapScripts - fi - - manageHistory ${1:-} - } - - purge() { - if [ -f /etc/systemd/system/pihole-speedtest.timer ]; then - rm -f /etc/systemd/system/pihole-speedtest.service - rm -f /etc/systemd/system/pihole-speedtest.timer - systemctl daemon-reload - fi - - rm -rf "$html_dir"/*_admin - rm -rf $opt_dir/speedtestmod - rm -rf $etc_dir/mod - rm -f "$curr_wp".* - rm -f "$curr_db".* - rm -f "$curr_db"_* - rm -f $etc_dir/last_speedtest.* - isEmpty $curr_db && rm -f $curr_db || : - pihole updatechecker - } - - update() { - if [[ -d /run/systemd/system ]]; then - echo "Updating Pi-hole..." - PIHOLE_SKIP_OS_CHECK=true sudo -E pihole -up - else - echo "Systemd not found. Skipping Pi-hole update..." - fi - - if [ "${1:-}" == "un" ]; then - purge - exit 0 - fi - - installMod - } - abort() { echo "Process Aborting..." + aborted=1 if [ -d $core_dir/.git/refs/remotes/old ]; then - download /etc .pihole old Pi-hole + download /etc .pihole "" Pi-hole swapScripts if [ -d $core_dir/advanced/Scripts/speedtestmod ]; then - cp -af $core_dir/advanced/Scripts/speedtestmod/. $opt_dir/speedtestmod/ + \cp -af $core_dir/advanced/Scripts/speedtestmod/. $opt_dir/speedtestmod/ pihole -a -s fi fi - [ ! -d $html_dir/admin/.git/refs/remotes/old ] || download $html_dir admin old web - [ -d $etc_dir/speedtest ] && [ -d $etc_dir/speedtest/.git/refs/remotes/old ] && download $etc_dir speedtest old $st_ver || : - [ -f $curr_wp ] && cat $curr_wp | grep -q SpeedTest && purge || : + [ -d $etc_dir/speedtest ] && [ -d $etc_dir/speedtest/.git/refs/remotes/old ] && download $etc_dir speedtest "" $st_ver || : + [ ! -d $html_dir/admin/.git/refs/remotes/old ] || download $html_dir admin "" web + [ -f $curr_wp ] && ! cat $curr_wp | grep -q SpeedTest && purge || : [ -f $last_db ] && [ ! -f $curr_db ] && mv $last_db $curr_db || : - aborted=1 printf "Please try again before reporting an issue.\n\n$(date)\n" } commit() { for dir in $core_dir $html_dir/admin $etc_dir/speedtest; do - if [ -d $dir/.git/refs/remotes/old ]; then - cd $dir - git remote -v | grep -q "old" && git remote remove old || : - fi + [ ! -d $dir ] && continue || cd $dir + ! git remote -v | grep -q "old" || git remote remove old + git clean -ffdx done printf "Done!\n\n$(date)\n" } @@ -246,24 +140,124 @@ if [[ "${SKIP_MOD:-}" != true ]]; then trap 'abort' INT TERM shopt -s dotglob - local db=$([ "$op" == "up" ] && echo "${3:-}" || [ "$op" == "un" ] && echo "${2:-}" || echo "$op") - case $op in - db) - manageHistory $db - ;; - un) - uninstall $db - purge - ;; - up) - uninstall $db - update ${2:-} - ;; - *) - uninstall $db - installMod - ;; - esac + local up=false + local un=false + local db=false + + for arg in "$@"; do + case $arg in + up) up=true ;; + un) un=true ;; + db) db=true ;; + esac + done + + if $db; then + if [ -f $curr_db ] && ! isEmpty $curr_db; then + echo "Flushing Database..." + mv -f $curr_db $last_db + [ ! -f $etc_dir/last_speedtest ] || mv -f $etc_dir/last_speedtest $etc_dir/last_speedtest.old + + if [ -f /var/log/pihole/speedtest.log ]; then + mv -f /var/log/pihole/speedtest.log /var/log/pihole/speedtest.log.old + rm -f $etc_dir/speedtest.log + fi + elif [ -f $last_db ]; then + echo "Restoring Database..." + mv -f $last_db $curr_db + [ ! -f $etc_dir/last_speedtest.old ] || mv -f $etc_dir/last_speedtest.old $etc_dir/last_speedtest + + if [ -f /var/log/pihole/speedtest.log.old ]; then + mv -f /var/log/pihole/speedtest.log.old /var/log/pihole/speedtest.log + \cp -af /var/log/pihole/speedtest.log $etc_dir/speedtest.log + fi + fi + fi + + if $up || $un || [ "$#" -eq 0 ]; then + local working_dir=$(pwd) + cd ~ + + if [ -f $curr_wp ] && cat $curr_wp | grep -q SpeedTest; then + echo "Restoring Pi-hole..." + pihole -a -s -1 + + if $un && [ -d $html_dir/admin.bak ]; then + [ -e $html_dir/admin ] && rm -rf $html_dir/admin + mv -f $html_dir/admin.bak $html_dir/admin + else + download $html_dir admin https://github.com/pi-hole/AdminLTE web + fi + + if $un && [ -d $core_dir.bak ]; then + [ -e $core_dir ] && rm -rf $core_dir + mv -f $core_dir.bak $core_dir + else + download /etc .pihole https://github.com/pi-hole/pi-hole Pi-hole + fi + + [ ! -d $etc_dir/speedtest ] || rm -rf $etc_dir/speedtest + st_ver=$(pihole -v -s | cut -d ' ' -f 6) + [ "$st_ver" != "HEAD" ] || st_ver=$(pihole -v -s | cut -d ' ' -f 7) + swapScripts + fi + + if $up; then + if [ -d /run/systemd/system ]; then + echo "Updating Pi-hole..." + PIHOLE_SKIP_OS_CHECK=true sudo -E pihole -up + else + echo "Systemd not found. Skipping Pi-hole update..." + fi + fi + + if $un; then + if [ -f /etc/systemd/system/pihole-speedtest.timer ]; then + rm -f /etc/systemd/system/pihole-speedtest.service + rm -f /etc/systemd/system/pihole-speedtest.timer + systemctl daemon-reload + fi + + rm -rf $opt_dir/speedtestmod + rm -f "$curr_db".* + rm -f $etc_dir/last_speedtest.* + ! isEmpty $curr_db || rm -f $curr_db + else + if [ ! -f /usr/local/bin/pihole ]; then + echo "Installing Pi-hole..." + curl -sSL https://install.pi-hole.net | sudo bash + fi + + echo "Installing Mod..." + local PHP_VERSION=$(php -v | head -n 1 | awk '{print $2}' | cut -d "." -f 1,2) + local PKG_MANAGER=$(command -v apt-get || command -v dnf || command -v yum) + local PKGS=(bc sqlite3 jq tmux wget "php$PHP_VERSION-sqlite3") + local missingPkgs=() + + for pkg in "${PKGS[@]}"; do + ! notInstalled "$pkg" || missingPkgs+=("$pkg") + done + + if [ ${#missingPkgs[@]} -gt 0 ]; then + [[ "$PKG_MANAGER" != *"apt-get"* ]] || apt-get update >/dev/null + $PKG_MANAGER install -y "${missingPkgs[@]}" &>/dev/null + fi + + [ ! -d $core_dir ] || \cp -af $core_dir $core_dir.bak + [ ! -d $html_dir/admin ] || \cp -af $html_dir/admin $html_dir/admin.bak + download $etc_dir speedtest https://github.com/arevindh/pihole-speedtest + touch $etc_dir/speedtest/updated + download /etc .pihole https://github.com/arevindh/pi-hole Pi-hole + swapScripts + \cp -af $core_dir/advanced/Scripts/speedtestmod/. $opt_dir/speedtestmod/ + pihole -a -s + download $html_dir admin https://github.com/arevindh/AdminLTE web + fi + + pihole updatechecker + [ -d $working_dir ] && cd $working_dir + fi + exit 0 } diff --git a/advanced/Scripts/speedtestmod/speedtest.sh b/advanced/Scripts/speedtestmod/speedtest.sh index b52e2eaa71..0d04d9b485 100755 --- a/advanced/Scripts/speedtestmod/speedtest.sh +++ b/advanced/Scripts/speedtestmod/speedtest.sh @@ -47,7 +47,7 @@ savetest() { rm -f "$temp_file" chmod 644 /tmp/speedtest_results mv -f /tmp/speedtest_results /var/log/pihole/speedtest.log - cp -af /var/log/pihole/speedtest.log /etc/pihole/speedtest.log + \cp -af /var/log/pihole/speedtest.log /etc/pihole/speedtest.log rm -f "$out" sqlite3 /etc/pihole/speedtest.db "$create_table" sqlite3 /etc/pihole/speedtest.db "insert into speedtest values (NULL, '${start_time}', '${stop_time}', '${isp}', '${from_ip}', '${server}', ${server_dist}, ${server_ping}, ${download}, ${upload}, '${share_url}');" diff --git a/advanced/Scripts/webpage.sh b/advanced/Scripts/webpage.sh index 4b8e8a311b..8889beef25 100755 --- a/advanced/Scripts/webpage.sh +++ b/advanced/Scripts/webpage.sh @@ -612,6 +612,7 @@ generate_cron_schedule() { addOrEditKeyValPair "${setupVars}" "SPEEDTESTSCHEDULE" "$(echo "scale=3; $total_seconds / 3600" | bc)" fi + [ -d /opt/pihole/speedtestmod ] || return sudo bash -c "cat > $(printf %q "$schedule_script")" << EOF #!/bin/bash # Schedule script to handle complex cron schedules @@ -623,10 +624,8 @@ schedule=\$(grep "SPEEDTESTSCHEDULE" "$setupVars" | cut -f2 -d"=") SKIP_MOD=true source /opt/pihole/speedtestmod/mod.sh -# if "pihole -v" includes "N/A" or doesn't include "speedtest", get v -if [[ "\$(/usr/local/bin/pihole -v)" == *"N/A"* ]] || [[ "\$(/usr/local/bin/pihole -v)" != *"Speedtest"* ]]; then - [ -d /etc/pihole/speedtest ] || download /etc/pihole speedtest https://github.com/arevindh/pihole-speedtest - /usr/local/bin/pihole updatechecker +if [ ! -f /etc/pihole/speedtest/updated ]; then + /usr/bin/tmux new-session -d "download /etc/pihole speedtest https://github.com/arevindh/pihole-speedtest ; touch /etc/pihole/speedtest/updated ; /usr/local/bin/pihole updatechecker" fi # if schedule is set and interval is "nan", set the speedtest interval to the schedule