-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
dashboards.sh
executable file
·219 lines (177 loc) · 5.21 KB
/
dashboards.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env bash
# Author: Carlos Cuezva
# Created: January 2023
# Last updated: 03/03/2023
# Source: https://github.com/CarlosCuezva/dashboards-Grafana-Teslamate/blob/menu/dashboards.sh
#
# URL specifies the URL of the Grafana instance.
#
# TOKEN specifies the security key of the API, it's generated in Grafana.
#
# DASHBOARD_DIRECTORY represents the path to the directory where the JSON files corresponding to the dashboards exist.
#
# DESTINATION_DIRECTORY specifies the folder where the dashboards will be saved.
#
# DIRECTORY_UID specifies the unique UID of folder where the dashboards will be saved.
set -o errexit
main() {
local task=$1
case $task in
autoupdate) autoupdate;;
*) menu;;
esac
}
menu() {
clear
local get_current_version=$(get_current_version)
local get_last_release=$(get_last_release)
echo "
INSTALLED VERSION: ${get_current_version}
LAST RELEASE: ${get_last_release}
"
COLUMNS=12
PS3='Please, enter your choice: '
options=("Generate config file" "Download and install/update the latest version" "Install/update downloaded dashboards")
echo -e "What do you want to do?\n"
select opt in "${options[@]}" Quit
do
case $opt in
"Generate config file")
config_file
break
;;
"Download and install/update the latest version")
download "true"
restore "false"
break
;;
"Install/update downloaded dashboards")
restore "false"
break
;;
"Quit")
break
;;
*) echo "Invalid option \"$REPLY\"";;
esac
done
}
restore() {
local auto=$1
source ./config.sh
echo "
URL: $URL
DASHBOARDS_DIRECTORY: $DASHBOARDS_DIRECTORY
DESTINATION_DIRECTORY: $DESTINATION_DIRECTORY
"
create_folder
find "$DASHBOARDS_DIRECTORY" -type f -name \*.json -print0 |
while IFS= read -r -d '' dashboard_path; do
curl \
--silent --show-error --output /dev/null \
--location \
--request POST "$URL/api/dashboards/db" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $TOKEN" \
--data-raw "{
\"dashboard\": $(cat "$dashboard_path"),
\"folderUid\": \"$DIRECTORY_UID\", \
\"overwrite\": true,
\"inputs\": [
{
\"name\":\"DS_CLOUDWATCH\",
\"type\":\"datasource\",
\"pluginId\":\"cloudwatch\",
\"value\":\"Teslamate\"
}
]
}"
echo "RESTORED $(basename "$dashboard_path")"
done
if [ "$auto" = "false" ]; then
echo -e "\nInstallation/update process completed"
read -n 1 -s -r -p "Press any key to continue"
menu
fi
}
create_folder() {
curl \
--silent --show-error --output /dev/null \
--location \
--request POST "$URL/api/folders" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $TOKEN" \
--data-raw "{
\"uid\": \"$DIRECTORY_UID\",
\"title\": \"$DESTINATION_DIRECTORY\"
}"
}
config_file() {
filename="config.sh"
echo -e "\n"
read -p "Enter the Grafana URL (e.g. http://localhost:3000): " url
url="${url:=http://localhost:3000}"
read -p "Enter the relative or absolute path of the dashboards directory (e.g. ./dashboards): " dashboards_directory
dashboards_directory="${dashboards_directory:=./dashboards}"
read -p "Enter the Grafana TOKEN: " token
#TODO: check if token is valid
if [ -z "$token" ]
then
echo -e "\nGrafana TOKEN can't be empty"
read -n 1 -s -r -p "Press any key to continue"
else
[ -f "$filename" ] && rm $filename
cat <<EOT >> $filename
URL="$url"
TOKEN="$token"
DASHBOARDS_DIRECTORY="$dashboards_directory"
DESTINATION_DIRECTORY="Teslamate - Custom"
DIRECTORY_UID="AySq122Vh"
EOT
echo -e "\nFile \"$filename\" generated correctly."
read -n 1 -s -r -p "Press any key to continue"
fi
menu
}
download() {
local auto=$1
local GH_USER="CarlosCuezva"
local GH_REPO="dashboards-Grafana-Teslamate"
local GH_BRANCH=$(get_last_release)
echo -e "\n"
curl -sJL -o "${GH_REPO}-${GH_BRANCH}.tar.gz" "https://github.com/${GH_USER}/${GH_REPO}/archive/refs/tags/${GH_BRANCH}.tar.gz" && \
tar -xzf ./"${GH_REPO}-${GH_BRANCH}.tar.gz" > /dev/null && \
rm ./"${GH_REPO}-${GH_BRANCH}.tar.gz" && \
cp -Rf ./"${GH_REPO}-${GH_BRANCH:1}"/* ./ && \
rm -rf ./"${GH_REPO}-${GH_BRANCH:1}"
local version_file=./version
[ -f "$version_file" ] && rm $version_file
cat <<EOT >> $version_file
$GH_BRANCH
EOT
if [ "$auto" = "false" ]; then
echo -e "\nDownloaded version \"$GH_BRANCH\" successfully"
read -n 1 -s -r -p "Press any key to continue"
fi
}
get_current_version(){
local file=./version
if [ -f "$file" ]; then
local c=`cat $file`
echo "$c"
else
echo "unknown"
fi
}
get_last_release() {
local GH_USER="CarlosCuezva"
local GH_REPO="dashboards-Grafana-Teslamate"
echo $(curl -s https://api.github.com/repos/$GH_USER/$GH_REPO/releases/latest \
| grep "tag_name" \
| awk '{print substr($2, 2, length($2)-3)}');
}
autoupdate() {
download "true"
restore "true"
}
main "$@"