# Dockerfile
# @author Craig McDaniel
#
# Set up an image that we can run Ansible inside of it for building and deploying.
#

FROM debian:trixie-slim

# This makes sure any apt config scripts never ask us anything
ENV DEBIAN_FRONTEND=noninteractive

# Set timezone to UTC
ENV TZ=UTC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# busnet group/user
RUN groupadd -g 1001 busnet \
    && useradd -u 1001 -g 1001 -d /opt/busnet -s /bin/bash busnet \
    && mkdir /opt/busnet \
    && chown -R busnet:busnet /opt/busnet


# This sounds scarier than it is. It allows us to install Python libraries with pip directly.
# It makes more sense when you install this on your main OS, but I want full control over this
# container image. -CM
ENV PIP_BREAK_SYSTEM_PACKAGES 1

# Install packages that we need
RUN apt-get update
#RUN apt-get -y install software-properties-common apt-transport-https apt-utils ca-certificates sshpass
RUN apt-get -y install ca-certificates sshpass gpg python3-minimal python3-pip python3-docker git \
                       rsync curl less netcat-openbsd
RUN pip3 install requests botocore boto3

# Use pip to get the latest version of Ansible and libs
RUN pip3 install ansible cryptography

# Install some Ansible modules.
RUN ansible-galaxy collection install community.docker

# Install just the docker CLI, not anything else. We use the CLI to connect to the host OS docker daemon.
#RUN curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg \
#    && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null \
#    && apt-get update \
#    && apt-get install -y docker-ce-cli \
#    && pip install docker

# Install just the docker CLI, not anything else. We use the CLI to connect to the host OS docker daemon.
RUN . /etc/os-release \
    && curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg \
    && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian ${VERSION_CODENAME} stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null \
    && apt-get update \
    && apt-get install -y docker-ce-cli \
    && pip install docker

# Clean up packages that we don't need after installing things
#RUN apt-get -y remove software-properties-common apt-transport-https apt-utils build-essential python3-pip \
#    && apt-get -y autoremove \
#    && apt-get -y clean

# Remove docs and man pages
RUN rm -rf /usr/share/doc \
    && rm -rf /usr/share/man \
    && rm -rf /usr/share/locale

# Ansible environment variables
# Note: It is assumed the container mount point "/opt/busnet/git/" will be provided at runtime via a bind mount.
ENV ANSIBLE_ROLES_PATH=/opt/busnet/git/devops/ansible/roles
ENV ANSIBLE_INVENTORY=/opt/busnet/git/devops/ansible/inventory

WORKDIR /opt/busnet/git/devops/ansible