{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
FROM docker-config-engine-stretch

ARG docker_container_name
RUN [ -f /etc/rsyslog.conf ] && sed -ri "s/%syslogtag%/$docker_container_name#%syslogtag%/;" /etc/rsyslog.conf

# Enable -O for all Python calls
ENV PYTHONOPTIMIZE 1

# Make apt-get non-interactive
ENV DEBIAN_FRONTEND=noninteractive

# Update apt's cache of available packages
# Install curl so we can download and install pip later
# Also install major root CA certificates for curl to reference
# Install gcc which is required for installing hiredis
# Install libdpkg-perl which is required for python3.6-3.6.0 as one of its specs i.e. no-pie-compile.specs
# The file referenced (`/usr/share/dpkg/no-pie-compile.specs`) is in the `libdpkg-perl` package on Debian
RUN apt-get update   && \
    apt-get install -y  \
        curl            \
        ca-certificates \
        gcc             \
        make            \
        libdpkg-perl    \
        ipmitool

{% if docker_snmp_sv2_debs.strip() -%}
# Copy locally-built Debian package dependencies
{{ copy_files("debs/", docker_snmp_sv2_debs.split(' '), "/debs/") }}

# Install locally-built Debian packages and implicitly install their dependencies
{{ install_debian_packages(docker_snmp_sv2_debs.split(' ')) }}
{%- endif %}

# Fix for hiredis compilation issues for ARM
# python will throw for missing locale 
RUN apt-get install -y locales
RUN locale-gen "en_US.UTF-8"
RUN dpkg-reconfigure --frontend noninteractive locales
ENV LC_CTYPE=en_US.UTF-8
RUN sed -i '/^#.* en_US.* /s/^#//' /etc/locale.gen
RUN locale-gen

# Install up-to-date version of pip
RUN curl https://bootstrap.pypa.io/get-pip.py | python3.6

# Install pyyaml dependency for use by some plugins
# Install smbus dependency for use by some plugins
RUN python3.6 -m pip install --no-cache-dir \
        hiredis                             \
        pyyaml                              \
        smbus

{% if docker_snmp_sv2_whls.strip() -%}
# Copy locally-built Python wheel dependencies
{{ copy_files("python-wheels/", docker_snmp_sv2_whls.split(' '), "/python-wheels/") }}

# Install locally-built Python wheel dependencies
{{ install_python_wheels(docker_snmp_sv2_whls.split(' ')) }}
{% endif %}

RUN python3.6 -m sonic_ax_impl install

# Clean up
RUN apt-get -y purge     \
        libpython3.6-dev \
        libpython3.6     \
        curl             \
        gcc              \
        make             \
        libdpkg-perl     \
        # Note: these packages should be removed with autoremove but actually not, so explicitly purged
        libldap-2.4-2    \
        libsasl2-2       \
        libsasl2-modules \
        libsasl2-modules-db && \
    apt-get clean -y        && \
    apt-get autoclean -y    && \
    apt-get autoremove -y --purge && \
    find / | grep -E "__pycache__" | xargs rm -rf && \
    rm -rf /debs /python-wheels ~/.cache

COPY ["start.sh", "/usr/bin/"]
COPY ["supervisord.conf", "/etc/supervisor/conf.d/"]
COPY ["*.j2", "/usr/share/sonic/templates/"]
COPY ["files/supervisor-proc-exit-listener", "/usr/bin"]
COPY ["critical_processes", "/etc/supervisor"]

# Although exposing ports is not needed for host net mode, keep it for possible bridge mode
EXPOSE 161/udp 162/udp

ENTRYPOINT ["/usr/bin/supervisord"]