From ea0229a8ab81cbe3ee588de84b3223a12d1a33ab Mon Sep 17 00:00:00 2001 From: Galonza Peter Date: Sun, 17 Oct 2021 23:37:45 +0300 Subject: [PATCH 01/14] added wsgi for gunicorn --- src/dashboard.py | 13 +++++++++++++ src/wsgi.py | 6 ++++++ 2 files changed, 19 insertions(+) create mode 100644 src/wsgi.py diff --git a/src/dashboard.py b/src/dashboard.py index 99a4c19..c073e35 100644 --- a/src/dashboard.py +++ b/src/dashboard.py @@ -1161,6 +1161,19 @@ def check_update(): else: return "true" +def run_wsgi(): + init_dashboard() + global config + config = configparser.ConfigParser(strict=False) + config.read('wg-dashboard.ini') + global app_ip + app_ip = config.get("Server", "app_ip") + global app_port + app_port = config.get("Server", "app_port") + global wg_conf_path + wg_conf_path = config.get("Server", "wg_conf_path") + config.clear() + return app if __name__ == "__main__": init_dashboard() diff --git a/src/wsgi.py b/src/wsgi.py new file mode 100644 index 0000000..97c15b1 --- /dev/null +++ b/src/wsgi.py @@ -0,0 +1,6 @@ +import dashboard + + +if __name__ in "__main__": + dashboard.run_wsgi() + dashboard.app.run() From f0f9ac92e61d6adde88c752596f0d0ca7bc4d0af Mon Sep 17 00:00:00 2001 From: Galonza Peter Date: Mon, 18 Oct 2021 02:24:09 +0300 Subject: [PATCH 02/14] added gunicorn start --- src/dashboard.py | 22 ++++++++++++++-------- src/gunicorn.conf.py | 6 ++++++ src/wgd.sh | 30 ++++++++++++++++++++++-------- src/wsgi.py | 6 ------ 4 files changed, 42 insertions(+), 22 deletions(-) create mode 100644 src/gunicorn.conf.py delete mode 100644 src/wsgi.py diff --git a/src/dashboard.py b/src/dashboard.py index c073e35..691ebb5 100644 --- a/src/dashboard.py +++ b/src/dashboard.py @@ -1161,8 +1161,12 @@ def check_update(): else: return "true" -def run_wsgi(): +""" +Configure DashBoard before start web-server +""" +def run_dashboard(): init_dashboard() + update = check_update() global config config = configparser.ConfigParser(strict=False) config.read('wg-dashboard.ini') @@ -1175,13 +1179,15 @@ def run_wsgi(): config.clear() return app -if __name__ == "__main__": - init_dashboard() - update = check_update() - config = configparser.ConfigParser(strict=False) - config.read('wg-dashboard.ini') +""" +Get host and port for web-server +""" +def get_host_bind(): app_ip = config.get("Server", "app_ip") app_port = config.get("Server", "app_port") - wg_conf_path = config.get("Server", "wg_conf_path") - config.clear() + + return app_ip, app_port + +if __name__ == "__main__": + run_dashboard() app.run(host=app_ip, debug=False, port=app_port) \ No newline at end of file diff --git a/src/gunicorn.conf.py b/src/gunicorn.conf.py new file mode 100644 index 0000000..5ff8f4d --- /dev/null +++ b/src/gunicorn.conf.py @@ -0,0 +1,6 @@ +import dashboard + +app_host, app_port = dashboard.get_host_bind() +bind = f"{app_host}:{app_port}" +daemon = True +pidfile = './gunicorn.pid' diff --git a/src/wgd.sh b/src/wgd.sh index 3676947..bba0828 100755 --- a/src/wgd.sh +++ b/src/wgd.sh @@ -2,6 +2,7 @@ app_name="dashboard.py" app_official_name="WGDashboard" +environment=$(if [[ $ENVIRONMENT ]] ; then echo $ENVIRONMENT else echo 'develop') dashes='------------------------------------------------------------' equals='============================================================' help () { @@ -50,15 +51,28 @@ check_wgd_status(){ } start_wgd () { - printf "%s\n" "$dashes" - printf "| Starting WGDashboard in the background. |\n" - if [ ! -d "log" ] - then mkdir "log" + if [[ $environment == 'production']]; then + 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') + /usr/local/bin/gunicorn --access-logfile log/access_"$d".log \ + --error-logfile log/error_"$d".log 'dashboard.run_dashboard()' + 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" 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" } stop_wgd() { diff --git a/src/wsgi.py b/src/wsgi.py deleted file mode 100644 index 97c15b1..0000000 --- a/src/wsgi.py +++ /dev/null @@ -1,6 +0,0 @@ -import dashboard - - -if __name__ in "__main__": - dashboard.run_wsgi() - dashboard.app.run() From 898694b9befae3b53df831bbc116633c89887754 Mon Sep 17 00:00:00 2001 From: Galonza Peter Date: Mon, 18 Oct 2021 02:40:08 +0300 Subject: [PATCH 03/14] fixed gunicorn start and added stop --- src/dashboard.py | 2 ++ src/wgd.sh | 12 ++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/dashboard.py b/src/dashboard.py index 691ebb5..9f16f1e 100644 --- a/src/dashboard.py +++ b/src/dashboard.py @@ -1183,6 +1183,8 @@ def run_dashboard(): Get host and port for web-server """ def get_host_bind(): + config = configparser.ConfigParser(strict=False) + config.read('wg-dashboard.ini') app_ip = config.get("Server", "app_ip") app_port = config.get("Server", "app_port") diff --git a/src/wgd.sh b/src/wgd.sh index bba0828..d17f98f 100755 --- a/src/wgd.sh +++ b/src/wgd.sh @@ -2,7 +2,7 @@ app_name="dashboard.py" app_official_name="WGDashboard" -environment=$(if [[ $ENVIRONMENT ]] ; then echo $ENVIRONMENT else echo 'develop') +environment=$(if [[ $ENVIRONMENT ]]; then echo $ENVIRONMENT; else echo 'develop'; fi) dashes='------------------------------------------------------------' equals='============================================================' help () { @@ -51,7 +51,7 @@ check_wgd_status(){ } start_wgd () { - if [[ $environment == 'production']]; then + if [[ $environment == 'production' ]]; then printf "%s\n" "$dashes" printf "| Starting WGDashboard in the background. |\n" if [ ! -d "log" ] @@ -59,7 +59,7 @@ start_wgd () { fi d=$(date '+%Y%m%d%H%M%S') /usr/local/bin/gunicorn --access-logfile log/access_"$d".log \ - --error-logfile log/error_"$d".log 'dashboard.run_dashboard()' + --error-logfile log/error_"$d".log 'dashboard:run_dashboard()' printf "| Log files is under log/ |\n" printf "%s\n" "$dashes" else @@ -76,7 +76,11 @@ start_wgd () { } stop_wgd() { - kill "$(ps aux | grep "[p]ython3 $app_name" | awk '{print $2}')" + if [[ $environment == 'production' ]]; then + kill $(cat ./gunicorn.pid) + else + kill "$(ps aux | grep "[p]ython3 $app_name" | awk '{print $2}')" + fi } start_wgd_debug() { From bb298164e41c856e1b6f88bf9963c2b1faa74312 Mon Sep 17 00:00:00 2001 From: Galonza Peter Date: Mon, 18 Oct 2021 02:41:06 +0300 Subject: [PATCH 04/14] added gunicorn in requirements --- src/requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/requirements.txt b/src/requirements.txt index 58178ca..73bb8e4 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -2,4 +2,5 @@ Flask tinydb ifcfg icmplib -flask-qrcode \ No newline at end of file +flask-qrcode +gunicorn \ No newline at end of file From e0bf6480761a2022c1448ce38babdb9544953ca0 Mon Sep 17 00:00:00 2001 From: Galonza Peter Date: Mon, 18 Oct 2021 02:58:54 +0300 Subject: [PATCH 05/14] added paths when command as root --- src/wgd.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/wgd.sh b/src/wgd.sh index d17f98f..b25c37a 100755 --- a/src/wgd.sh +++ b/src/wgd.sh @@ -58,7 +58,10 @@ start_wgd () { then mkdir "log" fi d=$(date '+%Y%m%d%H%M%S') - /usr/local/bin/gunicorn --access-logfile log/access_"$d".log \ + if [[ $USER == root ]]; then + export PATH=$PATH:/usr/local/bin:$HOME/.local/bin + fi + gunicorn --access-logfile log/access_"$d".log \ --error-logfile log/error_"$d".log 'dashboard:run_dashboard()' printf "| Log files is under log/ |\n" printf "%s\n" "$dashes" From c3eaaed43b16d7243e8038920c002686acf51661 Mon Sep 17 00:00:00 2001 From: Galonza Peter Date: Mon, 18 Oct 2021 03:10:50 +0300 Subject: [PATCH 06/14] fixed stop --- src/wgd.sh | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/wgd.sh b/src/wgd.sh index b25c37a..7a96199 100755 --- a/src/wgd.sh +++ b/src/wgd.sh @@ -42,11 +42,18 @@ install_wgd(){ check_wgd_status(){ - if ps aux | grep '[p]ython3 '$app_name > /dev/null; - then + if [[ $environment == 'production' ]]; then + if ps aux | grep -v grep | cat ./gunicorn.pid > /dev/null; then return 0 - else - return 1 + else + return 1 + fi + else + if ps aux | grep -v grep |grep '[p]ython3 '$app_name > /dev/null; then + return 0 + else + return 1 + fi fi } From 3918e220d21c0083ac131daa73a7faa4bf4b3954 Mon Sep 17 00:00:00 2001 From: Galonza Peter Date: Mon, 18 Oct 2021 03:13:01 +0300 Subject: [PATCH 07/14] style --- src/wgd.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wgd.sh b/src/wgd.sh index 7a96199..604dba6 100755 --- a/src/wgd.sh +++ b/src/wgd.sh @@ -49,7 +49,7 @@ check_wgd_status(){ return 1 fi else - if ps aux | grep -v grep |grep '[p]ython3 '$app_name > /dev/null; then + if ps aux | grep -v grep | grep '[p]ython3 '$app_name > /dev/null; then return 0 else return 1 From 2103d547a18c75efb602c6d1ef53ec46e9552f86 Mon Sep 17 00:00:00 2001 From: Galonza Peter Date: Sat, 23 Oct 2021 12:19:05 +0300 Subject: [PATCH 08/14] fixed initiation for gunicorn --- src/dashboard.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/dashboard.py b/src/dashboard.py index 9f16f1e..6472181 100644 --- a/src/dashboard.py +++ b/src/dashboard.py @@ -1183,6 +1183,7 @@ def run_dashboard(): Get host and port for web-server """ def get_host_bind(): + init_dashboard() config = configparser.ConfigParser(strict=False) config.read('wg-dashboard.ini') app_ip = config.get("Server", "app_ip") From c9b792c3709de7fa62e852cd9a947ca9c0131811 Mon Sep 17 00:00:00 2001 From: Galonza Peter Date: Sun, 24 Oct 2021 01:09:34 +0300 Subject: [PATCH 09/14] fixed update variable --- src/dashboard.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/dashboard.py b/src/dashboard.py index 6472181..cd8b0bc 100644 --- a/src/dashboard.py +++ b/src/dashboard.py @@ -1166,6 +1166,7 @@ Configure DashBoard before start web-server """ def run_dashboard(): init_dashboard() + global update update = check_update() global config config = configparser.ConfigParser(strict=False) From 37b616107500e4c93292cebda6fd25bb440423b5 Mon Sep 17 00:00:00 2001 From: Galonza Peter Date: Sun, 24 Oct 2021 01:43:00 +0300 Subject: [PATCH 10/14] fixed check gunicorn process --- src/wgd.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wgd.sh b/src/wgd.sh index 604dba6..2e03453 100755 --- a/src/wgd.sh +++ b/src/wgd.sh @@ -43,7 +43,7 @@ install_wgd(){ check_wgd_status(){ if [[ $environment == 'production' ]]; then - if ps aux | grep -v grep | cat ./gunicorn.pid > /dev/null; then + if ps aux | grep -v grep | grep $(cat ./gunicorn.pid) > /dev/null; then return 0 else return 1 From c0fbf4dd0c54ef223e8cb2e977d23a97de84b272 Mon Sep 17 00:00:00 2001 From: Galonza Peter Date: Sun, 24 Oct 2021 23:41:06 +0300 Subject: [PATCH 11/14] optimized performance # Conflicts: # src/gunicorn.conf.py --- src/gunicorn.conf.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/gunicorn.conf.py b/src/gunicorn.conf.py index 5ff8f4d..047ec33 100644 --- a/src/gunicorn.conf.py +++ b/src/gunicorn.conf.py @@ -1,6 +1,11 @@ +import multiprocessing import dashboard app_host, app_port = dashboard.get_host_bind() + +worker_class = 'gthread' +workers = multiprocessing.cpu_count() * 2 + 1 +threads = 2 bind = f"{app_host}:{app_port}" daemon = True pidfile = './gunicorn.pid' From 94a0d5a0a47232f6c1cfbea6332cba1f265f7440 Mon Sep 17 00:00:00 2001 From: Galonza Peter Date: Mon, 25 Oct 2021 00:24:49 +0300 Subject: [PATCH 12/14] refactored --- src/wgd.sh | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/wgd.sh b/src/wgd.sh index 2e03453..0e70579 100755 --- a/src/wgd.sh +++ b/src/wgd.sh @@ -57,21 +57,29 @@ check_wgd_status(){ fi } +gunicorn_start () { + 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') + if [[ $USER == root ]]; then + export PATH=$PATH:/usr/local/bin:$HOME/.local/bin + fi + gunicorn --access-logfile log/access_"$d".log \ + --error-logfile log/error_"$d".log 'dashboard:run_dashboard()' + printf "| Log files is under log/ |\n" + printf "%s\n" "$dashes" +} + +gunicorn_stop () { + kill $(cat ./gunicorn.pid) +} + start_wgd () { if [[ $environment == 'production' ]]; then - 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') - if [[ $USER == root ]]; then - export PATH=$PATH:/usr/local/bin:$HOME/.local/bin - fi - gunicorn --access-logfile log/access_"$d".log \ - --error-logfile log/error_"$d".log 'dashboard:run_dashboard()' - printf "| Log files is under log/ |\n" - printf "%s\n" "$dashes" + gunicorn_start else printf "%s\n" "$dashes" printf "| Starting WGDashboard in the background. |\n" @@ -87,7 +95,7 @@ start_wgd () { stop_wgd() { if [[ $environment == 'production' ]]; then - kill $(cat ./gunicorn.pid) + gunicorn_stop else kill "$(ps aux | grep "[p]ython3 $app_name" | awk '{print $2}')" fi From 24f269191c39f1a64fec25ee6987f94776625f76 Mon Sep 17 00:00:00 2001 From: Galonza Peter Date: Mon, 25 Oct 2021 01:16:02 +0300 Subject: [PATCH 13/14] =?UTF-8?q?added=20Let=E2=80=99s=20Encrypt=20via=20c?= =?UTF-8?q?ertbot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/certbot.ini | 4 ++++ src/requirements.txt | 3 ++- src/wgd.sh | 50 +++++++++++++++++++++++++++++++++++--------- 3 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 src/certbot.ini diff --git a/src/certbot.ini b/src/certbot.ini new file mode 100644 index 0000000..cf9b7a2 --- /dev/null +++ b/src/certbot.ini @@ -0,0 +1,4 @@ +authenticator = standalone +noninteractive = true +agree-tos = true +rsa-key-size = 2048 \ No newline at end of file diff --git a/src/requirements.txt b/src/requirements.txt index 73bb8e4..ff0b156 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -3,4 +3,5 @@ tinydb ifcfg icmplib flask-qrcode -gunicorn \ No newline at end of file +gunicorn +certbot \ No newline at end of file diff --git a/src/wgd.sh b/src/wgd.sh index 0e70579..39442c2 100755 --- a/src/wgd.sh +++ b/src/wgd.sh @@ -3,6 +3,14 @@ app_name="dashboard.py" app_official_name="WGDashboard" environment=$(if [[ $ENVIRONMENT ]]; then echo $ENVIRONMENT; else echo 'develop'; fi) +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 + dashes='------------------------------------------------------------' equals='============================================================' help () { @@ -57,20 +65,42 @@ check_wgd_status(){ fi } +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 +} + gunicorn_start () { - 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') - if [[ $USER == root ]]; then - export PATH=$PATH:/usr/local/bin:$HOME/.local/bin + if [[ $SSL ]]; then + if [ ! -d $cb_config_dir ]; then + certbot_create_ssl + else + certbot_renew_ssl fi + fi + 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') + if [[ $USER == root ]]; then + export PATH=$PATH:/usr/local/bin:$HOME/.local/bin + fi + if [[ $SSL ]]; then + gunicorn --certfile $cb_config_dir/live/"$SERVERURL"/cert.pem \ + --keyfile $cb_config_dir/live/"$SERVERURL"/privkey.pem \ + --access-logfile log/access_"$d".log \ + --error-logfile log/error_"$d".log 'dashboard:run_dashboard()' + else gunicorn --access-logfile log/access_"$d".log \ --error-logfile log/error_"$d".log 'dashboard:run_dashboard()' - printf "| Log files is under log/ |\n" - printf "%s\n" "$dashes" + fi + printf "| Log files is under log/ |\n" + printf "%s\n" "$dashes" } gunicorn_stop () { From 83f0df32d7f25d905ee569ce8e12d6a9d17b9b40 Mon Sep 17 00:00:00 2001 From: Galonza Peter Date: Thu, 28 Oct 2021 23:16:32 +0300 Subject: [PATCH 14/14] added thread --- src/gunicorn.conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gunicorn.conf.py b/src/gunicorn.conf.py index 047ec33..56b82ce 100644 --- a/src/gunicorn.conf.py +++ b/src/gunicorn.conf.py @@ -5,7 +5,7 @@ app_host, app_port = dashboard.get_host_bind() worker_class = 'gthread' workers = multiprocessing.cpu_count() * 2 + 1 -threads = 2 +threads = 4 bind = f"{app_host}:{app_port}" daemon = True pidfile = './gunicorn.pid'