#!/bin/bash

# Some of the contents of this file came from here - https://github.com/kubernetes/heapster/blob/ed5baadf04ea9f8e48fc7d44dad63a63af34ff9b/grafana/run.sh
HEADER_CONTENT_TYPE="Content-Type: application/json"
HEADER_ACCEPT="Accept: application/json"

GRAFANA_USER=${DEFAULT_USER:-admin}
GRAFANA_PASSWD=${DEFAULT_USER_PASSWORD:-admin}
GRAFANA_PORT=${BIND_PORT:-3000}

INFLUXDB_HOST=${DRYCC_MONITOR_INFLUXAPI_SERVICE_HOST:-"drycc-monitor-influxapi.drycc.svc.cluster.local"}
INFLUXDB_PORT=${DRYCC_MONITOR_INFLUXAPI_SERVICE_PORT_TRANSPORT:-8086}
INFLUXDB_DATABASE=${INFLUXDB_DATABASE:-kubernetes}
INFLUXDB_USER=${INFLUXDB_USER:-admin}
INFLUXDB_PASSWORD=${INFLUXDB_PASSWORD:-admin}

DASHBOARD_LOCATION=${DASHBOARD_JSON_PATH:-"/usr/share/grafana/api/dashboards"}
# Create the dashboards directory
mkdir /usr/share/grafana/dashboards

echo "Building grafana.ini!"
./envtpl -in grafana.ini.tpl >> grafana.ini
echo "Finished building grafana config..."
echo "###########################################"
echo "###########################################"
cat /usr/share/grafana/grafana.ini
echo "###########################################"
echo "###########################################"

GF_PATHS_PLUGINS=${PLUGINS_PATH:-"/var/lib/grafana/plugins"}
if [ ! -z "${GF_INSTALL_PLUGINS}" ]; then
  echo "Installing Grafana plugins..."
  OLDIFS=$IFS
  IFS=','
  for plugin in ${GF_INSTALL_PLUGINS}; do
    echo "Installing ${plugin} ..."
    grafana-cli  --pluginsDir "${GF_PATHS_PLUGINS}" plugins install "${plugin}"
    echo ""
    echo "Done installing ${plugin}"
  done
  IFS=$OLDIFS
fi

set -m
echo "Starting Grafana in the background"
exec /usr/sbin/grafana-server -config /usr/share/grafana/grafana.ini -homepath /usr/share/grafana &
echo "Waiting for Grafana to come up..."
until curl -q --fail --output /dev/null --silent "http://${GRAFANA_USER}:${GRAFANA_PASSWD}@localhost:${GRAFANA_PORT}/api/org"; do
  printf "."
  sleep 2
done
echo "Grafana is up and running."

# Allow access to dashboards without having to log in
export GF_AUTH_ANONYMOUS_ENABLED=true
export GF_SERVER_HTTP_PORT=${GRAFANA_PORT}

BACKEND_ACCESS_MODE=${BACKEND_ACCESS_MODE:-proxy}
INFLUXDB_SERVICE_URL=${INFLUXDB_SERVICE_URL}
if [ -n "$INFLUXDB_SERVICE_URL" ]; then
  echo "Influxdb service URL is provided."
else
  INFLUXDB_SERVICE_URL="http://${INFLUXDB_HOST}:${INFLUXDB_PORT}"
fi

echo "Using the following URL for InfluxDB: ${INFLUXDB_SERVICE_URL}"
echo "Using the following backend access mode for InfluxDB: ${BACKEND_ACCESS_MODE}"

echo "Creating default influxdb datasource..."
curl -i -XPOST -H "${HEADER_ACCEPT}" -H "${HEADER_CONTENT_TYPE}" "http://${GRAFANA_USER}:${GRAFANA_PASSWD}@localhost:${GRAFANA_PORT}/api/datasources" -d '
{
  "name": "influxdb-datasource",
  "type": "influxdb",
  "access": "'"${BACKEND_ACCESS_MODE}"'",
  "isDefault": true,
  "url": "'"${INFLUXDB_SERVICE_URL}"'",
  "password": "'"${INFLUXDB_PASSWORD}"'",
  "user": "'"${INFLUXDB_USER}"'",
  "database": "'"${INFLUXDB_DATABASE}"'"
}'

echo ""
echo "Importing default dashboards..."
for filename in ${DASHBOARD_LOCATION}/*.json; do
  echo "Importing ${filename} ..."
  curl -i -XPOST --data "@${filename}" -H "${HEADER_ACCEPT}" -H "${HEADER_CONTENT_TYPE}" "http://${GRAFANA_USER}:${GRAFANA_PASSWD}@localhost:${GRAFANA_PORT}/api/dashboards/db"
  echo ""
  echo "Done importing ${filename}"
done

echo "Bringing Grafana back to the foreground"
fg
