plugins.go 3.6 KB

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