"""Seed only empty installation-owned volumes; resumed live data is never chowned.""" import json import os from pathlib import Path import shutil import sys project, uid, gid, seeds = sys.argv[1:] uid, gid = int(uid), int(gid) assert uid > 0 and gid >= 0 def save(path, value): temporary = path.with_name(".thothii-initializing.tmp") fd = os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o600) with os.fdopen(fd, "w") as stream: json.dump(value, stream) stream.flush() os.fsync(stream.fileno()) os.replace(temporary, path) directory = os.open(path.parent, os.O_DIRECTORY) try: os.fsync(directory) finally: os.close(directory) for name, source in json.loads(seeds).items(): root = Path("/bootstrap") / name marker = root / ".thothii-volume.json" identity = {"project": project, "volume": name, "uid": uid, "gid": gid} if marker.exists(): assert not marker.is_symlink() assert marker.stat().st_uid == 0 and marker.stat().st_nlink == 1 state = json.loads(marker.read_text()) assert all(state.get(k) == v for k, v in identity.items()) assert state.get("status") in ("initializing", "ready") if state["status"] == "ready": current = root.stat() assert (current.st_uid, current.st_gid) == (uid, gid) continue else: # A leftover unpublished marker can result from interruption before rename. assert all(p.name == ".thothii-initializing.tmp" and p.is_file() and not p.is_symlink() for p in root.iterdir()) save(marker, dict(identity, status="initializing")) # No application can have used an initializing volume: all volumes become ready # before catalog/services are started. Re-copying distributed files is repeatable. if source: shutil.copytree(source, root, dirs_exist_ok=True, symlinks=True) for parent, directories, files in os.walk(root, followlinks=False): for entry in directories + files: if Path(parent) == root and entry in (marker.name, ".thothii-initializing.tmp"): continue os.chown(Path(parent) / entry, uid, gid, follow_symlinks=False) os.chown(root, uid, gid) os.chmod(root, 0o700) save(marker, dict(identity, status="ready"))