update_checker.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. "github.com/hashicorp/go-version"
  11. )
  12. var (
  13. httpClient http.Client = http.Client{Timeout: 10 * time.Second}
  14. )
  15. type GrafanaNetPlugin struct {
  16. Slug string `json:"slug"`
  17. Version string `json:"version"`
  18. }
  19. type GithubLatest struct {
  20. Stable string `json:"stable"`
  21. Testing string `json:"testing"`
  22. }
  23. func StartPluginUpdateChecker() {
  24. if !setting.CheckForUpdates {
  25. return
  26. }
  27. // do one check directly
  28. go checkForUpdates()
  29. ticker := time.NewTicker(time.Minute * 10)
  30. for {
  31. select {
  32. case <-ticker.C:
  33. checkForUpdates()
  34. }
  35. }
  36. }
  37. func getAllExternalPluginSlugs() string {
  38. var result []string
  39. for _, plug := range Plugins {
  40. if plug.IsCorePlugin {
  41. continue
  42. }
  43. result = append(result, plug.Id)
  44. }
  45. return strings.Join(result, ",")
  46. }
  47. func checkForUpdates() {
  48. log.Trace("Checking for updates")
  49. pluginSlugs := getAllExternalPluginSlugs()
  50. resp, err := httpClient.Get("https://grafana.com/api/plugins/versioncheck?slugIn=" + pluginSlugs + "&grafanaVersion=" + setting.BuildVersion)
  51. if err != nil {
  52. log.Trace("Failed to get plugins repo from grafana.com, %v", err.Error())
  53. return
  54. }
  55. defer resp.Body.Close()
  56. body, err := ioutil.ReadAll(resp.Body)
  57. if err != nil {
  58. log.Trace("Update check failed, reading response from grafana.com, %v", err.Error())
  59. return
  60. }
  61. gNetPlugins := []GrafanaNetPlugin{}
  62. err = json.Unmarshal(body, &gNetPlugins)
  63. if err != nil {
  64. log.Trace("Failed to unmarshal plugin repo, reading response from grafana.com, %v", err.Error())
  65. return
  66. }
  67. for _, plug := range Plugins {
  68. for _, gplug := range gNetPlugins {
  69. if gplug.Slug == plug.Id {
  70. plug.GrafanaNetVersion = gplug.Version
  71. plugVersion, err1 := version.NewVersion(plug.Info.Version)
  72. gplugVersion, err2 := version.NewVersion(gplug.Version)
  73. if err1 != nil || err2 != nil {
  74. plug.GrafanaNetHasUpdate = plug.Info.Version != plug.GrafanaNetVersion
  75. } else {
  76. plug.GrafanaNetHasUpdate = plugVersion.LessThan(gplugVersion)
  77. }
  78. }
  79. }
  80. }
  81. resp2, err := httpClient.Get("https://raw.githubusercontent.com/grafana/grafana/master/latest.json")
  82. if err != nil {
  83. log.Trace("Failed to get latest.json repo from github.com: %v", err.Error())
  84. return
  85. }
  86. defer resp2.Body.Close()
  87. body, err = ioutil.ReadAll(resp2.Body)
  88. if err != nil {
  89. log.Trace("Update check failed, reading response from github.com, %v", err.Error())
  90. return
  91. }
  92. var githubLatest GithubLatest
  93. err = json.Unmarshal(body, &githubLatest)
  94. if err != nil {
  95. log.Trace("Failed to unmarshal github.com latest, reading response from github.com: %v", err.Error())
  96. return
  97. }
  98. if strings.Contains(setting.BuildVersion, "-") {
  99. GrafanaLatestVersion = githubLatest.Testing
  100. GrafanaHasUpdate = !strings.HasPrefix(setting.BuildVersion, githubLatest.Testing)
  101. } else {
  102. GrafanaLatestVersion = githubLatest.Stable
  103. GrafanaHasUpdate = githubLatest.Stable != setting.BuildVersion
  104. }
  105. currVersion, err1 := version.NewVersion(setting.BuildVersion)
  106. latestVersion, err2 := version.NewVersion(GrafanaLatestVersion)
  107. if err1 == nil && err2 == nil {
  108. GrafanaHasUpdate = currVersion.LessThan(latestVersion)
  109. }
  110. }