plugins.go 7.0 KB

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