ARG RUNTIME_IMAGE=cruciblecoredevacr.azurecr.io/kab/agent-runtime-adk:0.35.7

# Stage 1: Terraform and its providers, verified against HashiCorp's checksums. The
# provider mirror lets fmt, validate and the schema lookup run with no network.
FROM ${RUNTIME_IMAGE} AS terraform
ARG TERRAFORM_VERSION=1.14.3
USER root
COPY terraform/ /build/
RUN python - <<PY
import hashlib, os, stat, urllib.request, zipfile
version = "${TERRAFORM_VERSION}"
base = f"https://releases.hashicorp.com/terraform/{version}/terraform_{version}"
archive, _ = urllib.request.urlretrieve(f"{base}_linux_amd64.zip")
sums = urllib.request.urlopen(f"{base}_SHA256SUMS").read().decode()
expected = next(line.split()[0] for line in sums.splitlines() if line.endswith("linux_amd64.zip"))
assert hashlib.sha256(open(archive, "rb").read()).hexdigest() == expected
os.makedirs("/build/bin", exist_ok=True)
zipfile.ZipFile(archive).extract("terraform", "/build/bin")
os.chmod("/build/bin/terraform", stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
PY
RUN /build/bin/terraform -chdir=/build providers mirror -platform=linux_amd64 /build/mirror

# Stage 2: the agent's packages, installed into /agent/vendor.
FROM ${RUNTIME_IMAGE} AS vendor
USER root
COPY requirements.txt /tmp/requirements.txt
RUN pip install --no-cache-dir --target /vendor -r /tmp/requirements.txt

# Stage 3: the agent.
FROM ${RUNTIME_IMAGE}
COPY --from=vendor /vendor /agent/vendor/
COPY --from=terraform /build/bin/terraform /agent/terraform/bin/terraform
COPY --from=terraform /build/mirror /agent/terraform/mirror/
COPY terraform/terraformrc /agent/terraform/terraformrc
COPY agent.yaml prompt.md /agent/
COPY schemas/ /agent/schemas/
COPY skills/ /agent/skills/
# The sub-agents' instructions, named by agent.yaml's sub_agents, and the roles' own
COPY prompts/ /agent/prompts/
# The agent's own code and each Cloud provider's bundle
COPY app/ /agent/app/
COPY providers/ /agent/providers/
COPY eval/ /agent/eval/
