#--------------------------------------------------------------------------#
# Copyright (C) 2021 by Tibit Communications, Inc.                         #
# All rights reserved.                                                     #
#                                                                          #
#    _______ ____  _ ______                                                #
#   /_  __(_) __ )(_)_  __/                                                #
#    / / / / __  / / / /                                                   #
#   / / / / /_/ / / / /                                                    #
#  /_/ /_/_____/_/ /_/                                                     #
#                                                                          #
#  Distributed as Tibit-Customer confidential.                             #
#                                                                          #
#--------------------------------------------------------------------------#
#
# Build command example:
#   export VERSION="r2.3.0"    # Or whatever release you have (will be the image tag as well)
#   export NOW=${date -u "+%Y-%m-%dT%H:%M:%SZ"}
#   docker build -t tibit-ponauto                                  \
#    --build-arg PKG_DIR="./"                                      \
#    --build-arg DEB_PKG_NAME="tibit-ponauto__amd64.deb" \
#    --build-arg org_label_schema_version=""             \
#    --build-arg org_label_schema_build_date=""              \
#    -f docker/Dockerfile .
#
# Run command example:
#    docker run -d -it --name ponauto \
#     --mount type=bind,source=/home/jenkins/jenkins_slave/workspace/ponauto-on-2004-bld-rl/build/docker/PonAutoInit.json,target=/etc/tibit/ponauto/PonAutoInit.json \
#     tibit-ponauto:
#
# Seed PON Automation Database:
#   The image created by this dockerfile can also be used to seed the PON Automation database
#   from an initial JSON file setting. Assuming your JSON files are in the /home/jenkins/jenkins_slave/workspace/ponauto-on-2004-bld-rl/build/database subdirectory
#   locally, run the following command
#
#   docker run -d -it --name ponauto-init \
#     --mount type=bind,source=/home/jenkins/jenkins_slave/workspace/ponauto-on-2004-bld-rl/build/docker/PonAutoInit.json,target=/etc/tibit/ponauto/PonAutoInit.json \
#     --mount type=bind,source=/home/jenkins/jenkins_slave/workspace/ponauto-on-2004-bld-rl/build/database,target=/opt/tibit/ponauto/database \
#     tibit-ponauto: /opt/tibit/ponauto/bin/AutoJsonToMongo \
#       -d /opt/tibit/ponauto -c /etc/tibit/ponauto/PonAutoInit.json
#
FROM ubuntu:20.04

ARG VERSION
#
# Build commands
#
# Run the debian package manager in non-interactive mode
ARG DEBIAN_FRONTEND=noninteractive
ARG PKG_DIR=""
ARG DEB_PKG_NAME=unknown


# Install the PON Automation binary package but mask the service so we can
# pick and choose the binary we run
RUN mkdir -p /tmp
WORKDIR "/tmp"
COPY R${VERSION}-PonAutomation-UB2004/tibit-ponauto_R${VERSION}_amd64.deb /tmp

# Update image with the PON Automation binaries and set to run as a non-root user
RUN  apt-get update --fix-missing -y \
  && apt-get install vim -y\
  && apt-get install supervisor -y \
  && apt-get install lsb-release "./tibit-ponauto_R${VERSION}_amd64.deb" -y --no-install-recommends \
  && rm -f "/tmp/tibit-ponauto_R${VERSION}_amd64.deb" \
  && rm -Rf /usr/share/doc && rm -Rf /usr/share/man \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/* \
  && mkdir -p /var/log/tibit \
  && useradd ponAuto && groupadd mcms \
  && chown -R ponAuto:mcms /etc/tibit/ponauto \
  && chown -R ponAuto:mcms /opt/tibit/ponauto \
  && chown -R ponAuto:mcms /var/log/tibit

WORKDIR "/opt/tibit/ponauto"
USER ponAuto:mcms

# Label image
ARG org_label_schema_version=unknown
ARG org_label_schema_vcs_url=unknown
ARG org_label_schema_vcs_ref=unknown
ARG org_label_schema_build_date=unknown
ARG org_label_vcs_commit_date=unknown
ARG org_label_vcs_dirty=unknown

LABEL org.label-schema.schema-version=1.0                      \
      org.label-schema.name=tibit-ponauto                      \
      org.label-schema.version=       \
      org.label-schema.vcs-url=       \
      org.label-schema.vcs-ref=       \
      org.label-schema.vcs-dirty=          \
      org.label-schema.build-date= \
      org.label-schema.vcs-commit-date=
#
# Runtime commands
#
# Run the binary PON Automation image
#CMD ["/opt/tibit/ponauto/bin/tibit-ponauto", "-c", "/etc/tibit/ponauto/PonAutoInit.json"]

COPY R${VERSION}-PonAutomation-UB2004/docker/supervisord.conf /etc/supervisor/supervisord.conf
EXPOSE 9001
CMD /usr/bin/supervisord -c /etc/supervisor/supervisord.conf


