Forráskód Böngészése

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

Torkel Ödegaard 10 éve
szülő
commit
65a7fa320a

+ 6 - 1
pkg/cmd/web.go

@@ -14,6 +14,7 @@ import (
 	"github.com/grafana/grafana/pkg/api/static"
 	"github.com/grafana/grafana/pkg/log"
 	"github.com/grafana/grafana/pkg/middleware"
+	"github.com/grafana/grafana/pkg/plugins"
 	"github.com/grafana/grafana/pkg/setting"
 )
 
@@ -34,7 +35,11 @@ func newMacaron() *macaron.Macaron {
 	mapStatic(m, setting.StaticRootPath, "img", "img")
 	mapStatic(m, setting.StaticRootPath, "fonts", "fonts")
 	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{
 		Directory:  path.Join(setting.StaticRootPath, "views"),

+ 7 - 0
pkg/plugins/models.go

@@ -12,6 +12,13 @@ type DataSourcePlugin struct {
 	Annotations        bool                   `json:"annotations"`
 	Metrics            bool                   `json:"metrics"`
 	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 {

+ 30 - 7
pkg/plugins/plugins.go

@@ -6,6 +6,7 @@ import (
 	"os"
 	"path"
 	"path/filepath"
+	"strings"
 
 	"github.com/grafana/grafana/pkg/log"
 	"github.com/grafana/grafana/pkg/setting"
@@ -14,6 +15,7 @@ import (
 var (
 	DataSources     map[string]DataSourcePlugin
 	ExternalPlugins []ExternalPlugin
+	StaticRoutes    []*StaticRootConfig
 )
 
 type PluginScanner struct {
@@ -21,12 +23,27 @@ type PluginScanner struct {
 	errors     []error
 }
 
-func Init() {
+func Init() error {
 	DataSources = make(map[string]DataSourcePlugin)
 	ExternalPlugins = make([]ExternalPlugin, 0)
+	StaticRoutes = make([]*StaticRootConfig, 0)
 
 	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 {
@@ -45,7 +62,7 @@ func scan(pluginDir string) error {
 	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 {
 		return err
 	}
@@ -55,17 +72,18 @@ func (scanner *PluginScanner) walker(path string, f os.FileInfo, err error) erro
 	}
 
 	if f.Name() == "plugin.json" {
-		err := scanner.loadPluginJson(path)
+		err := scanner.loadPluginJson(currentPath)
 		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)
 		}
 	}
 	return nil
 }
 
-func (scanner *PluginScanner) loadPluginJson(path string) error {
-	reader, err := os.Open(path)
+func (scanner *PluginScanner) loadPluginJson(pluginJsonFilePath string) error {
+	currentDir := filepath.Dir(pluginJsonFilePath)
+	reader, err := os.Open(pluginJsonFilePath)
 	if err != nil {
 		return err
 	}
@@ -96,6 +114,11 @@ func (scanner *PluginScanner) loadPluginJson(path string) error {
 		}
 
 		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" {

+ 2 - 4
pkg/setting/setting.go

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

+ 7 - 1
public/app/plugins/externalPlugins/example/_plugin.json

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

+ 2 - 0
public/app/require_config.js

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