From fe53c854473584a91848e4a4ccf11add859b40bf Mon Sep 17 00:00:00 2001 From: puls200 Date: Mon, 16 Oct 2023 15:34:18 +0200 Subject: [PATCH 1/7] made simple path change and added documentation pt 1 --- misc/Deployment.md | 43 +++++++++++++++++++++++++++++++++++ src/server/BreCal/local_db.py | 4 ++-- 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 misc/Deployment.md diff --git a/misc/Deployment.md b/misc/Deployment.md new file mode 100644 index 0000000..abf4930 --- /dev/null +++ b/misc/Deployment.md @@ -0,0 +1,43 @@ +# System deployment + +___ + +## Prerequisites + +## Client + +## Database + +## Backend / Flask app + +In order to not have complicated and error-prone copying manoevers a direct deployment from the repo is used using git. + +### File structure + +### Steps + +1) Created a ssh-key for the user that does the installation on the server following the Github [instructions](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent). +2) Deploy generated key to the Github user account. +3) In the shell, activate ssh-agent and add the key. For example: + +```bash +eval ($ssh-agent) +ssh-add ~/.ssh/od_ed25519 +``` + +4) Change to deployment folder + +```bash +cd /var/www/brecal_test +``` + +5) Perform sparse checkout on the Flask server subtree + +```bash +git clone -n git@github.com:puls200/brecal.git +cd +git sparse-checkout set --no-cone src/server +git checkout +``` + +6) Database credentials are stored outside the web root, we are using /var/www/secure. Here the file ```connection_data.json``` is placed, a different named copy for each instance. diff --git a/src/server/BreCal/local_db.py b/src/server/BreCal/local_db.py index 5b8c631..edecd3b 100644 --- a/src/server/BreCal/local_db.py +++ b/src/server/BreCal/local_db.py @@ -9,7 +9,7 @@ connection_pool = None def initPool(): try: - config_path = './src/server/BreCal/connection_data.json' + config_path = '../../../../secure/connection_data_test.json' print (os.getcwd()) if not os.path.exists(config_path): print ('cannot find ' + config_path) @@ -31,7 +31,7 @@ def initPool(): print(e) def getPoolConnection(): - config_path = './src/server/BreCal/connection_data.json' + config_path = '../../../../secure/connection_data_test.json' f = open(config_path); connection_data = json.load(f) return mysql.connector.connect(**connection_data) \ No newline at end of file From 6597c46a9dc78cfd0ea74eec748c4a3229b714aa Mon Sep 17 00:00:00 2001 From: puls200 Date: Mon, 16 Oct 2023 15:36:38 +0200 Subject: [PATCH 2/7] removed connection data file --- src/server/BreCal/connection_data.json | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 src/server/BreCal/connection_data.json diff --git a/src/server/BreCal/connection_data.json b/src/server/BreCal/connection_data.json deleted file mode 100644 index 16b58c0..0000000 --- a/src/server/BreCal/connection_data.json +++ /dev/null @@ -1,10 +0,0 @@ -{ -"host" : "localhost", -"port" : 33306, -"user" : "ds", -"password" : "HalloWach_2323XXL!!", -"pool_name" : "brecal_pool", -"pool_size" : 20, -"database" : "bremen_calling", -"autocommit" : true -} \ No newline at end of file From e5e262fd4fad7d936fc758547a4b0916fd88655e Mon Sep 17 00:00:00 2001 From: puls200 Date: Mon, 16 Oct 2023 17:31:57 +0200 Subject: [PATCH 3/7] improve connection handling on startup --- src/server/BreCal/__init__.py | 4 +--- src/server/BreCal/local_db.py | 10 +++++----- src/server/flaskapp.wsgi | 4 ++-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/server/BreCal/__init__.py b/src/server/BreCal/__init__.py index e15818a..3afbb8d 100644 --- a/src/server/BreCal/__init__.py +++ b/src/server/BreCal/__init__.py @@ -13,8 +13,6 @@ from .api import ships from .api import login from .api import user -sessions = dict() - def create_app(test_config=None): app = Flask(__name__, instance_relative_config=True) @@ -44,7 +42,7 @@ def create_app(test_config=None): logging.basicConfig(filename='brecal.log', level=logging.DEBUG, format='%(asctime)s | %(name)s | %(levelname)s | %(message)s') - local_db.initPool() + local_db.initPool(app.instance_path) logging.info('App started') return app diff --git a/src/server/BreCal/local_db.py b/src/server/BreCal/local_db.py index edecd3b..018b793 100644 --- a/src/server/BreCal/local_db.py +++ b/src/server/BreCal/local_db.py @@ -4,13 +4,14 @@ import logging import json import os -connection_pool = None +config_path = None -def initPool(): +def initPool(instancePath): try: + if(config_path == None): + config_path = os.path.join(instancePath,'../../../../secure/connection_data_test.json'); - config_path = '../../../../secure/connection_data_test.json' - print (os.getcwd()) + print (instancePath) if not os.path.exists(config_path): print ('cannot find ' + config_path) exit(1) @@ -31,7 +32,6 @@ def initPool(): print(e) def getPoolConnection(): - config_path = '../../../../secure/connection_data_test.json' f = open(config_path); connection_data = json.load(f) return mysql.connector.connect(**connection_data) \ No newline at end of file diff --git a/src/server/flaskapp.wsgi b/src/server/flaskapp.wsgi index 1dc7818..bb43a71 100644 --- a/src/server/flaskapp.wsgi +++ b/src/server/flaskapp.wsgi @@ -1,8 +1,8 @@ import sys import logging -sys.path.insert(0, '/var/www/brecal/server') -sys.path.insert(0, '/var/www/brecal/venv/lib/python3.10/site-packages/') +sys.path.insert(0, '/var/www/brecal_test/server') +sys.path.insert(0, '/var/www/venv/lib/python3.10/site-packages/') # Set up logging logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) From da0287ae6e84733ef80c49dd749640cd9d8996ed Mon Sep 17 00:00:00 2001 From: puls200 Date: Mon, 16 Oct 2023 17:42:45 +0200 Subject: [PATCH 4/7] fixes --- src/server/BreCal/local_db.py | 5 ++++- src/server/flaskapp.wsgi | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/server/BreCal/local_db.py b/src/server/BreCal/local_db.py index 018b793..1af1184 100644 --- a/src/server/BreCal/local_db.py +++ b/src/server/BreCal/local_db.py @@ -8,10 +8,12 @@ config_path = None def initPool(instancePath): try: + global config_path if(config_path == None): config_path = os.path.join(instancePath,'../../../../secure/connection_data_test.json'); - print (instancePath) + print (config_path) + if not os.path.exists(config_path): print ('cannot find ' + config_path) exit(1) @@ -32,6 +34,7 @@ def initPool(instancePath): print(e) def getPoolConnection(): + global config_path f = open(config_path); connection_data = json.load(f) return mysql.connector.connect(**connection_data) \ No newline at end of file diff --git a/src/server/flaskapp.wsgi b/src/server/flaskapp.wsgi index bb43a71..b71a8e7 100644 --- a/src/server/flaskapp.wsgi +++ b/src/server/flaskapp.wsgi @@ -1,7 +1,7 @@ import sys import logging -sys.path.insert(0, '/var/www/brecal_test/server') +sys.path.insert(0, '/var/www/brecal_test/src/server') sys.path.insert(0, '/var/www/venv/lib/python3.10/site-packages/') # Set up logging From 2857c8693a0e792eaa1dbcfa415e543508df90cd Mon Sep 17 00:00:00 2001 From: puls200 Date: Mon, 16 Oct 2023 17:46:30 +0200 Subject: [PATCH 5/7] fixes --- src/server/BreCal/__init__.py | 2 +- src/server/BreCal/local_db.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server/BreCal/__init__.py b/src/server/BreCal/__init__.py index 3afbb8d..8f0eddb 100644 --- a/src/server/BreCal/__init__.py +++ b/src/server/BreCal/__init__.py @@ -42,7 +42,7 @@ def create_app(test_config=None): logging.basicConfig(filename='brecal.log', level=logging.DEBUG, format='%(asctime)s | %(name)s | %(levelname)s | %(message)s') - local_db.initPool(app.instance_path) + local_db.initPool(os.path.dirname(app.instance_path)) logging.info('App started') return app diff --git a/src/server/BreCal/local_db.py b/src/server/BreCal/local_db.py index 1af1184..6787431 100644 --- a/src/server/BreCal/local_db.py +++ b/src/server/BreCal/local_db.py @@ -10,7 +10,7 @@ def initPool(instancePath): try: global config_path if(config_path == None): - config_path = os.path.join(instancePath,'../../../../secure/connection_data_test.json'); + config_path = os.path.join(instancePath,'../../../secure/connection_data_test.json'); print (config_path) From 26b71052ae49bf0e33494bfc069e98254374d7c2 Mon Sep 17 00:00:00 2001 From: puls200 Date: Mon, 16 Oct 2023 18:43:48 +0200 Subject: [PATCH 6/7] add missing key --- src/server/flaskapp.wsgi | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/server/flaskapp.wsgi b/src/server/flaskapp.wsgi index b71a8e7..88845b8 100644 --- a/src/server/flaskapp.wsgi +++ b/src/server/flaskapp.wsgi @@ -4,6 +4,9 @@ import logging sys.path.insert(0, '/var/www/brecal_test/src/server') sys.path.insert(0, '/var/www/venv/lib/python3.10/site-packages/') +# set the key +os.environ['SECRET_KEY'] = 'zdiTz8P3jXOc7jztIQAoelK4zztyuCpJ' + # Set up logging logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) From 3044ca26211b1abaf3d2f39c34d444b5039d8ab2 Mon Sep 17 00:00:00 2001 From: puls200 Date: Mon, 16 Oct 2023 18:47:22 +0200 Subject: [PATCH 7/7] add missing include --- src/server/flaskapp.wsgi | 1 + 1 file changed, 1 insertion(+) diff --git a/src/server/flaskapp.wsgi b/src/server/flaskapp.wsgi index 88845b8..e734f70 100644 --- a/src/server/flaskapp.wsgi +++ b/src/server/flaskapp.wsgi @@ -1,3 +1,4 @@ +import os import sys import logging