WGDashboard/src/wgd.sh

167 lines
6.1 KiB
Bash
Raw Normal View History

2021-05-04 01:32:34 -04:00
#!/bin/bash
app_name="dashboard.py"
app_official_name="WGDashboard"
2021-10-18 02:40:08 +03:00
environment=$(if [[ $ENVIRONMENT ]]; then echo $ENVIRONMENT; else echo 'develop'; fi)
2021-05-05 14:38:10 -04:00
dashes='------------------------------------------------------------'
2021-09-03 14:40:57 -04:00
equals='============================================================'
2021-05-04 01:32:34 -04:00
help () {
2021-09-08 12:39:25 -04:00
printf "=================================================================================\n"
printf "+ <WGDashboard> by Donald Zou - https://github.com/donaldzou +\n"
2021-09-08 12:39:25 -04:00
printf "=================================================================================\n"
printf "| Usage: ./wgd.sh <option> |\n"
printf "| |\n"
printf "| Available options: |\n"
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 12:39:25 -04:00
printf "| Thank you for using! Your support is my motivation ;) |\n"
printf "=================================================================================\n"
2021-05-04 01:32:34 -04:00
}
2021-09-02 22:09:06 -04:00
install_wgd(){
2021-09-03 14:22:14 -04:00
# Check Python3 version
version_pass=$(python3 -c 'import sys; print("1") if (sys.version_info.major == 3 and sys.version_info.minor >= 7) else print("0");')
if [ $version_pass == "0" ]
then printf "| WGDashboard required Python3.7+ |\n"
2021-09-03 14:26:27 -04:00
printf "%s\n" "$dashes"
2021-09-03 14:22:14 -04:00
exit 1
2021-09-03 13:55:03 -04:00
fi
2021-09-02 22:24:07 -04:00
rm db/hi.txt > /dev/null 2>&1
2021-09-03 13:52:23 -04:00
if [ ! -d "log" ]
then mkdir "log"
fi
2021-09-02 22:09:06 -04:00
printf "| Installing latest Python dependencies |\n"
python3 -m pip install -r requirements.txt > /dev/null 2>&1
printf "| WGDashboard installed successfully! |\n"
2021-09-02 22:24:07 -04:00
printf "| Starting Dashboard |\n"
2021-09-02 22:09:06 -04:00
start_wgd
}
2021-05-04 01:32:34 -04:00
check_wgd_status(){
if ps aux | grep '[p]ython3 '$app_name > /dev/null;
then
return 0
else
return 1
fi
}
start_wgd () {
2021-10-18 02:40:08 +03:00
if [[ $environment == 'production' ]]; then
2021-10-18 02:24:09 +03:00
printf "%s\n" "$dashes"
printf "| Starting WGDashboard in the background. |\n"
if [ ! -d "log" ]
then mkdir "log"
fi
d=$(date '+%Y%m%d%H%M%S')
2021-10-18 02:58:54 +03:00
if [[ $USER == root ]]; then
export PATH=$PATH:/usr/local/bin:$HOME/.local/bin
fi
gunicorn --access-logfile log/access_"$d".log \
2021-10-18 02:40:08 +03:00
--error-logfile log/error_"$d".log 'dashboard:run_dashboard()'
2021-10-18 02:24:09 +03:00
printf "| Log files is under log/ |\n"
printf "%s\n" "$dashes"
else
printf "%s\n" "$dashes"
printf "| Starting WGDashboard in the background. |\n"
if [ ! -d "log" ]
then mkdir "log"
fi
d=$(date '+%Y%m%d%H%M%S')
python3 "$app_name" > log/"$d".txt 2>&1 &
printf "| Log files is under log/ |\n"
printf "%s\n" "$dashes"
2021-09-03 17:32:51 -04:00
fi
2021-05-04 01:32:34 -04:00
}
stop_wgd() {
2021-10-18 02:40:08 +03:00
if [[ $environment == 'production' ]]; then
kill $(cat ./gunicorn.pid)
else
kill "$(ps aux | grep "[p]ython3 $app_name" | awk '{print $2}')"
fi
2021-05-04 01:32:34 -04:00
}
start_wgd_debug() {
2021-09-03 14:30:13 -04:00
printf "%s\n" "$dashes"
printf "| Starting WGDashboard in the foreground. |\n"
2021-05-04 01:32:34 -04:00
python3 "$app_name"
2021-09-03 14:30:13 -04:00
printf "%s\n" "$dashes"
2021-05-04 01:32:34 -04:00
}
2021-05-04 21:26:40 -04:00
update_wgd() {
2021-09-09 12:43:49 -04: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 14:38:10 -04:00
printf "%s\n" "$dashes"
2021-09-03 14:30:13 -04:00
printf "| Are you sure you want to update to the %s? (Y/N): " "$new_ver"
2021-05-05 14:38:10 -04:00
read up
if [ "$up" = "Y" ]; then
printf "| Shutting down WGDashboard... |\n"
2021-09-02 22:52:01 -04:00
kill "$(ps aux | grep "[p]ython3 $app_name" | awk '{print $2}')"
2021-05-05 14:38:10 -04:00
printf "| Downloading %s from GitHub... |\n" "$new_ver"
2021-09-02 21:56:50 -04:00
git stash > /dev/null 2>&1
2021-05-05 15:09:34 -04:00
git pull https://github.com/donaldzou/wireguard-dashboard.git $new_ver --force > /dev/null 2>&1
2021-09-02 21:56:50 -04:00
printf "| Installing latest Python dependencies |\n"
2021-09-02 22:09:06 -04:00
python3 -m pip install -r requirements.txt > /dev/null 2>&1
2021-05-05 14:38:10 -04:00
printf "| Update Successfully! |\n"
2021-09-02 22:09:06 -04:00
start_wgd
2021-05-04 21:26:40 -04:00
else
2021-07-02 13:23:04 -04:00
printf "%s\n" "$dashes"
2021-09-03 14:30:13 -04:00
printf "| Update Canceled. |\n"
2021-07-02 13:23:04 -04:00
printf "%s\n" "$dashes"
2021-05-04 21:26:40 -04:00
fi
}
2021-05-04 01:32:34 -04:00
if [ "$#" != 1 ];
then
help
else
if [ "$1" = "start" ]; then
if check_wgd_status; then
2021-09-03 14:30:13 -04:00
printf "%s\n" "$dashes"
printf "| WGDashboard is already running. |\n"
2021-09-03 14:30:13 -04:00
printf "%s\n" "$dashes"
2021-05-04 01:32:34 -04:00
else
start_wgd
fi
elif [ "$1" = "stop" ]; then
if check_wgd_status; then
2021-09-03 14:30:13 -04:00
printf "%s\n" "$dashes"
2021-05-04 01:32:34 -04:00
stop_wgd
printf "| WGDashboard is stopped. |\n"
2021-09-03 14:30:13 -04:00
printf "%s\n" "$dashes"
2021-05-04 01:32:34 -04:00
else
2021-09-03 14:30:13 -04:00
printf "%s\n" "$dashes"
printf "| WGDashboard is not running. |\n"
2021-09-03 14:30:13 -04:00
printf "%s\n" "$dashes"
2021-05-04 01:32:34 -04:00
fi
elif [ "$1" = "update" ]; then
2021-05-04 21:26:40 -04:00
update_wgd
2021-09-02 22:13:00 -04:00
elif [ "$1" = "install" ]; then
install_wgd
2021-05-04 01:32:34 -04:00
elif [ "$1" = "restart" ]; then
if check_wgd_status; then
2021-09-08 12:39:25 -04:00
printf "%s\n" "$dashes"
2021-05-04 01:32:34 -04:00
stop_wgd
printf "| WGDashboard is stopped. |\n"
2021-09-08 12:39:25 -04:00
sleep 2
start_wgd
2021-05-04 01:32:34 -04:00
else
start_wgd
2021-05-04 01:32:34 -04:00
fi
elif [ "$1" = "debug" ]; then
if check_wgd_status; then
printf "| WGDashboard is already running. |\n"
2021-05-04 01:32:34 -04:00
else
start_wgd_debug
fi
else
help
fi
fi