update_checker.go 2.5 KB

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