From 3322e564fd1fc298a11a6be5dbbc6bb0c17db2a3 Mon Sep 17 00:00:00 2001 From: Emile Vauge Date: Thu, 27 Oct 2016 16:17:02 +0200 Subject: [PATCH] Add version check Signed-off-by: Emile Vauge --- README.md | 2 +- configuration.go | 2 ++ docs/toml.md | 22 ++++++++++++++++++ glide.lock | 10 +++++++-- glide.yaml | 4 +++- traefik.go | 19 ++++++++++++++-- traefik.sample.toml | 14 ++++++++++++ version/version.go | 54 +++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 121 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 74a832141..23898c243 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ Run it and forget it! - Circuit breakers on backends - Round Robin, rebalancer load-balancers - Rest Metrics -- [Tiny](https://imagelayers.io/?images=traefik) [official](https://hub.docker.com/r/_/traefik/) docker image included +- [Tiny](https://microbadger.com/images/traefik) [official](https://hub.docker.com/r/_/traefik/) docker image included - SSL backends support - SSL frontend support (with SNI) - Clean AngularJS Web UI diff --git a/configuration.go b/configuration.go index a677145da..2e55a4ec2 100644 --- a/configuration.go +++ b/configuration.go @@ -25,6 +25,7 @@ type TraefikConfiguration struct { type GlobalConfiguration struct { GraceTimeOut int64 `short:"g" description:"Duration to give active requests a chance to finish during hot-reload"` Debug bool `short:"d" description:"Enable debug mode"` + CheckNewVersion bool `description:"Periodically check if a new version has been released"` AccessLogsFile string `description:"Access logs file"` TraefikLogsFile string `description:"Traefik logs file"` LogLevel string `short:"l" description:"Log level"` @@ -409,6 +410,7 @@ func NewTraefikConfiguration() *TraefikConfiguration { DefaultEntryPoints: []string{}, ProvidersThrottleDuration: time.Duration(2 * time.Second), MaxIdleConnsPerHost: 200, + CheckNewVersion: true, }, ConfigFile: "", } diff --git a/docs/toml.md b/docs/toml.md index b58f911ca..cc40ca0c7 100644 --- a/docs/toml.md +++ b/docs/toml.md @@ -9,6 +9,28 @@ # Global configuration ################################################################ +# Timeout in seconds. +# Duration to give active requests a chance to finish during hot-reloads +# +# Optional +# Default: 10 +# +# graceTimeOut = 10 + +# Enable debug mode +# +# Optional +# Default: false +# +# debug = true + +# Periodically check if a new version has been released +# +# Optional +# Default: true +# +# checkNewVersion = false + # Traefik logs file # If not defined, logs to stdout # diff --git a/glide.lock b/glide.lock index bec1206d5..574af3619 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: 45d9abd00276bba5aaeb92cd5f2464e404bba3cf90f37aa538d4866041626327 -updated: 2016-10-26T14:26:07.740582437+02:00 +hash: d2bb370ffd3c0b528587581bc77d900c98921c02df581594bf19655968aa9849 +updated: 2016-10-27T15:54:07.397981259+02:00 imports: - name: github.com/abbot/go-http-auth version: cb4372376e1e00e9f6ab9ec142e029302c9e7140 @@ -168,6 +168,10 @@ imports: - proto - name: github.com/golang/glog version: fca8c8854093a154ff1eb580aae10276ad6b1b5f +- name: github.com/google/go-github + version: ed33550bbf49ddffcc36e1309d3f58ed0b6c2305 + subpackages: + - github - name: github.com/google/go-querystring version: 9235644dd9e52eeae6fa48efd539fdc351a0af53 subpackages: @@ -180,6 +184,8 @@ imports: - api - name: github.com/hashicorp/go-cleanhttp version: ad28ea4487f05916463e2423a55166280e8254b5 +- name: github.com/hashicorp/go-version + version: deeb027c13a95d56c7585df3fe29207208c6706e - name: github.com/hashicorp/serf version: b03bf85930b2349eb04b97c8fac437495296e3e7 subpackages: diff --git a/glide.yaml b/glide.yaml index 1436021bc..205fdf1eb 100644 --- a/glide.yaml +++ b/glide.yaml @@ -107,4 +107,6 @@ import: - package: github.com/coreos/go-systemd version: v12 subpackages: - - daemon \ No newline at end of file + - daemon +- package: github.com/google/go-github +- package: github.com/hashicorp/go-version \ No newline at end of file diff --git a/traefik.go b/traefik.go index 9f924b3b5..36da79202 100644 --- a/traefik.go +++ b/traefik.go @@ -11,6 +11,7 @@ import ( "runtime" "strings" "text/template" + "time" "github.com/Sirupsen/logrus" "github.com/containous/flaeg" @@ -20,12 +21,12 @@ import ( "github.com/containous/traefik/log" "github.com/containous/traefik/middlewares" "github.com/containous/traefik/provider" + "github.com/containous/traefik/safe" "github.com/containous/traefik/types" "github.com/containous/traefik/version" + "github.com/coreos/go-systemd/daemon" "github.com/docker/libkv/store" "github.com/satori/go.uuid" - - "github.com/coreos/go-systemd/daemon" ) var versionTemplate = `Version: {{.Version}} @@ -263,6 +264,20 @@ func run(traefikConfiguration *TraefikConfiguration) { } jsonConf, _ := json.Marshal(globalConfiguration) log.Infof("Traefik version %s built on %s", version.Version, version.BuildDate) + + if globalConfiguration.CheckNewVersion { + ticker := time.NewTicker(24 * time.Hour) + safe.Go(func() { + version.CheckNewVersion() + for { + select { + case <-ticker.C: + version.CheckNewVersion() + } + } + }) + } + if len(traefikConfiguration.ConfigFile) != 0 { log.Infof("Using TOML configuration file %s", traefikConfiguration.ConfigFile) } diff --git a/traefik.sample.toml b/traefik.sample.toml index 905e55579..c53effd28 100644 --- a/traefik.sample.toml +++ b/traefik.sample.toml @@ -10,6 +10,20 @@ # # graceTimeOut = 10 +# Enable debug mode +# +# Optional +# Default: false +# +# debug = true + +# Periodically check if a new version has been released +# +# Optional +# Default: true +# +# checkNewVersion = false + # Traefik logs file # If not defined, logs to stdout # diff --git a/version/version.go b/version/version.go index ba72b1a42..7e4149caf 100644 --- a/version/version.go +++ b/version/version.go @@ -1,5 +1,12 @@ package version +import ( + "github.com/containous/traefik/log" + "github.com/google/go-github/github" + goversion "github.com/hashicorp/go-version" + "net/url" +) + var ( // Version holds the current version of traefik. Version = "dev" @@ -8,3 +15,50 @@ var ( // BuildDate holds the build date of traefik. BuildDate = "I don't remember exactly" ) + +// 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 + releases, resp, err := client.Repositories.ListReleases("containous", "traefik", nil) + if err != nil { + log.Warnf("Error checking new version: %s", err) + return + } + + if resp.StatusCode != 200 { + 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 + } + } +}