MrTechie's picture
Deploy backend 116dcd5
f1664b4 verified
Raw History Blame Contribute Delete
3.31 kB
"""Get the engines into the image from the commit that engines.lock pins, never from a working tree.
On the host, `export` writes two tarballs with `git archive`, which reads committed objects only,
so uncommitted edits, client files, virtual environments and caches in the checkout cannot leak
in. In the image, `install` checks that each tarball was archived from the pinned commit and
unpacks it.
uv run python docker/engines.py export
python engines.py install engines.tar --pin engines.lock --into /opt/engines --lock out.lock
"""
import argparse
import shutil
import subprocess
import sys
import tarfile
import tomllib
from pathlib import Path
HERE = Path(__file__).resolve().parent
RUNTIME = (
"Depreciation Calculator/WDV_Python_App/asset_master.json",
"Depreciation Calculator/WDV_Python_App/wdv_core.py",
"Sales Reco Automation v6.0/Toolkit Engine/src",
)
# The engines' own synthetic samples and builders, for the test image only.
FIXTURES = (
"Bank Statement Extractor/qa_bank_extractor",
"Sales Reco Automation v6.0/Toolkit Engine/tests",
)
TARBALLS = {"engines.tar": RUNTIME, "engine-fixtures.tar": FIXTURES}
def read_pin(lock_file: Path) -> tuple[Path, str]:
engine = tomllib.loads(lock_file.read_text(encoding="utf-8"))["engine"]
return Path(engine["path"]), engine["commit"]
def export(lock_file: Path, out_dir: Path) -> None:
checkout, commit = read_pin(lock_file)
git = shutil.which("git")
if git is None:
sys.exit("git is not on PATH")
for name, paths in TARBALLS.items():
archive = [git, "-C", str(checkout), "archive", "--format=tar", "-o", str(out_dir / name)]
subprocess.run( # noqa: S603 git with fixed arguments and the pinned commit.
[*archive, f"{commit}^{{commit}}", "--", *paths], check=True
)
print(f"{out_dir / name} from {checkout} at {commit}")
def install(tarball: Path, lock_file: Path, into: Path, lock_out: Path | None) -> None:
_, commit = read_pin(lock_file)
with tarfile.open(tarball) as archive:
archived = archive.pax_headers.get("comment", "")
if not archived.startswith(commit):
sys.exit(f"{tarball.name} comes from commit {archived or 'unknown'}, not {commit}")
archive.extractall(into, filter="data")
if lock_out is not None:
lines = ("[engine]", f"path = '{into.as_posix()}'", f'commit = "{commit}"')
lock_out.write_text("\n".join((*lines, f'archived = "{archived}"', "")), encoding="utf-8")
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
commands = parser.add_subparsers(dest="command", required=True)
out = commands.add_parser("export")
out.add_argument("--pin", type=Path, default=HERE.parent / "engines.lock")
out.add_argument("--out", type=Path, default=HERE)
put = commands.add_parser("install")
put.add_argument("tarball", type=Path)
put.add_argument("--pin", type=Path, required=True)
put.add_argument("--into", type=Path, required=True)
put.add_argument("--lock", type=Path)
args = parser.parse_args()
if args.command == "export":
export(args.pin, args.out)
else:
install(args.tarball, args.pin, args.into, args.lock)
if __name__ == "__main__":
main()