1#!/bin/sh
2##
3# gget
4# A script to get and install grafana versions
5# for usage information see "show_help" below.
6#
7
8latest=$(wget -O - 'https://raw.githubusercontent.com/grafana/grafana/main/latest.json' | jq -r '.stable')
9canary=$(wget -O - "https://grafana.com/api/grafana/versions" | jq ".items[0].version" | tr -d '"')
10
11show_help() {
12    echo "Usage: gget <version>"
13    echo ""
14    echo "where <version> can be:"
15    echo "  1) A version from https://grafana.com/grafana/download (ex x.y.z)"
16    echo "  2) latest (currently $latest)"
17    echo "  3) canary (currently $canary)"
18	echo ""
19	echo "  -h, --help: Display this help message"
20    echo ""
21	exit 0
22}
23
24opts=$(getopt -o h --long help -n 'gget' -- "$@")
25[ $? -eq 0 ] || {
26	show_help
27}
28
29eval set -- "$opts"
30while true; do
31	case "$1" in
32	-h | --help)
33        show_help
34		;;
35	--)
36		shift
37		break
38		;;
39	*)
40		break
41		;;
42	esac
43	shift
44done
45
46[ -z "$1" ] && show_help
47
48# Make sure the script is being run as root
49if [ $EUID -ne 0 ]; then
50  echo "This script must be run as root"
51  exit 1
52fi
53
54##
55# MAIN
56#
57# Enough setup, let's actually do something
58#
59version=$1
60if [ "$version" == "latest" ]; then
61  version="$latest"
62  wget -O - "https://dl.grafana.com/oss/release/grafana-${version}.linux-amd64.tar.gz" | tar -C /opt -zxf -
63elif [ "$version" == "canary" ]; then
64  version="$canary"
65  wget -O - "https://dl.grafana.com/oss/main/grafana-${version}.linux-amd64.tar.gz" | tar -C /opt -zxf -
66else
67  wget -O - "https://dl.grafana.com/oss/release/grafana-${version}.linux-amd64.tar.gz" | tar -C /opt -zxf -
68fi
69
70/bin/rm -rf /opt/grafana > /dev/null 2>&1 || true
71ln -s /opt/grafana-${version} /opt/grafana
72
73# nohup /opt/grafana/bin/grafana-server -config /opt/grafana/conf/defaults.ini -homepath /opt/grafana >/dev/null 2>&1 &
74