ARG PYVERSION={{config.python_version}}
FROM {{base_image_name_and_tag}} AS truss_server

ENV PYTHON_EXECUTABLE="{{ config.base_image.python_executable_path or 'python3' }}"

{% block fail_fast %}
RUN grep -w 'ID=debian\|ID_LIKE=debian' /etc/os-release || { echo "ERROR: Supplied base image is not a debian image"; exit 1; }
RUN $PYTHON_EXECUTABLE -c "import sys; sys.exit(0) if sys.version_info.major == {{supported_python_major_version_in_custom_base_image}} and sys.version_info.minor >={{min_supported_python_minor_version_in_custom_base_image}} and sys.version_info.minor <={{max_supported_python_minor_version_in_custom_base_image}} else sys.exit(1)" \
    || { echo "ERROR: Supplied base image does not have {{min_supported_python_version_in_custom_base_image}} <= python <= {{max_supported_python_version_in_custom_base_image}}"; exit 1; }
{% endblock %}

RUN pip install --upgrade pip --no-cache-dir \
    && rm -rf /root/.cache/pip

{% block base_image_patch %}
{% endblock %}

{% if config.model_framework.value == 'huggingface_transformer' %}
    {% if config.resources.use_gpu %}
# HuggingFace pytorch gpu support needs mkl
RUN pip install mkl
    {% endif %}
{% endif%}

{% block post_base %}
{% endblock %}

# NB(nikhil): We need to install additional toolchains to support installing
# python dependencies (currently blake3 and dockerfile) on arm64 architecture.
# Currently, we only support arm64 in local dev, but we need these for:
#   1) Control server (depends on truss)
#   2) Python DX / Chains (depends on truss)
{% block install_arm64_requirements %}
    {%- if should_install_arm64_requirements %}
RUN apt-get update && apt-get install --yes --no-install-recommends golang-go
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"
RUN rustup update stable && rustup default stable
    {%- endif %}
{% endblock %}

{% block install_system_requirements %}
    {%- if should_install_system_requirements %}
COPY ./{{system_packages_filename}} {{system_packages_filename}}
RUN apt-get update && apt-get install --yes --no-install-recommends $(cat {{system_packages_filename}}) \
    && apt-get autoremove -y \
    && apt-get clean -y \
    && rm -rf /var/lib/apt/lists/*
    {%- endif %}
{% endblock %}


{% block install_requirements %}
    {%- if should_install_user_requirements_file %}
COPY ./{{user_supplied_requirements_filename}} {{user_supplied_requirements_filename}}
RUN pip install -r {{user_supplied_requirements_filename}} --no-cache-dir && rm -rf /root/.cache/pip
    {%- endif %}
    {%- if should_install_requirements %}
COPY ./{{config_requirements_filename}} {{config_requirements_filename}}
RUN pip install -r {{config_requirements_filename}} --no-cache-dir && rm -rf /root/.cache/pip
    {%- endif %}
{% endblock %}



{%- if not config.docker_server %}
ENV APP_HOME="/app"
WORKDIR $APP_HOME
{%- endif %}

{% block app_copy %}
{% endblock %}


{% block bundled_packages_copy %}
    {%- if bundled_packages_dir_exists %}
COPY ./{{config.bundled_packages_dir}} /packages
    {%- endif %}
{% endblock %}


{% for env_var_name, env_var_value in config.environment_variables.items() %}
ENV {{ env_var_name }}="{{ env_var_value }}"
{% endfor %}

{% block run %}
{% endblock %}
