2015-09-11 14:37:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-09-24 15:16:13 +00:00
|
|
|
fmtlog "log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2015-09-12 13:10:03 +00:00
|
|
|
"github.com/BurntSushi/toml"
|
2015-09-24 12:32:37 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-09-12 13:10:03 +00:00
|
|
|
"github.com/codegangsta/negroni"
|
2015-09-15 19:38:54 +00:00
|
|
|
"github.com/emilevauge/traefik/middlewares"
|
2015-09-11 14:37:13 +00:00
|
|
|
"github.com/gorilla/mux"
|
2015-09-25 22:20:45 +00:00
|
|
|
"github.com/mailgun/manners"
|
2015-09-24 12:32:37 +00:00
|
|
|
"github.com/mailgun/oxy/cbreaker"
|
2015-09-11 14:37:13 +00:00
|
|
|
"github.com/mailgun/oxy/forward"
|
|
|
|
"github.com/mailgun/oxy/roundrobin"
|
2015-09-12 13:10:03 +00:00
|
|
|
"github.com/thoas/stats"
|
|
|
|
"github.com/unrolled/render"
|
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
2015-09-25 22:20:45 +00:00
|
|
|
"runtime"
|
2015-09-11 14:37:13 +00:00
|
|
|
)
|
|
|
|
|
2015-09-11 23:55:10 +00:00
|
|
|
var (
|
2015-09-12 13:10:03 +00:00
|
|
|
globalConfigFile = kingpin.Arg("conf", "Main configration file.").Default("traefik.toml").String()
|
2015-09-11 23:55:10 +00:00
|
|
|
currentConfiguration = new(Configuration)
|
2015-09-12 13:10:03 +00:00
|
|
|
metrics = stats.New()
|
2015-09-24 12:32:37 +00:00
|
|
|
oxyLogger = &OxyLogger{}
|
2015-09-12 13:10:03 +00:00
|
|
|
templatesRenderer = render.New(render.Options{
|
|
|
|
Directory: "templates",
|
|
|
|
Asset: Asset,
|
2015-09-11 23:55:10 +00:00
|
|
|
AssetNames: AssetNames,
|
|
|
|
})
|
|
|
|
)
|
2015-09-11 14:37:13 +00:00
|
|
|
|
|
|
|
func main() {
|
2015-09-25 22:20:45 +00:00
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
2015-09-11 23:55:10 +00:00
|
|
|
kingpin.Parse()
|
2015-09-19 11:02:59 +00:00
|
|
|
fmtlog.SetFlags(fmtlog.Lshortfile | fmtlog.LstdFlags)
|
2015-09-25 22:20:45 +00:00
|
|
|
var srv *manners.GracefulServer
|
2015-09-11 14:37:13 +00:00
|
|
|
var configurationRouter *mux.Router
|
2015-09-21 16:05:56 +00:00
|
|
|
var configurationChan = make(chan *Configuration, 10)
|
2015-09-16 17:08:01 +00:00
|
|
|
defer close(configurationChan)
|
2015-09-11 14:37:13 +00:00
|
|
|
var sigs = make(chan os.Signal, 1)
|
2015-09-25 20:02:55 +00:00
|
|
|
defer close(sigs)
|
2015-09-25 22:20:45 +00:00
|
|
|
var stopChan = make(chan bool)
|
|
|
|
defer close(stopChan)
|
|
|
|
var providers = []Provider{}
|
2015-09-11 14:37:13 +00:00
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
// load global configuration
|
2015-09-24 15:16:13 +00:00
|
|
|
globalConfiguration := LoadFileConfig(*globalConfigFile)
|
2015-09-11 14:37:13 +00:00
|
|
|
|
2015-09-24 15:16:13 +00:00
|
|
|
loggerMiddleware := middlewares.NewLogger(globalConfiguration.AccessLogsFile)
|
2015-09-12 13:20:56 +00:00
|
|
|
defer loggerMiddleware.Close()
|
|
|
|
|
2015-09-11 14:37:13 +00:00
|
|
|
// logging
|
2015-09-24 15:16:13 +00:00
|
|
|
level, err := log.ParseLevel(strings.ToLower(globalConfiguration.LogLevel))
|
2015-09-11 14:37:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error getting level", err)
|
|
|
|
}
|
2015-09-24 12:32:37 +00:00
|
|
|
log.SetLevel(level)
|
2015-09-11 14:48:52 +00:00
|
|
|
|
2015-09-24 15:16:13 +00:00
|
|
|
if len(globalConfiguration.TraefikLogsFile) > 0 {
|
|
|
|
fi, err := os.OpenFile(globalConfiguration.TraefikLogsFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
2015-09-12 13:20:56 +00:00
|
|
|
defer fi.Close()
|
2015-09-11 14:37:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error opening file", err)
|
2015-09-12 13:10:03 +00:00
|
|
|
} else {
|
2015-09-24 12:32:37 +00:00
|
|
|
log.SetOutput(fi)
|
|
|
|
log.SetFormatter(&log.TextFormatter{DisableColors: true, FullTimestamp: true, DisableSorting: true})
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-09-24 12:32:37 +00:00
|
|
|
} else {
|
|
|
|
log.SetFormatter(&log.TextFormatter{FullTimestamp: true, DisableSorting: true})
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 15:16:13 +00:00
|
|
|
configurationRouter = LoadDefaultConfig(globalConfiguration)
|
2015-09-11 14:37:13 +00:00
|
|
|
|
|
|
|
// listen new configurations from providers
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
configuration := <-configurationChan
|
2015-09-24 12:32:37 +00:00
|
|
|
log.Infof("Configuration receveived %+v", configuration)
|
2015-09-11 14:37:13 +00:00
|
|
|
if configuration == nil {
|
|
|
|
log.Info("Skipping empty configuration")
|
2015-09-12 13:10:03 +00:00
|
|
|
} else if reflect.DeepEqual(currentConfiguration, configuration) {
|
2015-09-11 14:37:13 +00:00
|
|
|
log.Info("Skipping same configuration")
|
|
|
|
} else {
|
2015-09-24 15:16:13 +00:00
|
|
|
newConfigurationRouter, err := LoadConfig(configuration, globalConfiguration)
|
2015-09-24 12:32:37 +00:00
|
|
|
if err == nil {
|
2015-09-19 11:02:59 +00:00
|
|
|
currentConfiguration = configuration
|
|
|
|
configurationRouter = newConfigurationRouter
|
2015-09-25 20:02:55 +00:00
|
|
|
oldServer := srv
|
2015-09-25 22:20:45 +00:00
|
|
|
newsrv := prepareServer(configurationRouter, globalConfiguration, oldServer, loggerMiddleware, metrics)
|
|
|
|
go startServer(newsrv, globalConfiguration)
|
|
|
|
srv = newsrv
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
if oldServer != nil {
|
|
|
|
log.Info("Stopping old server")
|
|
|
|
oldServer.Close()
|
|
|
|
}
|
2015-09-24 12:32:37 +00:00
|
|
|
} else {
|
2015-09-19 11:02:59 +00:00
|
|
|
log.Error("Error loading new configuration, aborted ", err)
|
|
|
|
}
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// configure providers
|
2015-09-24 15:16:13 +00:00
|
|
|
if globalConfiguration.Docker != nil {
|
|
|
|
providers = append(providers, globalConfiguration.Docker)
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-09-24 15:16:13 +00:00
|
|
|
if globalConfiguration.Marathon != nil {
|
|
|
|
providers = append(providers, globalConfiguration.Marathon)
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-09-24 15:16:13 +00:00
|
|
|
if globalConfiguration.File != nil {
|
|
|
|
if len(globalConfiguration.File.Filename) == 0 {
|
2015-09-11 14:37:13 +00:00
|
|
|
// no filename, setting to global config file
|
2015-09-24 15:16:13 +00:00
|
|
|
globalConfiguration.File.Filename = *globalConfigFile
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-09-24 15:16:13 +00:00
|
|
|
providers = append(providers, globalConfiguration.File)
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-09-24 15:16:13 +00:00
|
|
|
if globalConfiguration.Web != nil {
|
|
|
|
providers = append(providers, globalConfiguration.Web)
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-09-24 15:16:13 +00:00
|
|
|
if globalConfiguration.Consul != nil {
|
|
|
|
providers = append(providers, globalConfiguration.Consul)
|
2015-09-21 16:05:56 +00:00
|
|
|
}
|
2015-09-11 14:37:13 +00:00
|
|
|
|
|
|
|
// start providers
|
|
|
|
for _, provider := range providers {
|
2015-09-24 12:32:37 +00:00
|
|
|
log.Infof("Starting provider %v %+v", reflect.TypeOf(provider), provider)
|
2015-09-11 14:37:13 +00:00
|
|
|
currentProvider := provider
|
|
|
|
go func() {
|
|
|
|
currentProvider.Provide(configurationChan)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
sig := <-sigs
|
2015-09-24 12:32:37 +00:00
|
|
|
log.Infof("I have to go... %+v", sig)
|
2015-09-25 22:20:45 +00:00
|
|
|
log.Info("Stopping server")
|
|
|
|
srv.Close()
|
|
|
|
stopChan <- true
|
2015-09-11 14:37:13 +00:00
|
|
|
}()
|
|
|
|
|
2015-09-25 20:02:55 +00:00
|
|
|
//negroni.Use(middlewares.NewCircuitBreaker(oxyLogger))
|
|
|
|
//negroni.Use(middlewares.NewRoutes(configurationRouter))
|
2015-09-25 22:20:45 +00:00
|
|
|
srv = prepareServer(configurationRouter, globalConfiguration, nil, loggerMiddleware, metrics)
|
|
|
|
go startServer(srv, globalConfiguration)
|
2015-09-12 13:10:03 +00:00
|
|
|
|
2015-09-25 22:20:45 +00:00
|
|
|
<-stopChan
|
|
|
|
log.Info("Shutting down")
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 22:20:45 +00:00
|
|
|
func startServer(srv *manners.GracefulServer, globalConfiguration *GlobalConfiguration) {
|
|
|
|
log.Info("Starting server")
|
2015-09-25 20:02:55 +00:00
|
|
|
if len(globalConfiguration.CertFile) > 0 && len(globalConfiguration.KeyFile) > 0 {
|
|
|
|
err := srv.ListenAndServeTLS(globalConfiguration.CertFile, globalConfiguration.KeyFile)
|
|
|
|
if err != nil {
|
|
|
|
netOpError, ok := err.(*net.OpError)
|
|
|
|
if ok && netOpError.Err.Error() != "use of closed network connection" {
|
|
|
|
log.Fatal("Error creating server: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err := srv.ListenAndServe()
|
|
|
|
if err != nil {
|
|
|
|
netOpError, ok := err.(*net.OpError)
|
|
|
|
if ok && netOpError.Err.Error() != "use of closed network connection" {
|
|
|
|
log.Fatal("Error creating server: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 22:20:45 +00:00
|
|
|
log.Info("Server stopped")
|
2015-09-25 20:02:55 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 22:20:45 +00:00
|
|
|
func prepareServer(router *mux.Router, globalConfiguration *GlobalConfiguration, oldServer *manners.GracefulServer, middlewares ...negroni.Handler) *manners.GracefulServer {
|
|
|
|
log.Info("Preparing server")
|
2015-09-25 20:02:55 +00:00
|
|
|
// middlewares
|
|
|
|
var negroni = negroni.New()
|
|
|
|
for _, middleware := range middlewares {
|
|
|
|
negroni.Use(middleware)
|
|
|
|
}
|
|
|
|
negroni.UseHandler(router)
|
|
|
|
|
2015-09-25 22:20:45 +00:00
|
|
|
if oldServer == nil {
|
|
|
|
return manners.NewWithServer(
|
|
|
|
&http.Server{
|
|
|
|
Addr: globalConfiguration.Port,
|
|
|
|
Handler: negroni,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
server, err := oldServer.HijackListener(&http.Server{
|
2015-09-25 20:02:55 +00:00
|
|
|
Addr: globalConfiguration.Port,
|
|
|
|
Handler: negroni,
|
2015-09-25 22:20:45 +00:00
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error hijacking server %s", err)
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return server
|
|
|
|
}
|
2015-09-25 20:02:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-24 15:16:13 +00:00
|
|
|
func LoadConfig(configuration *Configuration, globalConfiguration *GlobalConfiguration) (*mux.Router, error) {
|
2015-09-11 14:37:13 +00:00
|
|
|
router := mux.NewRouter()
|
2015-09-12 13:10:03 +00:00
|
|
|
router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
|
2015-09-11 14:37:13 +00:00
|
|
|
backends := map[string]http.Handler{}
|
2015-09-15 14:09:21 +00:00
|
|
|
for frontendName, frontend := range configuration.Frontends {
|
2015-09-24 12:32:37 +00:00
|
|
|
log.Debugf("Creating frontend %s", frontendName)
|
2015-09-19 11:02:59 +00:00
|
|
|
fwd, _ := forward.New(forward.Logger(oxyLogger))
|
2015-09-17 14:29:44 +00:00
|
|
|
newRoute := router.NewRoute().Name(frontendName)
|
2015-09-15 14:09:21 +00:00
|
|
|
for routeName, route := range frontend.Routes {
|
2015-09-24 12:32:37 +00:00
|
|
|
log.Debugf("Creating route %s %s:%s", routeName, route.Rule, route.Value)
|
2015-09-15 16:35:32 +00:00
|
|
|
newRouteReflect := Invoke(newRoute, route.Rule, route.Value)
|
2015-09-12 11:20:54 +00:00
|
|
|
newRoute = newRouteReflect[0].Interface().(*mux.Route)
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-09-15 14:09:21 +00:00
|
|
|
if backends[frontend.Backend] == nil {
|
2015-09-24 12:32:37 +00:00
|
|
|
log.Debugf("Creating backend %s", frontend.Backend)
|
2015-09-25 09:44:19 +00:00
|
|
|
var lb http.Handler
|
|
|
|
rr, _ := roundrobin.New(fwd)
|
|
|
|
lbMethod, err := NewLoadBalancerMethod(configuration.Backends[frontend.Backend].LoadBalancer)
|
|
|
|
if err != nil {
|
|
|
|
configuration.Backends[frontend.Backend].LoadBalancer = &LoadBalancer{Method: "wrr"}
|
|
|
|
}
|
|
|
|
switch lbMethod {
|
|
|
|
case drr:
|
|
|
|
log.Debugf("Creating load-balancer drr")
|
|
|
|
rebalancer, _ := roundrobin.NewRebalancer(rr, roundrobin.RebalancerLogger(oxyLogger))
|
|
|
|
lb = rebalancer
|
|
|
|
for serverName, server := range configuration.Backends[frontend.Backend].Servers {
|
|
|
|
url, err := url.Parse(server.URL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
log.Debugf("Creating server %s %s", serverName, url.String())
|
|
|
|
rebalancer.UpsertServer(url, roundrobin.Weight(server.Weight))
|
|
|
|
}
|
|
|
|
case wrr:
|
|
|
|
log.Debugf("Creating load-balancer wrr")
|
|
|
|
lb = rr
|
|
|
|
for serverName, server := range configuration.Backends[frontend.Backend].Servers {
|
|
|
|
url, err := url.Parse(server.URL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
log.Debugf("Creating server %s %s", serverName, url.String())
|
|
|
|
rr.UpsertServer(url, roundrobin.Weight(server.Weight))
|
2015-09-19 11:02:59 +00:00
|
|
|
}
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-09-25 09:44:19 +00:00
|
|
|
var negroni = negroni.New()
|
|
|
|
if configuration.Backends[frontend.Backend].CircuitBreaker != nil {
|
|
|
|
log.Debugf("Creating circuit breaker %s", configuration.Backends[frontend.Backend].CircuitBreaker.Expression)
|
|
|
|
negroni.Use(middlewares.NewCircuitBreaker(lb, configuration.Backends[frontend.Backend].CircuitBreaker.Expression, cbreaker.Logger(oxyLogger)))
|
|
|
|
} else {
|
|
|
|
negroni.UseHandler(lb)
|
|
|
|
}
|
|
|
|
backends[frontend.Backend] = negroni
|
2015-09-12 13:10:03 +00:00
|
|
|
} else {
|
2015-09-24 12:32:37 +00:00
|
|
|
log.Debugf("Reusing backend %s", frontend.Backend)
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-09-25 09:44:19 +00:00
|
|
|
// stream.New(backends[frontend.Backend], stream.Retry("IsNetworkError() && Attempts() <= " + strconv.Itoa(globalConfiguration.Replay)), stream.Logger(oxyLogger))
|
|
|
|
|
|
|
|
newRoute.Handler(backends[frontend.Backend])
|
2015-09-12 11:20:54 +00:00
|
|
|
err := newRoute.GetError()
|
|
|
|
if err != nil {
|
2015-09-25 09:44:19 +00:00
|
|
|
log.Error("Error building route: %s", err)
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-19 11:02:59 +00:00
|
|
|
return router, nil
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Invoke(any interface{}, name string, args ...interface{}) []reflect.Value {
|
|
|
|
inputs := make([]reflect.Value, len(args))
|
2015-09-15 20:32:09 +00:00
|
|
|
for i := range args {
|
2015-09-11 14:37:13 +00:00
|
|
|
inputs[i] = reflect.ValueOf(args[i])
|
|
|
|
}
|
|
|
|
return reflect.ValueOf(any).MethodByName(name).Call(inputs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadFileConfig(file string) *GlobalConfiguration {
|
|
|
|
configuration := NewGlobalConfiguration()
|
|
|
|
if _, err := toml.DecodeFile(file, configuration); err != nil {
|
2015-09-11 17:25:49 +00:00
|
|
|
log.Fatal("Error reading file ", err)
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-09-24 12:32:37 +00:00
|
|
|
log.Debugf("Global configuration loaded %+v", configuration)
|
2015-09-11 14:37:13 +00:00
|
|
|
return configuration
|
2015-09-24 12:32:37 +00:00
|
|
|
}
|