restructure
This commit is contained in:
40
src/initialize.py
Normal file
40
src/initialize.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import os
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from database import setup_db_conn
|
||||||
|
|
||||||
|
def initialize_config():
|
||||||
|
load_dotenv()
|
||||||
|
log = logging.getLogger('uvicorn')
|
||||||
|
ENV_INVENTORYPATH = os.getenv('cluster_inventory_path')
|
||||||
|
ENV_REDISHOST = os.getenv('redis_host')
|
||||||
|
ENV_REDISPORT = os.getenv('redis_port')
|
||||||
|
|
||||||
|
log.info(f"Fount 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
|
||||||
|
try:
|
||||||
|
with open(ENV_INVENTORYPATH, 'r') as f:
|
||||||
|
inv = yaml.safe_load(f)
|
||||||
|
inventory = json.dumps(inv)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"FATAL: Cannot read inventory file {ENV_INVENTORYPATH}. Err: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
print(f'[INFO] Importing configuration to DB...')
|
||||||
|
try:
|
||||||
|
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.")
|
||||||
|
return True
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"FATAL: Redis DB error: {e}")
|
||||||
|
return False
|
||||||
32
src/main.py
32
src/main.py
@@ -14,40 +14,10 @@ from fastapi import FastAPI
|
|||||||
|
|
||||||
|
|
||||||
from database import setup_db_conn, get_inventory_from_redis, read_config_from_db
|
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 utils import setup_logging
|
||||||
|
|
||||||
|
|
||||||
def initialize_config():
|
|
||||||
load_dotenv()
|
|
||||||
ENV_INVENTORYPATH = os.getenv('cluster_inventory_path')
|
|
||||||
ENV_REDISHOST = os.getenv('redis_host')
|
|
||||||
ENV_REDISPORT = os.getenv('redis_port')
|
|
||||||
|
|
||||||
log.info(f"Fount 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
|
|
||||||
try:
|
|
||||||
with open(ENV_INVENTORYPATH, 'r') as f:
|
|
||||||
inv = yaml.safe_load(f)
|
|
||||||
inventory = json.dumps(inv)
|
|
||||||
except Exception as e:
|
|
||||||
print(f"FATAL: Cannot read inventory file {ENV_INVENTORYPATH}. Err: {e}")
|
|
||||||
return False
|
|
||||||
|
|
||||||
print(f'[INFO] Importing configuration to DB...')
|
|
||||||
try:
|
|
||||||
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.")
|
|
||||||
return True
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print(f"FATAL: Redis DB error: {e}")
|
|
||||||
return False
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
''' make loading it async'''
|
''' make loading it async'''
|
||||||
|
|||||||
38
src/start.py
38
src/start.py
@@ -1,38 +0,0 @@
|
|||||||
import os
|
|
||||||
import json
|
|
||||||
from pathlib import Path
|
|
||||||
from dotenv import load_dotenv
|
|
||||||
from redis import Redis
|
|
||||||
|
|
||||||
from pydantic import BaseModel, ValidationError, SecretStr, AnyHttpUrl
|
|
||||||
from typing import Optional, Literal, List, Union
|
|
||||||
|
|
||||||
|
|
||||||
def initialize_config():
|
|
||||||
load_dotenv()
|
|
||||||
MY_ENV_VAR = os.getenv('MY_ENV_VAR')
|
|
||||||
print(MY_ENV_VAR)
|
|
||||||
|
|
||||||
def config_init(redisclient: Redis):
|
|
||||||
print(f'[INFO] Importing configuration to local DB...')
|
|
||||||
try:
|
|
||||||
#redisclient = Redis(host=redishost, port=redisport, decode_responses=True)
|
|
||||||
inv = {
|
|
||||||
'1': {'hostname': 'jamaica.muccbc.hq.netapp.com', 'username': '', 'password': ''},
|
|
||||||
'2': {'hostname': 'trinidad.muccbc.hq.netapp.com', 'username': '', 'password': ''}
|
|
||||||
}
|
|
||||||
|
|
||||||
inventory = json.dumps(inv)
|
|
||||||
redisclient.hset('cluster_inventory', mapping={'inventory': inventory})
|
|
||||||
redisclient.close()
|
|
||||||
return True
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print(f"FATAL: Redis DB error: {e}")
|
|
||||||
return False
|
|
||||||
|
|
||||||
load_dotenv()
|
|
||||||
|
|
||||||
MY_ENV_VAR = os.getenv('MY_ENV_VAR')
|
|
||||||
|
|
||||||
print(MY_ENV_VAR)
|
|
||||||
Reference in New Issue
Block a user