2016-07-21 14:33:49 +00:00
|
|
|
package version
|
2015-11-01 21:59:59 +00:00
|
|
|
|
2016-10-27 14:17:02 +00:00
|
|
|
import (
|
2017-04-11 15:10:46 +00:00
|
|
|
"context"
|
2017-11-09 15:12:04 +00:00
|
|
|
"net/http"
|
2016-12-30 08:21:13 +00:00
|
|
|
"net/url"
|
2020-05-04 09:40:46 +00:00
|
|
|
"time"
|
2016-12-30 08:21:13 +00:00
|
|
|
|
2019-08-29 07:56:04 +00:00
|
|
|
"github.com/google/go-github/v28/github"
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/gorilla/mux"
|
2016-10-27 14:17:02 +00:00
|
|
|
goversion "github.com/hashicorp/go-version"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
2017-11-09 15:12:04 +00:00
|
|
|
"github.com/unrolled/render"
|
2016-10-27 14:17:02 +00:00
|
|
|
)
|
|
|
|
|
2015-11-01 21:59:59 +00:00
|
|
|
var (
|
2015-11-06 17:11:57 +00:00
|
|
|
// Version holds the current version of traefik.
|
2016-06-02 13:17:04 +00:00
|
|
|
Version = "dev"
|
2016-07-05 19:03:37 +00:00
|
|
|
// Codename holds the current version codename of traefik.
|
|
|
|
Codename = "cheddar" // beta cheese
|
2015-11-06 17:11:57 +00:00
|
|
|
// BuildDate holds the build date of traefik.
|
2016-06-02 13:17:04 +00:00
|
|
|
BuildDate = "I don't remember exactly"
|
2020-05-04 09:40:46 +00:00
|
|
|
// StartDate holds the start date of traefik.
|
|
|
|
StartDate = time.Now()
|
2023-07-19 14:56:05 +00:00
|
|
|
// DisableDashboardAd disables ad in the dashboard.
|
|
|
|
DisableDashboardAd = false
|
2015-11-01 21:59:59 +00:00
|
|
|
)
|
2016-10-27 14:17:02 +00:00
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// Handler expose version routes.
|
2017-11-09 15:12:04 +00:00
|
|
|
type Handler struct{}
|
|
|
|
|
2020-07-07 12:42:03 +00:00
|
|
|
var templatesRenderer = render.New(render.Options{
|
|
|
|
Directory: "nowhere",
|
|
|
|
})
|
2017-11-09 15:12:04 +00:00
|
|
|
|
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").
|
2017-11-09 15:12:04 +00:00
|
|
|
HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
|
|
|
v := struct {
|
2023-07-19 14:56:05 +00:00
|
|
|
Version string
|
|
|
|
Codename string
|
|
|
|
StartDate time.Time `json:"startDate"`
|
|
|
|
UUID string `json:"uuid,omitempty"`
|
|
|
|
DisableDashboardAd bool `json:"disableDashboardAd,omitempty"`
|
2017-11-09 15:12:04 +00:00
|
|
|
}{
|
2023-07-19 14:56:05 +00:00
|
|
|
Version: Version,
|
|
|
|
Codename: Codename,
|
|
|
|
StartDate: StartDate,
|
|
|
|
DisableDashboardAd: DisableDashboardAd,
|
2017-11-09 15:12:04 +00:00
|
|
|
}
|
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
|
|
|
}
|
2017-11-09 15:12:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// CheckNewVersion checks if a new version is available.
|
2016-10-27 14:17:02 +00:00
|
|
|
func CheckNewVersion() {
|
|
|
|
if Version == "dev" {
|
|
|
|
return
|
|
|
|
}
|
2019-09-13 17:28:04 +00:00
|
|
|
|
|
|
|
logger := log.WithoutContext()
|
|
|
|
|
2016-10-27 14:17:02 +00:00
|
|
|
client := github.NewClient(nil)
|
2019-09-13 17:28:04 +00:00
|
|
|
|
2019-08-29 07:56:04 +00:00
|
|
|
updateURL, err := url.Parse("https://update.traefik.io/")
|
2016-10-27 14:17:02 +00:00
|
|
|
if err != nil {
|
2019-09-13 17:28:04 +00:00
|
|
|
logger.Warnf("Error checking new version: %s", err)
|
2016-10-27 14:17:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
client.BaseURL = updateURL
|
2019-09-13 17:28:04 +00:00
|
|
|
|
2020-09-16 13:46:04 +00:00
|
|
|
releases, resp, err := client.Repositories.ListReleases(context.Background(), "traefik", "traefik", nil)
|
2016-10-27 14:17:02 +00:00
|
|
|
if err != nil {
|
2019-09-13 17:28:04 +00:00
|
|
|
logger.Warnf("Error checking new version: %s", err)
|
2016-10-27 14:17:02 +00:00
|
|
|
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)
|
2016-10-27 14:17:02 +00:00
|
|
|
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)
|
2016-10-27 14:17:02 +00:00
|
|
|
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)
|
2016-10-27 14:17:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(currentVersion.Prerelease()) == 0 && len(releaseVersion.Prerelease()) > 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if releaseVersion.GreaterThan(currentVersion) {
|
2024-07-17 07:28:03 +00:00
|
|
|
logger.Warnf("A new release of Traefik has been found: %s. Please consider updating.", releaseVersion.String())
|
2016-10-27 14:17:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|