Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit 58ca149

Browse files
authored
Merge pull request #2 from Patrick-Eichhorn/deleteDashboardTag
Delete dashboard tag befor push to repo + extend docker-compose
2 parents 23f9402 + 68f95b3 commit 58ca149

File tree

4 files changed

+66
-35
lines changed

4 files changed

+66
-35
lines changed

docker-compose.yaml

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
1-
grafana:
1+
version: '3'
2+
services:
3+
# Grafana stage one
4+
grafana_stage_one:
25
image: grafana/grafana:8.1.2
3-
container_name: grafana-vhv
6+
container_name: grafana_stage_one
47
environment:
5-
- GF_PANELS_DISABLE_SANITIZE_HTML=TRUE
68
- GF_SECURITY_ADMIN_PASSWORD=demo
79
- GF_PATHS_PROVISIONING=/usr/share/grafana/custom/
810
- GF_PLUGINS_ALLOW_LOADING_UNSIGNED_PLUGINS=novatec-dashboardsync-datasource
911
- SSH_KNOWN_HOSTS=github.com
1012
- GF_ALLOWED_ORIGINS="http://localhost:3000"
1113
volumes:
12-
- ${PLUGIN_REPO}:/var/lib/grafana/plugins
13-
- ${GIT_SSH_KEY}:/usr/share/grafana/.GF_PANELS_DISABLE_SANITIZE_HTML
14+
- ${PLUGIN_REPO}:/var/lib/grafana/plugins/
15+
- ${GIT_SSH_KEY}:/usr/share/grafana/.ssh
1416
ports:
1517
- 3001:3000
18+
19+
# Grafana stage two
20+
grafana_stage_two:
21+
image: grafana/grafana:8.1.2
22+
container_name: grafana_stage_two
23+
environment:
24+
- GF_SECURITY_ADMIN_PASSWORD=demo
25+
- GF_PATHS_PROVISIONING=/usr/share/grafana/custom/
26+
- GF_PLUGINS_ALLOW_LOADING_UNSIGNED_PLUGINS=novatec-dashboardsync-datasource
27+
- SSH_KNOWN_HOSTS=github.com
28+
- GF_ALLOWED_ORIGINS="http://localhost:3000"
29+
volumes:
30+
- ${PLUGIN_REPO}:/var/lib/grafana/plugins/
31+
- ${GIT_SSH_KEY}:/usr/share/grafana/.ssh
32+
ports:
33+
- 3002:3000
34+

pkg/plugin/git_api.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ func (gitApi GitApi) PullRepo(repository git.Repository) {
154154
log.DefaultLogger.Error("worktree error" , "error", err)
155155
return
156156
} else {
157+
log.DefaultLogger.Debug("Pulling from Repo")
157158
err := w.Pull(&git.PullOptions{
158159
RemoteName: "origin",
159160
Auth: gitApi.authenticator,
@@ -186,30 +187,34 @@ func (gitApi GitApi) GetFileContent() map[string]map[string][]byte {
186187
fileMap := make(map[string]map[string][]byte)
187188

188189
for _, dir := range dirMap {
190+
// prepare fileMap for dir
191+
fileMap[dir] = make(map[string][]byte)
192+
189193
// read current in memory filesystem to get files
190194
files, err := gitApi.inMemoryFileSystem.ReadDir("./" + dir + "/")
191195
if err != nil {
192-
log.DefaultLogger.Error("inMemoryFileSystem error", "error", err)
196+
log.DefaultLogger.Error("inMemoryFileSystem ReadDir error", "error", err)
193197
return nil
194198
}
195199

196200
for _, file := range files {
197201

202+
log.DefaultLogger.Debug("file", "name", file.Name())
203+
198204
if file.IsDir() {
199205
continue
200206
}
201207

202-
src, err := gitApi.inMemoryFileSystem.Open(file.Name())
208+
src, err := gitApi.inMemoryFileSystem.Open("./" + dir + "/" + file.Name())
203209

204210
if err != nil {
205-
log.DefaultLogger.Error("inMemoryFileSystem error", "error", err)
211+
log.DefaultLogger.Error("inMemoryFileSystem Open error", "error", err)
206212
return nil
207213
}
208214
byteFile, err := ioutil.ReadAll(src)
209215
if err != nil {
210216
log.DefaultLogger.Error("read error", "error", err)
211217
} else {
212-
fileMap[dir] = make(map[string][]byte)
213218
fileMap[dir][file.Name()] = byteFile
214219
src.Close()
215220
}

pkg/plugin/grafana_api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (grafanaApi GrafanaApi) UpdateDashboardObjectByID(dashboard sdk.Board, fold
5151
func (grafanaApi GrafanaApi) CreateFolder(folderName string) int {
5252
folder := sdk.Folder{Title: folderName}
5353
folder, err := grafanaApi.grafanaClient.CreateFolder(context.Background(), folder)
54-
if err != nil {
54+
if err != nil && folderName != "General" {
5555
log.DefaultLogger.Error("get folders error", "error", err.Error())
5656
}
5757
return folder.ID
@@ -61,7 +61,7 @@ func (grafanaApi GrafanaApi) CreateFolder(folderName string) int {
6161
func (grafanaApi GrafanaApi) GetOrCreateFolderID(folderName string) int {
6262
folders, err := grafanaApi.grafanaClient.GetAllFolders(context.Background())
6363
if err != nil {
64-
log.DefaultLogger.Error("get folders error", "error", err.Error())
64+
log.DefaultLogger.Error("get all folders error", "error", err.Error())
6565
}
6666
for _, folder := range folders {
6767
if folder.Title == folderName {
@@ -87,7 +87,7 @@ func (grafanaApi GrafanaApi) CreateDashboardObjects(fileMap map[string]map[strin
8787
if err != nil {
8888
log.DefaultLogger.Error("set dashboard error", "error", err.Error())
8989
}
90-
log.DefaultLogger.Info("Dashboard created", "name", dashboardName)
90+
log.DefaultLogger.Debug("Dashboard created", "name", dashboardName)
9191
}
9292
}
9393
}

pkg/plugin/plugin.go

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (d *SampleDatasource) Dispose() {
4444
// datasource configuration page which allows users to verify that
4545
// a datasource is working as expected.
4646
func (d *SampleDatasource) CheckHealth(_ context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
47-
log.DefaultLogger.Info("CheckHealth called", "request", req)
47+
log.DefaultLogger.Debug("Backend called with following request", "request", req)
4848

4949
uiProperties := make(map[string]string)
5050
_ = json.Unmarshal(req.PluginContext.DataSourceInstanceSettings.JSONData, &uiProperties)
@@ -82,56 +82,63 @@ func (d *SampleDatasource) CheckHealth(_ context.Context, req *backend.CheckHeal
8282
return nil, err
8383
}
8484
gitApi.FetchRepo(*repository)
85+
gitApi.PullRepo(*repository)
8586
fileMap := gitApi.GetFileContent()
8687
grafanaApi.CreateDashboardObjects(fileMap)
8788
log.DefaultLogger.Info("Dashboards created")
8889
}
8990

9091
if push == "true" {
92+
log.DefaultLogger.Info("Push to git repo", "url", pushGitURL)
93+
94+
gitApi := NewGitApi(pushGitURL, privateKeyFilePath)
95+
repository, err := gitApi.CloneRepo()
96+
if err != nil {
97+
return nil, err
98+
}
99+
gitApi.FetchRepo(*repository)
100+
91101
dashboards, err := grafanaApi.SearchDashboardsWithTag(dashboardTag)
92102
if err != nil {
93103
log.DefaultLogger.Error("search dashboard", "error", err.Error())
94104
}
95105
for _, dashboard := range dashboards {
96-
// get raw Json
97-
dashboardJson, boardProperties, err := grafanaApi.GetRawDashboardByID(dashboard.UID)
98-
if err != nil {
99-
log.DefaultLogger.Error("get raw dashboard", "error", err.Error())
100-
}
101-
102-
// get folder name and id, needed for update processes and git folder structure
103-
folderName := boardProperties.FolderTitle
104-
folderId := boardProperties.FolderID
105-
106-
// get dashboard Object TODO: Verify if raw Json manipulation is faster
107-
dashboardObject, _, err := grafanaApi.GetDashboardObjectByID(dashboard.UID)
106+
// get dashboard Object and Properties
107+
dashboardObject, boardProperties, err := grafanaApi.GetDashboardObjectByID(dashboard.UID)
108108
if err != nil {
109109
log.DefaultLogger.Error("get dashboard", "error", err.Error())
110110
}
111111

112-
// delete Tag from dashboard
112+
// delete Tag from dashboard Object
113113
dashboardWithDeletedTag := grafanaApi.DeleteTagFromDashboardObjectByID(dashboardObject, dashboardTag)
114114

115-
// update dashboard with deleted Tag
115+
// get folder name and id, required for update processes and git folder structure
116+
folderName := boardProperties.FolderTitle
117+
folderId := boardProperties.FolderID
118+
119+
// update dashboard with deleted Tag in Grafana
116120
_, err = grafanaApi.UpdateDashboardObjectByID(dashboardWithDeletedTag, folderId)
117121
if err != nil {
118122
log.DefaultLogger.Error("update dashboard", "error", err.Error())
119123
}
120-
log.DefaultLogger.Debug("Dashboard preparation successfully ")
121124

122-
// Todo: If dashboard found, else skip git workflow
123-
gitApi := NewGitApi(pushGitURL, privateKeyFilePath)
124-
repository, err := gitApi.CloneRepo()
125+
// get raw Json Dashboard, required for import and export
126+
dashboardJson, _, err := grafanaApi.GetRawDashboardByID(dashboard.UID)
125127
if err != nil {
126-
return nil, err
128+
log.DefaultLogger.Error("get raw dashboard", "error", err.Error())
127129
}
128-
gitApi.FetchRepo(*repository)
130+
131+
log.DefaultLogger.Debug("Dashboard preparation successfully ")
129132
gitApi.AddFileWithContent(folderName+"/"+dashboardObject.Title+".json", string(dashboardJson))
133+
log.DefaultLogger.Debug("Dashboard added to in memory file system")
134+
}
135+
136+
if len(dashboards) > 0 {
130137
gitApi.CommitWorktree(*repository, dashboardTag)
131138
gitApi.PushRepo(*repository)
132-
133-
log.DefaultLogger.Info("Dashboard pushed successfully ")
134139
}
140+
141+
log.DefaultLogger.Info("Dashboard pushed successfully")
135142
}
136143

137144
return &backend.CheckHealthResult{

0 commit comments

Comments
 (0)