traefik/pkg/version/version.go

110 lines
2.8 KiB
Go
Raw Normal View History

package version
import (
"context"
"net/http"
"net/url"
"time"
"github.com/google/go-github/v28/github"
2019-08-03 01:58:23 +00:00
"github.com/gorilla/mux"
goversion "github.com/hashicorp/go-version"
"github.com/traefik/traefik/v2/pkg/log"
"github.com/unrolled/render"
)
var (
// Version holds the current version of traefik.
Version = "dev"
// Codename holds the current version codename of traefik.
Codename = "cheddar" // beta cheese
// BuildDate holds the build date of traefik.
BuildDate = "I don't remember exactly"
// StartDate holds the start date of traefik.
StartDate = time.Now()
// DisableDashboardAd disables ad in the dashboard.
DisableDashboardAd = false
)
2020-05-11 10:06:07 +00:00
// Handler expose version routes.
type Handler struct{}
2020-07-07 12:42:03 +00:00
var templatesRenderer = render.New(render.Options{
Directory: "nowhere",
})
2018-11-14 09:18:03 +00:00
// Append adds version routes on a router.
func (v Handler) Append(router *mux.Router) {
2017-11-20 08:40:03 +00:00
router.Methods(http.MethodGet).Path("/api/version").
HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
v := struct {
Version string
Codename string
StartDate time.Time `json:"startDate"`
UUID string `json:"uuid,omitempty"`
DisableDashboardAd bool `json:"disableDashboardAd,omitempty"`
}{
Version: Version,
Codename: Codename,
StartDate: StartDate,
DisableDashboardAd: DisableDashboardAd,
}
2018-08-06 18:00:03 +00:00
if err := templatesRenderer.JSON(response, http.StatusOK, v); err != nil {
2019-09-13 17:28:04 +00:00
log.WithoutContext().Error(err)
2018-08-06 18:00:03 +00:00
}
})
}
2020-05-11 10:06:07 +00:00
// CheckNewVersion checks if a new version is available.
func CheckNewVersion() {
if Version == "dev" {
return
}
2019-09-13 17:28:04 +00:00
logger := log.WithoutContext()
client := github.NewClient(nil)
2019-09-13 17:28:04 +00:00
updateURL, err := url.Parse("https://update.traefik.io/")
if err != nil {
2019-09-13 17:28:04 +00:00
logger.Warnf("Error checking new version: %s", err)
return
}
client.BaseURL = updateURL
2019-09-13 17:28:04 +00:00
releases, resp, err := client.Repositories.ListReleases(context.Background(), "traefik", "traefik", nil)
if err != nil {
2019-09-13 17:28:04 +00:00
logger.Warnf("Error checking new version: %s", err)
return
}
2017-11-20 08:40:03 +00:00
if resp.StatusCode != http.StatusOK {
2019-09-13 17:28:04 +00:00
logger.Warnf("Error checking new version: status=%s", resp.Status)
return
}
currentVersion, err := goversion.NewVersion(Version)
if err != nil {
2019-09-13 17:28:04 +00:00
logger.Warnf("Error checking new version: %s", err)
return
}
for _, release := range releases {
releaseVersion, err := goversion.NewVersion(*release.TagName)
if err != nil {
2019-09-13 17:28:04 +00:00
logger.Warnf("Error checking new version: %s", err)
return
}
if len(currentVersion.Prerelease()) == 0 && len(releaseVersion.Prerelease()) > 0 {
continue
}
if releaseVersion.GreaterThan(currentVersion) {
logger.Warnf("A new release of Traefik has been found: %s. Please consider updating.", releaseVersion.String())
return
}
}
}