plugins.go 7.2 KB

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