diff --git a/.gitignore b/.gitignore index 5d381cc..a525eb5 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,5 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +# Project specific +config.json diff --git a/isp_maintenance/ispmgr.py b/isp_maintenance/ispmgr.py old mode 100644 new mode 100755 index e69de29..b6b6ea1 --- a/isp_maintenance/ispmgr.py +++ b/isp_maintenance/ispmgr.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +from settings.environment import env + + +# TODO: delete this demo +# Just show you which env get application or default values +for key, value in env.dump().items(): + print(key, '|', value) diff --git a/isp_maintenance/settings/__init__.py b/isp_maintenance/settings/__init__.py index e69de29..33b615d 100644 --- a/isp_maintenance/settings/__init__.py +++ b/isp_maintenance/settings/__init__.py @@ -0,0 +1,15 @@ +from settings.environment import BASE_DIR + +from settings.platform import ( + PLATFORM_TYPE, + PLATFORM_URL, + PLATFORM_CONFIG +) + +from settings.db import( + DB_ENGINE, + DB_HOST, + DB_PORT, + DB_USER, + DB_PASSWORD +) diff --git a/isp_maintenance/settings/db.py b/isp_maintenance/settings/db.py new file mode 100644 index 0000000..504bc6f --- /dev/null +++ b/isp_maintenance/settings/db.py @@ -0,0 +1,20 @@ +from settings.environment import env +from settings.platform import PLATFORM_CONFIG + + +#! Required because some instance use psql db: +DB_ENGINE = env.str( + 'DB_ENGINE', + PLATFORM_CONFIG.get('DatabaseType', 'mysql') +) + +# Connection parameters: +DB_HOST = env.str('DB_HOST', 'mysql') +DB_PORT = env.int('DB_PORT', 3306) +DB_USER = env.str('DB_USER', 'root') + +#! Do not pass password on production. Use value from config.json +DB_PASSWORD = env.str( + 'DB_PASSWORD', + PLATFORM_CONFIG.get('MysqlRootPassword', '') +) diff --git a/isp_maintenance/settings/environment.py b/isp_maintenance/settings/environment.py new file mode 100644 index 0000000..e20551a --- /dev/null +++ b/isp_maintenance/settings/environment.py @@ -0,0 +1,14 @@ +import pathlib +from environs import Env + + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = pathlib.Path(__file__).resolve().parent.parent + + +# Init environment: +env = Env() + +# read .env file, if it exists +# reed more about .env file here: https://github.com/sloria/environs +env.read_env() diff --git a/isp_maintenance/settings/platform.py b/isp_maintenance/settings/platform.py new file mode 100644 index 0000000..2165522 --- /dev/null +++ b/isp_maintenance/settings/platform.py @@ -0,0 +1,12 @@ +from settings.environment import env, BASE_DIR +from utils.helpers import parse_json_file + + +PLATFORM_TYPE = env.str('PLATFORM_TYPE', 'vm') + +PLATFORM_CONFIG = parse_json_file(f'{BASE_DIR}/config.json') + +PLATFORM_URL = env.url( + 'PLATFORM_URL', + f"https://{PLATFORM_CONFIG.get('DomainName' ,'replace.me')}" +) diff --git a/isp_maintenance/utils/helpers.py b/isp_maintenance/utils/helpers.py new file mode 100644 index 0000000..0a85dc2 --- /dev/null +++ b/isp_maintenance/utils/helpers.py @@ -0,0 +1,20 @@ +import json +import sys + + +def parse_json_file(file_path): + """ + Function read json file as usual config.json then parse it to python dict + + Args: + config_file_path (str): path to config file + + Returns: + dict: contains parse json content + """ + try: + with open(file_path, 'r') as f: + return json.load(f) + except Exception as error: + print(error) + sys.exit(1)