hosting/lib/ensure_packages_installed.py

116 lines
3.8 KiB
Python

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
) -> bool:
non_interactive_env = {
'DEBIAN_FRONTEND': 'noninteractive',
'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
}
start_time = time.time()
packages_to_install = []
for package in packages:
check_cmd = f"dpkg -s {package} > /dev/null 2>&1"
return_code, _, _ = await utils.async_run_command(check_cmd, env=non_interactive_env)
if return_code != 0:
packages_to_install.append(package)
if not packages_to_install:
log.debug("All packages are already installed.")
return True
update_cmd = (
"apt-get update -y --no-install-recommends"
)
return_code, stdout, stderr = await utils.async_run_command(
update_cmd,
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
install_cmd = (
"apt-get install -y --no-install-recommends --assume-yes "+
"-o Dpkg::Options::='--force-confdef' "+ # Default to existing config
"-o Dpkg::Options::='--force-confold' "+ # Keep existing config
f"{' '.join(packages_to_install)}"
)
# Calculate remaining timeout
remaining_timeout = None if total_timeout == None else max(0, total_timeout - (time.time() - start_time))
# Install packages
return_code, stdout, stderr = await utils.async_run_command(
install_cmd,
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
elif return_code == 100:
dpkg_rc, dpkg_stdout, dpkg_stderr = await utils.async_run_command(
"sudo dpkg --configure -a",
timeout=200,
env=non_interactive_env
)
# Install packages
return_code, stdout, stderr = await utils.async_run_command(
install_cmd,
timeout=remaining_timeout,
env=non_interactive_env
)
if LOGGING_ENABLED:
await ensure_packages_installed_log(f"post-dpkg install stdout: {stdout}")
await ensure_packages_installed_log(f"post-dpkg 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
else:
log.error(f"Failed to install packages: {stderr}")
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