FROM {{ namespace }}/{{ image_prefix }}openstack-base:{{ tag }}
MAINTAINER {{ maintainer }}

{% import "macros.j2" as macros with context %}

{% if install_type == 'binary' %}
    {% if base_distro in ['centos', 'fedora', 'oraclelinux', 'rhel'] %}

        {% set horizon_packages = [
                'openstack-dashboard',
                'httpd',
                'mod_wsgi',
                'gettext'
        ] %}

{{ macros.install_packages(horizon_packages | customizable("packages")) }}

{% block horizon_redhat_binary_setup %}
RUN useradd --user-group horizon \
    && sed -i -r 's,^(Listen 80),#\1,' /etc/httpd/conf/httpd.conf \
    && ln -s /usr/share/openstack-dashboard/openstack_dashboard /usr/lib/python2.7/site-packages/openstack_dashboard \
    && ln -s /usr/share/openstack-dashboard/static /usr/lib/python2.7/site-packages/static \
    && chown -R horizon: /etc/openstack-dashboard /usr/share/openstack-dashboard \
    && chown -R apache: /usr/share/openstack-dashboard/static \
    && sed -i "s|WEBROOT = '/dashboard/'|WEBROOT = '/'|" /etc/openstack-dashboard/local_settings \
    && cp /usr/share/openstack-dashboard/manage.py /usr/bin/manage.py \
    && /usr/bin/python /usr/bin/manage.py collectstatic --noinput --clear \
    && (cd /usr/lib/python2.7/site-packages/horizon && /usr/bin/python /usr/bin/manage.py compilemessages) \
    && (cd /usr/lib/python2.7/site-packages/openstack_dashboard && /usr/bin/python /usr/bin/manage.py compilemessages)
{% endblock %}

    {% elif base_distro in ['ubuntu'] %}

        {% set horizon_packages = [
                'openstack-dashboard',
                'apache2',
                'libapache2-mod-wsgi',
                'gettext'
        ] %}

{{ macros.install_packages(horizon_packages | customizable("packages")) }}

{% block horizon_ubuntu_binary_setup %}
RUN echo > /etc/apache2/ports.conf \
    && sed -i 's|\(ServerTokens \)OS|\1Prod|' /etc/apache2/conf-available/security.conf \
    && ln -s ../mods-available/headers.load  /etc/apache2/mods-enabled/headers.load \
    && ln -s ../mods-available/expires.load  /etc/apache2/mods-enabled/expires.load \
    && cp /usr/share/openstack-dashboard/openstack_dashboard/conf/*.json /etc/openstack-dashboard \
    && sed -i "s|WEBROOT='/horizon/'|WEBROOT = '/'|" /etc/openstack-dashboard/local_settings.py \
    && sed -i "s|^POLICY_FILES_PATH|POLICY_FILES_PATH = '/etc/openstack-dashboard'|" /etc/openstack-dashboard/local_settings.py \
    && cp /usr/share/openstack-dashboard/manage.py /usr/bin/manage.py \
    && /usr/bin/python /usr/bin/manage.py collectstatic --noinput \
    && rm /etc/apache2/conf-enabled/openstack-dashboard.conf \
    && rm /etc/openstack-dashboard/local_settings.py \
    && rm /usr/share/openstack-dashboard/openstack_dashboard/local/local_settings.py \
    && ln -s /etc/openstack-dashboard/local_settings /usr/share/openstack-dashboard/openstack_dashboard/local/local_settings.py \
    && ln -s /usr/share/openstack-dashboard/openstack_dashboard /usr/lib/python2.7/site-packages/openstack_dashboard \
    && ln -s /usr/share/openstack-dashboard/static /usr/lib/python2.7/site-packages/static \
    && (cd /usr/lib/python2.7/site-packages/horizon && /usr/bin/python /usr/bin/manage.py compilemessages) \
    && (cd /usr/lib/python2.7/site-packages/openstack_dashboard && /usr/bin/python /usr/bin/manage.py compilemessages)
{% endblock %}

    {% endif %}
{% elif install_type == 'source' %}
    {% if base_distro in ['centos', 'fedora', 'oraclelinux', 'rhel'] %}

        {% set horizon_packages = [
                'httpd',
                'mod_wsgi',
                'gettext'
        ] %}

{{ macros.install_packages(horizon_packages | customizable("packages")) }}

{% block horizon_redhat_source_setup %}
RUN sed -i -r 's,^(Listen 80),#\1,' /etc/httpd/conf/httpd.conf
{% endblock %}

    {% elif base_distro in ['ubuntu', 'debian'] %}

        {% set horizon_packages = [
                'apache2',
                'libapache2-mod-wsgi',
                'gettext'
        ] %}

{{ macros.install_packages(horizon_packages | customizable("packages")) }}

{% block horizon_ubuntu_source_setup %}
RUN echo > /etc/apache2/ports.conf \
    && ln -s ../mods-available/headers.load  /etc/apache2/mods-enabled/headers.load
{% endblock %}

    {% endif %}

ADD horizon-archive /horizon-source
ADD plugins-archive /
RUN ln -s horizon-source/* horizon \
    && /var/lib/kolla/venv/bin/pip --no-cache-dir install --upgrade -c requirements/upper-constraints.txt /horizon \
    && useradd --user-group horizon \
    && mkdir -p /etc/openstack-dashboard /home/horizon \
    && ln -s /etc/openstack-dashboard/local_settings /var/lib/kolla/venv/lib/python2.7/site-packages/openstack_dashboard/local/local_settings.py \
    && cp -r /horizon/openstack_dashboard/conf/* /etc/openstack-dashboard/ \
    && cp /horizon/openstack_dashboard/local/local_settings.py.example /etc/openstack-dashboard/local_settings \
    && cp /horizon/manage.py /var/lib/kolla/venv/bin/manage.py \
    && if [ "$(ls /plugins)" ]; then \
           pip --no-cache-dir install --upgrade -c requirements/upper-constraints.txt /plugins/*; \
           ln -s `find /plugins -name 'neutron-lbaas-dashboard-*' -type d`/neutron_lbaas_dashboard/enabled/_1481_project_ng_loadbalancersv2_panel.py \
                 /var/lib/kolla/venv/lib/python2.7/site-packages/openstack_dashboard/enabled/_1481_project_ng_loadbalancersv2_panel.py; \
       fi \
    && /var/lib/kolla/venv/bin/python /var/lib/kolla/venv/bin/manage.py collectstatic --noinput --clear \
    && (cd /var/lib/kolla/venv/lib/python2.7/site-packages/horizon && /var/lib/kolla/venv/bin/python /var/lib/kolla/venv/bin/manage.py compilemessages) \
    && (cd /var/lib/kolla/venv/lib/python2.7/site-packages/openstack_dashboard && /var/lib/kolla/venv/bin/python /var/lib/kolla/venv/bin/manage.py compilemessages) \
    && chown -R horizon: /etc/openstack-dashboard /home/horizon /var/lib/kolla/venv/lib/python2.7/site-packages/static

{% endif %}

COPY extend_start.sh /usr/local/bin/kolla_extend_start
RUN chmod 755 /usr/local/bin/kolla_extend_start

{% block horizon_footer %}{% endblock %}
{% block footer %}{% endblock %}
{{ include_footer }}