plugins.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. "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 []*PublicContent
  19. Apps map[string]*AppPlugin
  20. )
  21. type PluginScanner struct {
  22. pluginPath string
  23. errors []error
  24. }
  25. func Init() error {
  26. DataSources = make(map[string]*DataSourcePlugin)
  27. ApiPlugins = make(map[string]*ApiPlugin)
  28. StaticRoutes = make([]*PublicContent, 0)
  29. Panels = make(map[string]*PanelPlugin)
  30. Apps = make(map[string]*AppPlugin)
  31. scan(path.Join(setting.StaticRootPath, "app/plugins"))
  32. checkPluginPaths()
  33. // checkDependencies()
  34. return nil
  35. }
  36. // func checkDependencies() {
  37. // for appType, app := range Apps {
  38. // for _, reqPanel := range app.PanelPlugins {
  39. // if _, ok := Panels[reqPanel]; !ok {
  40. // log.Fatal(4, "App %s requires Panel type %s, but it is not present.", appType, reqPanel)
  41. // }
  42. // }
  43. // for _, reqDataSource := range app.DatasourcePlugins {
  44. // if _, ok := DataSources[reqDataSource]; !ok {
  45. // log.Fatal(4, "App %s requires DataSource type %s, but it is not present.", appType, reqDataSource)
  46. // }
  47. // }
  48. // for _, reqApiPlugin := range app.ApiPlugins {
  49. // if _, ok := ApiPlugins[reqApiPlugin]; !ok {
  50. // log.Fatal(4, "App %s requires ApiPlugin type %s, but it is not present.", appType, reqApiPlugin)
  51. // }
  52. // }
  53. // }
  54. // }
  55. func checkPluginPaths() error {
  56. for _, section := range setting.Cfg.Sections() {
  57. if strings.HasPrefix(section.Name(), "plugin.") {
  58. path := section.Key("path").String()
  59. if path != "" {
  60. log.Info("Plugin: Scaning dir %s", path)
  61. scan(path)
  62. }
  63. }
  64. }
  65. return nil
  66. }
  67. func scan(pluginDir string) error {
  68. scanner := &PluginScanner{
  69. pluginPath: pluginDir,
  70. }
  71. if err := util.Walk(pluginDir, true, true, scanner.walker); err != nil {
  72. return err
  73. }
  74. if len(scanner.errors) > 0 {
  75. return errors.New("Some plugins failed to load")
  76. }
  77. return nil
  78. }
  79. func (scanner *PluginScanner) walker(currentPath string, f os.FileInfo, err error) error {
  80. if err != nil {
  81. return err
  82. }
  83. if f.IsDir() {
  84. return nil
  85. }
  86. if f.Name() == "plugin.json" {
  87. err := scanner.loadPluginJson(currentPath)
  88. if err != nil {
  89. log.Error(3, "Failed to load plugin json file: %v, err: %v", currentPath, err)
  90. scanner.errors = append(scanner.errors, err)
  91. }
  92. }
  93. return nil
  94. }
  95. func addPublicContent(public *PublicContent, currentDir string) {
  96. if public != nil {
  97. public.Dir = path.Join(currentDir, public.Dir)
  98. StaticRoutes = append(StaticRoutes, public)
  99. }
  100. }
  101. func (scanner *PluginScanner) loadPluginJson(pluginJsonFilePath string) error {
  102. currentDir := filepath.Dir(pluginJsonFilePath)
  103. reader, err := os.Open(pluginJsonFilePath)
  104. if err != nil {
  105. return err
  106. }
  107. defer reader.Close()
  108. jsonParser := json.NewDecoder(reader)
  109. pluginJson := make(map[string]interface{})
  110. if err := jsonParser.Decode(&pluginJson); err != nil {
  111. return err
  112. }
  113. pluginType, exists := pluginJson["pluginType"]
  114. if !exists {
  115. return errors.New("Did not find pluginType property in plugin.json")
  116. }
  117. if pluginType == "datasource" {
  118. p := DataSourcePlugin{}
  119. reader.Seek(0, 0)
  120. if err := jsonParser.Decode(&p); err != nil {
  121. return err
  122. }
  123. if p.Type == "" {
  124. return errors.New("Did not find type property in plugin.json")
  125. }
  126. DataSources[p.Type] = &p
  127. addPublicContent(p.PublicContent, currentDir)
  128. }
  129. if pluginType == "panel" {
  130. p := PanelPlugin{}
  131. reader.Seek(0, 0)
  132. if err := jsonParser.Decode(&p); err != nil {
  133. return err
  134. }
  135. if p.Type == "" {
  136. return errors.New("Did not find type property in plugin.json")
  137. }
  138. Panels[p.Type] = &p
  139. addPublicContent(p.PublicContent, currentDir)
  140. }
  141. if pluginType == "api" {
  142. p := ApiPlugin{}
  143. reader.Seek(0, 0)
  144. if err := jsonParser.Decode(&p); err != nil {
  145. return err
  146. }
  147. if p.Type == "" {
  148. return errors.New("Did not find type property in plugin.json")
  149. }
  150. ApiPlugins[p.Type] = &p
  151. }
  152. if pluginType == "app" {
  153. p := AppPlugin{}
  154. reader.Seek(0, 0)
  155. if err := jsonParser.Decode(&p); err != nil {
  156. return err
  157. }
  158. if p.Type == "" {
  159. return errors.New("Did not find type property in plugin.json")
  160. }
  161. Apps[p.Type] = &p
  162. addPublicContent(p.PublicContent, currentDir)
  163. }
  164. return nil
  165. }
  166. func GetEnabledPlugins(orgApps []*models.AppPlugin) EnabledPlugins {
  167. enabledPlugins := NewEnabledPlugins()
  168. orgAppsMap := make(map[string]*models.AppPlugin)
  169. for _, orgApp := range orgApps {
  170. orgAppsMap[orgApp.Type] = orgApp
  171. }
  172. seenPanels := make(map[string]bool)
  173. seenApi := make(map[string]bool)
  174. for appType, installedApp := range Apps {
  175. var app AppPlugin
  176. app = *installedApp
  177. // check if the app is stored in the DB for this org and if so, use the
  178. // state stored there.
  179. if b, ok := orgAppsMap[appType]; ok {
  180. app.Enabled = b.Enabled
  181. app.Pinned = b.Pinned
  182. }
  183. // if app.Enabled {
  184. // for _, d := range app.DatasourcePlugins {
  185. // if ds, ok := DataSources[d]; ok {
  186. // enabledPlugins.DataSourcePlugins[d] = ds
  187. // }
  188. // }
  189. // for _, p := range app.PanelPlugins {
  190. // if panel, ok := Panels[p]; ok {
  191. // if _, ok := seenPanels[p]; !ok {
  192. // seenPanels[p] = true
  193. // enabledPlugins.PanelPlugins = append(enabledPlugins.PanelPlugins, panel)
  194. // }
  195. // }
  196. // }
  197. // for _, a := range app.ApiPlugins {
  198. // if api, ok := ApiPlugins[a]; ok {
  199. // if _, ok := seenApi[a]; !ok {
  200. // seenApi[a] = true
  201. // enabledPlugins.ApiPlugins = append(enabledPlugins.ApiPlugins, api)
  202. // }
  203. // }
  204. // }
  205. // enabledPlugins.AppPlugins = append(enabledPlugins.AppPlugins, &app)
  206. // }
  207. }
  208. // add all plugins that are not part of an App.
  209. for d, installedDs := range DataSources {
  210. if installedDs.App == "" {
  211. enabledPlugins.DataSources[d] = installedDs
  212. }
  213. }
  214. for p, panel := range Panels {
  215. if panel.App == "" {
  216. if _, ok := seenPanels[p]; !ok {
  217. seenPanels[p] = true
  218. enabledPlugins.Panels = append(enabledPlugins.Panels, panel)
  219. }
  220. }
  221. }
  222. for a, api := range ApiPlugins {
  223. if api.App == "" {
  224. if _, ok := seenApi[a]; !ok {
  225. seenApi[a] = true
  226. enabledPlugins.ApiList = append(enabledPlugins.ApiList, api)
  227. }
  228. }
  229. }
  230. return enabledPlugins
  231. }