albs-oval-errata-diff/albs_oval_errata_diff/start.py

115 lines
3.6 KiB
Python
Raw Normal View History

2022-12-28 16:21:40 +00:00
"""
service compares rhel oval with alma ovals and errata ovals
results available via API Call
"""
import copy
import logging
2022-12-28 16:57:00 +00:00
from logging.handlers import RotatingFileHandler
2022-12-28 16:21:40 +00:00
import threading
from time import sleep
2022-12-29 14:29:18 +00:00
from ipaddress import IPv4Address
2022-12-28 16:21:40 +00:00
import json
2022-12-29 14:29:18 +00:00
from aiohttp import web
from .config import get_config, Config
2022-12-28 16:21:40 +00:00
from .comparer import comparer_run
# This dict holds all current differences
2022-12-28 16:21:40 +00:00
diffs = {}
diffs_lock = threading.Lock()
2022-12-29 14:29:18 +00:00
async def web_handler(_):
"""
web_handler returns diffs as JSON file
"""
2022-12-28 16:21:40 +00:00
data = {}
try:
diffs_lock.acquire()
data = copy.deepcopy(diffs)
diffs_lock.release()
2022-12-29 14:29:18 +00:00
except Exception as err: # pylint: disable=broad-except
logging.critical("Unhandled exeption %s", err, exc_info=True)
2022-12-28 16:21:40 +00:00
return web.json_response(data=data)
2022-12-29 14:29:18 +00:00
def webserver_run(server_ip: IPv4Address, server_port: str):
"""
webserver_run starts webserver component
"""
2022-12-28 16:21:40 +00:00
app = web.Application()
app.add_routes([web.get('/', web_handler)])
2022-12-29 14:29:18 +00:00
web.run_app(app=app, host=str(server_ip), port=server_port)
2022-12-28 16:21:40 +00:00
2022-12-29 14:29:18 +00:00
def diff_checker(config: Config):
"""
2022-12-29 16:00:58 +00:00
Runs comparer component in infinite loop
2022-12-29 14:29:18 +00:00
"""
2022-12-28 16:21:40 +00:00
while True:
logging.info("Start comparing")
# generating new diff
try:
2022-12-29 14:29:18 +00:00
result = comparer_run(config)
except Exception as err: # pylint: disable=broad-except
logging.critical("Unhandled exeption %s", err, exc_info=True)
2022-12-28 16:21:40 +00:00
else:
logging.info("Finished comparing, updating diff dict")
diffs_lock.acquire()
2022-12-29 14:29:18 +00:00
global diffs # pylint: disable=invalid-name,global-statement
2022-12-28 16:21:40 +00:00
diffs = result
diffs_lock.release()
2022-12-29 14:29:18 +00:00
# dumping
logging.info("Saving results to disk")
try:
with open(config.diff_file, 'w', encoding='utf-8') as flw:
json.dump(result, flw, indent=4)
except Exception as err: # pylint: disable=broad-except
logging.critical("Unhandled exeption %s", err, exc_info=True)
2022-12-28 16:21:40 +00:00
logging.info("Done")
logging.info("Finished comparing, go to sleep for %d minutes",
2022-12-29 14:29:18 +00:00
config.update_interval_minutes)
sleep(config.update_interval_minutes * 60)
def start(yaml_path: str):
"""
start starts comparer and webserver components
each component runs in it`s own thread
"""
config = get_config(yaml_path)
# making sure that parent directories exist
for path in [config.log_file, config.log_file]:
if not path.parent.exists():
path.parent.mkdir()
# configuring logging
2022-12-28 16:21:40 +00:00
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)s %(funcName)s %(message)s',
2022-12-30 15:25:42 +00:00
handlers=[RotatingFileHandler(config.log_file, maxBytes=10000000, backupCount=3)])
2022-12-28 16:21:40 +00:00
logging.info("Trying to load diff file from disk")
try:
2022-12-29 14:29:18 +00:00
with open(config.diff_file, 'r', encoding='utf-8') as flr:
2022-12-28 16:21:40 +00:00
loaded_data = json.load(flr)
diffs_lock.acquire()
2022-12-29 14:29:18 +00:00
global diffs # pylint: disable=invalid-name,global-statement
2022-12-28 16:21:40 +00:00
diffs = loaded_data
diffs_lock.release()
2022-12-29 14:29:18 +00:00
except Exception as err: # pylint: disable=broad-except
2022-12-29 16:00:58 +00:00
logging.warning('Cant load data from disk %s', err)
2022-12-28 16:21:40 +00:00
else:
2022-12-29 16:00:58 +00:00
logging.info('Diff file was loaded')
2022-12-28 16:21:40 +00:00
logging.info("Starting diff_checker in background")
2022-12-29 14:29:18 +00:00
thread = threading.Thread(target=diff_checker, args=(config,))
2022-12-28 16:21:40 +00:00
thread.daemon = True
thread.start()
logging.info("Starting webserver")
2022-12-29 14:29:18 +00:00
webserver_run(config.server_ip, config.server_port)