FROM registry.access.redhat.com/ubi8/ruby-27:1-43.1638364006

LABEL summary="Zync - the sync tool" \
      description="Zync is going to take your 3scale data \
                   and push it somewhere else, reliably. \
                   Offers only one directional sync (from 3scale to other systems)." \
      io.k8s.display-name="Zync" \
      io.k8s.description="Zync is going to take your 3scale data \
                   and push it somewhere else, reliably. \
                   Offers only one directional sync (from 3scale to other systems)." \
      io.openshift.expose-services="8080:zync" \
      io.openshift.tags="sync, api management, 3scale, rhamp"

#Labels consumed by Red Hat build service
LABEL com.redhat.component="3scale-amp-zync-container" \
      maintainer="mcichra@redhat.com" \
      name="3scale-amp2/zync-rhel8" \
    version="1.14.1"

ENV RAILS_ENV=production \
   RAILS_LOG_TO_STDOUT=1 \
   BUNDLE_WITHOUT=development:test \
   TZ=:/etc/localtime \
   BUNDLE_GEMFILE=Gemfile \
   DISABLE_SPRING=1 \
   USER_UID=1001 \
   GEMS_REPO=https://repository.jboss.org/nexus/content/groups/rubygems_store/

EXPOSE 8080

USER root

RUN mkdir -p tmp log; chmod -vfR g+w tmp log

WORKDIR /opt/zync

ARG BUILD_TYPE=brew

COPY $BUILD_TYPE.repo /etc/yum.repos.d/brew.repo

RUN dnf -y module enable postgresql:12/client && dnf -y module install postgresql:12/client \
    && dnf -y install shared-mime-info

# Install Red Hat IT root Certificate Authority (CA), so we can interact with internal systems.
RUN curl -o /etc/pki/ca-trust/source/anchors/newca.crt https://password.corp.redhat.com/RH-IT-Root-CA.crt \
  && update-ca-trust extract

# Copy source code into container
ADD zync-*.tar.gz /tmp

# Apply product patches (product uses internally-mirrored github repositories for the build)
RUN cd /tmp/zync-* \
    && sed -i -e 's|github.com|code.engineering.redhat.com/gerrit|g' Gemfile* \
    && cd /opt/zync \
    && cp -pR /tmp/zync-*/* .

# Install dependencies from internal rubygems.org mirror
RUN cd /opt/zync \
    && gem install --no-document bundler:2.2.21 --source $GEMS_REPO \
    && bundle config mirror.http://rubygems.org $GEMS_REPO \
    && bundle config mirror.https://rubygems.org $GEMS_REPO \
    && bundle install --deployment --jobs $(grep -c processor /proc/cpuinfo) --retry=5 --full-index \
    && chgrp root /opt/zync/

# install licenses.xml
RUN mkdir -p /root/licenses/3scale-amp-zync \
    && cp /opt/zync/doc/licenses/licenses.xml /root/licenses/3scale-amp-zync/licenses.xml

# Remove Red Hat IT root Certificate Authority (CA). We don't need it in the final product.
RUN rm /etc/pki/ca-trust/source/anchors/newca.crt \
  && update-ca-trust extract

RUN cd /opt/zync \
   && rm -f log/*.log \
   && touch /opt/zync/entrypoint.sh \
   && chmod 755 /opt/zync/entrypoint.sh \
   && echo '#!/bin/sh' >> /opt/zync/entrypoint.sh \
   && echo 'bundle exec bin/rails db:setup 2> /dev/null || bundle exec bin/rails db:migrate' >> /opt/zync/entrypoint.sh \
   && echo 'exec bundle exec "$@"' >> /opt/zync/entrypoint.sh

USER ${USER_UID}

ENTRYPOINT ["/opt/zync/entrypoint.sh"]

CMD ["bin/rails", "server", "--binding", "0.0.0.0", "--port", "8080"]
