79 lines
3.3 KiB
Python
79 lines
3.3 KiB
Python
"""Installer migration from owned manual-only preferences to normal NM fallback."""
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
FAMILIES = ("ipv4", "ipv6")
|
|
|
|
|
|
def migrate(root: Path, adapter):
|
|
path = root / "operation.json"
|
|
receipt = root / "native-fallback-v1.json"
|
|
if not path.exists():
|
|
return
|
|
op = json.loads(path.read_text())
|
|
if op.get("network_policy") in ("native-fallback-v1", "native-fallback-v2"):
|
|
return
|
|
if op.get("phase") not in ("applied", "rolled_back", "superseded"):
|
|
raise ValueError("Завершите текущую операцию переключения перед обновлением.")
|
|
entries = sorted(op.get("profile_baseline") or op.get("baseline", []), key=lambda e: e["id"])
|
|
pending = root / "native-fallback-v1.pending.json"
|
|
if pending.exists():
|
|
adapter.restore_profiles(json.loads(pending.read_text())["changes"])
|
|
current = adapter.profile_preferences()
|
|
changes, skipped = [], []
|
|
for index, entry in enumerate(entries):
|
|
saved = entry.get("saved")
|
|
value = current.get(entry["id"], {}).get("saved")
|
|
if not saved or value is None or entry.get("kind") not in ("ethernet", "wifi"):
|
|
continue
|
|
old = entry.get("planned") or dict(
|
|
autoconnect=entry["kind"] == "ethernet" or entry["id"] == op["target"],
|
|
metrics={f: 10 if entry["id"] == op["target"] else 100 + index for f in FAMILIES},
|
|
defaults={f: entry["id"] != op["target"] for f in FAMILIES},
|
|
)
|
|
restored = dict(value, autoconnect=saved["autoconnect"], defaults=saved["defaults"])
|
|
if value != old and value != saved:
|
|
skipped.append(entry["id"])
|
|
continue
|
|
if value == restored:
|
|
continue
|
|
changes.append(dict(entry, saved=value, planned=restored))
|
|
# Persist the exact undo record before the first write. A failed installation
|
|
# restores already changed profiles and can retry; unrelated edits survive.
|
|
root.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
with pending.open("w") as stream:
|
|
os.chmod(pending, 0o600)
|
|
json.dump(dict(changes=changes, skipped=skipped), stream)
|
|
stream.flush()
|
|
os.fsync(stream.fileno())
|
|
live = None
|
|
try:
|
|
for entry in changes:
|
|
adapter.set_preference(entry, entry["planned"])
|
|
live = adapter.inspect(known=entries)
|
|
for entry in live["candidates"]:
|
|
if entry["id"] in {e["id"] for e in changes}:
|
|
adapter.release_reserves([entry], "")
|
|
except Exception:
|
|
adapter.restore_profiles(changes)
|
|
if live:
|
|
adapter.restore_metrics(live["candidates"])
|
|
raise
|
|
# Keep priorities, but take ownership only of profiles proved by this journal.
|
|
updated = adapter.profile_preferences()
|
|
op["profile_baseline"] = [
|
|
dict(e, saved=updated[e["id"]]["saved"], planned=updated[e["id"]]["saved"])
|
|
for e in entries if e["id"] in updated and e["id"] not in skipped
|
|
]
|
|
op["network_policy"] = "native-fallback-v1"
|
|
temporary = path.with_suffix(".migration.tmp")
|
|
with temporary.open("w") as stream:
|
|
os.chmod(temporary, 0o600)
|
|
json.dump(op, stream)
|
|
stream.flush()
|
|
os.fsync(stream.fileno())
|
|
temporary.replace(path)
|
|
pending.replace(receipt)
|