# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

############################################################
# Dockerfile to build Traffic Ops container images
# Based on CentOS 8
############################################################

# Keep the trafficops-common-deps in Dockerfile-go the same
# as trafficops-common-deps in Dockerfile to cache the same
# layer.
ARG RHEL_VERSION=8
FROM centos:${RHEL_VERSION} as trafficops-common-deps
ARG RHEL_VERSION=8

RUN if [[ "${RHEL_VERSION%%.*}" -eq 7 ]]; then \
        yum -y install dnf || exit 1; \
    fi

RUN set -o nounset -o errexit && \
    mkdir -p /etc/cron.d; \
    if [[ "${RHEL_VERSION%%.*}" -eq 7 ]]; then \
	    include_repo=''; \
    else \
	    include_repo='--repo=pgdg96'; \
    fi; \
    dnf -y install "https://download.postgresql.org/pub/repos/yum/reporpms/EL-${RHEL_VERSION%%.*}-x86_64/pgdg-redhat-repo-latest.noarch.rpm"; \
    dnf -y $include_repo -- install postgresql96; \
    dnf -y install epel-release; \
    dnf -y install      \
        jq              \
        bind-utils      \
        net-tools       \
        gettext         \
        perl-JSON-PP    \
        mkisofs         \
        isomd5sum       \
        nmap-ncat       \
        openssl;        \
    dnf clean all

FROM    trafficops-common-deps

# Override TRAFFIC_OPS_RPM arg to use a different one using --build-arg TRAFFIC_OPS_RPM=...  Can be local file or http://...
#
ARG TRAFFIC_OPS_RPM=traffic_ops/traffic_ops.rpm

COPY $TRAFFIC_OPS_RPM /
RUN to_rpm_filename="$(basename $TRAFFIC_OPS_RPM)" && \
    rpm -Uvh --nodeps "$to_rpm_filename" && \
    rm "$to_rpm_filename"

COPY enroller/server_template.json \
     traffic_ops/config.sh \
     traffic_ops/run-go.sh \
     traffic_ops/to-access.sh \
     /

COPY dns/set-dns.sh \
     dns/insert-self-into-dns.sh \
     /usr/local/sbin/

WORKDIR /opt/traffic_ops/app
EXPOSE  443
CMD     /run-go.sh
HEALTHCHECK --interval=10s --timeout=1s \
    CMD bash -c 'source /to-access.sh && [[ "$(curl -sk "https://${TO_FQDN}/api/${TO_API_VERSION}/ping" | jq .ping)" == \"pong\" ]]'
