Move configuration code to its own module
This commit is contained in:
parent
eee471e9a7
commit
189681c5fa
3 changed files with 97 additions and 89 deletions
|
@ -1,66 +1,11 @@
|
||||||
"""
|
"""
|
||||||
A module containing all console script functions.
|
A module containing all console script functions.
|
||||||
"""
|
"""
|
||||||
import argparse
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
|
||||||
import logging.config
|
|
||||||
from logging import Logger
|
|
||||||
from typing import Any, Dict, Tuple
|
|
||||||
import os
|
|
||||||
import yaml
|
|
||||||
|
|
||||||
from chweb.collector import Collector, Producer
|
from chweb.collector import Collector, Producer
|
||||||
from chweb.consumer import Consumer
|
from chweb.consumer import Consumer
|
||||||
from chweb.models import Config
|
from chweb.config import configure
|
||||||
|
|
||||||
|
|
||||||
def configure(name) -> Tuple[Config, Logger]:
|
|
||||||
"""
|
|
||||||
Gets the configuration and creates a Pydantic model from the parsed YAML.
|
|
||||||
"""
|
|
||||||
parser = argparse.ArgumentParser(
|
|
||||||
description='Website availibility checker.')
|
|
||||||
parser.add_argument('-c', '--config', type=str,
|
|
||||||
default="/etc/checker.yaml",
|
|
||||||
help=('The yaml config file. '
|
|
||||||
'Defaults to /etc/checker.yaml'))
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
with open(args.config, 'r') as conf_file:
|
|
||||||
conf = yaml.load(conf_file, Loader=yaml.FullLoader)
|
|
||||||
logging.config.dictConfig(conf['logging'])
|
|
||||||
|
|
||||||
config = create_config(conf)
|
|
||||||
logger = logging.getLogger("chweb.{}".format(name))
|
|
||||||
return (config, logger)
|
|
||||||
|
|
||||||
|
|
||||||
def create_config(conf: Dict[str, Any]):
|
|
||||||
kafka_servers_env = os.getenv('KAFKA_SERVERS')
|
|
||||||
if kafka_servers_env is not None:
|
|
||||||
kafka_servers = kafka_servers_env.split(',')
|
|
||||||
|
|
||||||
kafka_topic = os.getenv('KAFKA_TOPIC')
|
|
||||||
|
|
||||||
pg_db = os.getenv('POSTGRES_DB')
|
|
||||||
pg_host = os.getenv('POSTGRES_HOST')
|
|
||||||
pg_port = os.getenv('POSTGRES_PORT')
|
|
||||||
pg_user = os.getenv('POSTGRES_USER')
|
|
||||||
pg_pass = os.getenv('POSTGRES_PASS')
|
|
||||||
|
|
||||||
config = Config(**conf)
|
|
||||||
config.kafka.servers = (kafka_servers if kafka_servers_env
|
|
||||||
else config.kafka.servers)
|
|
||||||
config.kafka.topic = kafka_topic or config.kafka.topic
|
|
||||||
config.postgres.dbhost = pg_host or config.postgres.dbhost
|
|
||||||
config.postgres.dbname = pg_db or config.postgres.dbname
|
|
||||||
config.postgres.dbport = (int(pg_port) if pg_port is not None
|
|
||||||
else config.postgres.dbport)
|
|
||||||
config.postgres.dbuser = pg_user or config.postgres.dbuser
|
|
||||||
config.postgres.dbpass = pg_pass or config.postgres.dbpass
|
|
||||||
|
|
||||||
return config
|
|
||||||
|
|
||||||
|
|
||||||
def collect():
|
def collect():
|
||||||
|
|
60
src/chweb/config.py
Normal file
60
src/chweb/config.py
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
"""
|
||||||
|
A module containing all console script functions.
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import logging
|
||||||
|
import logging.config
|
||||||
|
from logging import Logger
|
||||||
|
from typing import Any, Dict, Tuple
|
||||||
|
import os
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from chweb.models import Config
|
||||||
|
|
||||||
|
|
||||||
|
def configure(name) -> Tuple[Config, Logger]:
|
||||||
|
"""
|
||||||
|
Gets the configuration and creates a Pydantic model from the parsed YAML.
|
||||||
|
"""
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description='Website availibility checker.')
|
||||||
|
parser.add_argument('-c', '--config', type=str,
|
||||||
|
default="/etc/checker.yaml",
|
||||||
|
help=('The yaml config file. '
|
||||||
|
'Defaults to /etc/checker.yaml'))
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
with open(args.config, 'r') as conf_file:
|
||||||
|
conf = yaml.load(conf_file, Loader=yaml.FullLoader)
|
||||||
|
logging.config.dictConfig(conf['logging'])
|
||||||
|
|
||||||
|
config = create_config(conf)
|
||||||
|
logger = logging.getLogger("chweb.{}".format(name))
|
||||||
|
return (config, logger)
|
||||||
|
|
||||||
|
|
||||||
|
def create_config(conf: Dict[str, Any]):
|
||||||
|
kafka_servers_env = os.getenv('KAFKA_SERVERS')
|
||||||
|
if kafka_servers_env is not None:
|
||||||
|
kafka_servers = kafka_servers_env.split(',')
|
||||||
|
|
||||||
|
kafka_topic = os.getenv('KAFKA_TOPIC')
|
||||||
|
|
||||||
|
pg_db = os.getenv('POSTGRES_DB')
|
||||||
|
pg_host = os.getenv('POSTGRES_HOST')
|
||||||
|
pg_port = os.getenv('POSTGRES_PORT')
|
||||||
|
pg_user = os.getenv('POSTGRES_USER')
|
||||||
|
pg_pass = os.getenv('POSTGRES_PASS')
|
||||||
|
|
||||||
|
config = Config(**conf)
|
||||||
|
config.kafka.servers = (kafka_servers if kafka_servers_env
|
||||||
|
else config.kafka.servers)
|
||||||
|
config.kafka.topic = kafka_topic or config.kafka.topic
|
||||||
|
config.postgres.dbhost = pg_host or config.postgres.dbhost
|
||||||
|
config.postgres.dbname = pg_db or config.postgres.dbname
|
||||||
|
config.postgres.dbport = (int(pg_port) if pg_port is not None
|
||||||
|
else config.postgres.dbport)
|
||||||
|
config.postgres.dbuser = pg_user or config.postgres.dbuser
|
||||||
|
config.postgres.dbpass = pg_pass or config.postgres.dbpass
|
||||||
|
|
||||||
|
return config
|
|
@ -1,8 +1,9 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import pytest
|
import pytest
|
||||||
from chweb.cmd import create_config
|
from chweb.config import create_config
|
||||||
from chweb.models import Check
|
from chweb.models import Check
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def config():
|
def config():
|
||||||
config_dict = {
|
config_dict = {
|
||||||
|
@ -26,6 +27,7 @@ def config():
|
||||||
}
|
}
|
||||||
return create_config(config_dict)
|
return create_config(config_dict)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def config_invalid():
|
def config_invalid():
|
||||||
config_dict = {
|
config_dict = {
|
||||||
|
@ -49,6 +51,7 @@ def config_invalid():
|
||||||
}
|
}
|
||||||
return create_config(config_dict)
|
return create_config(config_dict)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def check():
|
def check():
|
||||||
return Check(
|
return Check(
|
||||||
|
|
Loading…
Reference in a new issue