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"
2020-05-04 12:40:46 +03:00
"time"
2016-12-30 11:21:13 +03:00
2019-08-29 10:56:04 +03:00
"github.com/google/go-github/v28/github"
2019-08-03 04:58:23 +03:00
"github.com/gorilla/mux"
2016-10-27 17:17:02 +03:00
goversion "github.com/hashicorp/go-version"
2020-09-16 16:46:04 +03:00
"github.com/traefik/traefik/v2/pkg/log"
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"
2020-05-04 12:40:46 +03:00
// StartDate holds the start date of traefik.
StartDate = time . Now ( )
// UUID instance uuid.
UUID string
2021-03-22 21:18:04 +03:00
// PilotEnabled activate integration of pilot into the dashboard.
PilotEnabled bool
2015-11-02 00:59:59 +03:00
)
2016-10-27 17:17:02 +03:00
2020-05-11 13:06:07 +03:00
// Handler expose version routes.
2017-11-09 18:12:04 +03:00
type Handler struct { }
2020-07-07 15:42:03 +03:00
var templatesRenderer = render . New ( render . Options {
Directory : "nowhere" ,
} )
2017-11-09 18:12:04 +03:00
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 {
2021-03-22 21:18:04 +03:00
Version string
Codename string
StartDate time . Time ` json:"startDate" `
UUID string ` json:"uuid,omitempty" `
PilotEnabled bool ` json:"pilotEnabled" `
2017-11-09 18:12:04 +03:00
} {
2021-03-22 21:18:04 +03:00
Version : Version ,
Codename : Codename ,
StartDate : StartDate ,
UUID : UUID ,
PilotEnabled : PilotEnabled ,
2017-11-09 18:12:04 +03:00
}
2018-08-06 21:00:03 +03:00
if err := templatesRenderer . JSON ( response , http . StatusOK , v ) ; err != nil {
2019-09-13 20:28:04 +03:00
log . WithoutContext ( ) . Error ( err )
2018-08-06 21:00:03 +03:00
}
2017-11-09 18:12:04 +03:00
} )
}
2020-05-11 13:06:07 +03:00
// CheckNewVersion checks if a new version is available.
2016-10-27 17:17:02 +03:00
func CheckNewVersion ( ) {
if Version == "dev" {
return
}
2019-09-13 20:28:04 +03:00
logger := log . WithoutContext ( )
2016-10-27 17:17:02 +03:00
client := github . NewClient ( nil )
2019-09-13 20:28:04 +03:00
2019-08-29 10:56:04 +03:00
updateURL , err := url . Parse ( "https://update.traefik.io/" )
2016-10-27 17:17:02 +03:00
if err != nil {
2019-09-13 20:28:04 +03:00
logger . Warnf ( "Error checking new version: %s" , err )
2016-10-27 17:17:02 +03:00
return
}
client . BaseURL = updateURL
2019-09-13 20:28:04 +03:00
2020-09-16 16:46:04 +03:00
releases , resp , err := client . Repositories . ListReleases ( context . Background ( ) , "traefik" , "traefik" , nil )
2016-10-27 17:17:02 +03:00
if err != nil {
2019-09-13 20:28:04 +03:00
logger . Warnf ( "Error checking new version: %s" , err )
2016-10-27 17:17:02 +03:00
return
}
2017-11-20 11:40:03 +03:00
if resp . StatusCode != http . StatusOK {
2019-09-13 20:28:04 +03:00
logger . Warnf ( "Error checking new version: status=%s" , resp . Status )
2016-10-27 17:17:02 +03:00
return
}
currentVersion , err := goversion . NewVersion ( Version )
if err != nil {
2019-09-13 20:28:04 +03:00
logger . Warnf ( "Error checking new version: %s" , err )
2016-10-27 17:17:02 +03:00
return
}
for _ , release := range releases {
releaseVersion , err := goversion . NewVersion ( * release . TagName )
if err != nil {
2019-09-13 20:28:04 +03:00
logger . Warnf ( "Error checking new version: %s" , err )
2016-10-27 17:17:02 +03:00
return
}
if len ( currentVersion . Prerelease ( ) ) == 0 && len ( releaseVersion . Prerelease ( ) ) > 0 {
continue
}
if releaseVersion . GreaterThan ( currentVersion ) {
2019-09-13 20:28:04 +03:00
logger . Warnf ( "A new release has been found: %s. Please consider updating." , releaseVersion . String ( ) )
2016-10-27 17:17:02 +03:00
return
}
}
}