postgres healthcheck works
This commit is contained in:
parent
91c79357f8
commit
c38ce5cfe6
9 changed files with 203 additions and 8 deletions
13
anaxi/cli.py
13
anaxi/cli.py
|
|
@ -4,7 +4,10 @@ import click
|
|||
import pyfiglet
|
||||
|
||||
from anaxi.logging import get_anaxi_logger
|
||||
from anaxi.processes import run_cosmos_db_healthcheck_process
|
||||
from anaxi.processes import (
|
||||
run_cosmos_db_healthcheck_process,
|
||||
run_postgres_healthcheck_process,
|
||||
)
|
||||
|
||||
logger = get_anaxi_logger(__name__)
|
||||
|
||||
|
|
@ -36,3 +39,11 @@ def cosmos_db_healthcheck(cosmos_db_id):
|
|||
logger.info("Starting a Cosmos DB healthcheck.")
|
||||
run_cosmos_db_healthcheck_process(cosmos_db_id)
|
||||
logger.info("Finished the Cosmos DB healthcheck.")
|
||||
|
||||
|
||||
@cli.command()
|
||||
@click.option("--postgres-database", type=click.STRING, required=True)
|
||||
def postgres_healthcheck(postgres_database):
|
||||
logger.info("Starting a Postgres healthcheck.")
|
||||
run_postgres_healthcheck_process(postgres_database)
|
||||
logger.info("Finished the Postgres healthcheck.")
|
||||
|
|
|
|||
|
|
@ -3,8 +3,9 @@ import pathlib
|
|||
from typing import Dict
|
||||
|
||||
from anaxi.file_persistence import read_yaml
|
||||
from anaxi.logging import get_anaxi_logger
|
||||
|
||||
logger = logging.getLogger()
|
||||
logger = get_anaxi_logger(__name__)
|
||||
|
||||
|
||||
class CosmosDBDatabaseConfig:
|
||||
|
|
@ -38,3 +39,38 @@ class CosmosDBDatabaseConfig:
|
|||
raise e
|
||||
|
||||
return database_configs
|
||||
|
||||
|
||||
class PostgresDatabaseConfig:
|
||||
|
||||
config_root_key: str = "postgres-databases"
|
||||
|
||||
def __init__(self, host: str, port: int, dbname: str, user: str, password) -> None:
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.dbname = dbname
|
||||
self.user = user
|
||||
self.password = password
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, a_dict) -> "PostgresDatabaseConfig":
|
||||
return PostgresDatabaseConfig(**a_dict)
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, path: pathlib.Path) -> Dict[str, "PostgresDatabaseConfig"]:
|
||||
yaml_contents = read_yaml(path)
|
||||
|
||||
postgres_contents = yaml_contents[PostgresDatabaseConfig.config_root_key]
|
||||
|
||||
try:
|
||||
database_configs = {
|
||||
db_name: PostgresDatabaseConfig.from_dict(db_config)
|
||||
for db_name, db_config in postgres_contents.items()
|
||||
}
|
||||
except KeyError as e:
|
||||
logger.error(
|
||||
"Error reading Postgres config yaml file. The file seems malformed. Review the example in the repository."
|
||||
)
|
||||
raise e
|
||||
|
||||
return database_configs
|
||||
|
|
|
|||
|
|
@ -11,3 +11,6 @@ class PATHS:
|
|||
cosmos_db_config_file_path: pathlib.Path = config_home_path / pathlib.Path(
|
||||
"cosmos-db.yml"
|
||||
)
|
||||
postgres_config_file_path: pathlib.Path = config_home_path / pathlib.Path(
|
||||
"postgres.yml"
|
||||
)
|
||||
|
|
|
|||
27
anaxi/postgres_tools.py
Normal file
27
anaxi/postgres_tools.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import psycopg2
|
||||
from psycopg2.sql import SQL
|
||||
|
||||
from anaxi.config import PostgresDatabaseConfig
|
||||
|
||||
|
||||
def create_postgres_connection_from_config(config: PostgresDatabaseConfig):
|
||||
return psycopg2.connect(
|
||||
dbname=config.dbname,
|
||||
user=config.user,
|
||||
password=config.password,
|
||||
host=config.host,
|
||||
port=config.port,
|
||||
)
|
||||
|
||||
|
||||
def simply_query(config: PostgresDatabaseConfig, query: SQL):
|
||||
with psycopg2.connect(
|
||||
dbname=config.dbname,
|
||||
user=config.user,
|
||||
password=config.password,
|
||||
host=config.host,
|
||||
port=config.port,
|
||||
) as db_connection:
|
||||
cursor = db_connection.cursor()
|
||||
cursor.execute(query)
|
||||
return cursor.fetchall()
|
||||
|
|
@ -1,7 +1,10 @@
|
|||
from anaxi.config import CosmosDBDatabaseConfig
|
||||
from psycopg2.sql import SQL
|
||||
|
||||
from anaxi.config import CosmosDBDatabaseConfig, PostgresDatabaseConfig
|
||||
from anaxi.constants import PATHS
|
||||
from anaxi.cosmos_tools import create_cosmos_client_from_config
|
||||
from anaxi.logging import get_anaxi_logger
|
||||
from anaxi.postgres_tools import simply_query
|
||||
|
||||
logger = get_anaxi_logger(__name__)
|
||||
|
||||
|
|
@ -25,8 +28,27 @@ def run_cosmos_db_healthcheck_process(cosmos_db_id: str) -> None:
|
|||
cosmos_client = create_cosmos_client_from_config(relevant_cosmos_db_config)
|
||||
logger.info("Client created.")
|
||||
|
||||
logger.info("Throwing a SELECT 1;")
|
||||
logger.info("Sending a SELECT 1;")
|
||||
response = cosmos_client.query_databases(query="SELECT 1")
|
||||
logger.info(f"Response: {response.next()}")
|
||||
|
||||
return
|
||||
|
||||
|
||||
def run_postgres_healthcheck_process(postgres_database: str) -> None:
|
||||
logger.info("Reading Postgres config file...")
|
||||
postgres_configs = PostgresDatabaseConfig.from_yaml(PATHS.postgres_config_file_path)
|
||||
logger.info(f"Found file with {len(postgres_configs)} entries.")
|
||||
try:
|
||||
relevant_postgres_config = postgres_configs[postgres_database]
|
||||
except KeyError as e:
|
||||
logger.error(
|
||||
f"Couldn't find a config entry for database with id: {postgres_database}"
|
||||
)
|
||||
raise e
|
||||
|
||||
logger.info("Connecting and sending a SELECT 1...")
|
||||
query_result = simply_query(config=relevant_postgres_config, query=SQL("SELECT 1;"))
|
||||
logger.info(f"Response: {query_result}")
|
||||
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue