2016-07-21 17:33:49 +03:00
package version
2015-11-02 00:59:59 +03:00
2016-10-27 17:17:02 +03:00
import (
2017-04-11 18:10:46 +03:00
"context"
2017-11-09 18:12:04 +03:00
"net/http"
2016-12-30 11:21:13 +03:00
"net/url"
2017-11-09 18:12:04 +03:00
"github.com/containous/mux"
2019-03-15 11:42:03 +03:00
"github.com/containous/traefik/pkg/log"
2016-10-27 17:17:02 +03:00
"github.com/google/go-github/github"
goversion "github.com/hashicorp/go-version"
2017-11-09 18:12:04 +03:00
"github.com/unrolled/render"
2016-10-27 17:17:02 +03:00
)
2015-11-02 00:59:59 +03:00
var (
2015-11-06 20:11:57 +03:00
// Version holds the current version of traefik.
2016-06-02 16:17:04 +03:00
Version = "dev"
2016-07-05 22:03:37 +03:00
// Codename holds the current version codename of traefik.
Codename = "cheddar" // beta cheese
2015-11-06 20:11:57 +03:00
// BuildDate holds the build date of traefik.
2016-06-02 16:17:04 +03:00
BuildDate = "I don't remember exactly"
2015-11-02 00:59:59 +03:00
)
2016-10-27 17:17:02 +03:00
2017-11-09 18:12:04 +03:00
// Handler expose version routes
type Handler struct { }
var (
templatesRenderer = render . New ( render . Options {
Directory : "nowhere" ,
} )
)
2018-11-14 12:18:03 +03:00
// Append adds version routes on a router.
func ( v Handler ) Append ( router * mux . Router ) {
2017-11-20 11:40:03 +03:00
router . Methods ( http . MethodGet ) . Path ( "/api/version" ) .
2017-11-09 18:12:04 +03:00
HandlerFunc ( func ( response http . ResponseWriter , request * http . Request ) {
v := struct {
Version string
Codename string
} {
Version : Version ,
Codename : Codename ,
}
2018-08-06 21:00:03 +03:00
if err := templatesRenderer . JSON ( response , http . StatusOK , v ) ; err != nil {
log . Error ( err )
}
2017-11-09 18:12:04 +03:00
} )
}
2016-10-27 17:17:02 +03:00
// CheckNewVersion checks if a new version is available
func CheckNewVersion ( ) {
if Version == "dev" {
return
}
client := github . NewClient ( nil )
updateURL , err := url . Parse ( "https://update.traefik.io" )
if err != nil {
log . Warnf ( "Error checking new version: %s" , err )
return
}
client . BaseURL = updateURL
2017-04-11 18:10:46 +03:00
releases , resp , err := client . Repositories . ListReleases ( context . Background ( ) , "containous" , "traefik" , nil )
2016-10-27 17:17:02 +03:00
if err != nil {
log . Warnf ( "Error checking new version: %s" , err )
return
}
2017-11-20 11:40:03 +03:00
if resp . StatusCode != http . StatusOK {
2016-10-27 17:17:02 +03:00
log . Warnf ( "Error checking new version: status=%s" , resp . Status )
return
}
currentVersion , err := goversion . NewVersion ( Version )
if err != nil {
log . Warnf ( "Error checking new version: %s" , err )
return
}
for _ , release := range releases {
releaseVersion , err := goversion . NewVersion ( * release . TagName )
if err != nil {
log . Warnf ( "Error checking new version: %s" , err )
return
}
if len ( currentVersion . Prerelease ( ) ) == 0 && len ( releaseVersion . Prerelease ( ) ) > 0 {
continue
}
if releaseVersion . GreaterThan ( currentVersion ) {
log . Warnf ( "A new release has been found: %s. Please consider updating." , releaseVersion . String ( ) )
return
}
}
}