##############################################################
# Target: noobaa_npm
#   Title: building the noobaa npm (using cached npm)
#
# Currently, we are getting the noobaa code via tarball.
# This Target will build the node_modules.
##############################################################
FROM registry.redhat.io/ubi8/ubi as noobaa_builder

COPY noobaa.tar.gz .
RUN tar -zxvf noobaa.tar.gz

RUN dnf group install -y "Development Tools" && \
    dnf module enable -y nodejs:12 && \
    dnf install -y python2 \
    nodejs-devel-12.18.4 \
    npm-6.14.6 \
    yasm && \
    dnf clean all

COPY build_deps.tar.gz .
RUN tar -zxvf build_deps.tar.gz

RUN alternatives --set python /usr/bin/python2

WORKDIR /noobaa
RUN npm --nodedir=/usr rebuild --offline --prod
RUN npm --nodedir=/usr install --offline --prod --python=/usr/bin/python2

#Building the native
RUN npm --nodedir=/usr run build:native

RUN mkdir -p /noobaa_init_files && \
    cp -p ./build/Release/kube_pv_chown /noobaa_init_files

##############################################################
# Target: noobaa_tarball
#   Title: Getting noobaa into a tar. We are doing it in 
#	   different target so it will be clean
##############################################################

FROM registry.redhat.io/ubi8/ubi as noobaa_tarball

RUN dnf install -y patch && \
    dnf clean all 

COPY noobaa.tar.gz .
RUN tar -zxvf noobaa.tar.gz
RUN rm -rf /noobaa/frontend/dist/noobaa_icon*

COPY --from=noobaa_builder /noobaa/node_modules/ /noobaa/node_modules/
COPY --from=noobaa_builder /noobaa_init_files/ /noobaa_init_files/
COPY --from=noobaa_builder /noobaa/build/Release/ /noobaa/build/Release/
COPY noobaa_icon*  /noobaa/frontend/dist/assets/
COPY EULA.pdf /noobaa/
COPY base.patch /noobaa/
COPY config-local.js /noobaa/ 

WORKDIR /noobaa

RUN patch -p1 < base.patch

RUN tar \
    --transform='s:^:noobaa-core/:' \
    --exclude='src/native/aws-cpp-sdk' \
    --exclude='src/native/third_party' \
    -czf noobaa-NVA.tar.gz \
    LICENSE \
    package.json \
    EULA.pdf \
    platform_restrictions.json \
    config.js \
    config-local.js \
    .nvmrc \
    src/ \
    frontend/dist/ \
    build/Release/ \
    node_modules/ 

##############################################################
# Target: kubectl_cmd
#   Title: getting the kubectl from ose-cli-artifacts image
#
##############################################################
FROM openshift/ose-cli-artifacts:v4.5 as kubectl_cmd

RUN kubectl version

##############################################################################

FROM registry.redhat.io/ubi8/ubi

ENV container docker
ENV PORT 8080
ENV SSL_PORT 8443
ENV ENDPOINT_PORT 6001
ENV ENDPOINT_SSL_PORT 6443
ENV WEB_NODE_OPTIONS ''
ENV BG_NODE_OPTIONS ''
ENV HOSTED_AGENTS_NODE_OPTIONS ''
ENV ENDPOINT_NODE_OPTIONS ''

RUN dnf update -y && \
    dnf clean all

RUN dnf module enable -y nodejs:12 && \
    dnf install -y \
    bash \
    lsof \
    openssl \
    rsyslog-8.1911.0 \
    strace \
    wget \
    curl \
    nc \
    vim \
    less \
    bash-completion \
    nodejs-12.18.4 \
    npm-6.14.6 \
    supervisor && \ 
    dnf clean all

RUN ln -sf $(which node) /usr/local/bin/node && \
    ln -sf $(which npm) /usr/local/bin/npm

COPY --from=kubectl_cmd /usr/bin/kubectl /usr/local/bin/
COPY --from=kubectl_cmd /usr/bin/oc /usr/bin/
#We are running kubectl version so it will fail if kubectl is not working
RUN /usr/local/bin/kubectl version

RUN mkdir -p /data/ && \
    mkdir -p /log

COPY --from=noobaa_tarball /noobaa/src/deploy/NVA_build/supervisord.orig ./src/deploy/NVA_build/
COPY --from=noobaa_tarball /noobaa/src/deploy/NVA_build/supervisord.orig /tmp/supervisord
COPY --from=noobaa_tarball /noobaa/src/deploy/NVA_build/supervisorctl.bash_completion /etc/bash_completion.d/supervisorctl
COPY --from=noobaa_tarball /noobaa/src/deploy/NVA_build/rsyslog.conf /etc/rsyslog.conf
COPY --from=noobaa_tarball /noobaa/src/deploy/NVA_build/noobaa_syslog.conf /etc/rsyslog.d/
COPY --from=noobaa_tarball /noobaa/src/deploy/NVA_build/logrotate_noobaa.conf /etc/logrotate.d/noobaa/
COPY --from=noobaa_tarball /noobaa/src/deploy/NVA_build/noobaa_init.sh /noobaa_init_files/

COPY --from=noobaa_tarball /noobaa/src/deploy/NVA_build/setup_platform.sh /usr/bin/setup_platform.sh
RUN /usr/bin/setup_platform.sh 

RUN chmod 775 /noobaa_init_files && \
    chgrp -R 0 /noobaa_init_files/ && \
    chmod -R g=u /noobaa_init_files/

COPY --from=noobaa_builder /noobaa_init_files/kube_pv_chown /noobaa_init_files/
RUN mkdir -m 777 /root/node_modules && \
    chown root:root /noobaa_init_files/kube_pv_chown && \
    chmod 755 /noobaa_init_files/kube_pv_chown && \
    chmod u+s /noobaa_init_files/kube_pv_chown

COPY --from=noobaa_tarball /noobaa/noobaa-NVA.tar.gz /tmp/

###############
# PORTS SETUP #
###############
EXPOSE 60100
EXPOSE 80
EXPOSE 443
EXPOSE 8080
EXPOSE 8443
EXPOSE 8444
EXPOSE 27000
EXPOSE 26050

###############
# EXEC SETUP #
###############
# run as non root user that belongs to root 
USER 10001:0
CMD ["/usr/bin/supervisord", "start"]

LABEL maintainer="Liran Mauda (lmauda@redhat.com)"
LABEL com.redhat.component="noobaa-core-container"
LABEL io.k8s.display-name="Noobaa Core based on RHEL 8"
LABEL io.k8s.description="Noobaa Core Container based on Red Hat Enterprise Linux 8 Image"
LABEL name="mcg-core"
LABEL version="5.6.0"
LABEL release="42.6a01c5fc0.5.6"
LABEL summary="Provides the latest NooBaa Core container for OpenShift Container Storage"
LABEL description="OpenShift Container Storage NooBaa Core container"
LABEL io.openshift.tags="ocs"
LABEL ocs.tags="v4.6"
LABEL upstream-vcs-ref="6a01c5fc01236b11a538a4d72c417d83061df2bb"
