From 40d2098982570e14b9ede83eeccbf57325305517 Mon Sep 17 00:00:00 2001 From: David Trattnig <david.trattnig@o94.at> Date: Thu, 6 May 2021 15:02:01 +0200 Subject: [PATCH] PostgreSQL support. #73 --- README.md | 27 ++++++++++++++++--- .../sample/sample-development.engine-api.ini | 6 +++-- config/sample/sample-docker.engine-api.ini | 6 +++-- .../sample/sample-production.engine-api.ini | 6 +++-- contrib/postgresql-create-database.sql | 4 +++ contrib/postgresql-create-test-databases.sql | 8 ++++++ contrib/postgresql-requirements.txt | 1 + src/base/config.py | 14 +++++++--- 8 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 contrib/postgresql-create-database.sql create mode 100644 contrib/postgresql-create-test-databases.sql create mode 100644 contrib/postgresql-requirements.txt diff --git a/README.md b/README.md index 7f5b6f8..badfd95 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ - [Getting started](#getting-started) - [Requirements](#requirements) - [Installation](#installation) + - [Setting up the database](#setting-up-the-database) - [Configuration](#configuration) - [Engine 1 Node](#engine-1-node) - [Engine 2 Node](#engine-2-node) @@ -115,8 +116,10 @@ The most simple way to get started is by running Engine API using [Docker](https If you are not planning to go with Docker or just want to setup a local development environment, then you'll need: - [Python 3.8+](https://www.python.org/) -- [MariaDB](https://mariadb.org/) -- [Virtualenv](https://virtualenv.pypa.io/en/latest/) +- [`pip`](https://pip.pypa.io/en/stable/) +- [`git`](https://git-scm.com/) +- [PostgreSQL 13+](https://www.postgresql.org/) or [MariaDB 10+](https://mariadb.org/) +- [Virtualenv](https://virtualenv.pypa.io/en/latest/) (Optional, development only) ### Installation @@ -136,12 +139,30 @@ Install the required dependencies ```bash pip3 install -r requirements.txt -# Additional requirements for the chosen database +``` + +#### Setting up the database + +The primary database supported by AURA is PostgreSQL. + +```bash +# Additional Python packages for PostgreSQL +pip3 install -r contrib/postgresql-requirements.txt +# Create database and user (change password in script) +sudo -u postgres psql -f contrib/postgresql-create-database.sql +``` + +Alternatively you can also use MariaDB: + +```bash +# Additional Python packages for MariaDB pip3 install -r contrib/mariadb-requirements.txt # Create database and user (change password in script) sudo mysql -u root -p < contrib/mariadb-database.sql ``` +You might want to change the password for the database user created by the relevant script. + ### Configuration Copy the sample configuration file in `./config/sample/sample-production.engine-api` to `config` and edit the file. diff --git a/config/sample/sample-development.engine-api.ini b/config/sample/sample-development.engine-api.ini index faebbb6..29ac700 100644 --- a/config/sample/sample-development.engine-api.ini +++ b/config/sample/sample-development.engine-api.ini @@ -5,8 +5,10 @@ [database] -db_user="aura" +# Use 'postgresql' or 'mysql' +db_type="postgresql" db_name="aura_engine_api" +db_user="aura_engine_api" db_pass="---SECRET--PASSWORD---" db_host="localhost" db_charset="utf8" @@ -26,7 +28,7 @@ api_cors="*" enable_federation="false" # Defines the engine number id for identification of record sources. Default values are: -# +# # 1 ... Engine 1 (main node) # 2 ... Engine 2 (main node, not needed for single deployment) # 0 ... Sync Host (sync node, not needed for single engine deployment) diff --git a/config/sample/sample-docker.engine-api.ini b/config/sample/sample-docker.engine-api.ini index 084980e..f18d26a 100644 --- a/config/sample/sample-docker.engine-api.ini +++ b/config/sample/sample-docker.engine-api.ini @@ -5,8 +5,10 @@ [database] -db_user="aura" +# Use 'postgresql' or 'mysql' +db_type="postgresql" db_name="aura_engine_api" +db_user="aura_engine_api" db_pass="---SECRET--PASSWORD---" db_host="localhost" db_charset="utf8" @@ -26,7 +28,7 @@ api_cors="*" enable_federation="false" # Defines the engine number id for identification of record sources. Default values are: -# +# # 1 ... Engine 1 (main node) # 2 ... Engine 2 (main node, not needed for single deployment) # 0 ... Sync Host (sync node, not needed for single engine deployment) diff --git a/config/sample/sample-production.engine-api.ini b/config/sample/sample-production.engine-api.ini index 919c2fe..85d67de 100644 --- a/config/sample/sample-production.engine-api.ini +++ b/config/sample/sample-production.engine-api.ini @@ -5,8 +5,10 @@ [database] -db_user="aura" +# Use 'postgresql' or 'mysql' +db_type="postgresql" db_name="aura_engine_api" +db_user="aura_engine_api" db_pass="---SECRET--PASSWORD---" db_host="localhost" db_charset="utf8" @@ -26,7 +28,7 @@ api_cors="*" enable_federation="false" # Defines the engine number id for identification of record sources. Default values are: -# +# # 1 ... Engine 1 (main node) # 2 ... Engine 2 (main node, not needed for single deployment) # 0 ... Sync Host (sync node, not needed for single engine deployment) diff --git a/contrib/postgresql-create-database.sql b/contrib/postgresql-create-database.sql new file mode 100644 index 0000000..8926681 --- /dev/null +++ b/contrib/postgresql-create-database.sql @@ -0,0 +1,4 @@ +\c postgres +create database aura_engine_api; +create user aura_engine_api with encrypted password '1234'; +grant all privileges on database aura_engine_api to aura_engine_api; diff --git a/contrib/postgresql-create-test-databases.sql b/contrib/postgresql-create-test-databases.sql new file mode 100644 index 0000000..e668df8 --- /dev/null +++ b/contrib/postgresql-create-test-databases.sql @@ -0,0 +1,8 @@ +\c postgres +create database aura_engine_api_0; +create database aura_engine_api_1; +create database aura_engine_api_2; +create user aura_engine_api with encrypted password '1234'; +grant all privileges on database aura_engine_api_0 to aura_engine_api; +grant all privileges on database aura_engine_api_1 to aura_engine_api; +grant all privileges on database aura_engine_api_2 to aura_engine_api; diff --git a/contrib/postgresql-requirements.txt b/contrib/postgresql-requirements.txt new file mode 100644 index 0000000..4ae932d --- /dev/null +++ b/contrib/postgresql-requirements.txt @@ -0,0 +1 @@ +psycopg2-binary==2.8.6 \ No newline at end of file diff --git a/src/base/config.py b/src/base/config.py index 41cca87..e6a1d77 100644 --- a/src/base/config.py +++ b/src/base/config.py @@ -27,16 +27,16 @@ from configparser import ConfigParser class AuraConfig: - """ + """ AuraConfig Class - + Holds the Aura Configuration as in the file `engine-api.ini`. """ ini_path = "" logger = None - def __init__(self, ini_path="/etc/aura/engine-api.ini"): + def __init__(self, ini_path="/etc/aura/engine-api.ini"): """ Initializes the configuration, defaults to `/etc/aura/engine.ini`. If this file doesn't exist it uses `./config/engine.ini` from @@ -151,5 +151,11 @@ class AuraConfig: db_user = str(self.get("db_user")) db_pass = str(self.get("db_pass")) db_host = str(self.get("db_host")) + db_type = str(self.get("db_type")) db_charset = self.get("db_charset", "utf8") - return "mysql://" + db_user + ":" + db_pass + "@" + db_host + "/" + db_name + "?charset=" + db_charset \ No newline at end of file + if db_type == "mysql": + return "mysql://" + db_user + ":" + db_pass + "@" + db_host + "/" + db_name + "?charset=" + db_charset + elif db_type == "postgresql": + return f"postgresql+psycopg2://{db_user}:{db_pass}@{db_host}/{db_name}?client_encoding={db_charset}" + else: + return f"Error: invalid database type '{db_type}'" \ No newline at end of file -- GitLab