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

Commit 4f11d18

Browse files
authored
Refactored into a standalone application (#22)
1 parent 608bcae commit 4f11d18

33 files changed

+721
-14935
lines changed

.github/workflows/build-prerelease.yml

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,23 @@ jobs:
88
runs-on: ubuntu-latest
99
steps:
1010
- uses: actions/checkout@v2
11-
- name: Get Package Version
12-
id: package-version
13-
uses: martinbeentjes/npm-get-version-action@master
14-
- name: Set up Node.js
15-
uses: actions/setup-node@v2
16-
with:
17-
node-version: '14.18.2'
18-
- name: Update Version
19-
run: npm version ${{ steps.package-version.outputs.current-version}}-${{ github.run_number }} --no-git-tag-version
20-
- name: Install Plugin Frontend
21-
run: |
22-
yarn install --frozen-lockfile
23-
yarn build
2411
- name: Setup Go
2512
uses: actions/setup-go@v2
2613
with:
2714
go-version: '1.17.3'
2815
- name: Install Go Dependencies
2916
run: |
3017
go mod tidy
31-
- name: Install Plugin Backend
18+
- name: Install Application
3219
uses: magefile/mage-action@v1
3320
with:
3421
version: latest
3522
args: -v
36-
- uses: papeloto/action-zip@v1
37-
with:
38-
files: dist/
39-
dest: novatec-dashboardsync-datasource.zip
4023
- uses: "marvinpinto/action-automatic-releases@latest"
4124
with:
4225
repo_token: "${{ secrets.GITHUB_TOKEN }}"
4326
automatic_release_tag: "latest"
4427
prerelease: true
4528
title: "Development Build ${{ steps.package-version.outputs.current-version}}-${{ github.run_number }}"
4629
files: |
47-
novatec-dashboardsync-datasource.zip
30+
dist/*

.github/workflows/build-release.yml

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,23 @@ jobs:
1010
- uses: actions/checkout@v2
1111
- name: Set Release Version
1212
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/v}" >> $GITHUB_ENV
13-
- name: Set up Node.js
14-
uses: actions/setup-node@v2
15-
with:
16-
node-version: '14.18.2'
17-
- name: Update Version
18-
run: npm version ${{ env.RELEASE_VERSION }} --no-git-tag-version
19-
- name: Commit files
20-
run: |
21-
git config --local user.email "1217782+github-actions[bot]@users.noreply.github.com"
22-
git config --local user.name "github-actions[bot]"
23-
git add package.json
24-
git commit -m "[skip ci] Update version to latest release version"
25-
- name: Push changes
26-
uses: ad-m/github-push-action@master
27-
with:
28-
github_token: ${{ secrets.GITHUB_TOKEN }}
29-
branch: main
30-
- name: Install Plugin Frontend
31-
run: |
32-
yarn install --frozen-lockfile
33-
yarn build
3413
- name: Setup Go
3514
uses: actions/setup-go@v2
3615
with:
3716
go-version: '1.17.3'
3817
- name: Install Go Dependencies
3918
run: |
4019
go mod tidy
41-
- name: Install Plugin Backend
20+
- name: Install Application
4221
uses: magefile/mage-action@v1
4322
with:
4423
version: latest
4524
args: -v
46-
- uses: papeloto/action-zip@v1
47-
with:
48-
files: dist/
49-
dest: novatec-dashboardsync-datasource.zip
5025
- uses: "marvinpinto/action-automatic-releases@latest"
5126
with:
5227
repo_token: "${{ secrets.GITHUB_TOKEN }}"
5328
automatic_release_tag: "v${{ env.RELEASE_VERSION }}"
5429
prerelease: false
5530
title: "Version ${{ env.RELEASE_VERSION }}"
5631
files: |
57-
novatec-dashboardsync-datasource.zip
32+
dist/*

.prettierrc.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

.vscode/launch.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch Package",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "auto",
12+
"program": "pkg",
13+
"args": [
14+
"-c", "../configuration.yml"//, "--dry-run"
15+
]
16+
}
17+
]
18+
}

Magefile.go

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,75 @@ package main
55

66
import (
77
"fmt"
8-
// mage:import
9-
build "github.com/grafana/grafana-plugin-sdk-go/build"
8+
"os"
9+
"path/filepath"
10+
11+
"github.com/magefile/mage/mg"
12+
"github.com/magefile/mage/sh"
13+
// mg contains helpful utility functions, like Deps
1014
)
1115

12-
// Hello prints a message (shows that you can define custom Mage targets).
13-
func Hello() {
14-
fmt.Println("hello plugin developer!")
16+
// Default target to run when none is specified
17+
// If not set, running mage will list available targets
18+
// var Default = Build
19+
20+
func getExecutableName(os string, arch string) string {
21+
exeName := fmt.Sprintf("%s_%s_%s", "grafana-dashboard-synchronizer", os, arch)
22+
if os == "windows" {
23+
exeName = fmt.Sprintf("%s.exe", exeName)
24+
}
25+
return exeName
26+
}
27+
28+
// A build step that requires additional params, or platform specific steps for example
29+
func buildPlatform(os string, arch string) error {
30+
exeName := getExecutableName(os, arch)
31+
32+
envMap := make(map[string]string)
33+
34+
envMap["GOARCH"] = arch
35+
envMap["GOOS"] = os
36+
37+
// TODO: Change to sh.RunWithV once available.
38+
return sh.RunWith(envMap, "go", "build", "-o", filepath.Join("dist", exeName), "./pkg")
39+
}
40+
41+
func BuildWindows() error {
42+
return buildPlatform("windows", "amd64")
43+
}
44+
45+
func BuildLinux() error {
46+
return buildPlatform("linux", "amd64")
47+
}
48+
49+
func BuildLinuxARM() error {
50+
return buildPlatform("linux", "arm")
51+
}
52+
53+
func BuildLinuxARM64() error {
54+
return buildPlatform("linux", "arm64")
55+
}
56+
57+
func BuildDarwin() error {
58+
return buildPlatform("darwin", "amd64")
59+
}
60+
61+
func BuildDarwinARM64() error {
62+
return buildPlatform("darwin", "arm64")
63+
}
64+
65+
func BuildAll() { //revive:disable-line
66+
mg.Deps(Clean)
67+
68+
fmt.Println("Building all platforms...")
69+
70+
mg.Deps(BuildWindows, BuildLinux, BuildLinuxARM, BuildLinuxARM64, BuildDarwin, BuildDarwinARM64)
71+
}
72+
73+
// Clean up after yourself
74+
func Clean() {
75+
fmt.Println("Cleaning...")
76+
os.RemoveAll("dist")
1577
}
1678

17-
// Default configures the default target.
18-
var Default = build.BuildAll
79+
var Default = BuildAll

README.md

Lines changed: 65 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,87 @@
1-
# Grafana Dashboard Synchronization Backend Plugin
1+
# Grafana Dashboard Synchronizer
22

3-
A Grafana backend plugin for automatic synchronization of dashboard between multiple Grafana instances.
3+
A small CLI tool to do a tag-base automatic synchornization and backup Grafana of dashboards across multiple Grafana instances.
4+
5+
This application can be used to synchronize dashboards, using a Git repository, across multiple Grafana instances.
6+
A possible use case is: push Grafana dashboards from one Grafana instance to a Git repository and import them into another Grafana instance. In addition, users can use tags to determine for themselves when a dashboard should be synchronized.
47

5-
This plugin can be used to synchronize dashboards via a Git repository.
6-
A possible use case is: One Grafana instance is using the plugin to push dashboards to a Git repository,
7-
another Grafana instance is using it to pull the dashboards.
88
As an example this is useful to stage dashboards from "dev" to "prod" environments.
99

10-
## Getting started
10+
## Usage
1111

12-
A data source backend plugin consists of both frontend and backend components.
12+
The application can be used as follows:
1313

14-
### Frontend
14+
$ ./grafana-dashboard-synchronizer [options]
1515

16-
1. Install dependencies
16+
By default, the application will use a configuration file named `configuration.yml` next to the binary. A custom configuration file can be used using the `--config` or `-c` option flag:
1717

18-
```bash
19-
yarn install
20-
```
18+
$ ./grafana-dashboard-synchronizer --config /custom/configuration.yml
2119

22-
2. Build plugin in development mode or run in watch mode
20+
In addition, a dry-run flag can be used. When the `--dry-run` flag is used, the application does not perform any modifications. This can be useful when testing what changes would be made.
2321

24-
```bash
25-
yarn dev
26-
```
22+
$ ./grafana-dashboard-synchronizer --dry-run
2723

28-
or
24+
By default, the application logs in an easy-to-read text format. With the `--log-as-json` flag, the application generates logs in JSON format, which is convenient if the logs are processed by other tools such as Logstash:
2925

30-
```bash
31-
yarn watch
32-
```
26+
$ ./grafana-dashboard-synchronizer
27+
INFO[0000] Synchronizing Grafana dashboards...
28+
...
3329

34-
3. Build plugin in production mode
30+
compared to:
3531

36-
```bash
37-
yarn build
38-
```
32+
$ ./grafana-dashboard-synchronizer --log-as-json
33+
{"level":"info","msg":"Synchronizing Grafana dashboards...","time":"2022-02-08T16:41:26+01:00"}
34+
...
35+
36+
### Configuration
37+
38+
The configuration file can contain multiple jobs, which will be sequentially executed. Furthermore, the push (export) step of a job is executed before its pull (import) step.
39+
40+
See the following configuration for available configuration options:
3941

40-
### Backend
42+
- job-name: "example-job"
43+
# API token to interact with the specified Grafana instance
44+
grafana-token: "eyJrIjoiSEp4dzhGdVBxMUhBdm..."
45+
# Base URL of the Grafana instance
46+
grafana-url: "http://localhost:3000"
47+
# SSH-URL of the Git repository to use
48+
git-repository-url: "<GIT_REPOSITORY_URL>"
49+
# Private key to use for authentication against the Git repository
50+
private-key-file: "<PRIVATE_SSH_KEY>"
4151

42-
1. Update [Grafana plugin SDK for Go](https://grafana.com/docs/grafana/latest/developers/plugins/backend/grafana-plugin-sdk-for-go/) dependency to the latest minor version:
52+
# push (export) related configurations
53+
push-configuration:
54+
# whether to export dashboards
55+
enable: true
56+
# the branch to use for exporting dashboards
57+
git-branch: "push-branch"
58+
# only dashboards with match this pattern will be considered in the sync process
59+
filter: ""
60+
# the tag to determine which dashboards should be exported
61+
tag-pattern: "agent"
62+
# whether the sync-tag should be kept during exporting
63+
push-tags: true
64+
65+
# pull (import) related configurations
66+
pull-configuration:
67+
# whether to import dashboards
68+
enable: true
69+
# the branch to use for importing dashboards
70+
git-branch: "pull-branch"
71+
# only dashboards with match this pattern will be considered in the sync process
72+
filter: ""
73+
74+
## Development
75+
76+
### Getting started
77+
78+
1. Update and get dependencies:
4379

4480
```bash
45-
go get -u github.com/grafana/grafana-plugin-sdk-go
4681
go mod tidy
4782
```
4883

49-
2. Build backend plugin binaries for Linux, Windows and Darwin:
84+
2. Build binaries for Linux, Windows and Darwin:
5085

5186
```bash
5287
mage -v
@@ -58,36 +93,12 @@ A data source backend plugin consists of both frontend and backend components.
5893
mage -l
5994
```
6095

61-
### Local Development
62-
63-
Set environment variables
64-
```
65-
PLUGIN_REPO = local path to cloned repo
66-
GIT_SSH_KEY = path to private git sshkey
67-
```
68-
69-
Build frontend and backend and start docker-compose
96+
### Releasing the Application
7097

71-
```
72-
docker-compose up
73-
```
74-
75-
Under datasources the Grafana Dashboard Plugin Sync should be available now
76-
77-
## Releasing the Plugin
78-
79-
The release process of the plugin is automated using Github Actions.
98+
The release process of the application is automated using Github Actions.
8099
On each push to the `main` branch, a new prerelease is created and the corresponding commit is tagged "latest".
81100
Old prereleases will be deleted.
82101

83102
To create a normal release, the commit that is used as the basis for the release must be tagged with the following format: `v*.*.*`.
84103
After that, the release is built and created with the version number extracted from the tag.
85104
Furthermore, a new commit is created, which sets the current version in the `main` branch to the version that has been released.
86-
87-
## Learn more
88-
89-
- [Build a data source backend plugin tutorial](https://grafana.com/tutorials/build-a-data-source-backend-plugin)
90-
- [Grafana documentation](https://grafana.com/docs/)
91-
- [Grafana Tutorials](https://grafana.com/tutorials/) - Grafana Tutorials are step-by-step guides that help you make the most of Grafana
92-
- [Grafana UI Library](https://developers.grafana.com/ui) - UI components to help you build interfaces using Grafana Design System
93-
- [Grafana plugin SDK for Go](https://grafana.com/docs/grafana/latest/developers/plugins/backend/grafana-plugin-sdk-for-go/)

configuration.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
##########################
2+
# example configuration
3+
##########################
4+
5+
# API token to interact with the specified Grafana instance
6+
# Base URL of the Grafana instance
7+
grafana-url: "http://localhost:3000"
8+
# SSH-URL of the Git repository to use
9+
private-key-file: "<PRIVATE_KEY_FILE>"
10+
# Private key to use for authentication against the Git repository
11+
12+
# push (export) related configurations
13+
push-configuration:
14+
# whether to export dashboards
15+
enable: true
16+
# the branch to use for exporting dashboards
17+
# only dashboards with match this pattern will be considered in the sync process
18+
filter: ""
19+
# the tag to determine which dashboards should be exported
20+
# whether the sync-tag should be kept during exporting
21+
push-tags: true
22+
23+
# pull (import) related configurations
24+
pull-configuration:
25+
# whether to import dashboards
26+
enable: true
27+
# the branch to use for importing dashboards
28+
# only dashboards with match this pattern will be considered in the sync process
29+
filter: ""

0 commit comments

Comments
 (0)