2019-09-25 14:26:29 +02:00
|
|
|
#!/usr/bin/env python3
|
2024-01-22 11:35:13 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-09-25 14:26:29 +02:00
|
|
|
import asyncio
|
2020-08-10 00:02:07 +02:00
|
|
|
import logging
|
2020-05-31 21:20:54 +02:00
|
|
|
import sys
|
2020-08-10 00:02:07 +02:00
|
|
|
|
2022-07-08 21:11:25 +02:00
|
|
|
from diskcache import Cache
|
2020-08-10 00:02:07 +02:00
|
|
|
|
2022-07-04 01:03:24 +02:00
|
|
|
from matrix_alertbot.alertmanager import AlertmanagerClient
|
2022-06-13 20:55:01 +02:00
|
|
|
from matrix_alertbot.config import Config
|
2024-01-22 11:35:13 +01:00
|
|
|
from matrix_alertbot.matrix import MatrixClientPool
|
2022-07-04 01:03:24 +02:00
|
|
|
from matrix_alertbot.webhook import Webhook
|
2019-09-25 14:26:29 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-07-08 21:11:25 +02:00
|
|
|
def main() -> None:
|
|
|
|
"""The first function that is run when starting the bot"""
|
|
|
|
|
|
|
|
# Read user-configured options from a config file.
|
|
|
|
# A different config file path can be specified as the first command line argument
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
config_path = sys.argv[1]
|
|
|
|
else:
|
|
|
|
config_path = "config.yaml"
|
|
|
|
|
|
|
|
# Read the parsed config file and create a Config object
|
|
|
|
config = Config(config_path)
|
2022-07-04 01:03:24 +02:00
|
|
|
|
2022-07-08 21:11:25 +02:00
|
|
|
# Configure the cache
|
|
|
|
cache = Cache(config.cache_dir)
|
2022-07-04 01:03:24 +02:00
|
|
|
|
2022-07-08 23:04:04 +02:00
|
|
|
# Configure Alertmanager client
|
2022-08-08 00:28:36 +02:00
|
|
|
alertmanager_client = AlertmanagerClient(config.alertmanager_url, cache)
|
2022-07-08 23:04:04 +02:00
|
|
|
|
2024-01-22 11:35:13 +01:00
|
|
|
# Create matrix clients
|
|
|
|
matrix_client_pool = MatrixClientPool(alertmanager_client, cache, config)
|
2022-07-08 23:04:04 +02:00
|
|
|
# Configure webhook server
|
2024-01-22 11:35:13 +01:00
|
|
|
webhook_server = Webhook(matrix_client_pool, alertmanager_client, cache, config)
|
2022-07-08 23:04:04 +02:00
|
|
|
|
2022-07-04 01:03:24 +02:00
|
|
|
loop = asyncio.get_event_loop()
|
2024-01-22 11:35:13 +01:00
|
|
|
loop.create_task(matrix_client_pool.switch_active_client())
|
2022-07-09 12:31:05 +02:00
|
|
|
loop.create_task(webhook_server.start())
|
2024-01-22 11:35:13 +01:00
|
|
|
for account in config.accounts:
|
|
|
|
loop.create_task(matrix_client_pool.start(account, config))
|
2022-07-04 01:03:24 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
loop.run_forever()
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(e)
|
2022-07-08 23:04:04 +02:00
|
|
|
finally:
|
|
|
|
loop.run_until_complete(webhook_server.close())
|
2022-08-08 00:28:36 +02:00
|
|
|
loop.run_until_complete(alertmanager_client.close())
|
2024-01-22 11:35:13 +01:00
|
|
|
loop.run_until_complete(matrix_client_pool.close())
|
2022-07-10 18:09:25 +02:00
|
|
|
cache.close()
|