Commit 257b824d by Torkel Ödegaard

feat(plugins): better logging and handling of loading plugins, try to create…

feat(plugins): better logging and handling of loading plugins, try to create plugins dir if it does not exist, fixes #3974
parent 509b37eb
...@@ -31,7 +31,7 @@ func newMacaron() *macaron.Macaron { ...@@ -31,7 +31,7 @@ func newMacaron() *macaron.Macaron {
for _, route := range plugins.StaticRoutes { for _, route := range plugins.StaticRoutes {
pluginRoute := path.Join("/public/plugins/", route.PluginId) pluginRoute := path.Join("/public/plugins/", route.PluginId)
log.Info("Plugin: Adding static route %s -> %s", pluginRoute, route.Directory) log.Info("Plugins: Adding route %s -> %s", pluginRoute, route.Directory)
mapStatic(m, route.Directory, "", pluginRoute) mapStatic(m, route.Directory, "", pluginRoute)
} }
......
...@@ -57,7 +57,9 @@ func (app *AppPlugin) Load(decoder *json.Decoder, pluginDir string) error { ...@@ -57,7 +57,9 @@ func (app *AppPlugin) Load(decoder *json.Decoder, pluginDir string) error {
return err return err
} }
app.PluginDir = pluginDir if err := app.registerPlugin(pluginDir); err != nil {
return err
}
Apps[app.Id] = app Apps[app.Id] = app
return nil return nil
......
...@@ -17,8 +17,10 @@ func (p *DataSourcePlugin) Load(decoder *json.Decoder, pluginDir string) error { ...@@ -17,8 +17,10 @@ func (p *DataSourcePlugin) Load(decoder *json.Decoder, pluginDir string) error {
return err return err
} }
p.PluginDir = pluginDir if err := p.registerPlugin(pluginDir); err != nil {
DataSources[p.Id] = p return err
}
DataSources[p.Id] = p
return nil return nil
} }
...@@ -6,7 +6,6 @@ import ( ...@@ -6,7 +6,6 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/util" "github.com/grafana/grafana/pkg/util"
) )
...@@ -38,13 +37,10 @@ func (fp *FrontendPluginBase) initFrontendPlugin() { ...@@ -38,13 +37,10 @@ func (fp *FrontendPluginBase) initFrontendPlugin() {
} }
func (fp *FrontendPluginBase) setPathsBasedOnApp(app *AppPlugin) { func (fp *FrontendPluginBase) setPathsBasedOnApp(app *AppPlugin) {
// log.Info("Module Before: %v", fp.Module)
// find out plugins path relative to app static root
appSubPath := strings.Replace(fp.PluginDir, app.StaticRootAbs, "", 1) appSubPath := strings.Replace(fp.PluginDir, app.StaticRootAbs, "", 1)
fp.IncludedInAppId = app.Id fp.IncludedInAppId = app.Id
fp.BaseUrl = app.BaseUrl fp.BaseUrl = app.BaseUrl
fp.Module = util.JoinUrlFragments("plugins/"+app.Id, appSubPath) + "/module" fp.Module = util.JoinUrlFragments("plugins/"+app.Id, appSubPath) + "/module"
log.Info("setting paths based on app: subpath = %v, module: %v", appSubPath, fp.Module)
} }
func (fp *FrontendPluginBase) handleModuleDefaults() { func (fp *FrontendPluginBase) handleModuleDefaults() {
......
...@@ -2,6 +2,11 @@ package plugins ...@@ -2,6 +2,11 @@ package plugins
import ( import (
"encoding/json" "encoding/json"
"errors"
"strings"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/setting"
) )
type PluginLoader interface { type PluginLoader interface {
...@@ -18,6 +23,20 @@ type PluginBase struct { ...@@ -18,6 +23,20 @@ type PluginBase struct {
PluginDir string `json:"-"` PluginDir string `json:"-"`
} }
func (pb *PluginBase) registerPlugin(pluginDir string) error {
if _, exists := Plugins[pb.Id]; exists {
return errors.New("Plugin with same id already exists")
}
if !strings.HasPrefix(pluginDir, setting.StaticRootPath) {
log.Info("Plugins: Registering plugin %v", pb.Name)
}
pb.PluginDir = pluginDir
Plugins[pb.Id] = pb
return nil
}
type PluginInfo struct { type PluginInfo struct {
Author PluginInfoLink `json:"author"` Author PluginInfoLink `json:"author"`
Description string `json:"description"` Description string `json:"description"`
......
...@@ -11,8 +11,10 @@ func (p *PanelPlugin) Load(decoder *json.Decoder, pluginDir string) error { ...@@ -11,8 +11,10 @@ func (p *PanelPlugin) Load(decoder *json.Decoder, pluginDir string) error {
return err return err
} }
p.PluginDir = pluginDir if err := p.registerPlugin(pluginDir); err != nil {
Panels[p.Id] = p return err
}
Panels[p.Id] = p
return nil return nil
} }
...@@ -19,6 +19,7 @@ var ( ...@@ -19,6 +19,7 @@ var (
Panels map[string]*PanelPlugin Panels map[string]*PanelPlugin
StaticRoutes []*PluginStaticRoute StaticRoutes []*PluginStaticRoute
Apps map[string]*AppPlugin Apps map[string]*AppPlugin
Plugins map[string]*PluginBase
PluginTypes map[string]interface{} PluginTypes map[string]interface{}
) )
...@@ -32,14 +33,30 @@ func Init() error { ...@@ -32,14 +33,30 @@ func Init() error {
StaticRoutes = make([]*PluginStaticRoute, 0) StaticRoutes = make([]*PluginStaticRoute, 0)
Panels = make(map[string]*PanelPlugin) Panels = make(map[string]*PanelPlugin)
Apps = make(map[string]*AppPlugin) Apps = make(map[string]*AppPlugin)
Plugins = make(map[string]*PluginBase)
PluginTypes = map[string]interface{}{ PluginTypes = map[string]interface{}{
"panel": PanelPlugin{}, "panel": PanelPlugin{},
"datasource": DataSourcePlugin{}, "datasource": DataSourcePlugin{},
"app": AppPlugin{}, "app": AppPlugin{},
} }
log.Info("Plugins: Scan starting")
scan(path.Join(setting.StaticRootPath, "app/plugins")) scan(path.Join(setting.StaticRootPath, "app/plugins"))
scan(setting.PluginsPath)
// check if plugins dir exists
if _, err := os.Stat(setting.PluginsPath); os.IsNotExist(err) {
log.Warn("Plugins: Plugin dir %v does not exist", setting.PluginsPath)
if err = os.MkdirAll(setting.PluginsPath, os.ModePerm); err != nil {
log.Warn("Plugins: Failed to create plugin dir: %v, error: %v", setting.PluginsPath, err)
} else {
log.Info("Plugins: Plugin dir %v created", setting.PluginsPath)
scan(setting.PluginsPath)
}
} else {
scan(setting.PluginsPath)
}
// check plugin paths defined in config
checkPluginPaths() checkPluginPaths()
for _, panel := range Panels { for _, panel := range Panels {
...@@ -55,32 +72,11 @@ func Init() error { ...@@ -55,32 +72,11 @@ func Init() error {
return nil return nil
} }
// func checkDependencies() {
// for appType, app := range Apps {
// for _, reqPanel := range app.PanelPlugins {
// if _, ok := Panels[reqPanel]; !ok {
// log.Fatal(4, "App %s requires Panel type %s, but it is not present.", appType, reqPanel)
// }
// }
// for _, reqDataSource := range app.DatasourcePlugins {
// if _, ok := DataSources[reqDataSource]; !ok {
// log.Fatal(4, "App %s requires DataSource type %s, but it is not present.", appType, reqDataSource)
// }
// }
// for _, reqApiPlugin := range app.ApiPlugins {
// if _, ok := ApiPlugins[reqApiPlugin]; !ok {
// log.Fatal(4, "App %s requires ApiPlugin type %s, but it is not present.", appType, reqApiPlugin)
// }
// }
// }
// }
func checkPluginPaths() error { func checkPluginPaths() error {
for _, section := range setting.Cfg.Sections() { for _, section := range setting.Cfg.Sections() {
if strings.HasPrefix(section.Name(), "plugin.") { if strings.HasPrefix(section.Name(), "plugin.") {
path := section.Key("path").String() path := section.Key("path").String()
if path != "" { if path != "" {
log.Info("Plugin: Scaning dir %s", path)
scan(path) scan(path)
} }
} }
...@@ -93,6 +89,8 @@ func scan(pluginDir string) error { ...@@ -93,6 +89,8 @@ func scan(pluginDir string) error {
pluginPath: pluginDir, pluginPath: pluginDir,
} }
log.Info("Plugins: Scaning dir %s", pluginDir)
if err := util.Walk(pluginDir, true, true, scanner.walker); err != nil { if err := util.Walk(pluginDir, true, true, scanner.walker); err != nil {
if pluginDir != "data/plugins" { if pluginDir != "data/plugins" {
log.Warn("Could not scan dir \"%v\" error: %s", pluginDir, err) log.Warn("Could not scan dir \"%v\" error: %s", pluginDir, err)
...@@ -123,7 +121,7 @@ func (scanner *PluginScanner) walker(currentPath string, f os.FileInfo, err erro ...@@ -123,7 +121,7 @@ func (scanner *PluginScanner) walker(currentPath string, f os.FileInfo, err erro
if f.Name() == "plugin.json" { if f.Name() == "plugin.json" {
err := scanner.loadPluginJson(currentPath) err := scanner.loadPluginJson(currentPath)
if err != nil { if err != nil {
log.Error(3, "Failed to load plugin json file: %v, err: %v", currentPath, err) log.Error(3, "Plugins: Failed to load plugin json file: %v, err: %v", currentPath, err)
scanner.errors = append(scanner.errors, err) scanner.errors = append(scanner.errors, err)
} }
} }
......
{ {
"type": "datasource", "type": "datasource",
"name": "InfluxDB 0.9.x", "name": "InfluxDB",
"id": "influxdb", "id": "influxdb",
"defaultMatchFormat": "regex values", "defaultMatchFormat": "regex values",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment