2021-05-04 07:32:34 +02:00
#!/bin/bash
2024-06-19 11:09:58 +02:00
# wgd.sh - Copyright(C) 2024 Donald Zou [https://github.com/donaldzou]
2022-01-13 01:53:36 +01:00
# Under Apache-2.0 License
2021-05-04 07:32:34 +02:00
app_name = "dashboard.py"
2021-09-09 03:56:31 +02:00
app_official_name = "WGDashboard"
2024-08-03 20:01:04 +02:00
venv_python = "./venv/bin/python3"
venv_gunicorn = "./venv/bin/gunicorn"
2022-01-18 16:42:23 +01:00
PID_FILE = ./gunicorn.pid
2021-10-18 01:40:08 +02:00
environment = $( if [ [ $ENVIRONMENT ] ] ; then echo $ENVIRONMENT ; else echo 'develop' ; fi )
2021-10-25 00:16:02 +02:00
if [ [ $CONFIGURATION_PATH ] ] ; then
cb_work_dir = $CONFIGURATION_PATH /letsencrypt/work-dir
cb_config_dir = $CONFIGURATION_PATH /letsencrypt/config-dir
else
cb_work_dir = /etc/letsencrypt
cb_config_dir = /var/lib/letsencrypt
fi
2021-05-05 20:38:10 +02:00
dashes = '------------------------------------------------------------'
2021-09-03 20:40:57 +02:00
equals = '============================================================'
2024-08-02 23:27:28 +02:00
helpMsg = "[WGDashboard] Please check ./log/install.txt for more details. For further assistance, please open a ticket on https://github.com/donaldzou/WGDashboard/issues/new/choose, I'm more than happy to help :)"
2021-05-04 07:32:34 +02:00
help ( ) {
2021-09-08 18:39:25 +02:00
printf "=================================================================================\n"
2021-12-25 23:13:21 +01: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( ) {
2024-08-02 23:27:28 +02:00
VIRTUAL_ENV = "./venv"
2021-11-25 15:55:56 +01:00
if [ ! -d $VIRTUAL_ENV ] ; then
2024-08-02 23:27:28 +02:00
printf "[WGDashboard] Creating Python Virtual Environment under ./venv\n"
2024-08-03 00:30:16 +02:00
{ python3 -m venv $VIRTUAL_ENV ; } >> ./log/install.txt
2021-11-25 15:55:56 +01:00
fi
2024-08-03 00:18:31 +02:00
printf "[WGDashboard] Activate Python Virtual Environment under ./venv\n"
. ${ VIRTUAL_ENV } /bin/activate
2021-11-25 15:55:56 +01:00
}
2024-08-02 23:27:28 +02:00
_determineOS( ) {
if [ -f /etc/os-release ] ; then
. /etc/os-release
OS = $ID
elif [ -f /etc/redhat-release ] ; then
OS = "redhat"
# elif [ -f /etc/arch-release ]; then
# OS="arch"
else
printf "[WGDashboard] Sorry, your OS is not supported. Currently the install script only support Debian-based, Red Hat-based OS."
printf "%s\n" " $helpMsg "
exit 1
fi
}
_installPython( ) {
case " $OS " in
ubuntu| debian)
2024-08-03 03:21:19 +02:00
{ sudo apt update ; sudo apt-get install -y python3; printf "\n\n" ; } & >> ./log/install.txt
2024-08-02 23:27:28 +02:00
; ;
centos| fedora| redhat)
if command -v dnf & > /dev/null; then
{ sudo dnf install -y python3; printf "\n\n" ; } >> ./log/install.txt
else
{ sudo yum install -y python3; printf "\n\n" ; } >> ./log/install.txt
fi
; ;
# arch)
# { sudo pacman -Sy python python-pip; printf "\n\n"; } >> ./log/install.txt
# ;;
# *)
# printf "[WGDashboard] Sorry, your OS is not supported. Currently the install script only support Debian-based, Red Hat-based and Arch-based OS."
# printf "%s" "$helpMsg"
# exit 1
# ;;
esac
if ! python3 --version > /dev/null 2>& 1
then
printf "[WGDashboard] Python is still not installed, halting script now.\n"
printf "%s\n" " $helpMsg "
exit 1
else
printf "[WGDashboard] \xE2\x9C\x94 Python is installed\n"
fi
}
_installPythonVenv( ) {
case " $OS " in
ubuntu| debian)
2024-08-03 03:21:19 +02:00
{ sudo apt update ; sudo apt-get install -y python3-venv; printf "\n\n" ; } & >> ./log/install.txt
2024-08-02 23:27:28 +02:00
; ;
centos| fedora| redhat)
if command -v dnf & > /dev/null; then
{ sudo dnf install -y python3-virtualenv; printf "\n\n" ; } >> ./log/install.txt
else
{ sudo yum install -y python3-virtualenv; printf "\n\n" ; } >> ./log/install.txt
fi
; ;
*)
printf "[WGDashboard] Sorry, your OS is not supported. Currently the install script only support Debian-based, Red Hat-based OS."
printf "%s\n" " $helpMsg "
exit 1
; ;
esac
if ! python3 -m venv -h > /dev/null 2>& 1
then
printf "[WGDashboard] Python Virtual Environment is still not installed, halting script now.\n"
printf "%s\n" " $helpMsg "
else
printf "[WGDashboard] \xE2\x9C\x94 Python Virtual Environment is installed\n"
fi
}
_installPythonPip( ) {
case " $OS " in
ubuntu| debian)
2024-08-03 03:21:19 +02:00
{ sudo apt update ; sudo apt-get install -y python3-pip; printf "\n\n" ; } & >> ./log/install.txt
2024-08-02 23:27:28 +02:00
; ;
centos| fedora| redhat)
if command -v dnf & > /dev/null; then
{ sudo dnf install -y python3-pip; printf "\n\n" ; } >> ./log/install.txt
else
{ sudo yum install -y python3-pip printf "\n\n" ; } >> ./log/install.txt
fi
; ;
*)
printf "[WGDashboard] Sorry, your OS is not support auto install. Currently the install script only support Debian-based, Red Hat-based OS."
printf "%s\n" " $helpMsg "
exit 1
; ;
esac
if ! python3 -m pip -h > /dev/null 2>& 1
then
printf "[WGDashboard] Python Package Manager (PIP) is still not installed, halting script now.\n"
printf "%s\n" " $helpMsg "
else
printf "[WGDashboard] \xE2\x9C\x94 Python Package Manager (PIP) is installed\n"
fi
}
_checkWireguard( ) {
if ! wg -h > /dev/null 2>& 1
then
printf "[WGDashboard] WireGuard is not installed. Please follow instruction on https://www.wireguard.com/install/ to install. \n"
exit 1
fi
if ! wg-quick -h > /dev/null 2>& 1
then
printf "[WGDashboard] WireGuard is not installed. Please follow instruction on https://www.wireguard.com/install/ to install. \n"
exit 1
fi
}
2021-09-03 04:09:06 +02:00
install_wgd( ) {
2024-08-02 23:27:28 +02:00
printf "[WGDashboard] Starting to install WGDashboard\n"
_checkWireguard
2024-08-04 09:20:44 +02:00
sudo chmod -R 755 /etc/wireguard/
2024-08-02 23:27:28 +02:00
if [ ! -d "log" ]
then
printf "[WGDashboard] Creating ./log folder\n"
mkdir "log"
fi
_determineOS
if ! python3 --version > /dev/null 2>& 1
then
printf "[WGDashboard] Python is not installed, trying to install now\n"
_installPython
else
printf "[WGDashboard] \xE2\x9C\x94 Python is installed\n"
fi
2024-08-03 00:30:16 +02:00
_installPythonVenv
_installPythonPip
2024-08-03 20:01:04 +02:00
2024-08-02 23:27:28 +02:00
2024-08-03 23:03:39 +02:00
version_pass = $( python3 -c 'import sys; print("1") if (sys.version_info.major == 3 and sys.version_info.minor >= 10) else print("0");' )
2021-09-03 20:22:14 +02:00
if [ $version_pass = = "0" ]
2024-08-02 23:27:28 +02:00
then
printf "[WGDashboard] WGDashboard required Python 3.7 or above\n"
exit 1
2021-12-25 23:07:33 +01:00
fi
2024-08-02 23:27:28 +02:00
if [ ! -d "db" ]
then
printf "[WGDashboard] Creating ./db folder\n"
mkdir "db"
2021-09-03 19:52:23 +02:00
fi
2024-08-02 23:27:28 +02:00
_check_and_set_venv
printf "[WGDashboard] Upgrading Python Package Manage (PIP)\n"
2024-08-03 00:18:31 +02:00
{ date; python3 -m pip install pip; printf "\n\n" ; } >> ./log/install.txt
2024-08-02 23:27:28 +02:00
printf "[WGDashboard] Installing latest Python dependencies\n"
2024-08-03 00:18:31 +02:00
{ date; python3 -m pip install -r requirements.txt ; printf "\n\n" ; } >> ./log/install.txt
2024-08-02 23:27:28 +02:00
printf "[WGDashboard] WGDashboard installed successfully!\n"
printf "[WGDashboard] Enter ./wgd.sh start to start the dashboard\n"
2021-09-03 04:09:06 +02:00
}
2021-05-04 07:32:34 +02:00
check_wgd_status( ) {
2022-01-18 16:42:23 +01:00
if test -f " $PID_FILE " ; then
if ps aux | grep -v grep | grep $( cat ./gunicorn.pid) > /dev/null; then
return 0
2021-10-18 02:10:50 +02:00
else
return 1
fi
else
2021-10-18 02:13:01 +02:00
if ps aux | grep -v grep | grep '[p]ython3 ' $app_name > /dev/null; then
2021-05-04 07:32:34 +02:00
return 0
2021-10-18 02:10:50 +02:00
else
return 1
fi
2021-05-04 07:32:34 +02:00
fi
}
2021-10-25 00:16:02 +02:00
certbot_create_ssl ( ) {
certbot certonly --config ./certbot.ini --email " $EMAIL " --work-dir $cb_work_dir --config-dir $cb_config_dir --domain " $SERVERURL "
}
certbot_renew_ssl ( ) {
certbot renew --work-dir $cb_work_dir --config-dir $cb_config_dir
}
2021-10-24 23:24:49 +02:00
gunicorn_start ( ) {
2021-10-25 00:16:02 +02:00
printf "%s\n" " $dashes "
2024-08-02 23:27:28 +02:00
printf "[WGDashboard] Starting WGDashboard with Gunicorn in the background.\n"
2021-10-25 00:16:02 +02:00
d = $( date '+%Y%m%d%H%M%S' )
if [ [ $USER = = root ] ] ; then
export PATH = $PATH :/usr/local/bin:$HOME /.local/bin
fi
2024-08-02 23:27:28 +02:00
_check_and_set_venv
2024-08-05 00:48:13 +02:00
sudo " $venv_gunicorn " --access-logfile log/access.log \
2024-06-19 11:09:58 +02:00
--log-level 'debug' --capture-output \
2024-08-05 01:33:05 +02:00
--config ./gunicorn.conf.py \
2024-08-05 00:48:13 +02:00
--error-logfile log/error.log --daemon 'dashboard:app'
2024-08-05 00:45:57 +02:00
sleep 5
2024-08-04 21:29:07 +02:00
checkPIDExist = 0
while [ $checkPIDExist -eq 0 ]
do
2024-08-04 21:44:55 +02:00
if test -f './gunicorn.pid' ; then
2024-08-04 21:29:07 +02:00
checkPIDExist = 1
fi
sleep 2
done
2024-08-02 23:27:28 +02:00
printf "[WGDashboard] Log files is under ./log\n"
2021-10-25 00:16:02 +02:00
printf "%s\n" " $dashes "
2021-10-24 23:24:49 +02:00
}
gunicorn_stop ( ) {
2024-08-03 20:01:04 +02:00
sudo kill $( cat ./gunicorn.pid)
2021-05-04 07:32:34 +02:00
}
start_wgd ( ) {
2024-08-02 23:27:28 +02:00
_checkWireguard
2022-01-18 16:42:23 +01:00
gunicorn_start
2021-05-04 07:32:34 +02:00
}
stop_wgd( ) {
2022-01-18 16:42:23 +01:00
if test -f " $PID_FILE " ; then
2021-10-24 23:24:49 +02:00
gunicorn_stop
2021-10-18 01:40:08 +02:00
else
kill " $( ps aux | grep " [p]ython3 $app_name " | awk '{print $2}' ) "
fi
2021-05-04 07:32:34 +02:00
}
start_wgd_debug( ) {
2021-09-03 20:30:13 +02:00
printf "%s\n" " $dashes "
2024-08-02 23:27:28 +02:00
_checkWireguard
2024-08-03 20:01:04 +02:00
printf "[WGDashboard] Starting WGDashboard in the foreground.\n"
sudo " $venv_python " " $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 "
2024-08-03 20:01:04 +02:00
printf "[WGDashboard] Are you sure you want to update to the %s? (Y/N): " " $new_ver "
2021-05-05 20:38:10 +02:00
read up
2024-08-03 20:01:04 +02:00
if [ " $up " = "Y" ] || [ " $up " = "y" ] ; then
printf "[WGDashboard] Shutting down WGDashboard\n"
2021-12-25 23:37:43 +01:00
if check_wgd_status; then
stop_wgd
2021-12-25 23:38:13 +01:00
fi
2021-12-25 23:07:33 +01:00
mv wgd.sh wgd.sh.old
2024-08-03 20:01:04 +02:00
printf "[WGDashboard] Downloading %s from GitHub..." " $new_ver "
{ date; git stash; git pull https://github.com/donaldzou/WGDashboard.git $new_ver --force; } >> ./log/update.txt
chmod +x ./wgd.sh
sudo ./wgd.sh install
printf "[WGDashboard] Update completed!\n"
2021-12-25 23:35:36 +01:00
printf "%s\n" " $dashes "
rm wgd.sh.old
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 "
2024-08-03 20:01:04 +02:00
printf "[WGDashboard] 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
2024-08-03 20:01:04 +02:00
printf "[WGDashboard] 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 "
2024-08-03 20:01:04 +02:00
printf "[WGDashboard] 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
2021-12-25 23:35:36 +01:00
printf "%s\n" " $dashes "
2021-09-03 04:13:00 +02:00
install_wgd
2021-12-25 23:35:36 +01:00
printf "%s\n" " $dashes "
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"
2022-01-18 18:16:10 +01:00
sleep 4
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