2016-04-11 11:21:48 -05:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2016-09-23 07:38:59 -05:00
|
|
|
"github.com/hashicorp/go-version"
|
2016-04-11 11:21:48 -05:00
|
|
|
)
|
|
|
|
|
2016-09-23 02:56:42 -05:00
|
|
|
var (
|
2018-04-27 15:14:36 -05:00
|
|
|
httpClient = http.Client{Timeout: 10 * time.Second}
|
2016-09-23 02:56:42 -05:00
|
|
|
)
|
|
|
|
|
2016-04-11 11:21:48 -05:00
|
|
|
type GrafanaNetPlugin struct {
|
2016-04-11 16:16:52 -05:00
|
|
|
Slug string `json:"slug"`
|
2016-04-11 11:21:48 -05:00
|
|
|
Version string `json:"version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GithubLatest struct {
|
|
|
|
Stable string `json:"stable"`
|
|
|
|
Testing string `json:"testing"`
|
|
|
|
}
|
|
|
|
|
2016-04-11 16:16:52 -05:00
|
|
|
func getAllExternalPluginSlugs() string {
|
2016-09-22 09:16:58 -05:00
|
|
|
var result []string
|
2016-04-11 16:16:52 -05:00
|
|
|
for _, plug := range Plugins {
|
|
|
|
if plug.IsCorePlugin {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-09-22 09:16:58 -05:00
|
|
|
result = append(result, plug.Id)
|
2016-04-11 16:16:52 -05:00
|
|
|
}
|
|
|
|
|
2016-09-22 09:16:58 -05:00
|
|
|
return strings.Join(result, ",")
|
2016-04-11 16:16:52 -05:00
|
|
|
}
|
|
|
|
|
2018-04-27 08:11:55 -05:00
|
|
|
func (pm *PluginManager) checkForUpdates() {
|
|
|
|
if !setting.CheckForUpdates {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pm.log.Debug("Checking for updates")
|
2016-04-11 11:21:48 -05:00
|
|
|
|
2016-04-11 16:16:52 -05:00
|
|
|
pluginSlugs := getAllExternalPluginSlugs()
|
2017-03-20 04:20:32 -05:00
|
|
|
resp, err := httpClient.Get("https://grafana.com/api/plugins/versioncheck?slugIn=" + pluginSlugs + "&grafanaVersion=" + setting.BuildVersion)
|
2016-04-11 11:21:48 -05:00
|
|
|
|
|
|
|
if err != nil {
|
2018-03-12 08:38:37 -05:00
|
|
|
log.Trace("Failed to get plugins repo from grafana.com, %v", err.Error())
|
2016-04-11 11:21:48 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2017-03-20 04:20:32 -05:00
|
|
|
log.Trace("Update check failed, reading response from grafana.com, %v", err.Error())
|
2016-04-11 11:21:48 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-11 16:16:52 -05:00
|
|
|
gNetPlugins := []GrafanaNetPlugin{}
|
|
|
|
err = json.Unmarshal(body, &gNetPlugins)
|
2016-04-11 11:21:48 -05:00
|
|
|
if err != nil {
|
2017-03-20 04:20:32 -05:00
|
|
|
log.Trace("Failed to unmarshal plugin repo, reading response from grafana.com, %v", err.Error())
|
2016-04-11 11:21:48 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, plug := range Plugins {
|
2016-04-11 16:16:52 -05:00
|
|
|
for _, gplug := range gNetPlugins {
|
|
|
|
if gplug.Slug == plug.Id {
|
|
|
|
plug.GrafanaNetVersion = gplug.Version
|
2016-09-23 07:38:59 -05:00
|
|
|
|
|
|
|
plugVersion, err1 := version.NewVersion(plug.Info.Version)
|
|
|
|
gplugVersion, err2 := version.NewVersion(gplug.Version)
|
|
|
|
|
|
|
|
if err1 != nil || err2 != nil {
|
|
|
|
plug.GrafanaNetHasUpdate = plug.Info.Version != plug.GrafanaNetVersion
|
|
|
|
} else {
|
|
|
|
plug.GrafanaNetHasUpdate = plugVersion.LessThan(gplugVersion)
|
|
|
|
}
|
2016-04-11 11:21:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-23 02:56:42 -05:00
|
|
|
resp2, err := httpClient.Get("https://raw.githubusercontent.com/grafana/grafana/master/latest.json")
|
2016-04-11 11:21:48 -05:00
|
|
|
if err != nil {
|
2018-03-12 08:38:37 -05:00
|
|
|
log.Trace("Failed to get latest.json repo from github.com: %v", err.Error())
|
2016-04-11 11:21:48 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp2.Body.Close()
|
|
|
|
body, err = ioutil.ReadAll(resp2.Body)
|
|
|
|
if err != nil {
|
2016-05-10 11:06:51 -05:00
|
|
|
log.Trace("Update check failed, reading response from github.com, %v", err.Error())
|
2016-04-11 11:21:48 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var githubLatest GithubLatest
|
|
|
|
err = json.Unmarshal(body, &githubLatest)
|
|
|
|
if err != nil {
|
2018-03-12 08:38:37 -05:00
|
|
|
log.Trace("Failed to unmarshal github.com latest, reading response from github.com: %v", err.Error())
|
2016-04-11 11:21:48 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(setting.BuildVersion, "-") {
|
|
|
|
GrafanaLatestVersion = githubLatest.Testing
|
2016-04-12 09:28:32 -05:00
|
|
|
GrafanaHasUpdate = !strings.HasPrefix(setting.BuildVersion, githubLatest.Testing)
|
2016-04-11 11:21:48 -05:00
|
|
|
} else {
|
|
|
|
GrafanaLatestVersion = githubLatest.Stable
|
|
|
|
GrafanaHasUpdate = githubLatest.Stable != setting.BuildVersion
|
|
|
|
}
|
2016-09-23 07:38:59 -05:00
|
|
|
|
|
|
|
currVersion, err1 := version.NewVersion(setting.BuildVersion)
|
|
|
|
latestVersion, err2 := version.NewVersion(GrafanaLatestVersion)
|
|
|
|
|
|
|
|
if err1 == nil && err2 == nil {
|
|
|
|
GrafanaHasUpdate = currVersion.LessThan(latestVersion)
|
|
|
|
}
|
2016-04-11 11:21:48 -05:00
|
|
|
}
|