Skip to content

Instantly share code, notes, and snippets.

@goncalomb
Created May 12, 2019 15:40
Show Gist options
  • Save goncalomb/260530951a34cbad2732b80919c2b70f to your computer and use it in GitHub Desktop.
Save goncalomb/260530951a34cbad2732b80919c2b70f to your computer and use it in GitHub Desktop.
Startup script to create Grafana based kiosks.
#!/bin/bash
# Copyright (c) 2019 Gonçalo Baltazar <[email protected]>
# MIT License
# Grafana Dashboard Kiosk using Google Chrome (or Chromium)
# opens an independent google chrome instance with a custom
# extension that injects Authorization headers for grafana
# GRAFANA_KIOSK_URL and GRAFANA_KIOSK_API_KEY are required
# GRAFANA_KIOSK_URL="https://example.com/grafana/"
# GRAFANA_KIOSK_URL_PATH="/d/..."
# GRAFANA_KIOSK_API_KEY="(grafana api key)"
# GRAFANA_KIOSK_CHROME="google-chrome"
[ -z "$GRAFANA_KIOSK_URL" ] && echo "Missing 'GRAFANA_KIOSK_URL'." && exit 1
[ -z "$GRAFANA_KIOSK_URL_PATH" ] && GRAFANA_KIOSK_URL_PATH="/"
[ -z "$GRAFANA_KIOSK_API_KEY" ] && echo "Missing 'GRAFANA_KIOSK_API_KEY'." && exit 1
[ -z "$GRAFANA_KIOSK_CHROME" ] && GRAFANA_KIOSK_CHROME="google-chrome"
CHROME_USER_DATA=$(mktemp -d)
touch "$CHROME_USER_DATA/First Run"
CHROME_EXTENSION=$(mktemp -d)
cat > "$CHROME_EXTENSION/manifest.json" <<EOF
{
"name": "Grafana Kiosk",
"version": "1.0",
"manifest_version": 2,
"permissions": [
"webRequest",
"webRequestBlocking",
"$GRAFANA_KIOSK_URL/*"
],
"background": {
"scripts": ["background.js"]
}
}
EOF
cat > "$CHROME_EXTENSION/background.js" <<EOF
(function() {
const networkFilters = {
urls: [
"$GRAFANA_KIOSK_URL/*"
].map(url => url.replace(/:?\/+/g, (s) => s[0] == ":" ? s : "/"))
};
chrome.webRequest.onBeforeSendHeaders.addListener((details) => {
details.requestHeaders.push({
name: "Authorization",
value: "Bearer $GRAFANA_KIOSK_API_KEY"
});
return { requestHeaders: details.requestHeaders };
}, networkFilters, ["blocking", "requestHeaders"]);
}());
EOF
"$GRAFANA_KIOSK_CHROME" \
--user-data-dir="$CHROME_USER_DATA" \
--password-store=basic \
--load-extension="$CHROME_EXTENSION" \
--disable-extensions-except="$CHROME_EXTENSION" \
--disable-translate \
--start-maximized \
--kiosk \
"$GRAFANA_KIOSK_URL$GRAFANA_KIOSK_URL_PATH"
rm -rf "$CHROME_USER_DATA"
rm -rf "$CHROME_EXTENSION"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment