{% raw %}
{% extends parent_template %}

{% block base_pip_conf %}
{% endraw %}

ENV PIP_INDEX_URL {{ nodepool_pypi_mirror }}
ENV PIP_TRUSTED_HOST {{ nodepool_mirror_host }}
{% if use_infra_wheels_mirror | default(true) %}
ENV PIP_EXTRA_INDEX_URL {{ nodepool_wheel_mirror }}
{% endif %}

RUN echo registry={{ nodepool_npmjs_proxy }} > /etc/npmrc \
    && mkdir -p /usr/etc \
    && ln -s /etc/npmrc /usr/etc/npmrc

{% raw %}
{% endblock %}

{% block base_centos_repo_overrides_post_copy %}
{% endraw %}

RUN sed -i \
        -e "s|^\(mirrorlist.*\)|#\1|" \
        -e "s|^#baseurl=http://mirror.centos.org|baseurl=http://{{ nodepool_mirror_host }}|" \
        /etc/yum.repos.d/CentOS-*.repo

{% raw %}
{% endblock %}

{% block base_centos_repo_overrides_post_rpm %}
{% endraw %}

{% raw %}
{% endblock %}

{% block base_centos_repo_overrides_post_yum -%}
{%- endraw -%}
    && sed -i \
        -e "s|^\(mirrorlist.*\)|#\1|" \
        -e "s|^#baseurl=http://mirror.centos.org|baseurl=http://{{ nodepool_mirror_host }}|" \
        -e "s|^#baseurl=https://mirror.centos.org|baseurl=http://{{ nodepool_mirror_host }}|" \
        -e "s|^baseurl=http://mirror.centos.org|baseurl=http://{{ nodepool_mirror_host }}|" \
        -e "s|^baseurl=https://mirror.centos.org|baseurl=http://{{ nodepool_mirror_host }}|" \
        /etc/yum.repos.d/CentOS-*.repo \
    && sed -i \
        -e "s|^\(metalink.*\)|#\1|" \
        -e "s|^#baseurl=http://download.example/pub|baseurl=http://{{ nodepool_mirror_host }}|" \
        -e "s|^#baseurl=https://download.example/pub|baseurl=http://{{ nodepool_mirror_host }}|" \
        /etc/yum.repos.d/epel*.repo
{%- raw -%}
{% endblock %}

{% block base_debian_after_sources_list %}
{% if base_distro == "debian" %}
{% endraw %}

RUN sed -i -e "s|http://deb.debian.org|[trusted=yes] http://{{ nodepool_mirror_host }}|" \
        -e "s|http://security.debian.org|[trusted=yes] http://{{ nodepool_mirror_host }}|" \
        /etc/apt/sources.list

{% raw %}
{% elif base_distro == "ubuntu" %}
{% endraw %}

RUN sed -i -e "s|mirror://mirrors.ubuntu.com/mirrors.txt|[trusted=yes] http://{{ nodepool_mirror_host }}/ubuntu/|" \
        -e "s|http://ubuntu-cloud.archive.canonical.com/ubuntu|[trusted=yes] http://{{ nodepool_mirror_host }}/ubuntu-cloud-archive|" \
        -e "s|http://ports.ubuntu.com|[trusted=yes] http://{{ nodepool_mirror_host }}/ubuntu-ports|" \
        /etc/apt/sources.list

{% raw %}
{% endif %}
{% endblock %}

{# Revert to upstream mirrors after build is complete #}

{% block footer %}
{% endraw %}

ENV PIP_INDEX_URL=
ENV PIP_TRUSTED_HOST=
{% if use_infra_wheels_mirror | default(true) %}
ENV PIP_EXTRA_INDEX_URL=
{% endif %}

RUN if [ -f /usr/etc/npmrc ]; then \
        unlink /usr/etc/npmrc; \
    fi \
    && rm -f /etc/npmrc

{% raw %}
{% if base_package_type == 'rpm' %}
{% endraw %}
RUN sed -i \
        -e "s|^#\(mirrorlist.*\)|\1|" \
        -e "s|^baseurl=http://{{ nodepool_mirror_host }}|#baseurl=http://mirror.centos.org|" \
        /etc/yum.repos.d/CentOS-*.repo \
    && sed -i \
        -e "s|^#\(metalink.*\)|\1|" \
        -e "s|^baseurl=http://{{ nodepool_mirror_host }}|#baseurl=http://download.example/pub|" \
        /etc/yum.repos.d/epel*.repo
{% raw %}
{% elif base_package_type == 'deb' %}
{% if base_distro == "debian" %}
{% endraw %}
RUN sed -i -e "s|\[trusted=yes\] http://{{ nodepool_mirror_host }}|http://deb.debian.org|" \
        -e "s|\[trusted=yes\] http://{{ nodepool_mirror_host }}|http://security.debian.org|" \
        /etc/apt/sources.list
{% raw %}
{% elif base_distro == "ubuntu" %}
{% endraw %}
RUN sed -i -e "s|\[trusted=yes\] http://{{ nodepool_mirror_host }}/ubuntu/|mirror://mirrors.ubuntu.com/mirrors.txt|" \
        -e "s|\[trusted=yes\] http://{{ nodepool_mirror_host }}/ubuntu-cloud-archive|http://ubuntu-cloud.archive.canonical.com/ubuntu|" \
        -e "s|\[trusted=yes\] http://{{ nodepool_mirror_host }}/ubuntu-ports|http://ports.ubuntu.com|" \
        /etc/apt/sources.list
{% raw %}
{% endif %}
{% endif %}
{% endblock %}
{% endraw %}
