plugins.go 3.0 KB

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