Separate command from the main package
This commit is contained in:
parent
a2db3e0499
commit
831a3e384b
12 changed files with 63 additions and 51 deletions
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package bug
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
@ -9,7 +9,9 @@ import (
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/containous/flaeg"
|
"github.com/containous/flaeg"
|
||||||
"github.com/containous/traefik/cmd/traefik/anonymize"
|
"github.com/containous/traefik/anonymize"
|
||||||
|
"github.com/containous/traefik/cmd"
|
||||||
|
"github.com/containous/traefik/cmd/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -83,8 +85,8 @@ Add more configuration information here.
|
||||||
`
|
`
|
||||||
)
|
)
|
||||||
|
|
||||||
// newBugCmd builds a new Bug command
|
// NewCmd builds a new Bug command
|
||||||
func newBugCmd(traefikConfiguration *TraefikConfiguration, traefikPointersConfiguration *TraefikConfiguration) *flaeg.Command {
|
func NewCmd(traefikConfiguration *cmd.TraefikConfiguration, traefikPointersConfiguration *cmd.TraefikConfiguration) *flaeg.Command {
|
||||||
|
|
||||||
//version Command init
|
//version Command init
|
||||||
return &flaeg.Command{
|
return &flaeg.Command{
|
||||||
|
@ -92,30 +94,30 @@ func newBugCmd(traefikConfiguration *TraefikConfiguration, traefikPointersConfig
|
||||||
Description: `Report an issue on Traefik bugtracker`,
|
Description: `Report an issue on Traefik bugtracker`,
|
||||||
Config: traefikConfiguration,
|
Config: traefikConfiguration,
|
||||||
DefaultPointersConfig: traefikPointersConfiguration,
|
DefaultPointersConfig: traefikPointersConfiguration,
|
||||||
Run: runBugCmd(traefikConfiguration),
|
Run: runCmd(traefikConfiguration),
|
||||||
Metadata: map[string]string{
|
Metadata: map[string]string{
|
||||||
"parseAllSources": "true",
|
"parseAllSources": "true",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func runBugCmd(traefikConfiguration *TraefikConfiguration) func() error {
|
func runCmd(traefikConfiguration *cmd.TraefikConfiguration) func() error {
|
||||||
return func() error {
|
return func() error {
|
||||||
|
|
||||||
body, err := createBugReport(traefikConfiguration)
|
body, err := createReport(traefikConfiguration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
sendBugReport(body)
|
sendReport(body)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createBugReport(traefikConfiguration *TraefikConfiguration) (string, error) {
|
func createReport(traefikConfiguration *cmd.TraefikConfiguration) (string, error) {
|
||||||
var version bytes.Buffer
|
var versionPrint bytes.Buffer
|
||||||
if err := getVersionPrint(&version); err != nil {
|
if err := version.GetPrint(&versionPrint); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,7 +135,7 @@ func createBugReport(traefikConfiguration *TraefikConfiguration) (string, error)
|
||||||
Version string
|
Version string
|
||||||
Configuration string
|
Configuration string
|
||||||
}{
|
}{
|
||||||
Version: version.String(),
|
Version: versionPrint.String(),
|
||||||
Configuration: config,
|
Configuration: config,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,7 +147,7 @@ func createBugReport(traefikConfiguration *TraefikConfiguration) (string, error)
|
||||||
return bug.String(), nil
|
return bug.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendBugReport(body string) {
|
func sendReport(body string) {
|
||||||
URL := bugTracker + "?body=" + url.QueryEscape(body)
|
URL := bugTracker + "?body=" + url.QueryEscape(body)
|
||||||
if err := openBrowser(URL); err != nil {
|
if err := openBrowser(URL); err != nil {
|
||||||
fmt.Printf("Please file a new issue at %s using this template:\n\n", bugTracker)
|
fmt.Printf("Please file a new issue at %s using this template:\n\n", bugTracker)
|
|
@ -1,9 +1,10 @@
|
||||||
package main
|
package bug
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/containous/traefik/cmd/traefik/anonymize"
|
"github.com/containous/traefik/anonymize"
|
||||||
|
"github.com/containous/traefik/cmd"
|
||||||
"github.com/containous/traefik/configuration"
|
"github.com/containous/traefik/configuration"
|
||||||
"github.com/containous/traefik/provider/file"
|
"github.com/containous/traefik/provider/file"
|
||||||
"github.com/containous/traefik/tls"
|
"github.com/containous/traefik/tls"
|
||||||
|
@ -11,8 +12,8 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_createBugReport(t *testing.T) {
|
func Test_createReport(t *testing.T) {
|
||||||
traefikConfiguration := &TraefikConfiguration{
|
traefikConfiguration := &cmd.TraefikConfiguration{
|
||||||
ConfigFile: "FOO",
|
ConfigFile: "FOO",
|
||||||
GlobalConfiguration: configuration.GlobalConfiguration{
|
GlobalConfiguration: configuration.GlobalConfiguration{
|
||||||
EntryPoints: configuration.EntryPoints{
|
EntryPoints: configuration.EntryPoints{
|
||||||
|
@ -37,7 +38,7 @@ func Test_createBugReport(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
report, err := createBugReport(traefikConfiguration)
|
report, err := createReport(traefikConfiguration)
|
||||||
assert.NoError(t, err, report)
|
assert.NoError(t, err, report)
|
||||||
|
|
||||||
// exported anonymous configuration
|
// exported anonymous configuration
|
||||||
|
@ -47,7 +48,7 @@ func Test_createBugReport(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_anonymize_traefikConfiguration(t *testing.T) {
|
func Test_anonymize_traefikConfiguration(t *testing.T) {
|
||||||
traefikConfiguration := &TraefikConfiguration{
|
traefikConfiguration := &cmd.TraefikConfiguration{
|
||||||
ConfigFile: "FOO",
|
ConfigFile: "FOO",
|
||||||
GlobalConfiguration: configuration.GlobalConfiguration{
|
GlobalConfiguration: configuration.GlobalConfiguration{
|
||||||
EntryPoints: configuration.EntryPoints{
|
EntryPoints: configuration.EntryPoints{
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package healthcheck
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
@ -9,27 +9,29 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/containous/flaeg"
|
"github.com/containous/flaeg"
|
||||||
|
"github.com/containous/traefik/cmd"
|
||||||
"github.com/containous/traefik/configuration"
|
"github.com/containous/traefik/configuration"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newHealthCheckCmd(traefikConfiguration *TraefikConfiguration, traefikPointersConfiguration *TraefikConfiguration) *flaeg.Command {
|
// NewCmd builds a new HealthCheck command
|
||||||
|
func NewCmd(traefikConfiguration *cmd.TraefikConfiguration, traefikPointersConfiguration *cmd.TraefikConfiguration) *flaeg.Command {
|
||||||
return &flaeg.Command{
|
return &flaeg.Command{
|
||||||
Name: "healthcheck",
|
Name: "healthcheck",
|
||||||
Description: `Calls traefik /ping to check health (web provider must be enabled)`,
|
Description: `Calls traefik /ping to check health (web provider must be enabled)`,
|
||||||
Config: traefikConfiguration,
|
Config: traefikConfiguration,
|
||||||
DefaultPointersConfig: traefikPointersConfiguration,
|
DefaultPointersConfig: traefikPointersConfiguration,
|
||||||
Run: runHealthCheck(traefikConfiguration),
|
Run: runCmd(traefikConfiguration),
|
||||||
Metadata: map[string]string{
|
Metadata: map[string]string{
|
||||||
"parseAllSources": "true",
|
"parseAllSources": "true",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func runHealthCheck(traefikConfiguration *TraefikConfiguration) func() error {
|
func runCmd(traefikConfiguration *cmd.TraefikConfiguration) func() error {
|
||||||
return func() error {
|
return func() error {
|
||||||
traefikConfiguration.GlobalConfiguration.SetEffectiveConfiguration(traefikConfiguration.ConfigFile)
|
traefikConfiguration.GlobalConfiguration.SetEffectiveConfiguration(traefikConfiguration.ConfigFile)
|
||||||
|
|
||||||
resp, errPing := healthCheck(traefikConfiguration.GlobalConfiguration)
|
resp, errPing := Do(traefikConfiguration.GlobalConfiguration)
|
||||||
if errPing != nil {
|
if errPing != nil {
|
||||||
fmt.Printf("Error calling healthcheck: %s\n", errPing)
|
fmt.Printf("Error calling healthcheck: %s\n", errPing)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -44,11 +46,11 @@ func runHealthCheck(traefikConfiguration *TraefikConfiguration) func() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func healthCheck(globalConfiguration configuration.GlobalConfiguration) (*http.Response, error) {
|
// Do try to do a healthcheck
|
||||||
|
func Do(globalConfiguration configuration.GlobalConfiguration) (*http.Response, error) {
|
||||||
if globalConfiguration.Ping == nil {
|
if globalConfiguration.Ping == nil {
|
||||||
return nil, errors.New("please enable `ping` to use health check")
|
return nil, errors.New("please enable `ping` to use health check")
|
||||||
}
|
}
|
||||||
|
|
||||||
pingEntryPoint, ok := globalConfiguration.EntryPoints[globalConfiguration.Ping.EntryPoint]
|
pingEntryPoint, ok := globalConfiguration.EntryPoints[globalConfiguration.Ping.EntryPoint]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("missing `ping` entrypoint")
|
return nil, errors.New("missing `ping` entrypoint")
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package storeconfig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
@ -10,9 +10,11 @@ import (
|
||||||
"github.com/containous/staert"
|
"github.com/containous/staert"
|
||||||
"github.com/containous/traefik/acme"
|
"github.com/containous/traefik/acme"
|
||||||
"github.com/containous/traefik/cluster"
|
"github.com/containous/traefik/cluster"
|
||||||
|
"github.com/containous/traefik/cmd"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newStoreConfigCmd(traefikConfiguration *TraefikConfiguration, traefikPointersConfiguration *TraefikConfiguration) *flaeg.Command {
|
// NewCmd builds a new StoreConfig command
|
||||||
|
func NewCmd(traefikConfiguration *cmd.TraefikConfiguration, traefikPointersConfiguration *cmd.TraefikConfiguration) *flaeg.Command {
|
||||||
return &flaeg.Command{
|
return &flaeg.Command{
|
||||||
Name: "storeconfig",
|
Name: "storeconfig",
|
||||||
Description: `Store the static traefik configuration into a Key-value stores. Traefik will not start.`,
|
Description: `Store the static traefik configuration into a Key-value stores. Traefik will not start.`,
|
||||||
|
@ -24,7 +26,8 @@ func newStoreConfigCmd(traefikConfiguration *TraefikConfiguration, traefikPointe
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func runStoreConfig(kv *staert.KvSource, traefikConfiguration *TraefikConfiguration) func() error {
|
// Run store config in KV
|
||||||
|
func Run(kv *staert.KvSource, traefikConfiguration *cmd.TraefikConfiguration) func() error {
|
||||||
return func() error {
|
return func() error {
|
||||||
if kv == nil {
|
if kv == nil {
|
||||||
return fmt.Errorf("error using command storeconfig, no Key-value store defined")
|
return fmt.Errorf("error using command storeconfig, no Key-value store defined")
|
||||||
|
@ -112,9 +115,9 @@ func runStoreConfig(kv *staert.KvSource, traefikConfiguration *TraefikConfigurat
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// createKvSource creates KvSource
|
// CreateKvSource creates KvSource
|
||||||
// TLS support is enable for Consul and Etcd backends
|
// TLS support is enable for Consul and Etcd backends
|
||||||
func createKvSource(traefikConfiguration *TraefikConfiguration) (*staert.KvSource, error) {
|
func CreateKvSource(traefikConfiguration *cmd.TraefikConfiguration) (*staert.KvSource, error) {
|
||||||
var kv *staert.KvSource
|
var kv *staert.KvSource
|
||||||
var kvStore store.Store
|
var kvStore store.Store
|
||||||
var err error
|
var err error
|
|
@ -14,6 +14,11 @@ import (
|
||||||
"github.com/containous/flaeg"
|
"github.com/containous/flaeg"
|
||||||
"github.com/containous/staert"
|
"github.com/containous/staert"
|
||||||
"github.com/containous/traefik/acme"
|
"github.com/containous/traefik/acme"
|
||||||
|
"github.com/containous/traefik/cmd"
|
||||||
|
"github.com/containous/traefik/cmd/bug"
|
||||||
|
"github.com/containous/traefik/cmd/healthcheck"
|
||||||
|
"github.com/containous/traefik/cmd/storeconfig"
|
||||||
|
cmdVersion "github.com/containous/traefik/cmd/version"
|
||||||
"github.com/containous/traefik/collector"
|
"github.com/containous/traefik/collector"
|
||||||
"github.com/containous/traefik/configuration"
|
"github.com/containous/traefik/configuration"
|
||||||
"github.com/containous/traefik/job"
|
"github.com/containous/traefik/job"
|
||||||
|
@ -33,8 +38,8 @@ import (
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// traefik config inits
|
// traefik config inits
|
||||||
traefikConfiguration := NewTraefikConfiguration()
|
traefikConfiguration := cmd.NewTraefikConfiguration()
|
||||||
traefikPointersConfiguration := NewTraefikDefaultPointersConfiguration()
|
traefikPointersConfiguration := cmd.NewTraefikDefaultPointersConfiguration()
|
||||||
|
|
||||||
// traefik Command init
|
// traefik Command init
|
||||||
traefikCmd := &flaeg.Command{
|
traefikCmd := &flaeg.Command{
|
||||||
|
@ -44,13 +49,13 @@ Complete documentation is available at https://traefik.io`,
|
||||||
Config: traefikConfiguration,
|
Config: traefikConfiguration,
|
||||||
DefaultPointersConfig: traefikPointersConfiguration,
|
DefaultPointersConfig: traefikPointersConfiguration,
|
||||||
Run: func() error {
|
Run: func() error {
|
||||||
run(&traefikConfiguration.GlobalConfiguration, traefikConfiguration.ConfigFile)
|
runCmd(&traefikConfiguration.GlobalConfiguration, traefikConfiguration.ConfigFile)
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// storeconfig Command init
|
// storeconfig Command init
|
||||||
storeConfigCmd := newStoreConfigCmd(traefikConfiguration, traefikPointersConfiguration)
|
storeConfigCmd := storeconfig.NewCmd(traefikConfiguration, traefikPointersConfiguration)
|
||||||
|
|
||||||
// init flaeg source
|
// init flaeg source
|
||||||
f := flaeg.New(traefikCmd, os.Args[1:])
|
f := flaeg.New(traefikCmd, os.Args[1:])
|
||||||
|
@ -65,10 +70,10 @@ Complete documentation is available at https://traefik.io`,
|
||||||
f.AddParser(reflect.TypeOf(types.Buckets{}), &types.Buckets{})
|
f.AddParser(reflect.TypeOf(types.Buckets{}), &types.Buckets{})
|
||||||
|
|
||||||
// add commands
|
// add commands
|
||||||
f.AddCommand(newVersionCmd())
|
f.AddCommand(cmdVersion.NewCmd())
|
||||||
f.AddCommand(newBugCmd(traefikConfiguration, traefikPointersConfiguration))
|
f.AddCommand(bug.NewCmd(traefikConfiguration, traefikPointersConfiguration))
|
||||||
f.AddCommand(storeConfigCmd)
|
f.AddCommand(storeConfigCmd)
|
||||||
f.AddCommand(newHealthCheckCmd(traefikConfiguration, traefikPointersConfiguration))
|
f.AddCommand(healthcheck.NewCmd(traefikConfiguration, traefikPointersConfiguration))
|
||||||
|
|
||||||
usedCmd, err := f.GetCommand()
|
usedCmd, err := f.GetCommand()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -99,12 +104,12 @@ Complete documentation is available at https://traefik.io`,
|
||||||
|
|
||||||
traefikConfiguration.ConfigFile = toml.ConfigFileUsed()
|
traefikConfiguration.ConfigFile = toml.ConfigFileUsed()
|
||||||
|
|
||||||
kv, err := createKvSource(traefikConfiguration)
|
kv, err := storeconfig.CreateKvSource(traefikConfiguration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmtlog.Printf("Error creating kv store: %s\n", err)
|
fmtlog.Printf("Error creating kv store: %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
storeConfigCmd.Run = runStoreConfig(kv, traefikConfiguration)
|
storeConfigCmd.Run = storeconfig.Run(kv, traefikConfiguration)
|
||||||
|
|
||||||
// if a KV Store is enable and no sub-command called in args
|
// if a KV Store is enable and no sub-command called in args
|
||||||
if kv != nil && usedCmd == traefikCmd {
|
if kv != nil && usedCmd == traefikCmd {
|
||||||
|
@ -137,7 +142,7 @@ Complete documentation is available at https://traefik.io`,
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(globalConfiguration *configuration.GlobalConfiguration, configFile string) {
|
func runCmd(globalConfiguration *configuration.GlobalConfiguration, configFile string) {
|
||||||
configureLogging(globalConfiguration)
|
configureLogging(globalConfiguration)
|
||||||
|
|
||||||
if len(configFile) > 0 {
|
if len(configFile) > 0 {
|
||||||
|
@ -178,7 +183,7 @@ func run(globalConfiguration *configuration.GlobalConfiguration, configFile stri
|
||||||
safe.Go(func() {
|
safe.Go(func() {
|
||||||
tick := time.Tick(t)
|
tick := time.Tick(t)
|
||||||
for range tick {
|
for range tick {
|
||||||
_, errHealthCheck := healthCheck(*globalConfiguration)
|
_, errHealthCheck := healthcheck.Do(*globalConfiguration)
|
||||||
if globalConfiguration.Ping == nil || errHealthCheck == nil {
|
if globalConfiguration.Ping == nil || errHealthCheck == nil {
|
||||||
if ok, _ := daemon.SdNotify(false, "WATCHDOG=1"); !ok {
|
if ok, _ := daemon.SdNotify(false, "WATCHDOG=1"); !ok {
|
||||||
log.Error("Fail to tick watchdog")
|
log.Error("Fail to tick watchdog")
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package version
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -17,17 +17,15 @@ Go version: {{.GoVersion}}
|
||||||
Built: {{.BuildTime}}
|
Built: {{.BuildTime}}
|
||||||
OS/Arch: {{.Os}}/{{.Arch}}`
|
OS/Arch: {{.Os}}/{{.Arch}}`
|
||||||
|
|
||||||
// newVersionCmd builds a new Version command
|
// NewCmd builds a new Version command
|
||||||
func newVersionCmd() *flaeg.Command {
|
func NewCmd() *flaeg.Command {
|
||||||
|
|
||||||
//version Command init
|
|
||||||
return &flaeg.Command{
|
return &flaeg.Command{
|
||||||
Name: "version",
|
Name: "version",
|
||||||
Description: `Print version`,
|
Description: `Print version`,
|
||||||
Config: struct{}{},
|
Config: struct{}{},
|
||||||
DefaultPointersConfig: struct{}{},
|
DefaultPointersConfig: struct{}{},
|
||||||
Run: func() error {
|
Run: func() error {
|
||||||
if err := getVersionPrint(os.Stdout); err != nil {
|
if err := GetPrint(os.Stdout); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Print("\n")
|
fmt.Print("\n")
|
||||||
|
@ -37,7 +35,8 @@ func newVersionCmd() *flaeg.Command {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getVersionPrint(wr io.Writer) error {
|
// GetPrint write Printable version
|
||||||
|
func GetPrint(wr io.Writer) error {
|
||||||
tmpl, err := template.New("").Parse(versionTemplate)
|
tmpl, err := template.New("").Parse(versionTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/containous/traefik/cmd/traefik/anonymize"
|
"github.com/containous/traefik/anonymize"
|
||||||
"github.com/containous/traefik/configuration"
|
"github.com/containous/traefik/configuration"
|
||||||
"github.com/containous/traefik/log"
|
"github.com/containous/traefik/log"
|
||||||
"github.com/containous/traefik/version"
|
"github.com/containous/traefik/version"
|
||||||
|
|
Loading…
Reference in a new issue