#!/bin/bash -eu
# Copyright (c) 2018 Grafana Labs.
# Copyright (c) 2020-2021 Red Hat.
. /etc/sysconfig/grafana-server
# setup permissions at the first start of the container with a volume mount
if [ ! -f "${DATA_DIR}/grafana.db" ]; then
touch "${DATA_DIR}/grafana.db"
chmod 640 "${DATA_DIR}/grafana.db"
fi
if [ ! -d "${PLUGINS_DIR}" ]; then
mkdir "${PLUGINS_DIR}"
fi
# only for upgrades: delete the old plugin directory inside the volume
if [ -d "${PLUGINS_DIR}/grafana-pcp" ]; then
rm -rf "${PLUGINS_DIR}/grafana-pcp"
fi
if [ -d "${PLUGINS_DIR}/performancecopilot-pcp-app" ] && [ ! -L "${PLUGINS_DIR}/performancecopilot-pcp-app" ]; then
rm -rf "${PLUGINS_DIR}/performancecopilot-pcp-app"
fi
# /var/lib/grafana is mounted as a volume to persist the grafana.db database and any additional installed plugins
# let's place a symlink (in the volume) to the actual location of the grafana-pcp plugin (not in the volume)
ln -sf /usr/share/performancecopilot-pcp-app "${PLUGINS_DIR}"
# install Grafana plugins listed in the GF_INSTALL_PLUGINS environment variable
if [ -n "${GF_INSTALL_PLUGINS}" ]; then
OLDIFS=$IFS
IFS=','
for plugin in ${GF_INSTALL_PLUGINS}; do
IFS=$OLDIFS
if [[ $plugin =~ .*\;.* ]]; then
pluginUrl=$(echo "$plugin" | cut -d';' -f 1)
pluginWithoutUrl=$(echo "$plugin" | cut -d';' -f 2)
grafana-cli --pluginUrl "${pluginUrl}" plugins install "${pluginWithoutUrl}"
else
grafana-cli plugins install "${plugin}"
fi
done
fi
exec /usr/sbin/grafana-server \
--homepath="${GRAFANA_HOME}" \
--config="${CONF_FILE}" \
--packaging="docker" \
cfg:default.log.mode="console" \
cfg:default.paths.logs="${LOG_DIR}" \
cfg:default.paths.data="${DATA_DIR}" \
cfg:default.paths.plugins="${PLUGINS_DIR}" \
cfg:default.paths.provisioning="${PROVISIONING_CFG_DIR}" \
"$@"