blob: a6e14e1627600a1a14e54e1681212870a9713048 [file] [log] [blame]
Scott Baker69654392021-09-17 13:50:16 -07001#!/bin/bash
2# SPDX-FileCopyrightText: 2021-present Open Networking Foundation <info@opennetworking.org>
3#
4# SPDX-License-Identifier: LicenseRef-ONF-Member-Only-1.0
5
6# script to create Grafana Orgs
7# Usage:
8# grafana-create-orgs.sh <ADMINUSER> <ADMINPASS> <umbrella-chart-name> <grafana-server> <dashboard-folder> orgs...
9# where org is a quoted string containing org name and then in square brackets a list of vcs
10# e.g. "acme[acme-chicago-robots acme-chicago-cameras]"
11set -e
12#set -x
13set -o pipefail
14set -u
15
16if [ "$#" -lt 6 ]; then
17 echo "At least 6 args are needed. Got $#"
18 exit 1
19fi
20ADMINUSER=$1
21ADMINPASS=$2
22BASE=$3
23SOURCE=$BASE-prometheus-server
24SERVICE=$4
25DASHBOARDS=$5
26shift
27shift
28shift
29shift
30shift
31for orgWithVcs in "$@"
32do
33 ORGASCII=${orgWithVcs%%map\[*\]} # Drop the [*] off the end
34 echo "Creating $orgWithVcs as $ORGASCII"
35 VCSLIST=${orgWithVcs##${ORGASCII}map\[*\]\ vcs:\[} # Drop everything off the front until "] vcs:["
36 VCSLIST=${VCSLIST%\]\]} # Drop the ]] off the end
37 DGLIST=${orgWithVcs##${ORGASCII}map\[devicegroup:\[} # Drop everything off the front until "devicegroup:[map["
38 DGLIST=${DGLIST%\]\ vcs:*\]\]}
39 DGLIST1=${DGLIST//" map["/";map["} # Replace all occurrence of " map["
40 IFS=';' read -r -a DGARRAY <<< $DGLIST1
41 for idx in ${!DGARRAY[@]}; do
42 DGARRAY[$idx]=${DGARRAY[$idx]// /;} # Replace all instances of space with ;
43 done
44 SUCCESS=-1
45 ORGID=-1
46 # Commented out for the moment - keeping everything in the Main Org. - see aether-roc-gui/docs/grafana.md
47 # echo "Calling /usr/bin/curl -H "Content-Type: application/json" -d '{"name":"$ORGASCII"}' http://$ADMINUSER:####@$SERVICE/api/orgs"
48 # while [ $SUCCESS -ne 0 ];
49 # do
50 # DATA={\"name\":\"$ORGASCII\"}
51 # echo "Creating Org $ORGASCII"
52 # /usr/bin/curl -o /tmp/curlout -H "Content-Type: application/json" -d "$DATA" http://$ADMINUSER:$ADMINPASS@$SERVICE/api/orgs
53 # SUCCESS=`echo $?`
54 # echo "SUCCESS $SUCCESS"
55 # if [ $SUCCESS -ne 0 ]
56 # then
57 # sleep $SLEEP
58 # echo "Waiting $SLEEP seconds for Grafana to start"
59 # else
60 # ORGID=$(grep -o "[0-9]*" /tmp/curlout)
61 # echo "Successful! Result $ORGID"
62 # fi
63 # done
64
65 # echo "Calling /api/user/using/$ORGID"
66 # /usr/bin/curl -s -X POST http://$ADMINUSER:$ADMINPASS@$SERVICE/api/user/using/$ORGID
67 # SUCCESS=`echo $?`
68 # echo "SUCCESS $SUCCESS"
69
70 echo "Creating folder in $ORGASCII"
71 FOLDER={\"uid\":\"$ORGASCII\",\"title\":\"$ORGASCII\"}
72 /usr/bin/curl -o /tmp/curlout -H "Content-Type: application/json" -d "$FOLDER" http://$ADMINUSER:$ADMINPASS@$SERVICE/api/folders
73 SUCCESS="$?"
74 echo "SUCCESS $SUCCESS"
75 cat /tmp/curlout
76
77 echo "Creating datasource in $ORGASCII"
78 DATASOURCE={\"name\":\"datasource-$ORGASCII\",\"type\":\"prometheus\",\"url\":\"http://$SOURCE\",\"access\":\"proxy\",\"basicAuth\":false}
79 /usr/bin/curl -s -o /tmp/curlout -H "Content-Type: application/json" -d "$DATASOURCE" http://$ADMINUSER:$ADMINPASS@$SERVICE/api/datasources
80 SUCCESS=`echo $?`
81 echo "SUCCESS $SUCCESS"
82 cat /tmp/curlout
83
84 echo "now create Dashboards with "$VCSLIST
85 grafana-create-vcs.sh $ADMINUSER $ADMINPASS $SERVICE $DASHBOARDS $ORGASCII $VCSLIST
86 grafana-create-device-group.sh $ADMINUSER $ADMINPASS $SERVICE $DASHBOARDS $ORGASCII $DGARRAY
87
88done
89