FROM ruby:2.7.1-alpine as base-image

LABEL maintainer="Trileuco Solutions <info@trileucosolutions.com>"

RUN apk add --no-cache tzdata

RUN cp /usr/share/zoneinfo/UTC /etc/localtime
RUN echo UTC > /etc/timezone

WORKDIR /usr/local/menarini
ENV RAILS_ENV ${RAILS_ENV:-production}
ENV SECRET_KEY_BASE ${SECRET_KEY_BASE:-}

ENV DB_HOST ${DB_HOST:-localhost}
ENV DB_DATABASE ${DB_DATABASE:-menarini_development}
ENV DB_USER ${DB_USER:-menarini}
ENV DB_PASSWORD ${DB_PASSWORD:-menarini_password}
ENV DB_CONNECT_TIMEOUT ${DB_CONNECT_TIMEOUT:-5}

ENV RAILS_MAX_THREADS ${RAILS_MAX_THREADS:-5}
ENV RAILS_MIN_THREADS ${RAILS_MIN_THREADS:-5}
ENV WEB_CONCURRENCY ${WEB_CONCURRENCY:-0}

ENV REDIS_HOST ${REDIS_HOST:-localhost}
ENV REDIS_PORT ${REDIS_PORT:-6379}
ENV REDIS_PASSWORD ${REDIS_PASSWORD}
ENV REDIS_DB ${REDIS_DB:-0}

ENV RAILS_LOG_TO_STDOUT ${RAILS_LOG_TO_STDOUT:-true}
ENV LOG_SQL_QUERIES ${LOG_SQL_QUERIES:-false}

ENV DEFAULT_TIME_ZONE ${DEFAULT_TIME_ZONE:-Europe/Lisbon}
ENV USE_MEMORY_CACHE_STORE ${USE_MEMORY_CACHE_STORE:-false}

ENV DOCUTEN_API_KEY ${DOCUTEN_API_KEY:-}
ENV DOCUTEN_API_URL ${DOCUTEN_API_URL:-}

ENV PORT ${PORT:-3001}
EXPOSE ${PORT:-3001}

############### Build stage ( This image will not be reused anymore ) ###############

FROM base-image as build-image

# RUN apk add g++ make git libpq xz-libs postgresql-dev
RUN apk add make g++ postgresql-dev git

COPY Gemfile* ./

RUN bundle config set without 'development test' \
    && bundle install --jobs 20 --retry 5 \
    && bundle binstubs railties \
    && bundle binstubs rake \
    && bundle binstubs resque-pool \
    && rm -rf /usr/local/bundle/cache/*.gem \
    && rm -rf /usr/local/bundle/cache/bundler/git/* \
    && find /usr/local/bundle/gems/ -name "*.c" -delete \
    && find /usr/local/bundle/gems/ -name "*.o" -delete \
    && mkdir -p /usr/local/bundle/bundler/gems/ \
    && find /usr/local/bundle/bundler/gems/ -name "*.c" -delete \
    && find /usr/local/bundle/bundler/gems/ -name "*.o" -delete

RUN scanelf --needed --nobanner --recursive /usr/local/bundle \
    | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
    | sort -u \
    | xargs -r apk info --installed \
    | sort -u > /tmp/menarini-gemrundeps

COPY . ./

# Remove folders not needed in resulting image
RUN rm -rf tmp/cache vendor/assets spec

############### Runtime Menarini stage ###############

FROM base-image as menarini-image

RUN apk add --no-cache --virtual .menarini-rundep bash

COPY --from=build-image /tmp/menarini-gemrundeps /tmp/menarini-gemrundeps
RUN runDeps="$( cat /tmp/menarini-gemrundeps )" \
    && apk add --no-cache --virtual .menarini-gemrundeps $runDeps

COPY --from=build-image /usr/local/bundle /usr/local/bundle
COPY --from=build-image /usr/local/menarini .

RUN adduser -S menarini -h /usr/local/menarini

RUN mkdir -p ./tmp \
    && chown -R menarini ./tmp /etc/localtime /etc/timezone

ARG GIT_VERSION
ENV GIT_VERSION ${GIT_VERSION}

USER menarini

HEALTHCHECK  --interval=30s --timeout=10s --start-period=15s \
  CMD wget "http://localhost:$PORT/status/all.json" -q -O - > /dev/null 2>&1 || exit 1

ENTRYPOINT ["./docker-entrypoint.sh"]

CMD ["rails", "server", "puma", "-b", "0.0.0.0"]
