plugins.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. if pluginDir != "data/plugins" {
  73. log.Warn("Could not scan dir \"%v\" error: %s", pluginDir, err)
  74. }
  75. return err
  76. }
  77. if len(scanner.errors) > 0 {
  78. return errors.New("Some plugins failed to load")
  79. }
  80. return nil
  81. }
  82. func (scanner *PluginScanner) walker(currentPath string, f os.FileInfo, err error) error {
  83. if err != nil {
  84. return err
  85. }
  86. if f.IsDir() {
  87. return nil
  88. }
  89. if f.Name() == "plugin.json" {
  90. err := scanner.loadPluginJson(currentPath)
  91. if err != nil {
  92. log.Error(3, "Failed to load plugin json file: %v, err: %v", currentPath, err)
  93. scanner.errors = append(scanner.errors, err)
  94. }
  95. }
  96. return nil
  97. }
  98. func addPublicContent(public *PublicContent, currentDir string) {
  99. if public != nil {
  100. public.Dir = path.Join(currentDir, public.Dir)
  101. StaticRoutes = append(StaticRoutes, public)
  102. }
  103. }
  104. func (scanner *PluginScanner) loadPluginJson(pluginJsonFilePath string) error {
  105. currentDir := filepath.Dir(pluginJsonFilePath)
  106. reader, err := os.Open(pluginJsonFilePath)
  107. if err != nil {
  108. return err
  109. }
  110. defer reader.Close()
  111. jsonParser := json.NewDecoder(reader)
  112. pluginJson := make(map[string]interface{})
  113. if err := jsonParser.Decode(&pluginJson); err != nil {
  114. return err
  115. }
  116. pluginType, exists := pluginJson["pluginType"]
  117. if !exists {
  118. return errors.New("Did not find pluginType property in plugin.json")
  119. }
  120. if pluginType == "datasource" {
  121. p := DataSourcePlugin{}
  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. DataSources[p.Type] = &p
  130. addPublicContent(p.PublicContent, currentDir)
  131. }
  132. if pluginType == "panel" {
  133. p := PanelPlugin{}
  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. Panels[p.Type] = &p
  142. addPublicContent(p.PublicContent, currentDir)
  143. }
  144. if pluginType == "api" {
  145. p := ApiPlugin{}
  146. reader.Seek(0, 0)
  147. if err := jsonParser.Decode(&p); err != nil {
  148. return err
  149. }
  150. if p.Type == "" {
  151. return errors.New("Did not find type property in plugin.json")
  152. }
  153. ApiPlugins[p.Type] = &p
  154. }
  155. if pluginType == "app" {
  156. p := AppPlugin{}
  157. reader.Seek(0, 0)
  158. if err := jsonParser.Decode(&p); err != nil {
  159. return err
  160. }
  161. if p.Type == "" {
  162. return errors.New("Did not find type property in plugin.json")
  163. }
  164. Apps[p.Type] = &p
  165. addPublicContent(p.PublicContent, currentDir)
  166. }
  167. return nil
  168. }
  169. func GetEnabledPlugins(orgApps []*models.AppPlugin) EnabledPlugins {
  170. enabledPlugins := NewEnabledPlugins()
  171. orgAppsMap := make(map[string]*models.AppPlugin)
  172. for _, orgApp := range orgApps {
  173. orgAppsMap[orgApp.Type] = orgApp
  174. }
  175. seenPanels := make(map[string]bool)
  176. seenApi := make(map[string]bool)
  177. for appType, installedApp := range Apps {
  178. var app AppPlugin
  179. app = *installedApp
  180. // check if the app is stored in the DB for this org and if so, use the
  181. // state stored there.
  182. if b, ok := orgAppsMap[appType]; ok {
  183. app.Enabled = b.Enabled
  184. app.Pinned = b.Pinned
  185. }
  186. // if app.Enabled {
  187. // for _, d := range app.DatasourcePlugins {
  188. // if ds, ok := DataSources[d]; ok {
  189. // enabledPlugins.DataSourcePlugins[d] = ds
  190. // }
  191. // }
  192. // for _, p := range app.PanelPlugins {
  193. // if panel, ok := Panels[p]; ok {
  194. // if _, ok := seenPanels[p]; !ok {
  195. // seenPanels[p] = true
  196. // enabledPlugins.PanelPlugins = append(enabledPlugins.PanelPlugins, panel)
  197. // }
  198. // }
  199. // }
  200. // for _, a := range app.ApiPlugins {
  201. // if api, ok := ApiPlugins[a]; ok {
  202. // if _, ok := seenApi[a]; !ok {
  203. // seenApi[a] = true
  204. // enabledPlugins.ApiPlugins = append(enabledPlugins.ApiPlugins, api)
  205. // }
  206. // }
  207. // }
  208. // enabledPlugins.AppPlugins = append(enabledPlugins.AppPlugins, &app)
  209. // }
  210. }
  211. // add all plugins that are not part of an App.
  212. for d, installedDs := range DataSources {
  213. if installedDs.App == "" {
  214. enabledPlugins.DataSources[d] = installedDs
  215. }
  216. }
  217. for p, panel := range Panels {
  218. if panel.App == "" {
  219. if _, ok := seenPanels[p]; !ok {
  220. seenPanels[p] = true
  221. enabledPlugins.Panels = append(enabledPlugins.Panels, panel)
  222. }
  223. }
  224. }
  225. for a, api := range ApiPlugins {
  226. if api.App == "" {
  227. if _, ok := seenApi[a]; !ok {
  228. seenApi[a] = true
  229. enabledPlugins.ApiList = append(enabledPlugins.ApiList, api)
  230. }
  231. }
  232. }
  233. return enabledPlugins
  234. }