backedn dev
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import json
|
||||
import logging
|
||||
from redis import Redis, ConnectionError
|
||||
from typing import List
|
||||
from pydantic import TypeAdapter
|
||||
from schema import ConfigSchema
|
||||
|
||||
|
||||
def setup_db_conn(redishost, redisport: str):
|
||||
@@ -8,7 +11,11 @@ def setup_db_conn(redishost, redisport: str):
|
||||
log = logging.getLogger('uvicorn')
|
||||
try:
|
||||
redisclient = Redis(host=redishost, port=redisport, decode_responses=True)
|
||||
log.info(f"Connected to Redis DB {redishost} on port {redisport}")
|
||||
if redisclient.ping():
|
||||
log.info(f"Connected to Redis DB {redishost} on port {redisport}")
|
||||
else:
|
||||
log.error(f"Cannot connect to Redis DB {redishost} on port {redisport}")
|
||||
exit(1)
|
||||
return redisclient
|
||||
except ConnectionError as e:
|
||||
print(f"FATAL: Redis DB {redishost} is unreachable on port {redisport}. Err: {e}")
|
||||
@@ -24,23 +31,10 @@ def get_inventory_from_redis(redisclient: Redis):
|
||||
return json.loads(cluster_inv['inventory'])
|
||||
return {}
|
||||
|
||||
def read_config_from_db(redisclient: Redis) -> ConfigSchema:
|
||||
def get_config_from_db(redisclient: Redis) -> ConfigSchema:
|
||||
''' Load inventory to global vars'''
|
||||
GLOBAL_INVENTORY = get_inventory_from_redis(redisclient)
|
||||
for item in GLOBAL_INVENTORY:
|
||||
config = ConfigSchema(
|
||||
hostname=GLOBAL_INVENTORY[item]['hostname'],
|
||||
username=GLOBAL_INVENTORY[item]['username'],
|
||||
password=GLOBAL_INVENTORY[item]['password'],
|
||||
)
|
||||
GLOBAL_INVENTORY += {item: config}
|
||||
return GLOBAL_INVENTORY
|
||||
|
||||
# def load_config() -> ConfigSchema:
|
||||
# logger.info("Loading config from .env file")
|
||||
# config = dotenv_values(".env")
|
||||
# return ConfigSchema(
|
||||
# hostname=config["CLUSTER1_HOSTNAME"],
|
||||
# username=config["CLUSTER1_USERNAME"],
|
||||
# password=config["CLUSTER1_PASSWORD"],
|
||||
# )
|
||||
GLOBAL_INVENTORY_VALID = TypeAdapter(List[ConfigSchema]).validate_python(GLOBAL_INVENTORY)
|
||||
|
||||
return GLOBAL_INVENTORY_VALID
|
||||
|
||||
@@ -6,6 +6,9 @@ import yaml
|
||||
from pathlib import Path
|
||||
from dotenv import load_dotenv
|
||||
from database import setup_db_conn
|
||||
from schema import ConfigSchema
|
||||
from typing import List
|
||||
from pydantic import TypeAdapter
|
||||
|
||||
def initialize_config():
|
||||
load_dotenv()
|
||||
@@ -14,7 +17,7 @@ def initialize_config():
|
||||
ENV_REDISHOST = os.getenv('redis_host')
|
||||
ENV_REDISPORT = os.getenv('redis_port')
|
||||
|
||||
log.info(f"Fount Cluster Inventory file at: {ENV_INVENTORYPATH}")
|
||||
log.info(f"Found Cluster Inventory file at: {ENV_INVENTORYPATH}")
|
||||
if not ENV_INVENTORYPATH or not Path(ENV_INVENTORYPATH).is_file():
|
||||
print(f"FATAL: Inventory file {ENV_INVENTORYPATH} is missing or not a file.")
|
||||
return False
|
||||
@@ -28,11 +31,12 @@ def initialize_config():
|
||||
|
||||
print(f'[INFO] Importing configuration to DB...')
|
||||
try:
|
||||
GLOBAL_INVENTORY_VALID = TypeAdapter(List[ConfigSchema]).validate_python(inv)
|
||||
redis_conn = setup_db_conn(ENV_REDISHOST, ENV_REDISPORT)
|
||||
redis_conn.hset('cluster_inventory', mapping={'inventory': inventory})
|
||||
redis_conn.close()
|
||||
|
||||
log.info("Configuration has been loaded.")
|
||||
log.info("Configuration has been loaded.")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
|
||||
17
src/main.py
17
src/main.py
@@ -13,9 +13,9 @@ from typing import Optional, Literal, List, Union
|
||||
from fastapi import FastAPI
|
||||
|
||||
|
||||
from database import setup_db_conn, get_inventory_from_redis, read_config_from_db
|
||||
from initialize import initialize_config
|
||||
from utils import setup_logging
|
||||
from database import setup_db_conn, get_inventory_from_redis, get_config_from_db
|
||||
from src.initialize import initialize_config
|
||||
from utils import setup_logging
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
@@ -23,12 +23,17 @@ async def lifespan(app: FastAPI):
|
||||
''' make loading it async'''
|
||||
log = logging.getLogger('uvicorn')
|
||||
cfg_init_result = initialize_config()
|
||||
|
||||
shared_redis_conn = setup_db_conn(os.getenv('redis_host'), os.getenv('redis_port'))
|
||||
if not shared_redis_conn:
|
||||
log.error("Cannot connect to Redis DB. Exiting...")
|
||||
exit(1)
|
||||
|
||||
inv_check = read_config_from_db(setup_db_conn(os.getenv('redis_host'), os.getenv('redis_port')))
|
||||
log.info(f"Data validity check (DEVELOPER MODE): {inv_check}")
|
||||
inv_check = get_config_from_db(shared_redis_conn)
|
||||
log.info(f"[DEBUG] Data validity healthcheck (DEVELOPER MODE): {inv_check}")
|
||||
if not cfg_init_result:
|
||||
log.error("Configuration initialization failed. Exiting...")
|
||||
exit(1)
|
||||
# exit(1)
|
||||
|
||||
yield
|
||||
log.info("Shutting down FastAPI app...")
|
||||
|
||||
Reference in New Issue
Block a user