# https://hub.docker.com/r/adoptopenjdk/openjdk8
# https://github.com/AdoptOpenJDK/openjdk-docker/blob/master/8/jdk/alpine/Dockerfile.hotspot.releases.slim
FROM adoptopenjdk/openjdk8:alpine-slim

MAINTAINER Marek Rychly <marek.rychly@gmail.com>

# https://hbase.apache.org/downloads.html
ARG HBASE_VERSION=2.2.0

ARG DOWNLOAD_CACHE
ARG APACHE_ORIG="http://www-eu.apache.org/dist"
#ARG APACHE_MIRROR="ftp://mirror.hosting90.cz/apache"
ARG APACHE_MIRROR="http://archive.apache.org/dist"

ENV HBASE_HOME="/opt/hbase"
ENV HBASE_CONF_DIR="${HBASE_HOME}/conf"
ENV \
HADOOP_CORE_CONF="${HBASE_CONF_DIR}/core-site.xml" \
HBASE_SITE_CONF="${HBASE_CONF_DIR}/hbase-site.xml" \
HBASE_POLICY_CONF="${HBASE_CONF_DIR}/hbase-policy.xml" \
HBASE_ENV_CONF="${HBASE_CONF_DIR}/hbase-env.sh" \
HBASE_USER="hbase"

COPY scripts /

RUN true \
# make the scripts executable
&& chmod 755 /*.sh \
# bash: hbase shell scripts require BASH, they are not compatible with Busybox ASH/SH
# sed: scripts to set HBase properties in XML files require GNU sed (the usage of busybox sed results into incorrect outputs)
&& apk add --no-cache --update gnupg attr sed bash \
\
# wait-for: HBase master and regionserver needs to wait for a HDFS namenode to connect
&& wget -O /usr/bin/wait-for "https://raw.githubusercontent.com/eficode/wait-for/master/wait-for" && chmod 755 /usr/bin/wait-for \
\
# download keys and trust them
&& ( [ -n "${DOWNLOAD_CACHE}" ] && cp -v "${DOWNLOAD_CACHE}/hbase.KEYS" /tmp \
	|| wget -O /tmp/hbase.KEYS "${APACHE_ORIG}/hbase/KEYS" ) \
&& gpg --import /tmp/hbase.KEYS \
&& echo "trust-model always" > ~/.gnupg/gpg.conf \
\
# download the package
&& ( [ -n "${DOWNLOAD_CACHE}" ] && cp -v "${DOWNLOAD_CACHE}/hbase-${HBASE_VERSION}-bin.tar.gz" /tmp \
	|| wget -O /tmp/hbase-${HBASE_VERSION}-bin.tar.gz "${APACHE_MIRROR}/hbase/${HBASE_VERSION}/hbase-${HBASE_VERSION}-bin.tar.gz" ) \
\
# download and verify signature
&& ( [ -n "${DOWNLOAD_CACHE}" ] && cp -v "${DOWNLOAD_CACHE}/hbase-${HBASE_VERSION}-bin.tar.gz.asc" /tmp \
	|| wget -O /tmp/hbase-${HBASE_VERSION}-bin.tar.gz.asc "${APACHE_ORIG}/hbase/${HBASE_VERSION}/hbase-${HBASE_VERSION}-bin.tar.gz.asc" ) \
&& for SIG in /tmp/*.asc; do gpg --verify "${SIG}" "${SIG%.asc}"; done \
\
# extract the package and remove garbage
&& mkdir -p "${HBASE_HOME}" \
&& tar -xzf "/tmp/hbase-${HBASE_VERSION}-bin.tar.gz" -C "${HBASE_HOME}" --strip-components 1 \
&& find "${HBASE_HOME}" \( -name '*.cmd' -o -name '*-sources.jar' -o -name '*-tests.jar' \) -delete \
&& rm -rf "${HBASE_HOME}/docs" \
\
# create a log directory and enable logging
&& mkdir -p ${HBASE_HOME}/logs \
&& chmod 777 ${HBASE_HOME}/logs \
&& sed -i "s|^\(hbase\.log\.dir=\).*\$|\1${HBASE_HOME}/logs|g" ${HBASE_HOME}/conf/log4j.properties \
\
# integrate with Java
&& echo "export JAVA_HOME=${JAVA_HOME}" >> ${HBASE_ENV_CONF} \
\
# set up permissions
&& addgroup -S "${HBASE_USER}" \
&& adduser -h "${HBASE_HOME}" -g "Apache HBase" -s /bin/sh -G "${HBASE_USER}" -S -D -H "${HBASE_USER}" \
&& chown -R "${HBASE_USER}:${HBASE_USER}" "${HBASE_HOME}" \
\
# set path for the shell
&& echo '#!/bin/sh' > /etc/profile.d/path-hbase.sh \
&& echo "export PATH=\"\${PATH}:${HBASE_HOME}/bin\"" >> /etc/profile.d/path-hbase.sh \
&& chmod 755 /etc/profile.d/path-hbase.sh \
\
# clean up
&& apk del gnupg \
&& rm -rf /tmp/* /var/tmp/* /var/cache/apk/* /root/.gnupg

ENTRYPOINT ["/entrypoint.sh"]

HEALTHCHECK CMD /healthcheck.sh
