ensure_packages_installed logging

This commit is contained in:
clore 2025-01-03 16:05:39 +00:00
parent 49f8d46b45
commit a9d57c18ae
1 changed files with 27 additions and 1 deletions

View File

@ -1,11 +1,17 @@
from lib import logging as logging_lib from lib import logging as logging_lib
import aiofiles
import os
from datetime import datetime
from typing import List from typing import List
from lib import utils from lib import utils
import time import time
log = logging_lib.log log = logging_lib.log
LOGGING_ENABLED = True
async def ensure_packages_installed( async def ensure_packages_installed(
packages: List[str] = [], packages: List[str] = [],
total_timeout: float = 300 total_timeout: float = 300
@ -39,6 +45,11 @@ async def ensure_packages_installed(
timeout=None if total_timeout == None else 180, timeout=None if total_timeout == None else 180,
env=non_interactive_env 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: if return_code != 0:
log.error(f"Failed to update package lists: {stderr}") log.error(f"Failed to update package lists: {stderr}")
return False return False
@ -62,10 +73,25 @@ async def ensure_packages_installed(
timeout=remaining_timeout, timeout=remaining_timeout,
env=non_interactive_env 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: if return_code == 0:
log.debug(f"Successfully installed packages: {packages_to_install}") log.debug(f"Successfully installed packages: {packages_to_install}")
return True return True
else: else:
log.error(f"Failed to install packages: {stderr}") log.error(f"Failed to install packages: {stderr}")
return False 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