feat(plugins): made plugins that live outside public work

This commit is contained in:
Torkel Ödegaard 2015-11-19 16:50:17 +01:00
parent f6772bb896
commit 65a7fa320a
6 changed files with 54 additions and 13 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/grafana/grafana/pkg/api/static" "github.com/grafana/grafana/pkg/api/static"
"github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/middleware"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
) )
@ -34,7 +35,11 @@ func newMacaron() *macaron.Macaron {
mapStatic(m, setting.StaticRootPath, "img", "img") mapStatic(m, setting.StaticRootPath, "img", "img")
mapStatic(m, setting.StaticRootPath, "fonts", "fonts") mapStatic(m, setting.StaticRootPath, "fonts", "fonts")
mapStatic(m, setting.StaticRootPath, "robots.txt", "robots.txt") mapStatic(m, setting.StaticRootPath, "robots.txt", "robots.txt")
mapStatic(m, setting.DataPath, "plugins", "_plugins")
for _, route := range plugins.StaticRoutes {
log.Info("Adding plugin static route %s -> %s", route.Url, route.Path)
mapStatic(m, route.Path, "", route.Url)
}
m.Use(macaron.Renderer(macaron.RenderOptions{ m.Use(macaron.Renderer(macaron.RenderOptions{
Directory: path.Join(setting.StaticRootPath, "views"), Directory: path.Join(setting.StaticRootPath, "views"),

View File

@ -12,6 +12,13 @@ type DataSourcePlugin struct {
Annotations bool `json:"annotations"` Annotations bool `json:"annotations"`
Metrics bool `json:"metrics"` Metrics bool `json:"metrics"`
BuiltIn bool `json:"builtIn"` BuiltIn bool `json:"builtIn"`
StaticRootConfig *StaticRootConfig `json:"staticRoot"`
}
type StaticRootConfig struct {
Url string `json:"url"`
Path string `json:"path"`
PluginRoot string `json:"-"`
} }
type ExternalPluginRoute struct { type ExternalPluginRoute struct {

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strings"
"github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
@ -14,6 +15,7 @@ import (
var ( var (
DataSources map[string]DataSourcePlugin DataSources map[string]DataSourcePlugin
ExternalPlugins []ExternalPlugin ExternalPlugins []ExternalPlugin
StaticRoutes []*StaticRootConfig
) )
type PluginScanner struct { type PluginScanner struct {
@ -21,12 +23,27 @@ type PluginScanner struct {
errors []error errors []error
} }
func Init() { func Init() error {
DataSources = make(map[string]DataSourcePlugin) DataSources = make(map[string]DataSourcePlugin)
ExternalPlugins = make([]ExternalPlugin, 0) ExternalPlugins = make([]ExternalPlugin, 0)
StaticRoutes = make([]*StaticRootConfig, 0)
scan(path.Join(setting.StaticRootPath, "app/plugins")) scan(path.Join(setting.StaticRootPath, "app/plugins"))
scan(path.Join(setting.DataPath, "plugins")) checkExternalPluginPaths()
return nil
}
func checkExternalPluginPaths() error {
for _, section := range setting.Cfg.Sections() {
if strings.HasPrefix(section.Name(), "plugin.") {
path := section.Key("path").String()
if path != "" {
log.Info("Plugin: scaning specific dir %s", path)
scan(path)
}
}
}
return nil
} }
func scan(pluginDir string) error { func scan(pluginDir string) error {
@ -45,7 +62,7 @@ func scan(pluginDir string) error {
return nil return nil
} }
func (scanner *PluginScanner) walker(path string, f os.FileInfo, err error) error { func (scanner *PluginScanner) walker(currentPath string, f os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} }
@ -55,17 +72,18 @@ func (scanner *PluginScanner) walker(path string, f os.FileInfo, err error) erro
} }
if f.Name() == "plugin.json" { if f.Name() == "plugin.json" {
err := scanner.loadPluginJson(path) err := scanner.loadPluginJson(currentPath)
if err != nil { if err != nil {
log.Error(3, "Failed to load plugin json file: %v, err: %v", path, err) log.Error(3, "Failed to load plugin json file: %v, err: %v", currentPath, err)
scanner.errors = append(scanner.errors, err) scanner.errors = append(scanner.errors, err)
} }
} }
return nil return nil
} }
func (scanner *PluginScanner) loadPluginJson(path string) error { func (scanner *PluginScanner) loadPluginJson(pluginJsonFilePath string) error {
reader, err := os.Open(path) currentDir := filepath.Dir(pluginJsonFilePath)
reader, err := os.Open(pluginJsonFilePath)
if err != nil { if err != nil {
return err return err
} }
@ -96,6 +114,11 @@ func (scanner *PluginScanner) loadPluginJson(path string) error {
} }
DataSources[p.Type] = p DataSources[p.Type] = p
if p.StaticRootConfig != nil {
p.StaticRootConfig.Path = path.Join(currentDir, p.StaticRootConfig.Path)
StaticRoutes = append(StaticRoutes, p.StaticRootConfig)
}
} }
if pluginType == "externalPlugin" { if pluginType == "externalPlugin" {

View File

@ -275,13 +275,11 @@ func loadSpecifedConfigFile(configFile string) {
defaultSec, err := Cfg.GetSection(section.Name()) defaultSec, err := Cfg.GetSection(section.Name())
if err != nil { if err != nil {
log.Error(3, "Unknown config section %s defined in %s", section.Name(), configFile) defaultSec, _ = Cfg.NewSection(section.Name())
continue
} }
defaultKey, err := defaultSec.GetKey(key.Name()) defaultKey, err := defaultSec.GetKey(key.Name())
if err != nil { if err != nil {
log.Error(3, "Unknown config key %s defined in section %s, in file %s", key.Name(), section.Name(), configFile) defaultKey, _ = defaultSec.NewKey(key.Name(), key.Value())
continue
} }
defaultKey.SetValue(key.Value()) defaultKey.SetValue(key.Value())
} }

View File

@ -37,5 +37,11 @@
"adminOnly": false, "adminOnly": false,
} }
] ]
} },
"staticRoot": {
"url": "_plugins/test_ds",
"path": "public"
},
} }

View File

@ -3,6 +3,8 @@ require.config({
baseUrl: 'public', baseUrl: 'public',
paths: { paths: {
'_plugins': '../_plugins',
'lodash-src': 'vendor/lodash', 'lodash-src': 'vendor/lodash',
lodash: 'app/core/lodash_extended', lodash: 'app/core/lodash_extended',