forked from gliderlabs/registrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistrator.go
97 lines (79 loc) · 1.99 KB
/
registrator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"flag"
"log"
"net/url"
"os"
"github.com/cenkalti/backoff"
dockerapi "github.com/fsouza/go-dockerclient"
)
var hostIp = flag.String("ip", "", "IP for ports mapped to the host")
func getopt(name, def string) string {
if env := os.Getenv(name); env != "" {
return env
}
return def
}
func assert(err error) {
if err != nil {
log.Fatal("registrator: ", err)
}
}
func retry(fn func() error) error {
return backoff.Retry(fn, backoff.NewExponentialBackOff())
}
func mapdefault(m map[string]string, key, default_ string) string {
v, ok := m[key]
if !ok {
return default_
}
return v
}
type ServiceRegistry interface {
Register(service *Service) error
Deregister(service *Service) error
}
func NewServiceRegistry(uri *url.URL) ServiceRegistry {
factory := map[string]func(*url.URL) ServiceRegistry{
"consul": NewConsulRegistry,
"etcd": NewEtcdRegistry,
}[uri.Scheme]
if factory == nil {
log.Fatal("unrecognized registry backend: ", uri.Scheme)
}
log.Println("registrator: Using", uri.Scheme, "registry backend at", uri)
return factory(uri)
}
func main() {
flag.Parse()
if *hostIp != "" {
log.Println("registrator: Forcing host IP to", *hostIp)
}
docker, err := dockerapi.NewClient(getopt("DOCKER_HOST", "unix:///var/run/docker.sock"))
assert(err)
uri, err := url.Parse(flag.Arg(0))
assert(err)
registry := NewServiceRegistry(uri)
bridge := &RegistryBridge{
docker: docker,
registry: registry,
services: make(map[string][]*Service),
}
containers, err := docker.ListContainers(dockerapi.ListContainersOptions{})
assert(err)
for _, listing := range containers {
bridge.Add(listing.ID)
}
events := make(chan *dockerapi.APIEvents)
assert(docker.AddEventListener(events))
log.Println("registrator: Listening for Docker events...")
for msg := range events {
switch msg.Status {
case "start":
go bridge.Add(msg.ID)
case "die":
go bridge.Remove(msg.ID)
}
}
log.Fatal("registrator: docker event loop closed") // todo: reconnect?
}