update_checker.go 2.7 KB

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