ARG BASE_VERSION
FROM registry.gitlab.com/rychly-edu/docker/docker-hadoop-hbase-base:${BASE_VERSION:-latest}

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

# https://github.com/Merck/Halyard/releases
ARG HALYARD_VERSION=3.0

# https://search.maven.org/artifact/org.eclipse.jetty/jetty-runner/
ARG JETTY_RUNNER_VERSION=9.4.20.v20190813

ARG DOWNLOAD_CACHE

ENV \
HALYARD_HOME="/opt/halyard" \
JETTY_HOME="/opt/jetty-runner" \
JETTY_USER="jetty"

COPY scripts /

COPY patches /tmp

RUN true \
# make the scripts executable
&& chmod 755 /*.sh \
# netcat: required for wait-for, otherwise it wont be able to call it and will fail on timeout
&& apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get -y install unzip patch netcat \
# wait-for: rdf4j-* needs to wait for hbase master before querying it from the zookeeper
&& curl -LfsSo /usr/bin/wait-for "https://raw.githubusercontent.com/eficode/wait-for/master/wait-for" && chmod 755 /usr/bin/wait-for \
\
# download the package
&& ( [ -n "${DOWNLOAD_CACHE}" ] && cp -v "${DOWNLOAD_CACHE}/halyard-webapps-${HALYARD_VERSION}.zip" /tmp \
	|| curl -LfsSo "/tmp/halyard-webapps-${HALYARD_VERSION}.zip" "https://github.com/Merck/Halyard/releases/download/r${HALYARD_VERSION}/halyard-webapps-${HALYARD_VERSION}.zip" ) \
\
# extract the Halyard package and remove garbage
&& mkdir -p "${HALYARD_HOME}" \
&& unzip -q "/tmp/halyard-webapps-${HALYARD_VERSION}.zip" -d "${HALYARD_HOME}" \
\
# patch the WAR files to enable cross-origin requests
&& ( cd /tmp && jar -vxf "${HALYARD_HOME}/rdf4j-server.war" WEB-INF/web.xml && patch -p 1 </tmp/web.xml.patch && jar -vuf "${HALYARD_HOME}/rdf4j-server.war" WEB-INF/web.xml && cd - ) \
\
# download the Jetty-Runner package
&& mkdir -p "${JETTY_HOME}" \
&& ( [ -n "${DOWNLOAD_CACHE}" ] && cp -v "${DOWNLOAD_CACHE}/jetty-runner-${JETTY_RUNNER_VERSION}.jar" "${JETTY_HOME}" \
	|| curl -LfsSo "${JETTY_HOME}/jetty-runner-${JETTY_RUNNER_VERSION}.jar" "https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-runner/${JETTY_RUNNER_VERSION}/jetty-runner-${JETTY_RUNNER_VERSION}.jar" ) \
&& ln -vs "jetty-runner-${JETTY_RUNNER_VERSION}.jar" "${JETTY_HOME}/jetty-runner.jar" \
\
# set up Jetty-Runner permissions
&& addgroup --system "${JETTY_USER}" \
&& adduser --system --home "${JETTY_HOME}" --gecos "Eclipse Jetty" --shell /bin/sh --ingroup "${JETTY_USER}" --disabled-login --no-create-home "${JETTY_USER}" \
&& chown -R "${JETTY_USER}:${JETTY_USER}" "${JETTY_HOME}" \
\
# clean up
&& apt-get -y autoremove unzip patch \
&& apt-get clean \
&& rm -rf /tmp/* /var/tmp/* /var/lib/apt/lists/*

ENTRYPOINT ["/entrypoint.sh"]

HEALTHCHECK CMD /healthcheck.sh
