# ORBIT Dockerfile
#
# Copyright 2024 Terma BV
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the
# “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# the Dockerfile takes a RedHat UBI container image, and adds Terma ORBIT



# Base image
FROM registry.access.redhat.com/ubi9/ubi

# The ORBIT_VERSION number has to be passed as argument.
ARG ORBIT_VERSION


LABEL "com.terma.tgss.orbit.short_description"="Terma Ground Segment Suite (TGSS), ORBIT"
LABEL "com.terma.tgss.orbit.vendor"="Terma"
LABEL "com.terma.tgss.orbit.version"="$ORBIT_VERSION"


# RedHat Universal Base Image (also included in the container, see below)
# https://www.redhat.com/licenses/EULA_Red_Hat_Universal_Base_Image_English_20190422.pdf

ADD https://www.redhat.com/licenses/EULA_Red_Hat_Universal_Base_Image_English_20190422.pdf .

# Update system and install essential packages
RUN dnf -y update && \
    dnf upgrade -y && \
    dnf install -y wget

# Create a user
# Use environment variables for user customization (values can be overridden at runtime)
ARG USER_ID=1001
ARG GROUP_ID=1001
ARG USER=user
ARG HOME=/home/$USER

# Setup user and home directory
RUN groupadd -g $GROUP_ID $USER && \
    useradd -m -u $USER_ID -d $HOME -s /bin/bash -g $USER $USER

# Set the working directory to the user's home
WORKDIR $HOME

# Install Conda and configure the path
ENV PATH /opt/conda/bin:$PATH
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh && \
    bash ~/miniconda.sh -b -p /opt/conda && \
    rm ~/miniconda.sh && \
    ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh && \
    echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc && \
    echo "conda activate base" >> ~/.bashrc

# Add conda-forge channel and create a new Conda environment for ORBIT
RUN /opt/conda/bin/conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main && \
    /opt/conda/bin/conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r && \
    /opt/conda/bin/conda config --add channels conda-forge && \
    /opt/conda/bin/conda config --set channel_priority strict && \
    /opt/conda/bin/conda create -y --name orbit_env python=3.11 \
      geopandas=0.14.4 lxml=4.9.3 astropy=5.3.1 orekit=12.0.1 \
      matplotlib=3.9.2 pydantic=2.11.7 plotly=6.3.0 scipy=1.15.2 typer=0.16.0 pytest=8.4.1 && \
    /opt/conda/bin/conda clean -afy

# Set up the Conda environment and adjust PATH
RUN echo "source activate orbit_env" >> ~/.bashrc
ENV PATH /opt/conda/envs/orbit_env/bin:$PATH


# Set up application folders
RUN mkdir -p ORBIT ORBIT_conf
WORKDIR $HOME/ORBIT

# Copy application files
COPY t-orbit-server/target/orbit-app.jar ./orbit-app.jar

# Define environment variables for external usage, to be loaded from .env file
ENV ORBIT_HOME=$HOME/ORBIT
ENV ORBIT_PYTHON=/opt/conda/envs/orbit_env/bin/python3

# Application environment variables (load from .env file during docker run)
# These should be specified in an external .env file:
# ORBIT_BIN, ORBIT_LICENSE_KEY, ORBIT_CA, ORBIT_CA_PASS, CCS_USER, CCS_PASSWORD,
# CCS_POSTGRES_URL, CCS_MYSQL_URL, EUSST_CLIENT_ID, EUSST_CLIENT_SECRET,
# PLAN_USER, PLAN_URL, PLAN_PASSWORD, etc.

# Define runtime options for the ORBIT app
ENV APP_OPTIONS="-Dapp.folder=$ORBIT_HOME/src/main/resources"
ENV SPRING_OPTIONS="--spring.config.location=$ORBIT_HOME/src/main/resources/application.properties,$ORBIT_HOME/src/main/resources/configuration.properties"

# Copy the entrypoint script into the container filesystem
COPY docker/deploy/entrypoint.sh /entrypoint.sh

# Give execution permissions to the entrypoint script
RUN chmod +x /entrypoint.sh

# Update the permissions
RUN chown -R user $HOME

# Switch to non-root user using UID instead of username
USER 1001

# Expose the port used by the application
EXPOSE 8080

# Set the entrypoint script as the default command when the container starts
ENTRYPOINT ["/entrypoint.sh"]
