# OPTIONAL
# This is for CPU-only builds, in order to have a smaller image (only reason)
FROM ubuntu:22.04

WORKDIR /var/folders/kubify/
ENV DEBIAN_FRONTEND=noninteractive

RUN apt update
# for good security
RUN apt install -y  --no-install-recommends python3 software-properties-common
RUN apt install -y  --no-install-recommends sudo zsh bash git ansible awscli make wget apt-utils
RUN apt install -y  --no-install-recommends python3-alembic python3-flask-migrate
# Tox Multi-Py tests:
# RUN add-apt-repository ppa:deadsnakes/ppa && apt update
# RUN apt install -y  --no-install-recommends python3.7 python3.8 python3.9 python3.10
# APT Deps can go here:
RUN apt install -y  --no-install-recommends python3-alembic python3-flask-migrate curl
RUN wget -qO- https://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/8.5.13/flyway-commandline-8.5.13-linux-x64.tar.gz | tar xvz && sudo ln -sf `pwd`/flyway-8.5.13/flyway /usr/local/bin/

# Install EKS Control
RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"

# For full install (but CICD env sets KUBIFY_CI=1 at runtime to override this default):
ENV KUBIFY_CI 0
ENV KUBIFY_VERBOSE 0

# Copying the automation magic here (for building a trusted hardened container):
RUN mkdir -p /etc/ansible
RUN git config --global user.name "${GIT_FIRST_LAST_NAME}"
RUN git config --global user.email "${GIT_EMAIL}"

# Install Kindest Kind (local Kubernetes cluster)
RUN curl -Lo ./kind "https://kind.sigs.k8s.io/dl/v0.14.0/kind-$(uname)-amd64" && \
        chmod +x ./kind && \
            mv ./kind /usr/local/bin/kind

# Your services folder gets mounted automatically (for Kubify's rapid testing engine):
COPY ./ansible.cfg /etc/ansible/
COPY ./kubify/ops/ansible /tmp/ansible
RUN rm -rf /var/folders/kubify/.git /var/folders/kubify/
RUN ansible-playbook --connection=local "/tmp/ansible/install_kubify_on_debian_ubuntu_and_wsl2.yaml" --ask-become-pass -e ansible_python_interpreter=`which python3`
COPY setup.py .
COPY *.md .
COPY *.rst .
RUN apt install -y  --no-install-recommends libxml2 libxml2-dev libxslt-dev
RUN pip install Cython

# Dev
RUN pip install -e .[develop]

# Terraform:
# RUN git clone https://github.com/riywo/anyenv ~/.anyenv
# RUN echo 'export PATH="$HOME/.anyenv/bin:$PATH"' >> /etc/profile.d/anyenv.sh
# RUN echo 'eval "$(anyenv init -)"' >> /etc/profile.d/anyenv.sh
RUN git clone https://github.com/tfutils/tfenv ~/tools/tfenv
RUN ln -sf ~/tools/tfenv/bin/* /usr/local/bin
RUN apt install -y  --no-install-recommends unzip zip tar gzip
RUN tfenv install 1.2.4
RUN tfenv use 1.2.4
ADD https://github.com/gruntwork-io/terragrunt/releases/download/v0.38.3/terragrunt_linux_amd64 /usr/local/bin/terragrunt-amd64
ADD https://github.com/gruntwork-io/terragrunt/releases/download/v0.38.3/terragrunt_linux_arm64 /usr/local/bin/terragrunt-arm64
RUN chmod +x /usr/local/bin/terragrunt-*
RUN terragrunt-arm64 --version && ln -sf /usr/local/bin/terragrunt-arm64 /usr/local/bin/terragrunt
RUN terragrunt-amd64 --version && unlink /usr/local/bin/terragrunt
RUN terragrunt-amd64 --version && ln -sf /usr/local/bin/terragrunt-amd64 /usr/local/bin/terragrunt
RUN chmod +x /usr/local/bin/terragrunt

# Code:
COPY . .
RUN make fix
RUN pip install -e .[tests]
RUN pip install -e .[extras]

# Lint
RUN make lint

# Tox (test all python versions enabled)
# RUN make pythons # TODO: fix the 1 new error and then uncomment again

# Build Package (and Install Dependencies)
RUN make pip

# Security Checks (Bandit):
RUN make security

# Coverage:
RUN make coverage

# Tests (PyTest):
RUN make test

# Test Generating (the Help Docs):
RUN make help

# Package (Test Create Install Package):
RUN apt install -y  --no-install-recommends python3
RUN make package

# Release (Test Install Release):
RUN pip install -e .

# Clean (Smalller Image):
RUN make clean
RUN rm -rf /tmp/* /var/tmp/* ./.tox
RUN rm -rf ./kubify/ops/services/*/*/secr* ./.git
RUN rm -rf /var/lib/apt/lists/*
RUN apt remove -y python3.7 python3.8 python3.10 software-properties-common
RUN apt-get clean autoclean
RUN apt-get autoremove --yes
RUN rm -rf /var/lib/{apt,dpkg,cache,log}/
RUN apt-get update && \
    apt-get -y --no-install-recommends install curl \
        ca-certificates && \
    curl https://raw.githubusercontent.com/gadiener/docker-images-size-benchmark/master/main.go -o main.go && \
    apt-get purge -y curl \
        ca-certificates && \
    apt-get autoremove -y && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*
RUN rm -rf ./docs ./dist
RUN rm -rf /usr/lib/python2* /usr/lib/python3.7* /usr/lib/python3.8* /usr/lib/python3.10*

# Default is to run Tests:
CMD make test