-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRPM_sync_host_relay.py
More file actions
95 lines (80 loc) · 3.61 KB
/
RPM_sync_host_relay.py
File metadata and controls
95 lines (80 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import subprocess
import json
import os
import sys
import shutil
import argparse
import tempfile
def detect_system_type():
"""Detects if the system uses rpm-ostree or dnf."""
if os.path.exists('/run/ostree-booted'):
return 'ostree'
elif shutil.which('dnf'):
return 'dnf'
else:
return None
def get_installed_packages(system_type):
"""Get the set of installed package names based on system type."""
packages = set()
try:
if system_type == 'ostree':
result = subprocess.run(['rpm-ostree', 'status', '--json'], capture_output=True, text=True, check=True)
status = json.loads(result.stdout)
if status and status[0].get('packages'):
packages = set(status[0]['packages'])
elif system_type == 'dnf':
result = subprocess.run(['rpm', '-qa', '--qf', '%{NAME}\n'], capture_output=True, text=True, check=True)
packages = set(result.stdout.strip().split('\n'))
except FileNotFoundError:
print(f"Error: Command not found for system type '{system_type}'.", file=sys.stderr)
sys.exit(1)
except subprocess.CalledProcessError as e:
print(f"Error running package listing command: {e}", file=sys.stderr)
sys.exit(1)
except json.JSONDecodeError as e:
print(f"Error parsing JSON from rpm-ostree status: {e}", file=sys.stderr)
sys.exit(1)
return packages
def sync_packages_host_relay(remote_host, system_type):
"""Synchronize packages using host as relay (no direct VM-to-VM communication needed)."""
print(f"Detected system type: {system_type}")
print("Note: This version uses the host as a relay - no direct VM communication required")
# Get local packages
local_packages = get_installed_packages(system_type)
print(f"Local system has {len(local_packages)} packages")
# Create a temporary file to store package info
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
local_pkg_file = f.name
for package in sorted(list(local_packages)):
f.write(f"{package}\n")
print(f"Local packages written to {local_pkg_file}")
# Since we can't communicate directly with the remote host from this VM,
# we'll create a simple package list and let the user handle the sync manually
print(f"\nTo complete the sync:")
print(f"1. Copy this package list to the remote host ({remote_host})")
print(f"2. Run a similar script on the remote host")
print(f"3. Compare and install missing packages")
print(f"\nLocal package list saved to: {local_pkg_file}")
print("You can copy this file to the remote host for comparison")
# Show some sample packages
sample_packages = sorted(list(local_packages))[:10]
print(f"\nSample packages on this system:")
for pkg in sample_packages:
print(f" - {pkg}")
if len(local_packages) > 10:
print(f" ... and {len(local_packages) - 10} more packages")
# Clean up
try:
os.unlink(local_pkg_file)
except OSError:
pass
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Show installed packages for manual synchronization.")
parser.add_argument("-r", "--remote-host", required=True, help="The remote host (for reference only).")
args = parser.parse_args()
system_type = detect_system_type()
if system_type is None:
print("Error: Could not detect a supported package manager (rpm-ostree or dnf).", file=sys.stderr)
sys.exit(1)
sync_packages_host_relay(args.remote_host, system_type)
print("\nPackage listing complete.")