mirror of
https://git.isptech.ru/ISPsystem/isp-maintenance.git
synced 2025-02-01 10:50:52 +01:00
Add: settings values, json parser from file (function), demo example
This commit is contained in:
parent
961180a006
commit
c8c7060201
2
.gitignore
vendored
2
.gitignore
vendored
@ -160,3 +160,5 @@ cython_debug/
|
|||||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
#.idea/
|
#.idea/
|
||||||
|
|
||||||
|
# Project specific
|
||||||
|
config.json
|
||||||
|
9
isp_maintenance/ispmgr.py
Normal file → Executable file
9
isp_maintenance/ispmgr.py
Normal file → Executable file
@ -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)
|
@ -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
|
||||||
|
)
|
20
isp_maintenance/settings/db.py
Normal file
20
isp_maintenance/settings/db.py
Normal file
@ -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', '')
|
||||||
|
)
|
14
isp_maintenance/settings/environment.py
Normal file
14
isp_maintenance/settings/environment.py
Normal file
@ -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()
|
12
isp_maintenance/settings/platform.py
Normal file
12
isp_maintenance/settings/platform.py
Normal file
@ -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')}"
|
||||||
|
)
|
20
isp_maintenance/utils/helpers.py
Normal file
20
isp_maintenance/utils/helpers.py
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user