Skip to content

Commit

Permalink
Refactored into a standalone application (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusoe authored Feb 8, 2022
1 parent 608bcae commit 4f11d18
Show file tree
Hide file tree
Showing 33 changed files with 721 additions and 14,935 deletions.
21 changes: 2 additions & 19 deletions .github/workflows/build-prerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,23 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Get Package Version
id: package-version
uses: martinbeentjes/npm-get-version-action@master
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14.18.2'
- name: Update Version
run: npm version ${{ steps.package-version.outputs.current-version}}-${{ github.run_number }} --no-git-tag-version
- name: Install Plugin Frontend
run: |
yarn install --frozen-lockfile
yarn build
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: '1.17.3'
- name: Install Go Dependencies
run: |
go mod tidy
- name: Install Plugin Backend
- name: Install Application
uses: magefile/mage-action@v1
with:
version: latest
args: -v
- uses: papeloto/action-zip@v1
with:
files: dist/
dest: novatec-dashboardsync-datasource.zip
- uses: "marvinpinto/action-automatic-releases@latest"
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
automatic_release_tag: "latest"
prerelease: true
title: "Development Build ${{ steps.package-version.outputs.current-version}}-${{ github.run_number }}"
files: |
novatec-dashboardsync-datasource.zip
dist/*
29 changes: 2 additions & 27 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,23 @@ jobs:
- uses: actions/checkout@v2
- name: Set Release Version
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/v}" >> $GITHUB_ENV
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14.18.2'
- name: Update Version
run: npm version ${{ env.RELEASE_VERSION }} --no-git-tag-version
- name: Commit files
run: |
git config --local user.email "1217782+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add package.json
git commit -m "[skip ci] Update version to latest release version"
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: main
- name: Install Plugin Frontend
run: |
yarn install --frozen-lockfile
yarn build
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: '1.17.3'
- name: Install Go Dependencies
run: |
go mod tidy
- name: Install Plugin Backend
- name: Install Application
uses: magefile/mage-action@v1
with:
version: latest
args: -v
- uses: papeloto/action-zip@v1
with:
files: dist/
dest: novatec-dashboardsync-datasource.zip
- uses: "marvinpinto/action-automatic-releases@latest"
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
automatic_release_tag: "v${{ env.RELEASE_VERSION }}"
prerelease: false
title: "Version ${{ env.RELEASE_VERSION }}"
files: |
novatec-dashboardsync-datasource.zip
dist/*
3 changes: 0 additions & 3 deletions .prettierrc.js

This file was deleted.

18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "pkg",
"args": [
"-c", "../configuration.yml"//, "--dry-run"
]
}
]
}
75 changes: 68 additions & 7 deletions Magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,75 @@ package main

import (
"fmt"
// mage:import
build "github.com/grafana/grafana-plugin-sdk-go/build"
"os"
"path/filepath"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
// mg contains helpful utility functions, like Deps
)

// Hello prints a message (shows that you can define custom Mage targets).
func Hello() {
fmt.Println("hello plugin developer!")
// Default target to run when none is specified
// If not set, running mage will list available targets
// var Default = Build

func getExecutableName(os string, arch string) string {
exeName := fmt.Sprintf("%s_%s_%s", "grafana-dashboard-synchronizer", os, arch)
if os == "windows" {
exeName = fmt.Sprintf("%s.exe", exeName)
}
return exeName
}

// A build step that requires additional params, or platform specific steps for example
func buildPlatform(os string, arch string) error {
exeName := getExecutableName(os, arch)

envMap := make(map[string]string)

envMap["GOARCH"] = arch
envMap["GOOS"] = os

// TODO: Change to sh.RunWithV once available.
return sh.RunWith(envMap, "go", "build", "-o", filepath.Join("dist", exeName), "./pkg")
}

func BuildWindows() error {
return buildPlatform("windows", "amd64")
}

func BuildLinux() error {
return buildPlatform("linux", "amd64")
}

func BuildLinuxARM() error {
return buildPlatform("linux", "arm")
}

func BuildLinuxARM64() error {
return buildPlatform("linux", "arm64")
}

func BuildDarwin() error {
return buildPlatform("darwin", "amd64")
}

func BuildDarwinARM64() error {
return buildPlatform("darwin", "arm64")
}

func BuildAll() { //revive:disable-line
mg.Deps(Clean)

fmt.Println("Building all platforms...")

mg.Deps(BuildWindows, BuildLinux, BuildLinuxARM, BuildLinuxARM64, BuildDarwin, BuildDarwinARM64)
}

// Clean up after yourself
func Clean() {
fmt.Println("Cleaning...")
os.RemoveAll("dist")
}

// Default configures the default target.
var Default = build.BuildAll
var Default = BuildAll
119 changes: 65 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,87 @@
# Grafana Dashboard Synchronization Backend Plugin
# Grafana Dashboard Synchronizer

A Grafana backend plugin for automatic synchronization of dashboard between multiple Grafana instances.
A small CLI tool to do a tag-base automatic synchornization and backup Grafana of dashboards across multiple Grafana instances.

This application can be used to synchronize dashboards, using a Git repository, across multiple Grafana instances.
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.

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

## Getting started
## Usage

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

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

1. Install dependencies
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:

```bash
yarn install
```
$ ./grafana-dashboard-synchronizer --config /custom/configuration.yml

2. Build plugin in development mode or run in watch mode
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.

```bash
yarn dev
```
$ ./grafana-dashboard-synchronizer --dry-run

or
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:

```bash
yarn watch
```
$ ./grafana-dashboard-synchronizer
INFO[0000] Synchronizing Grafana dashboards...
...

3. Build plugin in production mode
compared to:

```bash
yarn build
```
$ ./grafana-dashboard-synchronizer --log-as-json
{"level":"info","msg":"Synchronizing Grafana dashboards...","time":"2022-02-08T16:41:26+01:00"}
...

### Configuration

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.

See the following configuration for available configuration options:

### Backend
- job-name: "example-job"
# API token to interact with the specified Grafana instance
grafana-token: "eyJrIjoiSEp4dzhGdVBxMUhBdm..."
# Base URL of the Grafana instance
grafana-url: "http://localhost:3000"
# SSH-URL of the Git repository to use
git-repository-url: "<GIT_REPOSITORY_URL>"
# Private key to use for authentication against the Git repository
private-key-file: "<PRIVATE_SSH_KEY>"

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:
# push (export) related configurations
push-configuration:
# whether to export dashboards
enable: true
# the branch to use for exporting dashboards
git-branch: "push-branch"
# only dashboards with match this pattern will be considered in the sync process
filter: ""
# the tag to determine which dashboards should be exported
tag-pattern: "agent"
# whether the sync-tag should be kept during exporting
push-tags: true

# pull (import) related configurations
pull-configuration:
# whether to import dashboards
enable: true
# the branch to use for importing dashboards
git-branch: "pull-branch"
# only dashboards with match this pattern will be considered in the sync process
filter: ""

## Development

### Getting started

1. Update and get dependencies:

```bash
go get -u github.com/grafana/grafana-plugin-sdk-go
go mod tidy
```

2. Build backend plugin binaries for Linux, Windows and Darwin:
2. Build binaries for Linux, Windows and Darwin:

```bash
mage -v
Expand All @@ -58,36 +93,12 @@ A data source backend plugin consists of both frontend and backend components.
mage -l
```

### Local Development

Set environment variables
```
PLUGIN_REPO = local path to cloned repo
GIT_SSH_KEY = path to private git sshkey
```
Build frontend and backend and start docker-compose
### Releasing the Application

```
docker-compose up
```
Under datasources the Grafana Dashboard Plugin Sync should be available now
## Releasing the Plugin
The release process of the plugin is automated using Github Actions.
The release process of the application is automated using Github Actions.
On each push to the `main` branch, a new prerelease is created and the corresponding commit is tagged "latest".
Old prereleases will be deleted.

To create a normal release, the commit that is used as the basis for the release must be tagged with the following format: `v*.*.*`.
After that, the release is built and created with the version number extracted from the tag.
Furthermore, a new commit is created, which sets the current version in the `main` branch to the version that has been released.
## Learn more
- [Build a data source backend plugin tutorial](https://grafana.com/tutorials/build-a-data-source-backend-plugin)
- [Grafana documentation](https://grafana.com/docs/)
- [Grafana Tutorials](https://grafana.com/tutorials/) - Grafana Tutorials are step-by-step guides that help you make the most of Grafana
- [Grafana UI Library](https://developers.grafana.com/ui) - UI components to help you build interfaces using Grafana Design System
- [Grafana plugin SDK for Go](https://grafana.com/docs/grafana/latest/developers/plugins/backend/grafana-plugin-sdk-for-go/)
29 changes: 29 additions & 0 deletions configuration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
##########################
# example configuration
##########################

# API token to interact with the specified Grafana instance
# Base URL of the Grafana instance
grafana-url: "http://localhost:3000"
# SSH-URL of the Git repository to use
private-key-file: "<PRIVATE_KEY_FILE>"
# Private key to use for authentication against the Git repository

# push (export) related configurations
push-configuration:
# whether to export dashboards
enable: true
# the branch to use for exporting dashboards
# only dashboards with match this pattern will be considered in the sync process
filter: ""
# the tag to determine which dashboards should be exported
# whether the sync-tag should be kept during exporting
push-tags: true

# pull (import) related configurations
pull-configuration:
# whether to import dashboards
enable: true
# the branch to use for importing dashboards
# only dashboards with match this pattern will be considered in the sync process
filter: ""
Loading

0 comments on commit 4f11d18

Please sign in to comment.