| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- package plugins
- import (
- "encoding/json"
- "errors"
- "os"
- "path"
- "path/filepath"
- "strings"
- "github.com/grafana/grafana/pkg/log"
- "github.com/grafana/grafana/pkg/setting"
- "github.com/grafana/grafana/pkg/util"
- )
- var (
- DataSources map[string]DataSourcePlugin
- Panels map[string]PanelPlugin
- StaticRoutes []*StaticRootConfig
- )
- type PluginScanner struct {
- pluginPath string
- errors []error
- }
- func Init() error {
- DataSources = make(map[string]DataSourcePlugin)
- StaticRoutes = make([]*StaticRootConfig, 0)
- Panels = make(map[string]PanelPlugin)
- scan(path.Join(setting.StaticRootPath, "app/plugins"))
- scan(path.Join(setting.PluginsPath))
- 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 dir %s", path)
- scan(path)
- }
- }
- }
- return nil
- }
- func scan(pluginDir string) error {
- scanner := &PluginScanner{
- pluginPath: pluginDir,
- }
- if err := util.Walk(pluginDir, true, true, scanner.walker); err != nil {
- return err
- }
- if len(scanner.errors) > 0 {
- return errors.New("Some plugins failed to load")
- }
- return nil
- }
- func (scanner *PluginScanner) walker(currentPath string, f os.FileInfo, err error) error {
- if err != nil {
- return err
- }
- if f.IsDir() {
- return nil
- }
- if f.Name() == "plugin.json" {
- err := scanner.loadPluginJson(currentPath)
- if err != nil {
- log.Error(3, "Failed to load plugin json file: %v, err: %v", currentPath, err)
- scanner.errors = append(scanner.errors, err)
- }
- }
- return nil
- }
- func addStaticRoot(staticRootConfig *StaticRootConfig, currentDir string) {
- if staticRootConfig != nil {
- staticRootConfig.Path = path.Join(currentDir, staticRootConfig.Path)
- StaticRoutes = append(StaticRoutes, staticRootConfig)
- }
- }
- func (scanner *PluginScanner) loadPluginJson(pluginJsonFilePath string) error {
- currentDir := filepath.Dir(pluginJsonFilePath)
- reader, err := os.Open(pluginJsonFilePath)
- if err != nil {
- return err
- }
- defer reader.Close()
- jsonParser := json.NewDecoder(reader)
- pluginJson := make(map[string]interface{})
- if err := jsonParser.Decode(&pluginJson); err != nil {
- return err
- }
- pluginType, exists := pluginJson["pluginType"]
- if !exists {
- return errors.New("Did not find pluginType property in plugin.json")
- }
- if pluginType == "datasource" {
- p := DataSourcePlugin{}
- reader.Seek(0, 0)
- if err := jsonParser.Decode(&p); err != nil {
- return err
- }
- if p.Type == "" {
- return errors.New("Did not find type property in plugin.json")
- }
- DataSources[p.Type] = p
- addStaticRoot(p.StaticRootConfig, currentDir)
- }
- if pluginType == "panel" {
- p := PanelPlugin{}
- reader.Seek(0, 0)
- if err := jsonParser.Decode(&p); err != nil {
- return err
- }
- if p.Type == "" {
- return errors.New("Did not find type property in plugin.json")
- }
- Panels[p.Type] = p
- addStaticRoot(p.StaticRootConfig, currentDir)
- }
- return nil
- }
|