Docker support

This commit is contained in:
emile 2015-09-02 21:13:25 +02:00
parent f184d62953
commit 8fcbd73edc
3 changed files with 74 additions and 20 deletions

16
docker.tmpl Normal file
View file

@ -0,0 +1,16 @@
[backends]{{range .Containers}}
[backends.{{.Config.Hostname}}]
[backends.{{.Config.Hostname}}.servers.{{.Config.Hostname}}]
url = "http://{{.NetworkSettings.IPAddress}}:80"
{{end}}
[routes]{{range .Containers}}
[routes.{{.Config.Hostname}}]
backends = ["{{.Config.Hostname}}"]
[routes.{{.Config.Hostname}}.rules.{{.Config.Hostname}}-hostname]
category = "Host"
value = "{{.Config.Hostname}}.zenika.fr"
[routes.{{.Config.Hostname}}.rules.{{.Config.Hostname}}-name]
category = "Host"
value = "{{.Name | replace "/"}}.zenika.fr"
{{end}}

View file

@ -12,9 +12,12 @@ import (
"os/signal" "os/signal"
"syscall" "syscall"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"encoding/json"
"reflect" "reflect"
"net/url" "net/url"
"github.com/fsouza/go-dockerclient"
"github.com/leekchan/gtf"
"bytes"
"github.com/unrolled/render"
) )
type Backend struct { type Backend struct {
@ -37,13 +40,13 @@ type Route struct {
type Config struct { type Config struct {
Backends map[string]Backend Backends map[string]Backend
Servers map[string]Server
Routes map[string]Route Routes map[string]Route
} }
var srv *graceful.Server var srv *graceful.Server
var userRouter *mux.Router var userRouter *mux.Router
var config = new(Config) var config = new(Config)
var renderer = render.New()
func main() { func main() {
sigs := make(chan os.Signal, 1) sigs := make(chan os.Signal, 1)
@ -90,33 +93,71 @@ func main() {
} }
} }
func LoadConfig() *mux.Router{ func LoadDockerConfig(){
if metadata, err := toml.DecodeFile("tortuous.toml", config); err != nil { endpoint := "unix:///var/run/docker.sock"
fmt.Println(err) client, _ := docker.NewClient(endpoint)
return nil containerList, _ := client.ListContainers(docker.ListContainersOptions{})
}else{ containersInspected := []docker.Container{}
fmt.Printf("Loaded config: %+v\n", metadata ) for _, container := range containerList {
containerInspected, _ := client.InspectContainer(container.ID)
containersInspected = append(containersInspected, *containerInspected)
} }
containers := struct {
Containers []docker.Container
}{
containersInspected,
}
tmpl, err := gtf.New("docker.tmpl").ParseFiles("docker.tmpl")
if err != nil { panic(err) }
var buffer bytes.Buffer
err = tmpl.Execute(&buffer, containers)
if err != nil { panic(err) }
fmt.Println(buffer.String())
if _, err := toml.Decode(buffer.String(), config); err != nil {
fmt.Println(err)
return
}
}
func LoadFileConfig(){
if _, err := toml.DecodeFile("tortuous.toml", config); err != nil {
fmt.Println(err)
return
}
}
func LoadConfig() *mux.Router{
//LoadDockerConfig()
LoadFileConfig()
router := mux.NewRouter() router := mux.NewRouter()
for routeName, route := range config.Routes { for routeName, route := range config.Routes {
fmt.Println("Creating route", routeName) fmt.Println("Creating route", routeName)
fwd, _ := forward.New() fwd, _ := forward.New()
newRoute:= router.NewRoute() newRoutes:= []*mux.Route{}
for ruleName, rule := range route.Rules{ for ruleName, rule := range route.Rules{
fmt.Println("Creating rule", ruleName) fmt.Println("Creating rule", ruleName)
newRoutes := Invoke(newRoute, rule.Category, rule.Value) newRouteReflect := Invoke(router.NewRoute(), rule.Category, rule.Value)
newRoute = newRoutes[0].Interface().(*mux.Route) newRoute := newRouteReflect[0].Interface().(*mux.Route)
newRoutes = append(newRoutes, newRoute)
} }
for _, backendName := range route.Backends { for _, backendName := range route.Backends {
fmt.Println("Creating backend", backendName) fmt.Println("Creating backend", backendName)
lb, _ := roundrobin.New(fwd) lb, _ := roundrobin.New(fwd)
rb, _ := roundrobin.NewRebalancer(lb)
for serverName, server := range config.Backends[backendName].Servers { for serverName, server := range config.Backends[backendName].Servers {
fmt.Println("Creating server", serverName) fmt.Println("Creating server", serverName)
url, _ := url.Parse(server.Url) url, _ := url.Parse(server.Url)
lb.UpsertServer(url) rb.UpsertServer(url)
}
for _, route := range newRoutes {
route.Handler(lb)
} }
newRoute.Handler(lb)
} }
} }
return router return router
@ -124,19 +165,16 @@ func LoadConfig() *mux.Router{
func ReloadConfigHandler(rw http.ResponseWriter, r *http.Request) { func ReloadConfigHandler(rw http.ResponseWriter, r *http.Request) {
userRouter = LoadConfig() userRouter = LoadConfig()
renderer.JSON(rw, http.StatusOK, map[string]interface{}{"status": "reloaded"})
} }
func RestartHandler(rw http.ResponseWriter, r *http.Request) { func RestartHandler(rw http.ResponseWriter, r *http.Request) {
srv.Stop(10 * time.Second) srv.Stop(10 * time.Second)
renderer.JSON(rw, http.StatusOK, map[string]interface{}{"status": "restarted"})
} }
func GetConfigHandler(rw http.ResponseWriter, r *http.Request) { func GetConfigHandler(rw http.ResponseWriter, r *http.Request) {
if jsonRes, err := json.Marshal(config); err != nil { renderer.JSON(rw, http.StatusOK, config)
fmt.Println(err)
return
}else{
fmt.Fprintf(rw, "%s", jsonRes)
}
} }
func Invoke(any interface{}, name string, args... interface{}) []reflect.Value { func Invoke(any interface{}, name string, args... interface{}) []reflect.Value {

View file

@ -1,7 +1,7 @@
[backends] [backends]
[backends.backend1] [backends.backend1]
[backends.backend1.servers.server1] [backends.backend1.servers.server1]
url = "http://172.17.0.3:80" url = "http://172.17.0.2:80"
[backends.backend1.servers.server2] [backends.backend1.servers.server2]
url = "http://172.17.0.3:80" url = "http://172.17.0.3:80"