plugins.go 3.5 KB

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