Skip to content

Commit

Permalink
v2.9 (#117)
Browse files Browse the repository at this point in the history
* v2.9
  • Loading branch information
ipitio committed May 2, 2024
1 parent 332b998 commit 9ed2515
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 119 deletions.
30 changes: 17 additions & 13 deletions advanced/Scripts/speedtestmod/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ download() {
# 0 if available, 1 if not
#######################################
isAvailable() {
if [[ "$PKG_MANAGER" == *"apt-get"* ]]; then
if [[ "$PKG_MANAGER" == *"apt"* ]]; then
# Check if there is a candidate and it is not "(none)"
apt-cache policy "$1" | grep -q "Candidate:" && ! apt-cache policy "$1" | grep -q "Candidate: (none)" && return 0 || return 1
elif [[ "$PKG_MANAGER" == *"dnf"* || "$PKG_MANAGER" == *"yum"* ]]; then
Expand All @@ -156,7 +156,7 @@ isAvailable() {
# 0 if the package is not installed, 1 if it is
#######################################
notInstalled() {
if [[ "$PKG_MANAGER" == *"apt-get"* ]]; then
if [[ "$PKG_MANAGER" == *"apt"* ]]; then
dpkg -s "$1" &>/dev/null || return 0
elif [[ "$PKG_MANAGER" == *"dnf"* || "$PKG_MANAGER" == *"yum"* ]]; then
rpm -q "$1" &>/dev/null || return 0
Expand Down Expand Up @@ -215,7 +215,7 @@ getCnf() {
# 0 if the installation was successful, 1 if it was not
#######################################
libreSpeed() {
echo "Installing LibreSpeed..."
echo "Installing librespeed-cli..."
$PKG_MANAGER remove -y speedtest-cli speedtest >/dev/null 2>&1

if notInstalled golang; then
Expand All @@ -224,12 +224,14 @@ libreSpeed() {
echo "Adding testing repo to sources.list.d"
echo "deb http://archive.raspbian.org/raspbian/ testing main" >/etc/apt/sources.list.d/testing.list
printf "Package: *\nPin: release a=testing\nPin-Priority: 50" >/etc/apt/preferences.d/limit-testing
$PKG_MANAGER update -y
$PKG_MANAGER update -y &>/dev/null
fi

$PKG_MANAGER install -y -t testing golang
isAvailable golang || $PKG_MANAGER update -y &>/dev/null
$PKG_MANAGER install -y -t testing golang >/dev/null 2>&1
else
$PKG_MANAGER install -y golang
[[ $PKG_MANAGER == *"apt"* ]] && ! isAvailable golang && $PKG_MANAGER update -y &>/dev/null || :
$PKG_MANAGER install -y golang >/dev/null 2>&1
fi
fi

Expand All @@ -243,7 +245,7 @@ libreSpeed() {
chmod +x /usr/bin/speedtest

if [[ -x /usr/bin/speedtest ]]; then
echo "Installed LibreSpeed."
echo "Installed librespeed-cli"
return 0
fi

Expand Down Expand Up @@ -275,9 +277,11 @@ swivelSpeed() {
/usr/bin/yum) "$PKG_MANAGER" install -y --allowerasing "$candidate" &>/dev/null ;;
esac

if ! notInstalled "$candidate"; then
if ! notInstalled "$candidate" && [[ -x /usr/bin/speedtest ]]; then
printf "Installed "
/usr/bin/speedtest --version || return 1
local version=
version=$(/usr/bin/speedtest --version) || return 1
echo "${version%%$'\n'*}"
return 0
fi

Expand All @@ -298,7 +302,7 @@ ooklaSpeed() {
if [[ "$PKG_MANAGER" == *"yum"* || "$PKG_MANAGER" == *"dnf"* && ! -f /etc/yum.repos.d/ookla_speedtest-cli.repo ]]; then
echo "Adding speedtest source for RPM..."
curl -sSLN https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh | sudo bash
elif [[ "$PKG_MANAGER" == *"apt-get"* && ! -f /etc/apt/sources.list.d/ookla_speedtest-cli.list ]]; then
elif [[ "$PKG_MANAGER" == *"apt"* && ! -f /etc/apt/sources.list.d/ookla_speedtest-cli.list ]]; then
echo "Adding speedtest source for DEB..."
if [[ -e /etc/os-release ]]; then
# shellcheck disable=SC1091
Expand All @@ -315,10 +319,10 @@ ooklaSpeed() {
fi
wget -O /tmp/script.deb.sh https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh >/dev/null 2>&1
chmod +x /tmp/script.deb.sh
os=$os dist=$dist /tmp/script.deb.sh
os=$os dist=$dist /tmp/script.deb.sh >/dev/null 2>&1
rm -f /tmp/script.deb.sh
else
curl -sSLN https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh | sudo bash
curl -sSLN https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh | sudo bash >/dev/null 2>&1
fi

sed -i 's/g]/g allow-insecure=yes trusted=yes]/' /etc/apt/sources.list.d/ookla_speedtest-cli.list
Expand Down Expand Up @@ -405,7 +409,7 @@ ExecStart=/usr/local/bin/pihole -a -sn
[Install]
WantedBy=multi-user.target
EOF'
sudo bash -c 'cat > /etc/systemd/system/pihole-speedtest.timer << EOF
sudo bash -c 'cat > /etc/systemd/system/pihole-speedtest.timer << EOF
[Unit]
Description=Pi-hole Speedtest Timer
Expand Down
137 changes: 82 additions & 55 deletions advanced/Scripts/speedtestmod/mod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
# shellcheck disable=SC2015
#

declare -r MOD_REPO="arevindh"
declare -r MOD_BRANCH="master"
declare -r CORE_BRANCH="master"
declare -r ADMIN_BRANCH="master"
declare -r HTML_DIR="/var/www/html"
declare -r CORE_DIR="/etc/.pihole"
declare -r OPT_DIR="/opt/pihole"
Expand All @@ -15,17 +19,18 @@ declare -r CURR_WP="$OPT_DIR/webpage.sh"
declare -r CURR_DB="$ETC_DIR/speedtest.db"
declare -r LAST_DB="$CURR_DB.old"
declare -r DB_TABLE="speedtest"
declare -i aborted=0
st_ver=""
mod_core_ver=""
mod_admin_ver=""
cleanup=true
declare cleanup
declare aborted
cleanup=$(mktemp)
aborted=$(mktemp)
echo "false" >"$cleanup"
echo "false" >"$aborted"
# shellcheck disable=SC2034
SKIP_INSTALL=true
# shellcheck disable=SC1091
source "$CORE_DIR/automated install/basic-install.sh"
# shellcheck disable=SC1090,SC1091
[[ -f "$OPT_DIR/speedtestmod/lib.sh" ]] && source "$OPT_DIR/speedtestmod/lib.sh" || source <(curl -sSLN https://github.com/arevindh/pi-hole/raw/master/advanced/Scripts/speedtestmod/lib.sh)
[[ -f "$OPT_DIR/speedtestmod/lib.sh" ]] && source "$OPT_DIR/speedtestmod/lib.sh" || source <(curl -sSLN https://github.com/"$MOD_REPO"/pi-hole/raw/"$CORE_BRANCH"/advanced/Scripts/speedtestmod/lib.sh)

#######################################
# Display the help message
Expand All @@ -41,35 +46,34 @@ help() {
"The Mod Script"
"Usage: sudo bash /path/to/mod.sh [options]"
" or: curl -sSLN //link/to/mod.sh | sudo bash [-s -- options]"
"(Re)install the latest release of the Speedtest Mod and/or the following options:"
" or: pihole -a -sm [options]"
"(Re)install Speedtest Mod and/or the following options:"
""
"Installation:"
" -u, --update, up also update Pi-hole"
" -r, --reinstall keep current version of the mod, if installed"
" -t, --testing install the beta version of the mod"
" -r, --reinstall repair currently installed version of the Mod"
" -t, --testing try unstable changes"
""
"Restoration:"
" -n, --uninstall, un purge the mod, keeping the speedtest package, logs, and database"
" -n, --uninstall, un purge the Mod, keeping the speedtest package, logs, and database"
" -b, --backup backup Pi-hole for faster offline restore"
" -o, --online force online restore of Pi-hole"
""
"Standalone:"
" -d, --database, db flush/restore the database if it's not/empty"
" -s, --speedtest[=<sivel|libre>] install Ookla's or the specified CLI even if another is already installed"
" -s, --speedtest[=<sivel|libre>] install Ookla's or the specified CLI immediately"
" -x, --verbose show the commands being run"
" -v, --version display the version of the mod and exit"
" -v, --version display the installed version of the Mod and exit"
" -h, --help display this help message and exit"
""
"Examples:"
" sudo bash /opt/pihole/speedtestmod/mod.sh -d -slibre"
" sudo bash /opt/pihole/speedtestmod/mod.sh --uninstall"
" curl -sSL https://github.com/arevindh/pihole-speedtest/raw/master/mod | sudo bash"
" curl -sSLN https://github.com/arevindh/pi-hole/raw/master/advanced/Scripts/speedtestmod/mod.sh | sudo bash -s -- -bo"
" pihole -a -sm -d -slibre"
" sudo bash /opt/pihole/speedtestmod/mod.sh --update"
" curl -sSL https://github.com/$MOD_REPO/pihole-speedtest/raw/$CORE_BRANCH/mod | sudo bash"
" curl -sSLN https://github.com/$MOD_REPO/pi-hole/raw/$CORE_BRANCH/advanced/Scripts/speedtestmod/mod.sh | sudo bash -s -- -bo"
)

printf "%s\n" "${help_text[@]}"
cleanup=false
exit 0
}

#######################################
Expand Down Expand Up @@ -166,9 +170,9 @@ purge() {
# The changes reverted
# shellcheck disable=SC2317 ###########
abort() {
if $cleanup && [[ $aborted -eq 0 ]]; then
if grep -q true "$cleanup" && grep -q false "$aborted"; then
echo "Process Aborting..."
aborted=1
echo "true" >"$aborted"

if [[ -d "$CORE_DIR"/.git/refs/remotes/old ]]; then
download /etc .pihole "" Pi-hole
Expand Down Expand Up @@ -200,7 +204,7 @@ abort() {
# The repositories cleaned up
# shellcheck disable=SC2317 ###########
commit() {
if $cleanup; then
if grep -q true "$cleanup"; then
for dir in $CORE_DIR $HTML_DIR/admin; do
[[ ! -d "$dir" ]] && continue || pushd "$dir" &>/dev/null || exit 1
! git remote -v | grep -q "old" || git remote remove old
Expand All @@ -223,20 +227,24 @@ commit() {
# CURR_DB
# LAST_DB
# ETC_DIR
# st_ver
# mod_core_ver
# mod_admin_ver
# cleanup
# Arguments:
# $@: The options for managing the installation
# Outputs:
# The installation managed
#######################################
main() {
set -u

local -r short_opts=-ubortnds::vxh
local -r long_opts=update,backup,online,reinstall,testing,uninstall,database,speedtest::,version,verbose,help
local parsed_opts
parsed_opts=$(getopt --options ${short_opts} --longoptions ${long_opts} --name "$0" -- "$@") || help

if ! parsed_opts=$(getopt --options ${short_opts} --longoptions ${long_opts} --name "$0" -- "$@"); then
help
return 1
fi

eval set -- "${parsed_opts}"

declare -a POSITIONAL EXTRA_ARGS
Expand All @@ -252,6 +260,9 @@ main() {
local select_test=false
local selected_test=""
local do_main=false
local st_ver=""
local mod_core_ver=""
local mod_admin_ver=""

while [[ $# -gt 0 ]]; do
case "$1" in
Expand Down Expand Up @@ -282,16 +293,24 @@ main() {
-d | --database) database=true ;;
-s | --speedtest)
select_test=true
[[ -n "$2" && ! "$2" =~ sivel|libre ]] && help || selected_test=$2

if [[ -n "$2" && ! "$2" =~ sivel|libre ]]; then
help
return 1
fi

selected_test=$2
shift
;;
-v | --version)
getVersion $MOD_DIR
cleanup=false
exit 0
return 0
;;
-x | --verbose) verbose=true ;;
-h | --help) help ;;
-h | --help)
help
return 0
;;
--) dashes=1 ;;
*) [[ $dashes -eq 0 ]] && POSITIONAL+=("$1") || EXTRA_ARGS+=("$1") ;;
esac
Expand All @@ -306,18 +325,31 @@ main() {
up) update=true ;;
un) uninstall=true ;;
db) database=true ;;
*) help ;;
*)
help
return 1
;;
esac
done

readonly update backup online reinstall stable uninstall database verbose cleanup select_test selected_test
echo "true" >"$cleanup"
! $do_main && ! $database && ! $select_test && do_main=true || :
readonly update backup online reinstall stable uninstall database verbose select_test selected_test do_main
printf "%s\n\nRunning the Mod Script by @ipitio...\n" "$(date)"
! $verbose || set -x

if $select_test; then
case $selected_test in
sivel) swivelSpeed ;;
libre) libreSpeed ;;
*) ooklaSpeed ;;
esac
fi

set -Eeo pipefail
trap '[ "$?" -eq "0" ] && commit || abort' EXIT
trap 'abort' INT TERM ERR
set -Eeuo pipefail
shopt -s dotglob
printf "%s\n\nRunning the Mod Script by @ipitio...\n" "$(date)"
! $do_main && ! $database && ! $select_test && do_main=true || :
! $verbose || set -x

if $database; then
if [[ -f $CURR_DB ]] && ! isEmpty $CURR_DB; then
Expand All @@ -341,16 +373,6 @@ main() {
fi
fi

if $select_test; then
set +Eeo pipefail # don't exit on error, not critical
case $selected_test in
sivel) swivelSpeed ;;
libre) libreSpeed ;;
*) ooklaSpeed ;;
esac
set -Eeo pipefail
fi

if $do_main; then
if [[ ! -f /usr/local/bin/pihole ]]; then
# https://discourse.pi-hole.net/t/pi-hole-as-part-of-a-post-installation-script/3523/15
Expand Down Expand Up @@ -453,7 +475,7 @@ EOF
if [[ ${#missingpkgs[@]} -gt 0 ]]; then
echo "Installing Missing Dependencies..."
if ! $PKG_MANAGER install -y "${missingpkgs[@]}" &>/dev/null; then
[[ "$PKG_MANAGER" == *"apt-get"* ]] || exit 1
[[ "$PKG_MANAGER" == *"apt"* ]] || exit 1
echo "And Updating Package Cache..."
$PKG_MANAGER update -y &>/dev/null
$PKG_MANAGER install -y "${missingpkgs[@]}" &>/dev/null
Expand All @@ -466,8 +488,10 @@ EOF
local -r installed_admin_ver=$(getVersion "web")
if [[ "$installed_core_ver" == *.* && "$installed_admin_ver" == *.* ]]; then
echo "Finding Latest Compatible Versions..."
mod_core_ver=$(git ls-remote "https://github.com/arevindh/pi-hole" | grep -q "$installed_core_ver" && git ls-remote "https://github.com/arevindh/pi-hole" | grep "$installed_core_ver" | awk '{print $2;}' | cut -d '/' -f 3 | sort -Vr | head -n1 || echo "")
mod_admin_ver=$(git ls-remote "https://github.com/arevindh/AdminLTE" | grep -q "$installed_admin_ver" && git ls-remote "https://github.com/arevindh/AdminLTE" | grep "$installed_admin_ver" | awk '{print $2;}' | cut -d '/' -f 3 | sort -Vr | head -n1 || echo "")
local -r remote_core_ver=$(git ls-remote "https://github.com/$MOD_REPO/pi-hole")
local -r remote_admin_ver=$(git ls-remote "https://github.com/$MOD_REPO/AdminLTE")
mod_core_ver=$(grep -q "$installed_core_ver" <<<"$remote_core_ver" && grep "$installed_core_ver" <<<"$remote_core_ver" | awk '{print $2;}' | cut -d '/' -f 3 | sort -Vr | head -n1 || echo "")
mod_admin_ver=$(grep -q "$installed_admin_ver" <<<"$remote_admin_ver" && grep "$installed_admin_ver" <<<"$remote_admin_ver" | awk '{print $2;}' | cut -d '/' -f 3 | sort -Vr | head -n1 || echo "")
fi
fi
elif [[ -d /run/systemd/system ]]; then
Expand All @@ -479,12 +503,12 @@ EOF

if $backup; then
echo "Creating Backup..."
download /etc .pihole.mod https://github.com/arevindh/pi-hole "$mod_core_ver" master $stable
download $HTML_DIR admin.mod https://github.com/arevindh/AdminLTE "$mod_admin_ver" master $stable
download /etc .pihole.mod https://github.com/"$MOD_REPO"/pi-hole "$mod_core_ver" "$CORE_BRANCH" $stable
download $HTML_DIR admin.mod https://github.com/"$MOD_REPO"/AdminLTE "$mod_admin_ver" "$ADMIN_BRANCH" $stable
fi

$reinstall && echo "Reinstalling Mod..." || echo "Installing Mod..."
download /etc pihole-speedtest https://github.com/arevindh/pihole-speedtest "$st_ver" master $stable
download /etc pihole-speedtest https://github.com/"$MOD_REPO"/pihole-speedtest "$st_ver" "$MOD_BRANCH" $stable
[[ -f $MOD_DIR/cnf ]] || touch $MOD_DIR/cnf
setCnf mod-$MOD_DIR "$(getVersion $MOD_DIR)" $MOD_DIR/cnf $reinstall
local stock_tag
Expand All @@ -506,11 +530,11 @@ EOF
fi
done

$backup || download /etc .pihole https://github.com/arevindh/pi-hole "$mod_core_ver" master $stable
$backup || download /etc .pihole https://github.com/"$MOD_REPO"/pi-hole "$mod_core_ver" "$CORE_BRANCH" $stable
swapScripts
\cp -af $CORE_DIR/advanced/Scripts/speedtestmod/. $OPT_DIR/speedtestmod/
pihole -a -s
$backup || download $HTML_DIR admin https://github.com/arevindh/AdminLTE "$mod_admin_ver" master $stable
$backup || download $HTML_DIR admin https://github.com/"$MOD_REPO"/AdminLTE "$mod_admin_ver" "$ADMIN_BRANCH" $stable
setCnf mod-$CORE_DIR "$(getVersion $CORE_DIR)" $MOD_DIR/cnf $reinstall
setCnf mod-$HTML_DIR/admin "$(getVersion $HTML_DIR/admin)" $MOD_DIR/cnf $reinstall
fi
Expand All @@ -531,5 +555,8 @@ fi
rm -f /tmp/pimod.log
touch /tmp/pimod.log
main "$@" 2>&1 | tee -a /tmp/pimod.log
$cleanup && mv -f /tmp/pimod.log /var/log/pihole/mod.log || rm -f /tmp/pimod.log
exit $aborted
grep -q false "$cleanup" || mv -f /tmp/pimod.log /var/log/pihole/mod.log && rm -f /tmp/pimod.log
return_status=$(<"$aborted")
rm -f "$cleanup"
rm -f "$aborted"
[[ "$return_status" == "true" ]] && exit 1 || exit 0
Loading

0 comments on commit 9ed2515

Please sign in to comment.