plugins.go 4.3 KB

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