2019-09-25 14:26:29 +02:00
|
|
|
import logging
|
|
|
|
import os
|
2020-08-10 00:02:07 +02:00
|
|
|
import re
|
2019-09-25 14:26:29 +02:00
|
|
|
import sys
|
2021-01-10 04:30:07 +01:00
|
|
|
from typing import Any, List, Optional
|
2020-08-10 00:02:07 +02:00
|
|
|
|
2022-07-04 01:03:24 +02:00
|
|
|
import pytimeparse
|
2020-08-10 00:02:07 +02:00
|
|
|
import yaml
|
|
|
|
|
2022-06-13 20:55:01 +02:00
|
|
|
from matrix_alertbot.errors import ConfigError
|
2019-09-25 14:26:29 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger()
|
2021-01-04 05:34:56 +01:00
|
|
|
logging.getLogger("peewee").setLevel(
|
|
|
|
logging.INFO
|
|
|
|
) # Prevent debug messages from peewee lib
|
2019-09-25 14:26:29 +02:00
|
|
|
|
|
|
|
|
2021-01-10 04:30:07 +01:00
|
|
|
class Config:
|
2021-01-11 01:22:10 +01:00
|
|
|
"""Creates a Config object from a YAML-encoded config file from a given filepath"""
|
|
|
|
|
2021-01-10 04:30:07 +01:00
|
|
|
def __init__(self, filepath: str):
|
2021-01-11 01:22:10 +01:00
|
|
|
self.filepath = filepath
|
2019-09-25 14:26:29 +02:00
|
|
|
if not os.path.isfile(filepath):
|
|
|
|
raise ConfigError(f"Config file '{filepath}' does not exist")
|
|
|
|
|
|
|
|
# Load in the config file at the given filepath
|
|
|
|
with open(filepath) as file_stream:
|
2021-01-11 01:22:10 +01:00
|
|
|
self.config_dict = yaml.safe_load(file_stream.read())
|
|
|
|
|
|
|
|
# Parse and validate config options
|
|
|
|
self._parse_config_values()
|
2019-09-25 14:26:29 +02:00
|
|
|
|
2022-06-14 23:37:54 +02:00
|
|
|
def _parse_config_values(self) -> None:
|
2021-01-11 01:22:10 +01:00
|
|
|
"""Read and validate each config option"""
|
2019-09-25 14:26:29 +02:00
|
|
|
# Logging setup
|
2020-08-10 00:02:07 +02:00
|
|
|
formatter = logging.Formatter(
|
|
|
|
"%(asctime)s | %(name)s [%(levelname)s] %(message)s"
|
|
|
|
)
|
2019-09-25 14:26:29 +02:00
|
|
|
|
2020-02-24 00:17:25 +01:00
|
|
|
log_level = self._get_cfg(["logging", "level"], default="INFO")
|
2019-09-25 14:26:29 +02:00
|
|
|
logger.setLevel(log_level)
|
|
|
|
|
2020-08-10 00:02:07 +02:00
|
|
|
file_logging_enabled = self._get_cfg(
|
|
|
|
["logging", "file_logging", "enabled"], default=False
|
|
|
|
)
|
|
|
|
file_logging_filepath = self._get_cfg(
|
|
|
|
["logging", "file_logging", "filepath"], default="bot.log"
|
|
|
|
)
|
2019-09-25 14:26:29 +02:00
|
|
|
if file_logging_enabled:
|
2022-06-14 23:37:54 +02:00
|
|
|
file_handler = logging.FileHandler(file_logging_filepath)
|
|
|
|
file_handler.setFormatter(formatter)
|
|
|
|
logger.addHandler(file_handler)
|
2019-09-25 14:26:29 +02:00
|
|
|
|
2020-08-10 00:02:07 +02:00
|
|
|
console_logging_enabled = self._get_cfg(
|
|
|
|
["logging", "console_logging", "enabled"], default=True
|
|
|
|
)
|
2019-09-25 14:26:29 +02:00
|
|
|
if console_logging_enabled:
|
2022-06-14 23:37:54 +02:00
|
|
|
console_handler = logging.StreamHandler(sys.stdout)
|
|
|
|
console_handler.setFormatter(formatter)
|
|
|
|
logger.addHandler(console_handler)
|
2019-09-25 14:26:29 +02:00
|
|
|
|
2020-02-24 23:13:28 +01:00
|
|
|
# Storage setup
|
2020-08-16 18:00:55 +02:00
|
|
|
self.store_path = self._get_cfg(["storage", "store_path"], required=True)
|
2019-09-25 14:26:29 +02:00
|
|
|
|
2020-02-25 00:56:09 +01:00
|
|
|
# Create the store folder if it doesn't exist
|
2020-08-16 17:17:57 +02:00
|
|
|
if not os.path.isdir(self.store_path):
|
|
|
|
if not os.path.exists(self.store_path):
|
|
|
|
os.mkdir(self.store_path)
|
2020-02-25 00:56:09 +01:00
|
|
|
else:
|
2020-08-10 00:02:07 +02:00
|
|
|
raise ConfigError(
|
2020-08-16 17:17:57 +02:00
|
|
|
f"storage.store_path '{self.store_path}' is not a directory"
|
2020-08-10 00:02:07 +02:00
|
|
|
)
|
2020-02-25 00:56:09 +01:00
|
|
|
|
2022-07-04 01:03:24 +02:00
|
|
|
# Cache setup
|
|
|
|
self.cache_dir = self._get_cfg(["cache", "directory"], required=True)
|
|
|
|
expire_time = self._get_cfg(["cache", "expire_time"], default="1w")
|
|
|
|
self.cache_expire_time = pytimeparse.parse(expire_time)
|
|
|
|
|
|
|
|
# Alertmanager client setup
|
|
|
|
self.alertmanager_url = self._get_cfg(["alertmanager", "url"], required=True)
|
2020-08-16 16:51:59 +02:00
|
|
|
|
2019-09-25 14:26:29 +02:00
|
|
|
# Matrix bot account setup
|
2020-02-24 00:17:25 +01:00
|
|
|
self.user_id = self._get_cfg(["matrix", "user_id"], required=True)
|
|
|
|
if not re.match("@.*:.*", self.user_id):
|
2019-09-25 14:26:29 +02:00
|
|
|
raise ConfigError("matrix.user_id must be in the form @name:domain")
|
|
|
|
|
2020-12-14 23:38:47 +01:00
|
|
|
self.user_password = self._get_cfg(["matrix", "user_password"], required=False)
|
|
|
|
self.user_token = self._get_cfg(["matrix", "user_token"], required=False)
|
|
|
|
if not self.user_token and not self.user_password:
|
|
|
|
raise ConfigError("Must supply either user token or password")
|
|
|
|
|
2020-02-24 23:13:28 +01:00
|
|
|
self.device_id = self._get_cfg(["matrix", "device_id"], required=True)
|
2020-08-10 00:02:07 +02:00
|
|
|
self.device_name = self._get_cfg(
|
|
|
|
["matrix", "device_name"], default="nio-template"
|
|
|
|
)
|
2022-07-04 01:03:24 +02:00
|
|
|
self.homeserver_url = self._get_cfg(["matrix", "url"], required=True)
|
2022-07-08 21:11:25 +02:00
|
|
|
self.room_id = self._get_cfg(["matrix", "room"], required=True)
|
2022-07-04 01:03:24 +02:00
|
|
|
|
|
|
|
self.address = self._get_cfg(["webhook", "address"], required=False)
|
|
|
|
self.port = self._get_cfg(["webhook", "port"], required=False)
|
|
|
|
self.socket = self._get_cfg(["webhook", "socket"], required=False)
|
|
|
|
if (
|
|
|
|
not (self.address or self.port or self.socket)
|
|
|
|
or (self.socket and self.address and self.port)
|
|
|
|
or (self.address and not self.port)
|
|
|
|
or (not self.address and self.port)
|
|
|
|
):
|
|
|
|
raise ConfigError(
|
|
|
|
"Must supply either webhook.socket or both webhook.address and webhook.port"
|
|
|
|
)
|
2020-02-24 00:17:25 +01:00
|
|
|
self.command_prefix = self._get_cfg(["command_prefix"], default="!c") + " "
|
|
|
|
|
|
|
|
def _get_cfg(
|
2021-01-04 05:34:56 +01:00
|
|
|
self,
|
|
|
|
path: List[str],
|
2021-01-10 04:30:07 +01:00
|
|
|
default: Optional[Any] = None,
|
|
|
|
required: Optional[bool] = True,
|
2020-02-24 00:17:25 +01:00
|
|
|
) -> Any:
|
|
|
|
"""Get a config option from a path and option name, specifying whether it is
|
|
|
|
required.
|
|
|
|
|
|
|
|
Raises:
|
2021-01-10 04:30:07 +01:00
|
|
|
ConfigError: If required is True and the object is not found (and there is
|
|
|
|
no default value provided), a ConfigError will be raised.
|
2020-02-24 00:17:25 +01:00
|
|
|
"""
|
|
|
|
# Sift through the the config until we reach our option
|
2021-01-11 01:22:10 +01:00
|
|
|
config = self.config_dict
|
2020-02-24 00:17:25 +01:00
|
|
|
for name in path:
|
|
|
|
config = config.get(name)
|
|
|
|
|
|
|
|
# If at any point we don't get our expected option...
|
|
|
|
if config is None:
|
|
|
|
# Raise an error if it was required
|
2020-12-14 23:27:58 +01:00
|
|
|
if required and not default:
|
2020-02-24 23:13:28 +01:00
|
|
|
raise ConfigError(f"Config option {'.'.join(path)} is required")
|
2020-02-24 00:17:25 +01:00
|
|
|
|
|
|
|
# or return the default value
|
|
|
|
return default
|
2019-09-25 14:26:29 +02:00
|
|
|
|
2021-01-10 04:30:07 +01:00
|
|
|
# We found the option. Return it.
|
2020-02-24 00:17:25 +01:00
|
|
|
return config
|