# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

# Apache Texera is an effort undergoing incubation at The Apache Software
# Foundation (ASF), sponsored by the Apache Incubator PMC. Incubation is
# required of all newly accepted projects until a further review indicates
# that the infrastructure, communications, and decision-making process have
# stabilized in a manner consistent with other successful ASF projects.
# While incubation status is not necessarily a reflection of the
# completeness or stability of the code, it does indicate that the project
# has yet to be fully endorsed by the ASF.

FROM sbtscala/scala-sbt:eclipse-temurin-jammy-11.0.17_8_1.9.3_2.13.11 AS build

# Set working directory
WORKDIR /texera

# Copy modules for building the service
COPY common/ common/
COPY computing-unit-managing-service/ computing-unit-managing-service/
COPY project/ project/
COPY build.sbt build.sbt

# Update system and install dependencies
RUN apt-get update && apt-get install -y \
    netcat \
    unzip \
    libpq-dev \
    && apt-get clean

# Add .git for runtime calls to jgit from OPversion
COPY .git .git
COPY LICENSE NOTICE DISCLAIMER ./
COPY licenses/ licenses/

RUN sbt clean ComputingUnitManagingService/dist

# Unzip the texera binary
RUN unzip computing-unit-managing-service/target/universal/computing-unit-managing-service-*.zip -d target/

FROM eclipse-temurin:11-jre-jammy AS runtime

WORKDIR /texera

COPY --from=build /texera/.git /texera/.git
# Copy the built texera binary from the build phase
COPY --from=build /texera/target/computing-unit-managing-service-* /texera/
# Copy resources directories from build phase
COPY --from=build /texera/common/config/src/main/resources /texera/common/config/src/main/resources
COPY --from=build /texera/computing-unit-managing-service/src/main/resources /texera/computing-unit-managing-service/src/main/resources
# Copy ASF licensing files. LICENSE-binary and NOTICE-binary describe the
# bundled third-party contents of this image and ship as /texera/LICENSE
# and /texera/NOTICE; licenses/ holds the per-license full texts referenced
# by LICENSE-binary.
COPY --from=build /texera/computing-unit-managing-service/LICENSE-binary /texera/LICENSE
COPY --from=build /texera/computing-unit-managing-service/NOTICE-binary /texera/NOTICE
COPY --from=build /texera/licenses /texera/licenses
COPY --from=build /texera/DISCLAIMER /texera/

RUN groupadd --system --gid 1001 texera \
 && useradd --system --uid 1001 --gid texera --home-dir /texera --no-create-home texera \
 && chown -R texera:texera /texera
USER texera

CMD ["bin/computing-unit-managing-service"]

EXPOSE 8888