diff --git a/lib/ensure_packages_installed.py b/lib/ensure_packages_installed.py index a38bea2..9abb651 100644 --- a/lib/ensure_packages_installed.py +++ b/lib/ensure_packages_installed.py @@ -1,11 +1,17 @@ from lib import logging as logging_lib +import aiofiles +import os +from datetime import datetime + from typing import List from lib import utils import time log = logging_lib.log +LOGGING_ENABLED = True + async def ensure_packages_installed( packages: List[str] = [], total_timeout: float = 300 @@ -39,6 +45,11 @@ async def ensure_packages_installed( timeout=None if total_timeout == None else 180, env=non_interactive_env ) + + if LOGGING_ENABLED: + await ensure_packages_installed_log(f"update stdout: {stdout}") + await ensure_packages_installed_log(f"update stderr: {stderr}\ncode: {str(return_code)}") + if return_code != 0: log.error(f"Failed to update package lists: {stderr}") return False @@ -62,10 +73,25 @@ async def ensure_packages_installed( timeout=remaining_timeout, env=non_interactive_env ) + + if LOGGING_ENABLED: + await ensure_packages_installed_log(f"install stdout: {stdout}") + await ensure_packages_installed_log(f"install stderr: {stderr}\ncode: {str(return_code)}") if return_code == 0: log.debug(f"Successfully installed packages: {packages_to_install}") return True else: log.error(f"Failed to install packages: {stderr}") - return False \ No newline at end of file + return False + +async def ensure_packages_installed_log(msg): + try: + log_file_path = "/opt/clore-hosting/ensure-packages-installed-log.txt" + os.makedirs(os.path.dirname(log_file_path), exist_ok=True) + current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + log_message = f"{current_time} | {msg}\n" + async with aiofiles.open(log_file_path, "a") as log_file: + await log_file.write(log_message) + except Exception as e: + pass \ No newline at end of file