Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix reinstall, uninstall, and updatecheck #106

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 45 additions & 24 deletions advanced/Scripts/speedtestmod/mod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ getVersion() {

if [ -d $1 ]; then
cd $1
foundVersion=$(git tag --points-at)
[ -z "$foundVersion" ] && foundVersion=$(git rev-parse HEAD 2>/dev/null) || foundVersion=$(echo "$foundVersion" | sort -V | tail -n1)
local tags=$(git ls-remote -t origin)
foundVersion=$(git rev-parse HEAD 2>/dev/null)

if [ -z "${2:-}" ]; then
! grep -q "$foundVersion" <<<"$tags" || foundVersion=$(grep "$foundVersion" <<<"$tags" | awk '{print $2;}' | cut -d '/' -f 3 | sort -V | tail -n1)
fi

cd - &>/dev/null
elif [ -x "$(command -v pihole)" ]; then
foundVersion=$(pihole -v | grep "$1" | cut -d ' ' -f 6)
[ "$foundVersion" != "HEAD" ] && [ "$foundVersion" != "$(git rev-parse --abbrev-ref HEAD)" ] || foundVersion=$(pihole -v | grep "$1" | cut -d ' ' -f 7)
local versions=$(pihole -v | grep "$1")
foundVersion=$(cut -d ' ' -f 6 <<<"$versions")
[ "$foundVersion" != "HEAD" ] && [ "$foundVersion" != "$(git rev-parse --abbrev-ref HEAD)" ] || foundVersion=$(cut -d ' ' -f 7 <<<"$versions")
fi

echo $foundVersion
Expand All @@ -31,7 +37,7 @@ download() {
cd "$dest"
git config --global --add safe.directory "$dest"

if [ ! -z "$desiredVersion" ]; then
if [ ! -z "$desiredVersion" ] && [[ "$desiredVersion" != *.* ]]; then
local repos=("Pi-hole" "web" "speedtest")

for repo in "${repos[@]}"; do
Expand All @@ -49,32 +55,35 @@ download() {
elif git remote -v | grep -q "old"; then
! git remote -v | grep -q "origin" || git remote remove origin
git remote rename old origin
url=$(git remote get-url origin)
fi

url=$(git remote get-url origin)
[[ "$url" == *"ipitio"* ]] && snapToTag=$(echo "$snapToTag" | grep -q "true" && echo "false" || echo "true")
[[ "$url" == *"ipitio"* ]] && snapToTag=$(grep -q "true" <<<"$snapToTag" && echo "false" || echo "true")
git fetch origin --depth=1 $branch:refs/remotes/origin/$branch -q
git reset --hard origin/"$branch" -q
git checkout -B "$branch" -q
local currentVersion=$(getVersion "$dest")
[[ "$currentVersion" != *.* ]] || currentVersion=$(git ls-remote -t "$url" | grep $currentVersion$ | awk '{print $1;}')
local tags=$(git ls-remote -t origin)

if [[ "$currentVersion" == *.* ]]; then
grep -q "$currentVersion$" <<<"$tags" && currentVersion=$(grep "$currentVersion$" <<<"$tags" | awk '{print $1;}') || $currentVersion $(git rev-parse origin/$branch)
fi

if [ -z "$desiredVersion" ]; then # if empty, get the latest version
if [ "$snapToTag" == "true" ]; then
local latestTag=$(git ls-remote -t "$url" | awk -F/ '{print $3}' | grep '^v[0-9]' | grep -v '\^{}' | sort -V | tail -n1)
local latestTag=$(awk -F/ '{print $3}' <<<"$tags" | grep '^v[0-9]' | grep -v '\^{}' | sort -V | tail -n1)
[ ! -z "$latestTag" ] && desiredVersion=$latestTag || desiredVersion=$currentVersion
fi
elif $aborting; then
desiredVersion=$(getVersion "$desiredVersion")
fi

[[ "$desiredVersion" != *.* ]] || desiredVersion=$(git ls-remote -t "$url" | grep $desiredVersion$ | awk '{print $1;}')

if [ "$currentVersion" != "$desiredVersion" ]; then
git fetch origin --depth=1 $desiredVersion -q
git -c advice.detachedHead=false checkout $desiredVersion -q
if [[ "$desiredVersion" == *.* ]]; then
grep -q "$desiredVersion$" <<<"$tags" && desiredVersion=$(grep "$desiredVersion$" <<<"$tags" | awk '{print $1;}') || desiredVersion=$currentVersion
fi

git fetch origin --depth=1 $desiredVersion -q
git -c advice.detachedHead=false checkout $desiredVersion -q
cd ..
}

Expand All @@ -97,8 +106,17 @@ setCnf() {
}

getCnf() {
local keydir=$(echo $2 | sed 's/^mod-//;s/^org-//')
local value=$(grep "^$2=" $1 | cut -d '=' -f 2)
[ -z "$value" ] && value=$(getVersion $(echo $2 | sed 's/^mod-//;s/^org-//'))
[ ! -z "$value" ] || value=$(getVersion $keydir "${3:-}")

if [ ! -z "${3:-}" ] && [[ "$value" == *.* ]]; then
cd $keydir
local tags=$(git ls-remote -t origin)
grep -q "$value$" <<<"$tags" && ver=$(grep "$value$" <<<"$tags" | awk '{print $1;}') || value=$(git rev-parse HEAD)
cd - &>/dev/null
fi

echo $value
}

Expand Down Expand Up @@ -134,16 +152,18 @@ if [[ "${SKIP_MOD:-}" != true ]]; then
echo " -u, --update, up also update Pi-hole, unless Systemd is not being used (ie. not in Docker)"
echo " -b, --backup preserve stock Pi-hole files for faster offline restore"
echo " -o, --online force online restore of stock Pi-hole files even if a backup exists"
echo " -i, --install skip restore of stock Pi-hole (for when not updating Pi-hole nor switching repos)"
echo " -r, --reinstall keep current version of the mod, if installed"
echo " -i, --install skip restore of stock Pi-hole*"
echo " -r, --reinstall keep current version of the mod, if installed*"
echo " -t, --testing install the latest commit"
echo " -n, --uninstall, un remove the mod and its files, but keep the database"
echo " -d, --database, db flush/restore the database if it's not/empty (and exit if this is the only arg given)"
echo " -v, --version display the version of the mod"
echo " -x, --verbose show the commands being run"
echo " -c, --careless skip checking for missing dependencies"
echo " -c, --careless skip check for missing dependencies"
echo " -h, --help display this help message"
echo ""
echo " *for when not updating Pi-hole nor switching repos"
echo ""
echo "Examples:"
echo " sudo bash /opt/pihole/speedtestmod/mod.sh -ubo"
echo " sudo bash /opt/pihole/speedtestmod/mod.sh -i -r -d"
Expand Down Expand Up @@ -319,10 +339,11 @@ if [[ "${SKIP_MOD:-}" != true ]]; then

if [ -f $curr_wp ] && cat $curr_wp | grep -q SpeedTest; then
if $reinstall; then
echo "Reinstalling Mod..."
mod_core_ver=$(getCnf $mod_dir/cnf mod-$core_dir)
mod_admin_ver=$(getCnf $mod_dir/cnf mod-$html_dir/admin)
st_ver=$(getCnf $mod_dir/cnf mod-$mod_dir)
# if hashes of current and stored versions are the same, use stored version, else use the current version
# since versions could be tags or commit hashes, and commits can have multiple tags
[ "$(getVersion $core_dir hash)" == "$(getCnf $mod_dir/cnf mod-$core_dir hash)" ] && mod_core_ver=$(getCnf $mod_dir/cnf mod-$core_dir) || mod_core_ver=$(getVersion $core_dir)
[ "$(getVersion $html_dir/admin hash)" == "$(getCnf $mod_dir/cnf mod-$html_dir/admin hash)" ] && mod_admin_ver=$(getCnf $mod_dir/cnf mod-$html_dir/admin) || mod_admin_ver=$(getVersion $html_dir/admin)
[ "$(getVersion $mod_dir hash)" == "$(getCnf $mod_dir/cnf mod-$mod_dir hash)" ] && st_ver=$(getCnf $mod_dir/cnf mod-$mod_dir) || st_ver=$(getVersion $mod_dir)
fi

if ! $install; then
Expand Down Expand Up @@ -379,7 +400,7 @@ if [[ "${SKIP_MOD:-}" != true ]]; then
fi
fi

echo "Swapping Repos..."
echo "Patching Repos..."
download /etc pihole-speedtest https://github.com/arevindh/pihole-speedtest "$st_ver" master $stable
[ -f $mod_dir/cnf ] || touch $mod_dir/cnf
setCnf mod-$mod_dir "$(getVersion $mod_dir)" $mod_dir/cnf $reinstall
Expand Down Expand Up @@ -408,7 +429,7 @@ if [[ "${SKIP_MOD:-}" != true ]]; then
done

$backup || download /etc .pihole https://github.com/arevindh/pi-hole "$mod_core_ver" master $stable
echo "Installing Mod..."
$reinstall && echo "Reinstalling Mod..." || echo "Installing Mod..."
swapScripts
\cp -af $core_dir/advanced/Scripts/speedtestmod/. $opt_dir/speedtestmod/
pihole -a -s
Expand Down
14 changes: 7 additions & 7 deletions advanced/Scripts/updatecheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
# This file is copyright under the latest version of the EUPL.
# Please see LICENSE file for your rights under this license.

SKIP_MOD=true
source /opt/pihole/speedtestmod/mod.sh

function get_local_branch() {
# Return active branch
cd "${1}" 2> /dev/null || return 1
Expand All @@ -20,7 +17,10 @@ function get_local_branch() {
function get_local_version() {
# Return active version
cd "${1}" 2> /dev/null || return 1
getCnf /etc/pihole-speedtest/cnf mod-$1
local tags=$(git ls-remote -t origin)
local foundVersion=$(git rev-parse HEAD 2>/dev/null)
! grep -q "$foundVersion" <<<"$tags" || foundVersion=$(grep "$foundVersion" <<<"$tags" | awk '{print $2;}' | cut -d '/' -f 3 | sort -V | tail -n1)
echo "${foundVersion}"
}

function get_local_hash() {
Expand All @@ -29,7 +29,7 @@ function get_local_hash() {
}

function get_remote_version() {
if [[ "${1}" == "docker-pi-hole" ]]; then
if [ "${1}" == "docker-pi-hole" ] || [ "${1}" == "FTL" ]; then
curl -s "https://api.github.com/repos/pi-hole/${1}/releases/latest" 2> /dev/null | jq --raw-output .tag_name || return 1
else
curl -s "https://api.github.com/repos/arevindh/${1}/releases/latest" 2> /dev/null | jq --raw-output .tag_name || { curl -s "https://api.github.com/repos/pi-hole/${1}/releases/latest" 2> /dev/null | jq --raw-output .tag_name || return 1; }
Expand Down Expand Up @@ -104,10 +104,10 @@ if [[ "${INSTALL_WEB_INTERFACE}" == true ]]; then
WEB_HASH="$(get_local_hash /var/www/html/admin)"
addOrEditKeyValPair "${VERSION_FILE}" "WEB_HASH" "${WEB_HASH}"

GITHUB_WEB_VERSION="$(get_remote_version web)"
GITHUB_WEB_VERSION="$(get_remote_version AdminLTE)"
addOrEditKeyValPair "${VERSION_FILE}" "GITHUB_WEB_VERSION" "${GITHUB_WEB_VERSION}"

GITHUB_WEB_HASH="$(get_remote_hash web "${WEB_BRANCH}")"
GITHUB_WEB_HASH="$(get_remote_hash AdminLTE "${WEB_BRANCH}")"
addOrEditKeyValPair "${VERSION_FILE}" "GITHUB_WEB_HASH" "${GITHUB_WEB_HASH}"

fi
Expand Down