update_checker.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package plugins
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "strings"
  7. "time"
  8. "github.com/grafana/grafana/pkg/log"
  9. "github.com/grafana/grafana/pkg/setting"
  10. )
  11. type GrafanaNetPlugin struct {
  12. Slug string `json:"slug"`
  13. Version string `json:"version"`
  14. }
  15. type GithubLatest struct {
  16. Stable string `json:"stable"`
  17. Testing string `json:"testing"`
  18. }
  19. func StartPluginUpdateChecker() {
  20. if !setting.CheckForUpdates {
  21. return
  22. }
  23. ticker := time.NewTicker(time.Second * 24)
  24. for {
  25. select {
  26. case <-ticker.C:
  27. checkForUpdates()
  28. }
  29. }
  30. }
  31. func getAllExternalPluginSlugs() string {
  32. str := ""
  33. for _, plug := range Plugins {
  34. if plug.IsCorePlugin {
  35. continue
  36. }
  37. str += plug.Id + ","
  38. }
  39. return str
  40. }
  41. func checkForUpdates() {
  42. log.Trace("Checking for updates")
  43. client := http.Client{Timeout: time.Duration(5 * time.Second)}
  44. pluginSlugs := getAllExternalPluginSlugs()
  45. resp, err := client.Get("https://grafana.net/api/plugins/versioncheck?slugIn=" + pluginSlugs + "&grafanaVersion=" + setting.BuildVersion)
  46. if err != nil {
  47. log.Trace("Failed to get plugins repo from grafana.net, %v", err.Error())
  48. return
  49. }
  50. defer resp.Body.Close()
  51. body, err := ioutil.ReadAll(resp.Body)
  52. if err != nil {
  53. log.Trace("Update check failed, reading response from grafana.net, %v", err.Error())
  54. return
  55. }
  56. gNetPlugins := []GrafanaNetPlugin{}
  57. err = json.Unmarshal(body, &gNetPlugins)
  58. if err != nil {
  59. log.Trace("Failed to unmarshal plugin repo, reading response from grafana.net, %v", err.Error())
  60. return
  61. }
  62. for _, plug := range Plugins {
  63. for _, gplug := range gNetPlugins {
  64. if gplug.Slug == plug.Id {
  65. plug.GrafanaNetVersion = gplug.Version
  66. plug.GrafanaNetHasUpdate = plug.Info.Version != plug.GrafanaNetVersion
  67. }
  68. }
  69. }
  70. resp2, err := client.Get("https://raw.githubusercontent.com/grafana/grafana/master/latest.json")
  71. if err != nil {
  72. log.Trace("Failed to get lates.json repo from github: %v", err.Error())
  73. return
  74. }
  75. defer resp2.Body.Close()
  76. body, err = ioutil.ReadAll(resp2.Body)
  77. if err != nil {
  78. log.Trace("Update check failed, reading response from github.net, %v", err.Error())
  79. return
  80. }
  81. var githubLatest GithubLatest
  82. err = json.Unmarshal(body, &githubLatest)
  83. if err != nil {
  84. log.Trace("Failed to unmarshal github latest, reading response from github: %v", err.Error())
  85. return
  86. }
  87. if strings.Contains(setting.BuildVersion, "-") {
  88. GrafanaLatestVersion = githubLatest.Testing
  89. GrafanaHasUpdate = githubLatest.Testing != setting.BuildVersion
  90. } else {
  91. GrafanaLatestVersion = githubLatest.Stable
  92. GrafanaHasUpdate = githubLatest.Stable != setting.BuildVersion
  93. }
  94. }