feat(inspector): implement slice 2 comprehensive host state inspector
This commit is contained in:
1 parent
2602b66a90
commit
66ca179937
11 files changed
+777
-25
No files matched your search
+45
-22
@@ -19,6 +19,7 @@ from linux_backuper.core.constants import (
|
||||
EXIT_SUCCESS,
|
||||
)
|
||||
from linux_backuper.core.detector import detect_distro
|
||||
from linux_backuper.core.inspector import SystemInspector
|
||||
from linux_backuper.models import SnapshotManifest
|
||||
|
||||
def check_root_permissions() -> bool:
|
||||
@@ -56,40 +57,58 @@ def handle_snapshot(args: argparse.Namespace) -> int:
|
||||
|
||||
os_info = detect_distro()
|
||||
archiver = SnapshotArchiver(target_dir=target_dir, compression=args.compression)
|
||||
inspector = SystemInspector(distro_id=os_info.distro_id)
|
||||
packages = inspector.inspect_packages()
|
||||
|
||||
if not args.json:
|
||||
sys.stderr.write(f"[INFO] Initializing snapshot '{snapshot_id}' (profile: {args.profile})...\n")
|
||||
sys.stderr.write(f"[INFO] Host OS: {os_info.distro_name} {os_info.version_id} ({os_info.arch})\n")
|
||||
sys.stderr.write(f"[INFO] Packages detected: {len(packages)} via {inspector.package_manager.get_manager_name()}\n")
|
||||
|
||||
# Pass 1: Collect files & calculate checksums
|
||||
# Pass 1: Collect data paths
|
||||
file_entries, files_sha256, uncompressed_bytes = archiver.collect_file_list(included_paths)
|
||||
|
||||
# Manifest preparation
|
||||
manifest = SnapshotManifest(
|
||||
schema_version="1.0",
|
||||
snapshot_id=snapshot_id,
|
||||
created_at=datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
||||
hostname=socket.gethostname(),
|
||||
profile=args.profile,
|
||||
compression=args.compression,
|
||||
os=os_info.to_dict(),
|
||||
package_manager="auto",
|
||||
package_count=0,
|
||||
included_paths=included_paths,
|
||||
stats={
|
||||
"file_count": len(file_entries),
|
||||
"uncompressed_bytes": uncompressed_bytes,
|
||||
"compressed_bytes": 0
|
||||
},
|
||||
files_sha256=files_sha256
|
||||
)
|
||||
# Pass 1b: Collect deep system telemetry meta files
|
||||
import shutil
|
||||
import tempfile
|
||||
from linux_backuper.core.archiver import calculate_file_sha256
|
||||
|
||||
# Pass 2: Create archive with manifest.json as the very first member
|
||||
temp_meta_dir = tempfile.mkdtemp(prefix="linux_backuper_meta_")
|
||||
try:
|
||||
meta_files_map = inspector.dump_meta_files(temp_meta_dir)
|
||||
for rel_path, abs_path in meta_files_map.items():
|
||||
file_entries.append((abs_path, rel_path))
|
||||
sha = calculate_file_sha256(abs_path)
|
||||
files_sha256[rel_path] = sha
|
||||
uncompressed_bytes += os.path.getsize(abs_path)
|
||||
|
||||
# Manifest preparation
|
||||
manifest = SnapshotManifest(
|
||||
schema_version="1.0",
|
||||
snapshot_id=snapshot_id,
|
||||
created_at=datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
||||
hostname=socket.gethostname(),
|
||||
profile=args.profile,
|
||||
compression=args.compression,
|
||||
os=os_info.to_dict(),
|
||||
package_manager=inspector.package_manager.get_manager_name(),
|
||||
package_count=len(packages),
|
||||
included_paths=included_paths,
|
||||
stats={
|
||||
"file_count": len(file_entries),
|
||||
"uncompressed_bytes": uncompressed_bytes,
|
||||
"compressed_bytes": 0
|
||||
},
|
||||
files_sha256=files_sha256
|
||||
)
|
||||
|
||||
# Pass 2: Create archive with manifest.json as the very first member
|
||||
archive_path = archiver.create_snapshot_archive(snapshot_id, manifest, file_entries)
|
||||
except Exception as e:
|
||||
sys.stderr.write(f"[ERROR] Failed to create snapshot archive: {e}\n")
|
||||
return EXIT_GENERIC_ERROR
|
||||
finally:
|
||||
shutil.rmtree(temp_meta_dir, ignore_errors=True)
|
||||
|
||||
if args.json:
|
||||
output_payload = {
|
||||
@@ -112,7 +131,11 @@ def handle_list(args: argparse.Namespace) -> int:
|
||||
"""Execute list command."""
|
||||
target_dir = args.target_dir or DEFAULT_BACKUP_DIR
|
||||
archiver = SnapshotArchiver(target_dir=target_dir)
|
||||
snapshots = archiver.list_snapshots()
|
||||
try:
|
||||
snapshots = archiver.list_snapshots()
|
||||
except PermissionError as e:
|
||||
sys.stderr.write(f"[ERROR] {e} (try running with sudo).\n")
|
||||
return EXIT_PERM_DENIED
|
||||
|
||||
if args.json:
|
||||
result = [s.to_dict() for s in snapshots]
|
||||
|
||||
Reference in new issue
Block a user