# Golang build container
FROM openshift/golang-builder:1.12 AS builder
ENV __doozer=update BUILD_RELEASE=202005201238 BUILD_VERSION=v4.3.22 OS_GIT_MAJOR=4 OS_GIT_MINOR=3 OS_GIT_PATCH=22 OS_GIT_TREE_STATE=clean OS_GIT_VERSION=4.3.22-202005201238 
ENV __doozer=merge OS_GIT_COMMIT=e2261e1 OS_GIT_VERSION=4.3.22-202005201238-e2261e1 SOURCE_DATE_EPOCH=1573476262 SOURCE_GIT_COMMIT=e2261e142909cb0fe70fc0996d7d375e57956589 SOURCE_GIT_TAG=e2261e14 SOURCE_GIT_URL=https://github.com/openshift/grafana 

WORKDIR $GOPATH/src/github.com/grafana/grafana

COPY go.mod go.sum ./
COPY vendor vendor

RUN go mod verify

COPY pkg pkg
COPY build.go build.go
COPY package.json package.json

RUN go run build.go build
# Need to copy the generated binaries to a non-platform specific location to handle
# s390x builds for example
RUN cp $GOPATH/src/github.com/grafana/grafana/bin/linux-$(go env GOARCH)/grafana-server \
       $GOPATH/src/github.com/grafana/grafana/bin/linux-$(go env GOARCH)/grafana-cli \
       /usr/bin/

# Final container
FROM openshift/ose-base:v4.3.22.20200520.123801
ENV __doozer=update BUILD_RELEASE=202005201238 BUILD_VERSION=v4.3.22 OS_GIT_MAJOR=4 OS_GIT_MINOR=3 OS_GIT_PATCH=22 OS_GIT_TREE_STATE=clean OS_GIT_VERSION=4.3.22-202005201238 
ENV __doozer=merge OS_GIT_COMMIT=e2261e1 OS_GIT_VERSION=4.3.22-202005201238-e2261e1 SOURCE_DATE_EPOCH=1573476262 SOURCE_GIT_COMMIT=e2261e142909cb0fe70fc0996d7d375e57956589 SOURCE_GIT_TAG=e2261e14 SOURCE_GIT_URL=https://github.com/openshift/grafana 


ARG GF_UID="472"
ARG GF_GID="472"

ENV PATH=/usr/share/grafana/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
    GF_PATHS_CONFIG="/etc/grafana/grafana.ini" \
    GF_PATHS_DATA="/var/lib/grafana" \
    GF_PATHS_HOME="/usr/share/grafana" \
    GF_PATHS_LOGS="/var/log/grafana" \
    GF_PATHS_PLUGINS="/var/lib/grafana/plugins" \
    GF_PATHS_PROVISIONING="/etc/grafana/provisioning"

WORKDIR $GF_PATHS_HOME

COPY conf ./conf

RUN mkdir -p "$GF_PATHS_HOME/.aws" && \
    # addgroup -S -g $GF_GID grafana && \
    # adduser -S -u $GF_UID -G grafana grafana && \
    # Note: the openshift base image does not include the addgroup and adduser commands so we have to use useradd/groupadd
    groupadd --system -g $GF_GID grafana && \
    useradd --system -u $GF_UID -g grafana grafana && \
    mkdir -p "$GF_PATHS_PROVISIONING/datasources" \
             "$GF_PATHS_PROVISIONING/dashboards" \
             "$GF_PATHS_PROVISIONING/notifiers" \
             "$GF_PATHS_LOGS" \
             "$GF_PATHS_PLUGINS" \
             "$GF_PATHS_DATA" && \
    cp "$GF_PATHS_HOME/conf/sample.ini" "$GF_PATHS_CONFIG" && \
    cp "$GF_PATHS_HOME/conf/ldap.toml" /etc/grafana/ldap.toml && \
    chown -R grafana:grafana "$GF_PATHS_DATA" "$GF_PATHS_HOME/.aws" "$GF_PATHS_LOGS" "$GF_PATHS_PLUGINS" "$GF_PATHS_PROVISIONING" && \
    chmod -R 777 "$GF_PATHS_DATA" "$GF_PATHS_HOME/.aws" "$GF_PATHS_LOGS" "$GF_PATHS_PLUGINS" "$GF_PATHS_PROVISIONING"

# Note: the openshift build requires a name to reference the build container in the '--from' option instead of using the index '0' as is done upstream
COPY --from=builder /usr/bin/grafana-server /usr/bin/grafana-cli ./bin/
COPY public ./public
COPY tools ./tools
COPY tools/phantomjs/render.js ./tools/phantomjs/render.js

# Add the piechart panel plugin to a temporary location.  This allows it to be installed at container startup to a volume mounted plugins dir.
# Note: In the openshift build 'ADD' does not automatically extract the tar.gz file, so we have to use 'COPY' and then extract the files.
RUN mkdir "${GF_PATHS_HOME}/plugins"
COPY grafana-piechart-panel-1.3.9.tar.gz "${GF_PATHS_HOME}/plugins"
RUN pushd ${GF_PATHS_HOME}/plugins && \
    tar -xf grafana-piechart-panel-1.3.9.tar.gz && \
    rm grafana-piechart-panel-1.3.9.tar.gz && \
    popd

EXPOSE 3000

COPY ./packaging/docker/run.sh /run.sh

USER grafana
ENTRYPOINT [ "/run.sh" ]

LABEL \
        io.k8s.display-name="Grafana" \
        io.k8s.description="Grafana is an open source, feature rich metrics dashboard and graph editor for Graphite, Elasticsearch, OpenTSDB, Prometheus and InfluxDB." \
        io.openshift.tags="openshift" \
        maintainer="Frederic Branczyk <fbranczy@redhat.com>" \
        License="GPLv2+" \
        vendor="Red Hat" \
        name="openshift/ose-grafana" \
        com.redhat.component="grafana-container" \
        io.openshift.maintainer.product="OpenShift Container Platform" \
        io.openshift.maintainer.component="Monitoring" \
        version="v4.3.22" \
        release="202005201238" \
        io.openshift.build.commit.id="e2261e142909cb0fe70fc0996d7d375e57956589" \
        io.openshift.build.source-location="https://github.com/openshift/grafana" \
        io.openshift.build.commit.url="https://github.com/openshift/grafana/commit/e2261e142909cb0fe70fc0996d7d375e57956589"

