
# build/make inside a container
#
ARG BASE_IMAGE
ARG pyspark_base=$BASE_IMAGE

FROM ${pyspark_base} as builder
ARG build_root=/build/nsx-pace/pace/infraclassification

# change user to allow tool install for buidler
USER root

# install build tools in container
RUN apt-get update && apt-get -y install make && \
    apt-get autoremove -y && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# setup build root dir
COPY . ${build_root}

WORKDIR ${build_root}

# install requirements
RUN pip3 install -r requirements.txt

# Protos are built from metrics/protobuf/generate_proto_bindings.sh in the outer makefile, and copied \
# into the build context already by making proto-cache a dep of the outer makefile, so just call test here

# RUN make test -- this will build the proto-cache and then run tests
ENV PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION="python"
ENV FILTER_TO_COMMON_PORTS="true"
ENV PARTITIONS_PER_CORE="2"
ENV K8S_CAPACITY_CHECK_ENABLED="true"
ENV EXECUTOR_INSTANCES="2"
ENV EXECUTOR_CORES="2"



#
#
FROM $BASE_IMAGE
ARG build_root=/build/nsx-pace/pace/infraclassification/infraclassifier
ARG run_root=/opt/vmware/nsxi/infraclassifier



#
# root priv section
#   If you want to execute any root priv opt (e.g. apt-get to install new pkg
#   do it between the "root priv section"
#

# change user to allow us to setup install new tools
USER root

# Setup ENV for docker image
#   this would be replace by helm later
ENV PYTHONPATH=${run_root}

RUN mkdir -p ${run_root}

# copy from builder into final image
#   copy the py file from builder to the image (same directory structure)
COPY --from=builder ${build_root} ${run_root}

# Set permissions for /opt/vmware/nsxi/infraclassifier and its subdirectories
RUN chown -R 1001:1001 ${run_root} && \
    chmod -R 770 ${run_root}

ENV HOME=/
ENV HOME_DIR=/home/vmware/
ENV PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION="python"
ENV FILTER_TO_COMMON_PORTS="true"

RUN pip3 install -r ${run_root}/requirements.txt

# change user back to 1001 (vmware)
USER 1001

#
# End root priv section

# for debugging
# RUN touch ${run_root}/image.done