FROM registry.access.redhat.com/ubi9/ubi:latest AS builder

RUN dnf update -y && \
    dnf install -y ansible git && \
    dnf clean all

RUN curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash

RUN ansible-galaxy collection install kubernetes.core community.general

RUN ansible-galaxy collection install \
    kubernetes.core.helm \
    community.general.terraform \
    kubernetes.core.k8s

COPY ./ansible /workspace/ansible
COPY ./helm /workspace/helm
COPY ./terraform /workspace/terraform

# Stage 2: Final image
FROM registry.access.redhat.com/ubi9/ubi-minimal:latest

# Copy necessary files and dependencies from the builder stage
COPY --from=builder /usr/local/bin/helm /usr/local/bin/helm
COPY --from=builder /usr/local/bin/ansible* /usr/local/bin/
COPY --from=builder /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages
COPY --from=builder /workspace /workspace

# Create working directory
WORKDIR /workspace

# Set up environment variable for API token
ARG API_TOKEN
ENV API_TOKEN=${API_TOKEN}

# Mount a volume to output logs
VOLUME ["/output_logs"]

# Command to run Ansible playbook and output logs to mounted directory
CMD ["bash", "-c", "ansible-playbook -i /workspace/ansible/inventory /workspace/ansible/playbook.yml --extra-vars \"api_token=${API_TOKEN}\" | tee /output_logs/playbook_output.log"]