update_checker.go 2.6 KB

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