2015-09-11 14:37:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-05-03 14:52:14 +00:00
|
|
|
"encoding/json"
|
2016-06-02 13:17:04 +00:00
|
|
|
"fmt"
|
2016-07-21 14:33:49 +00:00
|
|
|
fmtlog "log"
|
|
|
|
"net/http"
|
2016-07-23 14:54:37 +00:00
|
|
|
"crypto/tls"
|
2016-07-21 14:33:49 +00:00
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
2016-05-03 14:52:14 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/containous/flaeg"
|
|
|
|
"github.com/containous/staert"
|
2016-05-25 15:06:34 +00:00
|
|
|
"github.com/containous/traefik/acme"
|
2016-05-03 14:52:14 +00:00
|
|
|
"github.com/containous/traefik/middlewares"
|
2016-05-18 13:45:48 +00:00
|
|
|
"github.com/containous/traefik/provider"
|
2016-05-31 07:54:42 +00:00
|
|
|
"github.com/containous/traefik/types"
|
2016-07-21 14:33:49 +00:00
|
|
|
"github.com/containous/traefik/version"
|
2016-06-24 07:58:42 +00:00
|
|
|
"github.com/docker/libkv/store"
|
2015-09-11 23:55:10 +00:00
|
|
|
)
|
2015-09-11 14:37:13 +00:00
|
|
|
|
2016-06-02 13:17:04 +00:00
|
|
|
var versionTemplate = `Version: {{.Version}}
|
2016-07-05 19:03:37 +00:00
|
|
|
Codename: {{.Codename}}
|
2016-06-02 13:17:04 +00:00
|
|
|
Go version: {{.GoVersion}}
|
|
|
|
Built: {{.BuildTime}}
|
|
|
|
OS/Arch: {{.Os}}/{{.Arch}}`
|
|
|
|
|
2015-09-11 14:37:13 +00:00
|
|
|
func main() {
|
2015-09-25 22:20:45 +00:00
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
2016-05-03 14:52:14 +00:00
|
|
|
|
|
|
|
//traefik config inits
|
|
|
|
traefikConfiguration := NewTraefikConfiguration()
|
2016-05-25 15:06:34 +00:00
|
|
|
traefikPointersConfiguration := NewTraefikDefaultPointersConfiguration()
|
2016-05-03 14:52:14 +00:00
|
|
|
//traefik Command init
|
|
|
|
traefikCmd := &flaeg.Command{
|
|
|
|
Name: "traefik",
|
|
|
|
Description: `traefik is a modern HTTP reverse proxy and load balancer made to deploy microservices with ease.
|
|
|
|
Complete documentation is available at https://traefik.io`,
|
|
|
|
Config: traefikConfiguration,
|
|
|
|
DefaultPointersConfig: traefikPointersConfiguration,
|
|
|
|
Run: func() error {
|
|
|
|
run(traefikConfiguration)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
//version Command init
|
|
|
|
versionCmd := &flaeg.Command{
|
2016-05-24 12:58:25 +00:00
|
|
|
Name: "version",
|
|
|
|
Description: `Print version`,
|
|
|
|
Config: struct{}{},
|
|
|
|
DefaultPointersConfig: struct{}{},
|
2016-05-03 14:52:14 +00:00
|
|
|
Run: func() error {
|
2016-06-02 13:17:04 +00:00
|
|
|
tmpl, err := template.New("").Parse(versionTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
v := struct {
|
|
|
|
Version string
|
2016-07-05 19:03:37 +00:00
|
|
|
Codename string
|
2016-06-02 13:17:04 +00:00
|
|
|
GoVersion string
|
|
|
|
BuildTime string
|
|
|
|
Os string
|
|
|
|
Arch string
|
|
|
|
}{
|
2016-07-21 14:33:49 +00:00
|
|
|
Version: version.Version,
|
|
|
|
Codename: version.Codename,
|
2016-06-02 13:17:04 +00:00
|
|
|
GoVersion: runtime.Version(),
|
2016-07-21 14:33:49 +00:00
|
|
|
BuildTime: version.BuildDate,
|
2016-06-02 13:17:04 +00:00
|
|
|
Os: runtime.GOOS,
|
|
|
|
Arch: runtime.GOARCH,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(os.Stdout, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("\n")
|
2016-05-03 14:52:14 +00:00
|
|
|
return nil
|
2016-06-02 13:17:04 +00:00
|
|
|
|
2016-05-03 14:52:14 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-07-20 09:48:39 +00:00
|
|
|
//storeconfig Command init
|
|
|
|
var kv *staert.KvSource
|
|
|
|
var err error
|
|
|
|
|
|
|
|
storeconfigCmd := &flaeg.Command{
|
|
|
|
Name: "storeconfig",
|
|
|
|
Description: `Store the static traefik configuration into a Key-value stores. Traefik will not start.`,
|
|
|
|
Config: traefikConfiguration,
|
|
|
|
DefaultPointersConfig: traefikPointersConfiguration,
|
|
|
|
Run: func() error {
|
|
|
|
if kv == nil {
|
|
|
|
return fmt.Errorf("Error using command storeconfig, no Key-value store defined")
|
|
|
|
}
|
|
|
|
jsonConf, _ := json.Marshal(traefikConfiguration.GlobalConfiguration)
|
|
|
|
fmtlog.Printf("Storing configuration: %s\n", jsonConf)
|
|
|
|
return kv.StoreConfig(traefikConfiguration.GlobalConfiguration)
|
|
|
|
},
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"parseAllSources": "true",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-05-03 14:52:14 +00:00
|
|
|
//init flaeg source
|
|
|
|
f := flaeg.New(traefikCmd, os.Args[1:])
|
|
|
|
//add custom parsers
|
|
|
|
f.AddParser(reflect.TypeOf(EntryPoints{}), &EntryPoints{})
|
|
|
|
f.AddParser(reflect.TypeOf(DefaultEntryPoints{}), &DefaultEntryPoints{})
|
2016-06-02 13:17:04 +00:00
|
|
|
f.AddParser(reflect.TypeOf(types.Constraints{}), &types.Constraints{})
|
2016-05-18 13:45:48 +00:00
|
|
|
f.AddParser(reflect.TypeOf(provider.Namespaces{}), &provider.Namespaces{})
|
2016-05-25 15:06:34 +00:00
|
|
|
f.AddParser(reflect.TypeOf([]acme.Domain{}), &acme.Domains{})
|
|
|
|
|
2016-07-20 09:48:39 +00:00
|
|
|
//add commands
|
2016-05-03 14:52:14 +00:00
|
|
|
f.AddCommand(versionCmd)
|
2016-07-20 09:48:39 +00:00
|
|
|
f.AddCommand(storeconfigCmd)
|
2016-05-24 12:58:25 +00:00
|
|
|
if _, err := f.Parse(traefikCmd); err != nil {
|
|
|
|
fmtlog.Println(err)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
//staert init
|
|
|
|
s := staert.NewStaert(traefikCmd)
|
|
|
|
//init toml source
|
|
|
|
toml := staert.NewTomlSource("traefik", []string{traefikConfiguration.ConfigFile, "/etc/traefik/", "$HOME/.traefik/", "."})
|
2016-05-03 14:52:14 +00:00
|
|
|
|
|
|
|
//add sources to staert
|
|
|
|
s.AddSource(toml)
|
|
|
|
s.AddSource(f)
|
2016-05-27 08:04:56 +00:00
|
|
|
if _, err := s.LoadConfig(); err != nil {
|
2016-06-03 15:58:33 +00:00
|
|
|
fmtlog.Println(fmt.Errorf("Error reading TOML config file %s : %s", toml.ConfigFileUsed(), err))
|
2016-07-20 09:48:39 +00:00
|
|
|
os.Exit(-1)
|
2016-05-24 12:58:25 +00:00
|
|
|
}
|
2016-05-30 09:37:12 +00:00
|
|
|
|
|
|
|
traefikConfiguration.ConfigFile = toml.ConfigFileUsed()
|
|
|
|
|
2016-07-20 09:48:39 +00:00
|
|
|
kv, err = CreateKvSource(traefikConfiguration)
|
2016-06-24 07:58:42 +00:00
|
|
|
if err != nil {
|
|
|
|
fmtlog.Println(err)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
|
2016-07-20 09:48:39 +00:00
|
|
|
usedCmd, err := f.GetCommand()
|
|
|
|
if err != nil {
|
|
|
|
fmtlog.Println(err)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
// IF a KV Store is enable and no sub-command called in args
|
|
|
|
if kv != nil && usedCmd == traefikCmd {
|
2016-06-24 07:58:42 +00:00
|
|
|
s.AddSource(kv)
|
|
|
|
if _, err := s.LoadConfig(); err != nil {
|
2016-07-07 15:25:42 +00:00
|
|
|
fmtlog.Println(err)
|
2016-07-20 09:48:39 +00:00
|
|
|
os.Exit(-1)
|
2016-06-24 07:58:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 14:52:14 +00:00
|
|
|
if err := s.Run(); err != nil {
|
2016-01-13 21:46:44 +00:00
|
|
|
fmtlog.Println(err)
|
|
|
|
os.Exit(-1)
|
2015-11-23 14:41:16 +00:00
|
|
|
}
|
2016-05-03 14:52:14 +00:00
|
|
|
|
2016-01-13 21:46:44 +00:00
|
|
|
os.Exit(0)
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2016-05-03 14:52:14 +00:00
|
|
|
|
|
|
|
func run(traefikConfiguration *TraefikConfiguration) {
|
|
|
|
fmtlog.SetFlags(fmtlog.Lshortfile | fmtlog.LstdFlags)
|
|
|
|
|
|
|
|
// load global configuration
|
|
|
|
globalConfiguration := traefikConfiguration.GlobalConfiguration
|
|
|
|
|
|
|
|
http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = globalConfiguration.MaxIdleConnsPerHost
|
2016-07-23 14:54:37 +00:00
|
|
|
if globalConfiguration.InsecureSkipVerify {
|
|
|
|
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
|
|
|
}
|
2016-05-03 14:52:14 +00:00
|
|
|
loggerMiddleware := middlewares.NewLogger(globalConfiguration.AccessLogsFile)
|
|
|
|
defer loggerMiddleware.Close()
|
|
|
|
|
2016-06-02 09:37:51 +00:00
|
|
|
if globalConfiguration.File != nil && len(globalConfiguration.File.Filename) == 0 {
|
2016-05-30 09:37:12 +00:00
|
|
|
// no filename, setting to global config file
|
|
|
|
if len(traefikConfiguration.ConfigFile) != 0 {
|
2016-06-02 09:37:51 +00:00
|
|
|
globalConfiguration.File.Filename = traefikConfiguration.ConfigFile
|
2016-05-30 09:37:12 +00:00
|
|
|
} else {
|
|
|
|
log.Errorln("Error using file configuration backend, no filename defined")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-02 09:37:51 +00:00
|
|
|
if len(globalConfiguration.EntryPoints) == 0 {
|
|
|
|
globalConfiguration.EntryPoints = map[string]*EntryPoint{"http": {Address: ":80"}}
|
|
|
|
globalConfiguration.DefaultEntryPoints = []string{"http"}
|
2016-05-30 09:37:12 +00:00
|
|
|
}
|
|
|
|
|
2016-06-02 13:17:04 +00:00
|
|
|
if globalConfiguration.Debug {
|
|
|
|
globalConfiguration.LogLevel = "DEBUG"
|
|
|
|
}
|
|
|
|
|
2016-06-02 09:37:51 +00:00
|
|
|
// logging
|
|
|
|
level, err := log.ParseLevel(strings.ToLower(globalConfiguration.LogLevel))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error getting level", err)
|
|
|
|
}
|
|
|
|
log.SetLevel(level)
|
2016-05-03 14:52:14 +00:00
|
|
|
if len(globalConfiguration.TraefikLogsFile) > 0 {
|
|
|
|
fi, err := os.OpenFile(globalConfiguration.TraefikLogsFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
defer func() {
|
|
|
|
if err := fi.Close(); err != nil {
|
2016-07-20 09:48:39 +00:00
|
|
|
log.Error("Error closing file", err)
|
2016-05-03 14:52:14 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error opening file", err)
|
|
|
|
} else {
|
|
|
|
log.SetOutput(fi)
|
|
|
|
log.SetFormatter(&log.TextFormatter{DisableColors: true, FullTimestamp: true, DisableSorting: true})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.SetFormatter(&log.TextFormatter{FullTimestamp: true, DisableSorting: true})
|
|
|
|
}
|
|
|
|
jsonConf, _ := json.Marshal(globalConfiguration)
|
2016-07-21 14:33:49 +00:00
|
|
|
log.Infof("Traefik version %s built on %s", version.Version, version.BuildDate)
|
2016-05-30 15:57:57 +00:00
|
|
|
if len(traefikConfiguration.ConfigFile) != 0 {
|
|
|
|
log.Infof("Using TOML configuration file %s", traefikConfiguration.ConfigFile)
|
|
|
|
}
|
2016-05-03 14:52:14 +00:00
|
|
|
log.Debugf("Global configuration loaded %s", string(jsonConf))
|
|
|
|
server := NewServer(globalConfiguration)
|
|
|
|
server.Start()
|
|
|
|
defer server.Close()
|
|
|
|
log.Info("Shutting down")
|
|
|
|
}
|
2016-07-13 15:18:55 +00:00
|
|
|
|
|
|
|
// CreateKvSource creates KvSource
|
|
|
|
// TLS support is enable for Consul and ects backends
|
|
|
|
func CreateKvSource(traefikConfiguration *TraefikConfiguration) (*staert.KvSource, error) {
|
|
|
|
var kv *staert.KvSource
|
|
|
|
var store store.Store
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case traefikConfiguration.Consul != nil:
|
|
|
|
store, err = traefikConfiguration.Consul.CreateStore()
|
|
|
|
kv = &staert.KvSource{
|
|
|
|
Store: store,
|
|
|
|
Prefix: traefikConfiguration.Consul.Prefix,
|
|
|
|
}
|
|
|
|
case traefikConfiguration.Etcd != nil:
|
|
|
|
store, err = traefikConfiguration.Etcd.CreateStore()
|
|
|
|
kv = &staert.KvSource{
|
|
|
|
Store: store,
|
|
|
|
Prefix: traefikConfiguration.Etcd.Prefix,
|
|
|
|
}
|
|
|
|
case traefikConfiguration.Zookeeper != nil:
|
|
|
|
store, err = traefikConfiguration.Zookeeper.CreateStore()
|
|
|
|
kv = &staert.KvSource{
|
|
|
|
Store: store,
|
|
|
|
Prefix: traefikConfiguration.Zookeeper.Prefix,
|
|
|
|
}
|
|
|
|
case traefikConfiguration.Boltdb != nil:
|
|
|
|
store, err = traefikConfiguration.Boltdb.CreateStore()
|
|
|
|
kv = &staert.KvSource{
|
|
|
|
Store: store,
|
|
|
|
Prefix: traefikConfiguration.Boltdb.Prefix,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return kv, err
|
|
|
|
}
|