traefik/docker.go

107 lines
2.8 KiB
Go
Raw Normal View History

2015-09-07 08:38:58 +00:00
package main
2015-09-07 22:15:14 +00:00
import (
2015-09-07 08:38:58 +00:00
"github.com/fsouza/go-dockerclient"
"github.com/leekchan/gtf"
"bytes"
"github.com/BurntSushi/toml"
"log"
"text/template"
"strings"
2015-09-07 08:38:58 +00:00
)
var DockerFuncMap = template.FuncMap{
"getBackend": func(container docker.Container) string {
for key, value := range container.Config.Labels {
if (key == "træfik.backend") {
return value
}
}
return container.Config.Hostname
},
"getPort": func(container docker.Container) string {
for key, value := range container.Config.Labels {
if (key == "træfik.port") {
return value
}
}
for key, _ := range container.NetworkSettings.Ports {
return key.Port()
}
return ""
},
"getHost": getHost,
}
2015-09-07 08:38:58 +00:00
type DockerProvider struct {
2015-09-07 22:15:14 +00:00
Watch bool
Endpoint string
2015-09-07 08:38:58 +00:00
dockerClient *docker.Client
}
2015-09-07 22:15:14 +00:00
func (provider *DockerProvider) Provide(configurationChan chan <- *Configuration) {
2015-09-07 15:39:22 +00:00
provider.dockerClient, _ = docker.NewClient(provider.Endpoint)
2015-09-07 08:38:58 +00:00
dockerEvents := make(chan *docker.APIEvents)
2015-09-07 22:15:14 +00:00
if (provider.Watch) {
2015-09-07 16:10:33 +00:00
provider.dockerClient.AddEventListener(dockerEvents)
}
2015-09-07 08:38:58 +00:00
go func() {
for {
event := <-dockerEvents
log.Println("Event receveived", event)
2015-09-07 22:15:14 +00:00
configuration := provider.loadDockerConfig()
if (configuration != nil) {
configurationChan <- configuration
}
2015-09-07 08:38:58 +00:00
}
}()
2015-09-07 22:15:14 +00:00
configuration := provider.loadDockerConfig()
configurationChan <- configuration
2015-09-07 08:38:58 +00:00
}
2015-09-07 22:15:14 +00:00
func (provider *DockerProvider) loadDockerConfig() *Configuration {
configuration := new(Configuration)
2015-09-07 08:38:58 +00:00
containerList, _ := provider.dockerClient.ListContainers(docker.ListContainersOptions{})
containersInspected := []docker.Container{}
hosts := map[string][]docker.Container{}
2015-09-07 08:38:58 +00:00
for _, container := range containerList {
containerInspected, _ := provider.dockerClient.InspectContainer(container.ID)
containersInspected = append(containersInspected, *containerInspected)
hosts[getHost(*containerInspected)] = append(hosts[getHost(*containerInspected)], *containerInspected)
2015-09-07 08:38:58 +00:00
}
templateObjects := struct {
2015-09-07 08:38:58 +00:00
Containers []docker.Container
Hosts map[string][]docker.Container
2015-09-07 08:38:58 +00:00
}{
containersInspected,
hosts,
2015-09-07 08:38:58 +00:00
}
gtf.Inject(DockerFuncMap)
tmpl, err := template.New("docker.tmpl").Funcs(DockerFuncMap).ParseFiles("docker.tmpl")
2015-09-07 08:38:58 +00:00
if err != nil {
log.Println("Error reading file:", err)
return nil
2015-09-07 08:38:58 +00:00
}
var buffer bytes.Buffer
err = tmpl.Execute(&buffer, templateObjects)
2015-09-07 08:38:58 +00:00
if err != nil {
log.Println("Error with docker template:", err)
return nil
2015-09-07 08:38:58 +00:00
}
2015-09-07 22:15:14 +00:00
if _, err := toml.Decode(buffer.String(), configuration); err != nil {
log.Println("Error creating docker configuration:", err)
2015-09-07 08:38:58 +00:00
return nil
}
2015-09-07 22:15:14 +00:00
return configuration
}
func getHost(container docker.Container) string {
for key, value := range container.Config.Labels {
if (key == "træfik.host") {
return value
}
}
return strings.TrimPrefix(container.Name, "/")
2015-09-07 08:38:58 +00:00
}