#
# 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.
#

# Usage:
#   1. use ./build/dist to make binary distributions of Proton or download a release
#   2. Untar it and run the docker command below
#      docker build -f docker/Dockerfile -t repository/proton:tagname .
#   Options:
#     -f this docker file
#     -t the target repo and tag name
#     more options can be found with -h

ARG base_image=eclipse-temurin:8-jdk-focal
ARG hadoop_provided="hadoop_builtin"

FROM ${base_image} as builder_hadoop_provided
ONBUILD ARG hadoop_home_in_docker
ONBUILD ENV HADOOP_HOME ${hadoop_home_in_docker}

FROM ${base_image} as builder_hadoop_builtin
ONBUILD ENV HADOOP_HOME /opt/hadoop
ONBUILD RUN mkdir -p ${HADOOP_HOME}
ONBUILD COPY hadoop-binary ${HADOOP_HOME}

FROM builder_${hadoop_provided}

ARG proton_uid=10100
ARG hadoop_major_version=3
ARG include_prometheus_javaagent=true
USER root

ENV PROTON_HOME /opt/proton
ENV PROTON_LOG_DIR ${PROTON_HOME}/logs
ENV PROTON_PID_DIR ${PROTON_HOME}/pid
ENV PROTON_CONF_DIR ${PROTON_HOME}/conf

RUN set -ex && \
    sed -i 's/http:\/\/deb.\(.*\)/https:\/\/deb.\1/g' /etc/apt/sources.list && \
    apt-get update && \
    apt-get install -y bash tini libc6 libpam-modules krb5-user libnss3 procps iputils-ping vim && \
    ln -snf /bin/bash /bin/sh && \
    useradd -u ${proton_uid} -g root proton -d /home/proton -m && \
    mkdir -p ${PROTON_HOME} ${PROTON_LOG_DIR} ${PROTON_PID_DIR} && \
    rm -rf /var/cache/apt/*

COPY LICENSE README.md ${PROTON_HOME}/
COPY bin ${PROTON_HOME}/bin
COPY conf ${PROTON_HOME}/conf
COPY lib ${PROTON_HOME}/lib
COPY plugins ${PROTON_HOME}/plugins

RUN cp ${PROTON_HOME}/plugins/hadoop${hadoop_major_version}/proton-hadoop${hadoop_major_version}-bundle-*.jar ${HADOOP_HOME}/share/hadoop/hdfs/

RUN set -ex && \
    if [ ${include_prometheus_javaagent} = "true" ]; then \
        mkdir ${PROTON_HOME}/prometheus/ && \
        curl https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.17.2/jmx_prometheus_javaagent-0.17.2.jar \
          -o ${PROTON_HOME}/prometheus/jmx_prometheus_javaagent-0.17.2.jar && \
        touch ${PROTON_HOME}/prometheus/proton.yaml; \
    fi

RUN set -ex && \
    ln -snf ${PROTON_HOME}/bin/proton /usr/bin/proton && \
    ln -snf ${HADOOP_HOME}/bin/hadoop /usr/bin/hadoop && \
    ln -snf ${HADOOP_HOME}/bin/hdfs /usr/bin/hdfs

WORKDIR ${PROTON_HOME}
RUN chown -R proton:root ${PROTON_HOME} && \
    chmod ug+rw -R ${PROTON_HOME}

USER ${proton_uid}
