# Pinned on 0.28.2 until we can test mandatory RETENTION_MS settings from 0.29.0
# https://github.com/confluentinc/ksql/issues/10419
FROM confluentinc/ksqldb-server:0.28.2 AS ksqldb-server

USER root

# Install required packages for entrypoint and other scripts.
RUN yum -y update \
    && yum -y install findutils jq \
    && yum clean all

# Update python libraries
RUN python --version
RUN python -m pip install --upgrade pip certifi cryptography idna urllib3 setuptools

# Deploy KSQL queries and scripts.
COPY --chown=appuser:appuser queries /home/appuser/queries
RUN chmod -R u=rwX,g=rX,o-rwx /home/appuser/queries

COPY --chown=root:root entrypoint.sh upload-queries.sh healthcheck.sh /usr/local/bin/
RUN chmod 755 /usr/local/bin/entrypoint.sh /usr/local/bin/upload-queries.sh /usr/local/bin/healthcheck.sh

# Configure KSQL logging.
COPY --chown=appuser:appuser log4j.properties /opt/

USER appuser

# Set default Environment Variables for KSQL.
ENV KSQL_PORT=8088
ENV KSQL_KSQL_LOGGING_PROCESSING_TOPIC_AUTO_CREATE="true"
ENV KSQL_SECURITY_PROTOCOL="SSL"
ENV KSQL_KSQL_COMMIT_INTERVAL_MS=2000
ENV KSQL_KSQL_SINK_PARTITIONS=1
ENV KSQL_KSQL_STREAMS_REPLICATION_FACTOR=3
ENV KSQL_KSQL_STREAMS_NUM_STREAM_THREADS=3
ENV KSQL_KSQL_INTERNAL_TOPIC_REPLICAS=3
ENV KSQL_KSQL_STREAMS_CACHE_MAX_BYTES_BUFFERING=10000000
ENV KSQL_KSQL_STREAMS_AUTO_OFFSET_RESET="latest"
ENV KSQL_CONFLUENT_SUPPORT_METRICS_ENABLE="false"
ENV KSQL_KSQL_STREAMS_STATE_DIR="/tmp/kafka-streams"
ENV KSQL_KSQL_ACCESS_VALIDATOR_ENABLE="off"
ENV KSQL_LISTENERS="http://0.0.0.0:8088"
ENV KSQL_USE_QUERY_FILE="true"
ENV KSQL_PRODUCER_MAX_REQUEST_SIZE=15728640
ENV KSQL_LOG4J_OPTS="-Dlog4j.configuration=file:/opt/log4j.properties"
ENV KSQL_KSQL_QUERY_PULL_METRICS_ENABLED="true"

# For JMX Metrics.
ENV JMX_PORT=9095
ENV JMX_HOSTNAME='0.0.0.0'
ENV JMX_SSL=false
ENV JMX_AUTH=false
ENV KSQL_JMX_OPTS="-Dcom.sun.management.jmxremote=true"\
" -Dcom.sun.management.jmxremote.authenticate=$JMX_AUTH"\
" -Dcom.sun.management.jmxremote.ssl=$JMX_SSL"\
" -Dcom.sun.management.jmxremote.local.only=false"\
" -Dcom.sun.management.jmxremote.port=$JMX_PORT"\
" -Dcom.sun.management.jmxremote.rmi.port=$JMX_PORT"\
" -Djava.rmi.server.hostname=$JMX_HOSTNAME"\
" -Dcom.sun.management.jmxremote.host=$JMX_HOSTNAME"

EXPOSE ${KSQL_PORT}
EXPOSE ${JMX_PORT}

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

#FORCE REDEPLOY:1
