2017-11-30 10:43:28 -06:00
|
|
|
package dashboards
|
2017-11-23 04:29:06 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-12-27 09:32:39 -06:00
|
|
|
"errors"
|
2017-11-23 04:29:06 -06:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-12-01 07:20:52 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
|
|
|
|
2017-11-23 04:29:06 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
)
|
|
|
|
|
2017-12-27 08:04:32 -06:00
|
|
|
var (
|
|
|
|
checkDiskForChangesInterval time.Duration = time.Second * 3
|
2017-12-27 09:32:39 -06:00
|
|
|
|
|
|
|
ErrFolderNameMissing error = errors.New("Folder name missing")
|
2017-12-27 08:04:32 -06:00
|
|
|
)
|
|
|
|
|
2017-11-23 04:29:06 -06:00
|
|
|
type fileReader struct {
|
2018-01-23 13:22:04 -06:00
|
|
|
Cfg *DashboardsAsConfig
|
|
|
|
Path string
|
|
|
|
log log.Logger
|
|
|
|
dashboardRepo dashboards.Repository
|
|
|
|
cache *dashboardCache
|
2018-01-25 08:25:07 -06:00
|
|
|
createWalk func(fr *fileReader, folderId int64, provisionedDashboards map[string]*models.DashboardProvisioning, filesOnDisk map[string]bool) filepath.WalkFunc
|
2017-11-23 04:29:06 -06:00
|
|
|
}
|
|
|
|
|
2017-12-05 06:57:39 -06:00
|
|
|
func NewDashboardFileReader(cfg *DashboardsAsConfig, log log.Logger) (*fileReader, error) {
|
2018-01-17 07:33:51 -06:00
|
|
|
var path string
|
|
|
|
path, ok := cfg.Options["path"].(string)
|
2017-11-23 04:29:06 -06:00
|
|
|
if !ok {
|
2018-01-17 07:33:51 -06:00
|
|
|
path, ok = cfg.Options["folder"].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Failed to load dashboards. path param is not a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warn("[Deprecated] The folder property is deprecated. Please use path instead.")
|
2017-11-23 04:29:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
|
|
log.Error("Cannot read directory", "error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &fileReader{
|
2018-01-23 13:22:04 -06:00
|
|
|
Cfg: cfg,
|
|
|
|
Path: path,
|
|
|
|
log: log,
|
|
|
|
dashboardRepo: dashboards.GetRepository(),
|
|
|
|
cache: NewDashboardCache(),
|
|
|
|
createWalk: createWalkFn,
|
2017-11-23 04:29:06 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 07:01:10 -06:00
|
|
|
func (fr *fileReader) ReadAndListen(ctx context.Context) error {
|
2017-12-27 09:32:39 -06:00
|
|
|
if err := fr.startWalkingDisk(); err != nil {
|
2017-11-23 04:29:06 -06:00
|
|
|
fr.log.Error("failed to search for dashboards", "error", err)
|
|
|
|
}
|
|
|
|
|
2018-01-23 05:28:56 -06:00
|
|
|
ticker := time.NewTicker(checkDiskForChangesInterval)
|
|
|
|
|
2017-12-05 06:58:56 -06:00
|
|
|
running := false
|
|
|
|
|
2017-11-23 04:29:06 -06:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2018-01-23 05:28:56 -06:00
|
|
|
if !running { // avoid walking the filesystem in parallel. in-case fs is very slow.
|
2017-12-05 06:58:56 -06:00
|
|
|
running = true
|
|
|
|
go func() {
|
2017-12-27 09:32:39 -06:00
|
|
|
if err := fr.startWalkingDisk(); err != nil {
|
|
|
|
fr.log.Error("failed to search for dashboards", "error", err)
|
|
|
|
}
|
2017-12-05 06:58:56 -06:00
|
|
|
running = false
|
|
|
|
}()
|
|
|
|
}
|
2017-11-23 04:29:06 -06:00
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-27 09:32:39 -06:00
|
|
|
func (fr *fileReader) startWalkingDisk() error {
|
2017-11-23 04:29:06 -06:00
|
|
|
if _, err := os.Stat(fr.Path); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-28 06:37:02 -06:00
|
|
|
folderId, err := getOrCreateFolderId(fr.Cfg, fr.dashboardRepo)
|
2017-12-27 09:32:39 -06:00
|
|
|
if err != nil && err != ErrFolderNameMissing {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-24 08:22:03 -06:00
|
|
|
byPath, err := getProvisionedDashboardByPath(fr.dashboardRepo, fr.Cfg.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-25 08:25:07 -06:00
|
|
|
filesFoundOnDisk := map[string]bool{}
|
|
|
|
|
|
|
|
err = filepath.Walk(fr.Path, fr.createWalk(fr, folderId, byPath, filesFoundOnDisk))
|
|
|
|
|
|
|
|
//delete dashboards without files
|
|
|
|
var dashboardToDelete []int64
|
|
|
|
for path, provisioningData := range byPath {
|
|
|
|
_, existsInDatabase := filesFoundOnDisk[path]
|
|
|
|
if !existsInDatabase {
|
|
|
|
dashboardToDelete = append(dashboardToDelete, provisioningData.DashboardId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dashboardId := range dashboardToDelete {
|
|
|
|
fr.log.Debug("deleting provisioned dashboard. missing on disk", "id", dashboardId)
|
|
|
|
cmd := &models.DeleteDashboardCommand{OrgId: fr.Cfg.OrgId, Id: dashboardId}
|
|
|
|
err := bus.Dispatch(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-01-24 08:22:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func getProvisionedDashboardByPath(repo dashboards.Repository, name string) (map[string]*models.DashboardProvisioning, error) {
|
|
|
|
arr, err := repo.GetProvisionedDashboardData(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
byPath := map[string]*models.DashboardProvisioning{}
|
|
|
|
for _, pd := range arr {
|
|
|
|
byPath[pd.ExternalId] = pd
|
|
|
|
}
|
|
|
|
|
|
|
|
return byPath, nil
|
2017-12-27 08:04:32 -06:00
|
|
|
}
|
|
|
|
|
2017-12-28 06:37:02 -06:00
|
|
|
func getOrCreateFolderId(cfg *DashboardsAsConfig, repo dashboards.Repository) (int64, error) {
|
2017-12-27 09:32:39 -06:00
|
|
|
if cfg.Folder == "" {
|
|
|
|
return 0, ErrFolderNameMissing
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &models.GetDashboardQuery{Slug: models.SlugifyTitle(cfg.Folder), OrgId: cfg.OrgId}
|
|
|
|
err := bus.Dispatch(cmd)
|
|
|
|
|
|
|
|
if err != nil && err != models.ErrDashboardNotFound {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// dashboard folder not found. create one.
|
|
|
|
if err == models.ErrDashboardNotFound {
|
2018-01-23 05:28:56 -06:00
|
|
|
dash := &dashboards.SaveDashboardDTO{}
|
2017-12-27 09:32:39 -06:00
|
|
|
dash.Dashboard = models.NewDashboard(cfg.Folder)
|
|
|
|
dash.Dashboard.IsFolder = true
|
|
|
|
dash.Overwrite = true
|
|
|
|
dash.OrgId = cfg.OrgId
|
|
|
|
dbDash, err := repo.SaveDashboard(dash)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbDash.Id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cmd.Result.IsFolder {
|
2018-01-23 05:28:56 -06:00
|
|
|
return 0, fmt.Errorf("got invalid response. expected folder, found dashboard")
|
2017-12-27 09:32:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return cmd.Result.Id, nil
|
|
|
|
}
|
|
|
|
|
2018-01-18 05:04:12 -06:00
|
|
|
func resolveSymlink(fileinfo os.FileInfo, path string) (os.FileInfo, error) {
|
|
|
|
checkFilepath, err := filepath.EvalSymlinks(path)
|
|
|
|
if path != checkFilepath {
|
|
|
|
path = checkFilepath
|
|
|
|
fi, err := os.Lstat(checkFilepath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fi, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileinfo, err
|
|
|
|
}
|
|
|
|
|
2018-01-25 08:25:07 -06:00
|
|
|
func createWalkFn(fr *fileReader, folderId int64, provisionedDashboards map[string]*models.DashboardProvisioning, filesOnDisk map[string]bool) filepath.WalkFunc {
|
2017-12-27 08:04:32 -06:00
|
|
|
return func(path string, fileInfo os.FileInfo, err error) error {
|
2017-11-23 04:29:06 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-18 05:04:12 -06:00
|
|
|
isValid, err := validateWalkablePath(fileInfo)
|
|
|
|
if !isValid {
|
|
|
|
return err
|
2017-11-23 04:29:06 -06:00
|
|
|
}
|
|
|
|
|
2018-01-18 05:04:12 -06:00
|
|
|
resolvedFileInfo, err := resolveSymlink(fileInfo, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-01-17 08:47:44 -06:00
|
|
|
}
|
|
|
|
|
2018-01-25 08:25:07 -06:00
|
|
|
// mark file as provisioned
|
|
|
|
filesOnDisk[path] = true
|
|
|
|
|
2017-12-27 09:32:39 -06:00
|
|
|
cachedDashboard, exist := fr.cache.getCache(path)
|
2018-01-18 05:04:12 -06:00
|
|
|
if exist && cachedDashboard.UpdatedAt == resolvedFileInfo.ModTime() {
|
2017-11-23 04:29:06 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-27 09:32:39 -06:00
|
|
|
dash, err := fr.readDashboardFromFile(path, folderId)
|
2017-11-23 04:29:06 -06:00
|
|
|
if err != nil {
|
|
|
|
fr.log.Error("failed to load dashboard from ", "file", path, "error", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-24 08:22:03 -06:00
|
|
|
var dbDashboard *models.Dashboard
|
2018-01-25 08:25:07 -06:00
|
|
|
query := &models.GetDashboardQuery{}
|
2018-01-24 08:22:03 -06:00
|
|
|
provisionedData, allReadyProvisioned := provisionedDashboards[path]
|
|
|
|
|
|
|
|
if allReadyProvisioned {
|
2018-01-25 08:25:07 -06:00
|
|
|
dash.Dashboard.SetId(provisionedData.DashboardId)
|
|
|
|
|
|
|
|
query.Id = provisionedData.DashboardId
|
2018-01-24 08:22:03 -06:00
|
|
|
} else {
|
|
|
|
if dash.Dashboard.Id != 0 {
|
|
|
|
fr.log.Error("Cannot provision dashboard. Please remove the id property from the json file")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-25 08:25:07 -06:00
|
|
|
query.Slug = dash.Dashboard.Slug
|
2018-01-15 05:21:14 -06:00
|
|
|
}
|
2017-12-13 18:52:07 -06:00
|
|
|
|
2018-01-25 08:25:07 -06:00
|
|
|
err = bus.Dispatch(query)
|
|
|
|
dbDashboard = query.Result
|
2017-11-23 04:29:06 -06:00
|
|
|
|
2018-01-15 05:21:14 -06:00
|
|
|
// if we don't have the dashboard in the db, save it!
|
2017-11-23 04:29:06 -06:00
|
|
|
if err == models.ErrDashboardNotFound {
|
|
|
|
fr.log.Debug("saving new dashboard", "file", path)
|
2018-01-24 08:22:03 -06:00
|
|
|
err = saveDashboard(fr, path, dash)
|
2017-12-01 06:50:47 -06:00
|
|
|
return err
|
2017-11-23 04:29:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fr.log.Error("failed to query for dashboard", "slug", dash.Dashboard.Slug, "error", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-01 08:10:48 -06:00
|
|
|
// break if db version is newer then fil version
|
2018-01-24 08:22:03 -06:00
|
|
|
if dbDashboard.Updated.Unix() >= resolvedFileInfo.ModTime().Unix() {
|
2017-11-23 04:29:06 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-01 08:10:48 -06:00
|
|
|
fr.log.Debug("loading dashboard from disk into database.", "file", path)
|
2018-01-24 08:22:03 -06:00
|
|
|
err = saveDashboard(fr, path, dash)
|
2018-01-15 05:21:14 -06:00
|
|
|
|
2017-12-01 06:50:47 -06:00
|
|
|
return err
|
2017-12-27 08:04:32 -06:00
|
|
|
}
|
2017-11-23 04:29:06 -06:00
|
|
|
}
|
2018-01-24 08:22:03 -06:00
|
|
|
|
|
|
|
func saveDashboard(fr *fileReader, path string, dash *dashboards.SaveDashboardDTO) error {
|
2018-01-23 13:22:04 -06:00
|
|
|
d := &models.DashboardProvisioning{
|
|
|
|
ExternalId: path,
|
|
|
|
Name: fr.Cfg.Name,
|
|
|
|
}
|
2018-01-24 08:22:03 -06:00
|
|
|
|
2018-01-23 13:22:04 -06:00
|
|
|
_, err := fr.dashboardRepo.SaveProvisionedDashboard(dash, d)
|
2018-01-24 08:22:03 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-23 05:28:56 -06:00
|
|
|
|
2018-01-24 08:22:03 -06:00
|
|
|
fr.cache.addDashboardCache(path, dash)
|
|
|
|
return nil
|
2018-01-23 05:28:56 -06:00
|
|
|
}
|
2017-11-23 04:29:06 -06:00
|
|
|
|
2018-01-18 05:04:12 -06:00
|
|
|
func validateWalkablePath(fileInfo os.FileInfo) (bool, error) {
|
|
|
|
if fileInfo.IsDir() {
|
|
|
|
if strings.HasPrefix(fileInfo.Name(), ".") {
|
|
|
|
return false, filepath.SkipDir
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(fileInfo.Name(), ".json") {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2018-01-23 05:28:56 -06:00
|
|
|
func (fr *fileReader) readDashboardFromFile(path string, folderId int64) (*dashboards.SaveDashboardDTO, error) {
|
2017-11-23 04:29:06 -06:00
|
|
|
reader, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
data, err := simplejson.NewFromReader(reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-28 09:57:14 -06:00
|
|
|
stat, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-23 04:29:06 -06:00
|
|
|
|
2017-12-27 09:32:39 -06:00
|
|
|
dash, err := createDashboardJson(data, stat.ModTime(), fr.Cfg, folderId)
|
2017-11-28 09:57:14 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-11-23 04:29:06 -06:00
|
|
|
}
|
|
|
|
|
2017-12-27 09:32:39 -06:00
|
|
|
fr.cache.addDashboardCache(path, dash)
|
2017-11-23 04:29:06 -06:00
|
|
|
|
|
|
|
return dash, nil
|
|
|
|
}
|