<reponame>astrolabsoftware/fink-docker
# Copyright 2022 AstroLab Software
# Author: <NAME>
#
# Licensed 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.
ARG PYTHON_VERSION=py37_4.11.0
ARG SPARK_VERSION=3.1.3
ARG HBASE_VERSION=2.2.7
ARG KAFKA_VERSION=2.1.0
ARG HADOOP_VERSION=hadoop2.7
ARG ASTRONETTOKEN

# Stage 1
FROM centos:centos7 AS builder
LABEL maintainer="<EMAIL>"

ARG ASTRONETTOKEN
ENV ASTRONETTOKEN=$ASTRONETTOKEN

ARG PYTHON_VERSION
ENV PYTHON_VERSION=$PYTHON_VERSION

ARG SPARK_VERSION
ENV SPARK_VERSION=$SPARK_VERSION

ARG HBASE_VERSION
ENV HBASE_VERSION=$HBASE_VERSION

ARG KAFKA_VERSION
ENV KAFKA_VERSION=$KAFKA_VERSION

ARG HADOOP_VERSION
ENV HADOOP_VERSION=$HADOOP_VERSION

ENV USRLIBS /home/libs

ENV PATH=${USRLIBS}/miniconda/bin:/usr/local/bin:${PATH}

WORKDIR $USRLIBS
COPY . .

# # Install system build dependencies
RUN yum -y update \
&& yum -y install lsof which git wget \
&& yum -y groupinstall "Development Tools" \
&& yum -y clean all \
&& rm -rf /var/cache \
&& echo "export JAVA_HOME=$(dirname $(dirname $(readlink -f $(type -P java))))" > /etc/profile.d/javahome.sh

# install python
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-${PYTHON_VERSION}-Linux-x86_64.sh -O ~/miniconda.sh \
&& bash ~/miniconda.sh -b -p ${USRLIBS}/miniconda

# Install Apache Kafka, HBase and Spark. Install Python deps.
RUN source scripts/install_kafka.sh --version ${KAFKA_VERSION} \
&& source scripts/install_hbase.sh --version ${HBASE_VERSION} \
&& source scripts/install_spark.sh --spark-version ${SPARK_VERSION} --hadoop-version ${HADOOP_VERSION} \
&& cd $USRLIBS/centos7 \
&& pip install --no-cache-dir --upgrade pip setuptools wheel \
&& source ./install_python_deps.sh --astronet-token $ASTRONETTOKEN \
&& cd $USRLIBS \
&& find miniconda \( -type d -a -name test -o -name tests \) -o \( -type f -a -name '*.pyc' -o -name '*.pyo' \) -exec rm -rf '{}' \+

# Stage 2
FROM centos:centos7

ARG PYTHON_VERSION
ENV PYTHON_VERSION=$PYTHON_VERSION

ARG SPARK_VERSION
ENV SPARK_VERSION=$SPARK_VERSION

ARG HBASE_VERSION
ENV HBASE_VERSION=$HBASE_VERSION

ARG KAFKA_VERSION
ENV KAFKA_VERSION=$KAFKA_VERSION

ARG HADOOP_VERSION
ENV HADOOP_VERSION=$HADOOP_VERSION

ENV USRLIBS /home/libs

ENV SPARK_HOME=$USRLIBS/spark-${SPARK_VERSION}-bin-${HADOOP_VERSION}
ENV KAFKA_HOME=$USRLIBS/kafka
ENV SPARKLIB=${SPARK_HOME}/python:${SPARK_HOME}/python/lib/py4j-0.10.9-src.zip
ENV PATH=${USRLIBS}/miniconda/bin:${SPARK_HOME}/bin:${SPARK_HOME}/sbin:/usr/local/bin:${PATH}
ENV BINPATH=$PATH
ENV PYTHONPATH=${SPARKLIB}:${PYTHONPATH}

WORKDIR $USRLIBS

COPY --from=builder $USRLIBS $USRLIBS

# Install Java and useful utilities
RUN yum -y update \
&& yum -y install which git wget java-1.8.0-openjdk \
&& yum -y clean all \
&& rm -rf /var/cache \
&& echo "export JAVA_HOME=$(dirname $(dirname $(readlink -f $(type -P java))))" > /etc/profile.d/javahome.sh

ENV JAVA_HOME="$(dirname $(dirname $(readlink -f $(type -P java))))"

ENTRYPOINT source scripts/start_services.sh --kafka-version ${KAFKA_VERSION} --hbase-version ${HBASE_VERSION} && /bin/bash
