plugins.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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, pluginCommon *PluginCommon) (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": "public/plugins/" + pluginCommon.Id,
  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. pluginCommon := PluginCommon{}
  133. if err := jsonParser.Decode(&pluginCommon); err != nil {
  134. return err
  135. }
  136. if pluginCommon.Id == "" || pluginCommon.Type == "" {
  137. return errors.New("Did not find type and id property in plugin.json")
  138. }
  139. reader.Seek(0, 0)
  140. if newReader, err := interpolatePluginJson(reader, &pluginCommon); err != nil {
  141. return err
  142. } else {
  143. jsonParser = json.NewDecoder(newReader)
  144. }
  145. switch pluginCommon.Type {
  146. case "datasource":
  147. p := DataSourcePlugin{}
  148. if err := jsonParser.Decode(&p); err != nil {
  149. return err
  150. }
  151. DataSources[p.Id] = &p
  152. addPublicContent(p.PublicContent, currentDir)
  153. case "panel":
  154. p := PanelPlugin{}
  155. reader.Seek(0, 0)
  156. if err := jsonParser.Decode(&p); err != nil {
  157. return err
  158. }
  159. Panels[p.Id] = &p
  160. addPublicContent(p.PublicContent, currentDir)
  161. case "api":
  162. p := ApiPlugin{}
  163. reader.Seek(0, 0)
  164. if err := jsonParser.Decode(&p); err != nil {
  165. return err
  166. }
  167. ApiPlugins[p.Id] = &p
  168. case "app":
  169. p := AppPlugin{}
  170. reader.Seek(0, 0)
  171. if err := jsonParser.Decode(&p); err != nil {
  172. return err
  173. }
  174. Apps[p.Id] = &p
  175. addPublicContent(p.PublicContent, currentDir)
  176. default:
  177. return errors.New("Unkown plugin type " + pluginCommon.Type)
  178. }
  179. return nil
  180. }
  181. func GetEnabledPlugins(orgApps []*models.AppPlugin) EnabledPlugins {
  182. enabledPlugins := NewEnabledPlugins()
  183. orgAppsMap := make(map[string]*models.AppPlugin)
  184. for _, orgApp := range orgApps {
  185. orgAppsMap[orgApp.Type] = orgApp
  186. }
  187. seenPanels := make(map[string]bool)
  188. seenApi := make(map[string]bool)
  189. for appType, installedApp := range Apps {
  190. var app AppPlugin
  191. app = *installedApp
  192. // check if the app is stored in the DB for this org and if so, use the
  193. // state stored there.
  194. if b, ok := orgAppsMap[appType]; ok {
  195. app.Enabled = b.Enabled
  196. app.Pinned = b.Pinned
  197. }
  198. // if app.Enabled {
  199. // for _, d := range app.DatasourcePlugins {
  200. // if ds, ok := DataSources[d]; ok {
  201. // enabledPlugins.DataSourcePlugins[d] = ds
  202. // }
  203. // }
  204. // for _, p := range app.PanelPlugins {
  205. // if panel, ok := Panels[p]; ok {
  206. // if _, ok := seenPanels[p]; !ok {
  207. // seenPanels[p] = true
  208. // enabledPlugins.PanelPlugins = append(enabledPlugins.PanelPlugins, panel)
  209. // }
  210. // }
  211. // }
  212. // for _, a := range app.ApiPlugins {
  213. // if api, ok := ApiPlugins[a]; ok {
  214. // if _, ok := seenApi[a]; !ok {
  215. // seenApi[a] = true
  216. // enabledPlugins.ApiPlugins = append(enabledPlugins.ApiPlugins, api)
  217. // }
  218. // }
  219. // }
  220. // enabledPlugins.AppPlugins = append(enabledPlugins.AppPlugins, &app)
  221. // }
  222. }
  223. // add all plugins that are not part of an App.
  224. for d, installedDs := range DataSources {
  225. if installedDs.App == "" {
  226. enabledPlugins.DataSources[d] = installedDs
  227. }
  228. }
  229. for p, panel := range Panels {
  230. if panel.App == "" {
  231. if _, ok := seenPanels[p]; !ok {
  232. seenPanels[p] = true
  233. enabledPlugins.Panels = append(enabledPlugins.Panels, panel)
  234. }
  235. }
  236. }
  237. for a, api := range ApiPlugins {
  238. if api.App == "" {
  239. if _, ok := seenApi[a]; !ok {
  240. seenApi[a] = true
  241. enabledPlugins.ApiList = append(enabledPlugins.ApiList, api)
  242. }
  243. }
  244. }
  245. return enabledPlugins
  246. }