plugins.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package plugins
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "os"
  6. "path"
  7. "path/filepath"
  8. "strings"
  9. "github.com/grafana/grafana/pkg/log"
  10. "github.com/grafana/grafana/pkg/setting"
  11. "github.com/grafana/grafana/pkg/util"
  12. )
  13. var (
  14. DataSources map[string]DataSourcePlugin
  15. Panels map[string]PanelPlugin
  16. StaticRoutes []*StaticRootConfig
  17. )
  18. type PluginScanner struct {
  19. pluginPath string
  20. errors []error
  21. }
  22. func Init() error {
  23. DataSources = make(map[string]DataSourcePlugin)
  24. StaticRoutes = make([]*StaticRootConfig, 0)
  25. Panels = make(map[string]PanelPlugin)
  26. scan(path.Join(setting.StaticRootPath, "app/plugins"))
  27. scan(path.Join(setting.PluginsPath))
  28. checkExternalPluginPaths()
  29. return nil
  30. }
  31. func checkExternalPluginPaths() error {
  32. for _, section := range setting.Cfg.Sections() {
  33. if strings.HasPrefix(section.Name(), "plugin.") {
  34. path := section.Key("path").String()
  35. if path != "" {
  36. log.Info("Plugin: Scaning dir %s", path)
  37. scan(path)
  38. }
  39. }
  40. }
  41. return nil
  42. }
  43. func scan(pluginDir string) error {
  44. scanner := &PluginScanner{
  45. pluginPath: pluginDir,
  46. }
  47. if err := util.Walk(pluginDir, true, true, scanner.walker); err != nil {
  48. if pluginDir != "data/plugins" {
  49. log.Warn("Could not scan dir \"%v\" error: %s", pluginDir, err)
  50. }
  51. return err
  52. }
  53. if len(scanner.errors) > 0 {
  54. return errors.New("Some plugins failed to load")
  55. }
  56. return nil
  57. }
  58. func (scanner *PluginScanner) walker(currentPath string, f os.FileInfo, err error) error {
  59. if err != nil {
  60. return err
  61. }
  62. if f.IsDir() {
  63. return nil
  64. }
  65. if f.Name() == "plugin.json" {
  66. err := scanner.loadPluginJson(currentPath)
  67. if err != nil {
  68. log.Error(3, "Failed to load plugin json file: %v, err: %v", currentPath, err)
  69. scanner.errors = append(scanner.errors, err)
  70. }
  71. }
  72. return nil
  73. }
  74. func addStaticRoot(staticRootConfig *StaticRootConfig, currentDir string) {
  75. if staticRootConfig != nil {
  76. staticRootConfig.Path = path.Join(currentDir, staticRootConfig.Path)
  77. StaticRoutes = append(StaticRoutes, staticRootConfig)
  78. }
  79. }
  80. func (scanner *PluginScanner) loadPluginJson(pluginJsonFilePath string) error {
  81. currentDir := filepath.Dir(pluginJsonFilePath)
  82. reader, err := os.Open(pluginJsonFilePath)
  83. if err != nil {
  84. return err
  85. }
  86. defer reader.Close()
  87. jsonParser := json.NewDecoder(reader)
  88. pluginJson := make(map[string]interface{})
  89. if err := jsonParser.Decode(&pluginJson); err != nil {
  90. return err
  91. }
  92. pluginType, exists := pluginJson["pluginType"]
  93. if !exists {
  94. return errors.New("Did not find pluginType property in plugin.json")
  95. }
  96. if pluginType == "datasource" {
  97. p := DataSourcePlugin{}
  98. reader.Seek(0, 0)
  99. if err := jsonParser.Decode(&p); err != nil {
  100. return err
  101. }
  102. if p.Type == "" {
  103. return errors.New("Did not find type property in plugin.json")
  104. }
  105. DataSources[p.Type] = p
  106. addStaticRoot(p.StaticRootConfig, currentDir)
  107. }
  108. if pluginType == "panel" {
  109. p := PanelPlugin{}
  110. reader.Seek(0, 0)
  111. if err := jsonParser.Decode(&p); err != nil {
  112. return err
  113. }
  114. if p.Type == "" {
  115. return errors.New("Did not find type property in plugin.json")
  116. }
  117. Panels[p.Type] = p
  118. addStaticRoot(p.StaticRootConfig, currentDir)
  119. }
  120. return nil
  121. }