plugins.go 2.7 KB

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