2021-05-04 07:32:34 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
app_name="dashboard.py"
|
2021-09-09 03:56:31 +02:00
|
|
|
app_official_name="WGDashboard"
|
2021-05-05 20:38:10 +02:00
|
|
|
dashes='------------------------------------------------------------'
|
2021-09-03 20:40:57 +02:00
|
|
|
equals='============================================================'
|
2021-05-04 07:32:34 +02:00
|
|
|
help () {
|
2021-09-08 18:39:25 +02:00
|
|
|
printf "=================================================================================\n"
|
2021-09-09 03:56:31 +02:00
|
|
|
printf "+ <WGDashboard> by Donald Zou - https://github.com/donaldzou +\n"
|
2021-09-08 18:39:25 +02:00
|
|
|
printf "=================================================================================\n"
|
|
|
|
printf "| Usage: ./wgd.sh <option> |\n"
|
|
|
|
printf "| |\n"
|
|
|
|
printf "| Available options: |\n"
|
2021-09-09 03:56:31 +02:00
|
|
|
printf "| start: To start WGDashboard. |\n"
|
|
|
|
printf "| stop: To stop WGDashboard. |\n"
|
|
|
|
printf "| debug: To start WGDashboard in debug mode (i.e run in foreground). |\n"
|
|
|
|
printf "| update: To update WGDashboard to the newest version from GitHub. |\n"
|
|
|
|
printf "| install: To install WGDashboard. |\n"
|
2021-09-08 18:39:25 +02:00
|
|
|
printf "| Thank you for using! Your support is my motivation ;) |\n"
|
|
|
|
printf "=================================================================================\n"
|
2021-05-04 07:32:34 +02:00
|
|
|
}
|
|
|
|
|
2021-11-25 15:55:56 +01:00
|
|
|
_check_and_set_venv(){
|
|
|
|
# deb/ubuntu users: might need a 'apt install python3.8-venv'
|
|
|
|
# set up the local environment
|
|
|
|
APP_ROOT=`pwd`
|
|
|
|
VIRTUAL_ENV="${APP_ROOT%/*}/venv"
|
|
|
|
if [ ! -d $VIRTUAL_ENV ]; then
|
|
|
|
python3 -m venv $VIRTUAL_ENV
|
|
|
|
fi
|
2021-12-21 01:54:53 +01:00
|
|
|
. ${VIRTUAL_ENV}/bin/activate
|
2021-11-25 15:55:56 +01:00
|
|
|
}
|
|
|
|
|
2021-09-03 04:09:06 +02:00
|
|
|
install_wgd(){
|
2021-09-03 20:22:14 +02:00
|
|
|
# Check Python3 version
|
2021-12-25 22:08:30 +01:00
|
|
|
version_pass=$(python3 -c 'import sys; print("1") if (sys.version_info.major == 3 and sys.version_info.minor >= 7) else print("0");')
|
2021-09-03 20:22:14 +02:00
|
|
|
if [ $version_pass == "0" ]
|
2021-12-25 22:08:30 +01:00
|
|
|
then printf "| WGDashboard required Python3.7+ |\n"
|
2021-09-03 20:26:27 +02:00
|
|
|
printf "%s\n" "$dashes"
|
2021-09-03 20:22:14 +02:00
|
|
|
exit 1
|
2021-09-03 19:55:03 +02:00
|
|
|
fi
|
2021-09-03 04:24:07 +02:00
|
|
|
rm db/hi.txt > /dev/null 2>&1
|
2021-09-03 19:52:23 +02:00
|
|
|
if [ ! -d "log" ]
|
|
|
|
then mkdir "log"
|
|
|
|
fi
|
2021-09-03 04:09:06 +02:00
|
|
|
printf "| Installing latest Python dependencies |\n"
|
2021-11-25 15:55:56 +01:00
|
|
|
|
2021-12-25 22:29:33 +01:00
|
|
|
# _check_and_set_venv
|
|
|
|
# ${VIRTUAL_ENV}/bin/python3 -m pip install -U pip
|
2021-12-25 22:08:30 +01:00
|
|
|
# ${VIRTUAL_ENV}/bin/python3 -m pip install virtualenv
|
|
|
|
# set up the local environment
|
|
|
|
|
2021-12-25 22:29:33 +01:00
|
|
|
# ${VIRTUAL_ENV}/bin/python3 -m pip install -U -r requirements.txt
|
|
|
|
python3 -m pip install -U -r requirements.txt
|
2021-09-09 03:56:31 +02:00
|
|
|
printf "| WGDashboard installed successfully! |\n"
|
2021-11-25 15:55:56 +01:00
|
|
|
|
|
|
|
printf "| Preparing the systemctl unit file |\n"
|
2021-12-25 22:29:33 +01:00
|
|
|
# sed -i "s#{{APP_ROOT}}#${APP_ROOT}#" wg-dashboard.service
|
|
|
|
# sed -i "s#{{VIRTUAL_ENV}}#${VIRTUAL_ENV}#" wg-dashboard.service
|
2021-12-25 20:44:14 +01:00
|
|
|
# cat wg-dashboard.service | sudo SYSTEMD_EDITOR=tee systemctl edit --force --full wg-dashboard.service
|
2021-12-25 22:29:33 +01:00
|
|
|
# systemctl daemon-reload
|
|
|
|
# printf "| Consider 'systemctl enable wg-dashboard' |\n"
|
|
|
|
# printf " and 'systemctl start wg-dashboard'\n"
|
|
|
|
# printf " use '${0} stop' before starting with systemctl\n"
|
2021-11-25 15:55:56 +01:00
|
|
|
echo
|
|
|
|
printf "| Now starting Dashboard in background |\n"
|
2021-09-03 04:09:06 +02:00
|
|
|
start_wgd
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-04 07:32:34 +02:00
|
|
|
check_wgd_status(){
|
|
|
|
if ps aux | grep '[p]ython3 '$app_name > /dev/null;
|
|
|
|
then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
start_wgd () {
|
2021-11-25 15:55:56 +01:00
|
|
|
_check_and_set_venv
|
2021-09-03 20:30:13 +02:00
|
|
|
printf "%s\n" "$dashes"
|
2021-09-09 03:56:31 +02:00
|
|
|
printf "| Starting WGDashboard in the background. |\n"
|
2021-09-03 23:32:51 +02:00
|
|
|
if [ ! -d "log" ]
|
|
|
|
then mkdir "log"
|
|
|
|
fi
|
2021-05-04 07:32:34 +02:00
|
|
|
d=$(date '+%Y%m%d%H%M%S')
|
|
|
|
python3 "$app_name" > log/"$d".txt 2>&1 &
|
2021-09-03 04:24:40 +02:00
|
|
|
printf "| Log files is under log/ |\n"
|
2021-09-03 20:30:13 +02:00
|
|
|
printf "%s\n" "$dashes"
|
2021-05-04 07:32:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stop_wgd() {
|
|
|
|
kill "$(ps aux | grep "[p]ython3 $app_name" | awk '{print $2}')"
|
|
|
|
}
|
|
|
|
|
|
|
|
start_wgd_debug() {
|
2021-09-03 20:30:13 +02:00
|
|
|
printf "%s\n" "$dashes"
|
2021-11-25 15:55:56 +01:00
|
|
|
_check_and_set_venv
|
2021-09-09 03:56:31 +02:00
|
|
|
printf "| Starting WGDashboard in the foreground. |\n"
|
2021-05-04 07:32:34 +02:00
|
|
|
python3 "$app_name"
|
2021-09-03 20:30:13 +02:00
|
|
|
printf "%s\n" "$dashes"
|
2021-05-04 07:32:34 +02:00
|
|
|
}
|
|
|
|
|
2021-05-05 03:26:40 +02:00
|
|
|
update_wgd() {
|
2021-09-09 18:43:49 +02:00
|
|
|
new_ver=$(python3 -c "import json; import urllib.request; data = urllib.request.urlopen('https://api.github.com/repos/donaldzou/WGDashboard/releases/latest').read(); output = json.loads(data);print(output['tag_name'])")
|
2021-05-05 20:38:10 +02:00
|
|
|
printf "%s\n" "$dashes"
|
2021-09-03 20:30:13 +02:00
|
|
|
printf "| Are you sure you want to update to the %s? (Y/N): " "$new_ver"
|
2021-05-05 20:38:10 +02:00
|
|
|
read up
|
|
|
|
if [ "$up" = "Y" ]; then
|
2021-09-09 03:56:31 +02:00
|
|
|
printf "| Shutting down WGDashboard... |\n"
|
2021-09-03 04:52:01 +02:00
|
|
|
kill "$(ps aux | grep "[p]ython3 $app_name" | awk '{print $2}')"
|
2021-05-05 20:38:10 +02:00
|
|
|
printf "| Downloading %s from GitHub... |\n" "$new_ver"
|
2021-09-03 03:56:50 +02:00
|
|
|
git stash > /dev/null 2>&1
|
2021-05-05 21:09:34 +02:00
|
|
|
git pull https://github.com/donaldzou/wireguard-dashboard.git $new_ver --force > /dev/null 2>&1
|
2021-09-03 03:56:50 +02:00
|
|
|
printf "| Installing latest Python dependencies |\n"
|
2021-11-25 15:55:56 +01:00
|
|
|
_check_and_set_venv
|
2021-09-03 04:09:06 +02:00
|
|
|
python3 -m pip install -r requirements.txt > /dev/null 2>&1
|
2021-05-05 20:38:10 +02:00
|
|
|
printf "| Update Successfully! |\n"
|
2021-09-03 04:09:06 +02:00
|
|
|
start_wgd
|
2021-05-05 03:26:40 +02:00
|
|
|
else
|
2021-07-02 19:23:04 +02:00
|
|
|
printf "%s\n" "$dashes"
|
2021-09-03 20:30:13 +02:00
|
|
|
printf "| Update Canceled. |\n"
|
2021-07-02 19:23:04 +02:00
|
|
|
printf "%s\n" "$dashes"
|
2021-05-05 03:26:40 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-05-04 07:32:34 +02:00
|
|
|
|
|
|
|
if [ "$#" != 1 ];
|
|
|
|
then
|
|
|
|
help
|
|
|
|
else
|
|
|
|
if [ "$1" = "start" ]; then
|
|
|
|
if check_wgd_status; then
|
2021-09-03 20:30:13 +02:00
|
|
|
printf "%s\n" "$dashes"
|
2021-09-09 03:56:31 +02:00
|
|
|
printf "| WGDashboard is already running. |\n"
|
2021-09-03 20:30:13 +02:00
|
|
|
printf "%s\n" "$dashes"
|
2021-05-04 07:32:34 +02:00
|
|
|
else
|
|
|
|
start_wgd
|
|
|
|
fi
|
|
|
|
elif [ "$1" = "stop" ]; then
|
|
|
|
if check_wgd_status; then
|
2021-09-03 20:30:13 +02:00
|
|
|
printf "%s\n" "$dashes"
|
2021-05-04 07:32:34 +02:00
|
|
|
stop_wgd
|
2021-09-09 03:56:31 +02:00
|
|
|
printf "| WGDashboard is stopped. |\n"
|
2021-09-03 20:30:13 +02:00
|
|
|
printf "%s\n" "$dashes"
|
2021-05-04 07:32:34 +02:00
|
|
|
else
|
2021-09-03 20:30:13 +02:00
|
|
|
printf "%s\n" "$dashes"
|
2021-09-09 03:56:31 +02:00
|
|
|
printf "| WGDashboard is not running. |\n"
|
2021-09-03 20:30:13 +02:00
|
|
|
printf "%s\n" "$dashes"
|
2021-05-04 07:32:34 +02:00
|
|
|
fi
|
|
|
|
elif [ "$1" = "update" ]; then
|
2021-05-05 03:26:40 +02:00
|
|
|
update_wgd
|
2021-09-03 04:13:00 +02:00
|
|
|
elif [ "$1" = "install" ]; then
|
|
|
|
install_wgd
|
2021-05-04 07:32:34 +02:00
|
|
|
elif [ "$1" = "restart" ]; then
|
|
|
|
if check_wgd_status; then
|
2021-09-08 18:39:25 +02:00
|
|
|
printf "%s\n" "$dashes"
|
2021-05-04 07:32:34 +02:00
|
|
|
stop_wgd
|
2021-09-09 03:56:31 +02:00
|
|
|
printf "| WGDashboard is stopped. |\n"
|
2021-09-08 18:39:25 +02:00
|
|
|
sleep 2
|
2021-08-06 05:15:50 +02:00
|
|
|
start_wgd
|
2021-05-04 07:32:34 +02:00
|
|
|
else
|
2021-08-06 05:15:50 +02:00
|
|
|
start_wgd
|
2021-05-04 07:32:34 +02:00
|
|
|
fi
|
|
|
|
elif [ "$1" = "debug" ]; then
|
|
|
|
if check_wgd_status; then
|
2021-09-09 03:56:31 +02:00
|
|
|
printf "| WGDashboard is already running. |\n"
|
2021-05-04 07:32:34 +02:00
|
|
|
else
|
|
|
|
start_wgd_debug
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
help
|
|
|
|
fi
|
|
|
|
fi
|