From 49f8d46b452d90bb738915d6ecee5b7de1208aaa Mon Sep 17 00:00:00 2001 From: clore Date: Fri, 3 Jan 2025 03:59:24 +0000 Subject: [PATCH] remove spaces in ensure_packages_installed, add xfs migration logging --- lib/ensure_packages_installed.py | 16 ++++++++-------- lib/xfs.py | 20 +++++++++++++++++++- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/lib/ensure_packages_installed.py b/lib/ensure_packages_installed.py index df78a7b..a38bea2 100644 --- a/lib/ensure_packages_installed.py +++ b/lib/ensure_packages_installed.py @@ -30,8 +30,8 @@ async def ensure_packages_installed( return True update_cmd = ( - "apt-get update " - "-y " + "apt-get update" + "-y" "--no-install-recommends" ) return_code, stdout, stderr = await utils.async_run_command( @@ -44,12 +44,12 @@ async def ensure_packages_installed( 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 + "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)}" ) diff --git a/lib/xfs.py b/lib/xfs.py index 2949d8e..58d9978 100644 --- a/lib/xfs.py +++ b/lib/xfs.py @@ -6,6 +6,7 @@ from lib import networking from lib import get_specs from lib import utils +from datetime import datetime import asyncio import json import os @@ -66,8 +67,10 @@ def get_to_use_storage_values(max_free_space): def migrate(): docker_xfs_state = validate_docker_xfs() if docker_xfs_state == "skip": + migrate_log("skipping migration") return elif docker_xfs_state == "valid": + migrate_log("migration succeeded") return 'success' packages_available = asyncio.run(ensure_packages_installed.ensure_packages_installed( @@ -75,15 +78,18 @@ def migrate(): )) if not packages_available: + migrate_log("packages missing") return 'packages-missing' root_device = get_specs.get_root_device() if not root_device: + migrate_log("not supported boot device") return "not-supported-boot-device" device_name = os.path.basename(root_device).split('p')[0].rstrip('0123456789') if get_specs.is_usb_device(device_name): + migrate_log("not supported boot device") return "not-supported-boot-device" log.info("Starting migration to xfs") @@ -93,17 +99,19 @@ def migrate(): try: os.remove(DOCKER_DATA_IMG) except Exception as e: - print(f"Error while trying to remove {DOCKER_DATA_IMG}: {e}") + migrate_log("Failed to remove DOCKER_DATA_IMG") return "failure" max_free_space = utils.get_free_space_mb('/') + utils.get_directory_size_mb(DOCKER_ROOT) leave_free_space, min_xfs_size = get_to_use_storage_values(max_free_space) if leave_free_space == None: + migrate_log("can't get free space") return "failure" data_img_size = int(max_free_space - leave_free_space) if data_img_size < min_xfs_size: + migrate_log("not enought free space") return 'not-enough-space' docker_config_success = False @@ -126,6 +134,7 @@ def migrate(): if code==0: return 'success' else: + migrate_log("failed to migrate v1") configure_docker_daemon(remove=True) configure_fstab(remove=True) return 'failure' @@ -138,7 +147,16 @@ def migrate(): os.remove(DOCKER_DATA_IMG) except Exception as e: pass + migrate_log("failed to migrate v2") return 'failure' + +def migrate_log(msg): + log_file_path = "/opt/clore-hosting/migrate-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" + with open(log_file_path, "a") as log_file: + log_file.write(log_message) def validate_docker_xfs(): code_root, stdout_root, stderr_root = utils.run_command("df -T /")