ARG VERSION

# --------------------------------------------------------------------------------------------------
# Builder Image
# --------------------------------------------------------------------------------------------------
FROM cytopia/ansible:${VERSION}-tools as builder

# Required tools for building Python packages
RUN set -eux \
	&& apk add --no-cache \
		# build tools
		coreutils \
		g++ \
		gcc \
		make \
		musl-dev \
		openssl-dev \
		python3-dev \
		# misc tools
		krb5-dev \
		openldap-dev \
		postgresql-dev \
		postgresql-libs

# Python packages (copied to final image)
RUN set -eux \
	&& pip3 install --no-cache-dir --no-compile \
		PyMySQL \
		docker \
		docker-compose \
		jsondiff \
		netaddr \
		pexpect \
		psycopg2 \
		pyldap \
		pymongo \
		pypsexec \
		pywinrm \
		pywinrm[credssp] \
		pywinrm[kerberos] \
		smbprotocol \
	&& find /usr/lib/ -name '__pycache__' -print0 | xargs -0 -n1 rm -rf \
	&& find /usr/lib/ -name '*.pyc' -print0 | xargs -0 -n1 rm -rf


# --------------------------------------------------------------------------------------------------
# Final Image
# --------------------------------------------------------------------------------------------------
FROM cytopia/ansible:${VERSION}-tools as production
ARG VERSION
# https://github.com/opencontainers/image-spec/blob/master/annotations.md
#LABEL "org.opencontainers.image.created"=""
#LABEL "org.opencontainers.image.version"=""
#LABEL "org.opencontainers.image.revision"=""
LABEL "maintainer"="cytopia <cytopia@everythingcli.org>"
LABEL "org.opencontainers.image.authors"="cytopia <cytopia@everythingcli.org>"
LABEL "org.opencontainers.image.vendor"="cytopia"
LABEL "org.opencontainers.image.licenses"="MIT"
LABEL "org.opencontainers.image.url"="https://github.com/cytopia/docker-ansible"
LABEL "org.opencontainers.image.documentation"="https://github.com/cytopia/docker-ansible"
LABEL "org.opencontainers.image.source"="https://github.com/cytopia/docker-ansible"
LABEL "org.opencontainers.image.ref.name"="Ansible ${VERSION} infra"
LABEL "org.opencontainers.image.title"="Ansible ${VERSION} infra"
LABEL "org.opencontainers.image.description"="Ansible ${VERSION} infra"

# Additional binaries
RUN set -eux \
	&& apk add --no-cache \
		libpq \
		rsync \
		sshpass

COPY --from=builder /usr/lib/python3.10/site-packages/ /usr/lib/python3.10/site-packages/

WORKDIR /data
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["/bin/bash"]
