plugins.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/models"
  11. "github.com/grafana/grafana/pkg/setting"
  12. )
  13. var (
  14. DataSources map[string]DataSourcePlugin
  15. Panels map[string]PanelPlugin
  16. ExternalPlugins map[string]ExternalPlugin
  17. StaticRoutes []*StaticRootConfig
  18. Bundles map[string]PluginBundle
  19. )
  20. type PluginScanner struct {
  21. pluginPath string
  22. errors []error
  23. }
  24. func Init() error {
  25. DataSources = make(map[string]DataSourcePlugin)
  26. ExternalPlugins = make(map[string]ExternalPlugin)
  27. StaticRoutes = make([]*StaticRootConfig, 0)
  28. Panels = make(map[string]PanelPlugin)
  29. Bundles = make(map[string]PluginBundle)
  30. scan(path.Join(setting.StaticRootPath, "app/plugins"))
  31. checkExternalPluginPaths()
  32. return nil
  33. }
  34. func checkExternalPluginPaths() error {
  35. for _, section := range setting.Cfg.Sections() {
  36. if strings.HasPrefix(section.Name(), "plugin.") {
  37. path := section.Key("path").String()
  38. if path != "" {
  39. log.Info("Plugin: Scaning dir %s", path)
  40. scan(path)
  41. }
  42. }
  43. }
  44. return nil
  45. }
  46. func scan(pluginDir string) error {
  47. scanner := &PluginScanner{
  48. pluginPath: pluginDir,
  49. }
  50. if err := filepath.Walk(pluginDir, scanner.walker); err != nil {
  51. return err
  52. }
  53. if len(scanner.errors) > 0 {
  54. return errors.New("Some plugins failed to load")
  55. }
  56. return nil
  57. }
  58. func (scanner *PluginScanner) walker(currentPath string, f os.FileInfo, err error) error {
  59. if err != nil {
  60. return err
  61. }
  62. if f.IsDir() {
  63. return nil
  64. }
  65. if f.Name() == "plugin.json" {
  66. err := scanner.loadPluginJson(currentPath)
  67. if err != nil {
  68. log.Error(3, "Failed to load plugin json file: %v, err: %v", currentPath, err)
  69. scanner.errors = append(scanner.errors, err)
  70. }
  71. }
  72. return nil
  73. }
  74. func addStaticRoot(staticRootConfig *StaticRootConfig, currentDir string) {
  75. if staticRootConfig != nil {
  76. staticRootConfig.Path = path.Join(currentDir, staticRootConfig.Path)
  77. StaticRoutes = append(StaticRoutes, staticRootConfig)
  78. }
  79. }
  80. func (scanner *PluginScanner) loadPluginJson(pluginJsonFilePath string) error {
  81. currentDir := filepath.Dir(pluginJsonFilePath)
  82. reader, err := os.Open(pluginJsonFilePath)
  83. if err != nil {
  84. return err
  85. }
  86. defer reader.Close()
  87. jsonParser := json.NewDecoder(reader)
  88. pluginJson := make(map[string]interface{})
  89. if err := jsonParser.Decode(&pluginJson); err != nil {
  90. return err
  91. }
  92. pluginType, exists := pluginJson["pluginType"]
  93. if !exists {
  94. return errors.New("Did not find pluginType property in plugin.json")
  95. }
  96. if pluginType == "datasource" {
  97. p := DataSourcePlugin{}
  98. reader.Seek(0, 0)
  99. if err := jsonParser.Decode(&p); err != nil {
  100. return err
  101. }
  102. if p.Type == "" {
  103. return errors.New("Did not find type property in plugin.json")
  104. }
  105. DataSources[p.Type] = p
  106. addStaticRoot(p.StaticRootConfig, currentDir)
  107. }
  108. if pluginType == "panel" {
  109. p := PanelPlugin{}
  110. reader.Seek(0, 0)
  111. if err := jsonParser.Decode(&p); err != nil {
  112. return err
  113. }
  114. if p.Type == "" {
  115. return errors.New("Did not find type property in plugin.json")
  116. }
  117. Panels[p.Type] = p
  118. addStaticRoot(p.StaticRootConfig, currentDir)
  119. }
  120. if pluginType == "external" {
  121. p := ExternalPlugin{}
  122. reader.Seek(0, 0)
  123. if err := jsonParser.Decode(&p); err != nil {
  124. return err
  125. }
  126. if p.Type == "" {
  127. return errors.New("Did not find type property in plugin.json")
  128. }
  129. ExternalPlugins[p.Type] = p
  130. addStaticRoot(p.StaticRootConfig, currentDir)
  131. }
  132. if pluginType == "bundle" {
  133. p := PluginBundle{}
  134. reader.Seek(0, 0)
  135. if err := jsonParser.Decode(&p); err != nil {
  136. return err
  137. }
  138. if p.Type == "" {
  139. return errors.New("Did not find type property in plugin.json")
  140. }
  141. Bundles[p.Type] = p
  142. }
  143. return nil
  144. }
  145. func GetEnabledPlugins(orgBundles []*models.PluginBundle) EnabledPlugins {
  146. enabledPlugins := NewEnabledPlugins()
  147. orgBundlesMap := make(map[string]*models.PluginBundle)
  148. for _, orgBundle := range orgBundles {
  149. orgBundlesMap[orgBundle.Type] = orgBundle
  150. }
  151. for bundleType, bundle := range Bundles {
  152. enabled := bundle.Enabled
  153. // check if the bundle is stored in the DB.
  154. if b, ok := orgBundlesMap[bundleType]; ok {
  155. enabled = b.Enabled
  156. }
  157. if enabled {
  158. for _, d := range bundle.DatasourcePlugins {
  159. if ds, ok := DataSources[d]; ok {
  160. enabledPlugins.DataSourcePlugins[d] = &ds
  161. }
  162. }
  163. for _, p := range bundle.PanelPlugins {
  164. if panel, ok := Panels[p]; ok {
  165. enabledPlugins.PanelPlugins = append(enabledPlugins.PanelPlugins, &panel)
  166. }
  167. }
  168. for _, e := range bundle.ExternalPlugins {
  169. if external, ok := ExternalPlugins[e]; ok {
  170. enabledPlugins.ExternalPlugins = append(enabledPlugins.ExternalPlugins, &external)
  171. }
  172. }
  173. }
  174. }
  175. return enabledPlugins
  176. }