This repository has been archived by the owner on Dec 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
128 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,113 @@ | ||
package main | ||
|
||
import ( | ||
dockerapi "github.com/fsouza/go-dockerclient" | ||
"log" | ||
"os" | ||
"strings" | ||
"sync" | ||
dockerapi "github.com/fsouza/go-dockerclient" | ||
"log" | ||
"os" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
type HostEntry struct { | ||
IPAddress string | ||
CanonicalHostname string | ||
Aliases []string | ||
IPAddress string | ||
CanonicalHostname string | ||
Aliases []string | ||
} | ||
|
||
type Hosts struct { | ||
sync.Mutex | ||
docker *dockerapi.Client | ||
path string | ||
domain string | ||
entries map[string]HostEntry | ||
sync.Mutex | ||
docker *dockerapi.Client | ||
path string | ||
domain string | ||
entries map[string]HostEntry | ||
} | ||
|
||
func NewHosts(docker *dockerapi.Client, path, domain string) *Hosts { | ||
hosts := &Hosts{ | ||
docker: docker, | ||
path: path, | ||
domain: domain, | ||
} | ||
|
||
hosts.entries = make(map[string]HostEntry) | ||
|
||
// combination of docker, centos | ||
hosts.entries["__localhost4"] = HostEntry{ | ||
IPAddress: "127.0.0.1", | ||
CanonicalHostname: "localhost", | ||
Aliases: []string{"localhost4"}, | ||
} | ||
|
||
hosts.entries["__localhost6"] = HostEntry{ | ||
IPAddress: "::1", | ||
CanonicalHostname: "localhost", | ||
Aliases: []string{"localhost6", "ip6-localhost", "ip6-loopback"}, | ||
} | ||
|
||
// docker puts these in | ||
hosts.entries["fe00::0"] = HostEntry{"fe00::0", "ip6-localnet", nil} | ||
hosts.entries["ff00::0"] = HostEntry{"ff00::0", "ip6-mcastprefix", nil} | ||
hosts.entries["ff02::1"] = HostEntry{"ff02::1", "ip6-allnodes", nil} | ||
hosts.entries["ff02::2"] = HostEntry{"ff02::2", "ip6-allrouters", nil} | ||
|
||
return hosts | ||
hosts := &Hosts{ | ||
docker: docker, | ||
path: path, | ||
domain: domain, | ||
} | ||
|
||
hosts.entries = make(map[string]HostEntry) | ||
|
||
// combination of docker, centos | ||
hosts.entries["__localhost4"] = HostEntry{ | ||
IPAddress: "127.0.0.1", | ||
CanonicalHostname: "localhost", | ||
Aliases: []string{"localhost4"}, | ||
} | ||
|
||
hosts.entries["__localhost6"] = HostEntry{ | ||
IPAddress: "::1", | ||
CanonicalHostname: "localhost", | ||
Aliases: []string{"localhost6", "ip6-localhost", "ip6-loopback"}, | ||
} | ||
|
||
// docker puts these in | ||
hosts.entries["fe00::0"] = HostEntry{"fe00::0", "ip6-localnet", nil} | ||
hosts.entries["ff00::0"] = HostEntry{"ff00::0", "ip6-mcastprefix", nil} | ||
hosts.entries["ff02::1"] = HostEntry{"ff02::1", "ip6-allnodes", nil} | ||
hosts.entries["ff02::2"] = HostEntry{"ff02::2", "ip6-allrouters", nil} | ||
|
||
return hosts | ||
} | ||
|
||
func (h *Hosts) WriteFile() { | ||
file, err := os.Create(h.path) | ||
|
||
if err != nil { | ||
log.Println("unable to write to", h.path, err) | ||
return | ||
} | ||
|
||
defer file.Close() | ||
|
||
for _, entry := range h.entries { | ||
// <ip>\t<canonical>\t<alias1>\t…\t<aliasN>\n | ||
file.WriteString(strings.Join( | ||
append( | ||
[]string{entry.IPAddress, entry.CanonicalHostname}, | ||
entry.Aliases..., | ||
), | ||
"\t", | ||
) + "\n") | ||
} | ||
file, err := os.Create(h.path) | ||
|
||
if err != nil { | ||
log.Println("unable to write to", h.path, err) | ||
return | ||
} | ||
|
||
defer file.Close() | ||
|
||
for _, entry := range h.entries { | ||
// <ip>\t<canonical>\t<alias1>\t…\t<aliasN>\n | ||
file.WriteString(strings.Join( | ||
append( | ||
[]string{entry.IPAddress, entry.CanonicalHostname}, | ||
entry.Aliases..., | ||
), | ||
"\t", | ||
) + "\n") | ||
} | ||
} | ||
|
||
func (h *Hosts) Add(containerId string) { | ||
h.Lock() | ||
defer h.Unlock() | ||
|
||
container, err := h.docker.InspectContainer(containerId) | ||
if err != nil { | ||
log.Println("unable to inspect container:", containerId, err) | ||
return | ||
} | ||
|
||
entry := HostEntry{ | ||
IPAddress: container.NetworkSettings.IPAddress, | ||
CanonicalHostname: container.Config.Hostname, | ||
Aliases: []string{ | ||
// container.Name[1:], // could contain "_" | ||
}, | ||
} | ||
|
||
if h.domain != "" { | ||
entry.Aliases = | ||
append(h.entries[containerId].Aliases, container.Config.Hostname+"."+h.domain) | ||
} | ||
|
||
h.entries[containerId] = entry | ||
|
||
h.WriteFile() | ||
h.Lock() | ||
defer h.Unlock() | ||
|
||
container, err := h.docker.InspectContainer(containerId) | ||
if err != nil { | ||
log.Println("unable to inspect container:", containerId, err) | ||
return | ||
} | ||
|
||
entry := HostEntry{ | ||
IPAddress: container.NetworkSettings.IPAddress, | ||
CanonicalHostname: container.Config.Hostname, | ||
Aliases: []string{ | ||
// container.Name[1:], // could contain "_" | ||
}, | ||
} | ||
|
||
if h.domain != "" { | ||
entry.Aliases = | ||
append(h.entries[containerId].Aliases, container.Config.Hostname+"."+h.domain) | ||
} | ||
|
||
h.entries[containerId] = entry | ||
|
||
h.WriteFile() | ||
} | ||
|
||
func (h *Hosts) Remove(containerId string) { | ||
h.Lock() | ||
defer h.Unlock() | ||
h.Lock() | ||
defer h.Unlock() | ||
|
||
delete(h.entries, containerId) | ||
delete(h.entries, containerId) | ||
|
||
h.WriteFile() | ||
h.WriteFile() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,61 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
dockerapi "github.com/fsouza/go-dockerclient" | ||
"log" | ||
"os" | ||
"flag" | ||
dockerapi "github.com/fsouza/go-dockerclient" | ||
"log" | ||
"os" | ||
) | ||
|
||
func getopt(name, def string) string { | ||
if env := os.Getenv(name); env != "" { | ||
return env | ||
} | ||
return def | ||
if env := os.Getenv(name); env != "" { | ||
return env | ||
} | ||
return def | ||
} | ||
|
||
func assert(err error) { | ||
if err != nil { | ||
log.Fatal("docker-hosts: ", err) | ||
} | ||
if err != nil { | ||
log.Fatal("docker-hosts: ", err) | ||
} | ||
} | ||
|
||
func main() { | ||
domainName := flag.String("domain-name", "", "domain name to append") | ||
flag.Parse() | ||
domainName := flag.String("domain-name", "", "domain name to append") | ||
flag.Parse() | ||
|
||
hostsFile := flag.Arg(0) | ||
if hostsFile == "" { | ||
log.Fatal("no hosts file provided") | ||
} | ||
hostsFile := flag.Arg(0) | ||
if hostsFile == "" { | ||
log.Fatal("no hosts file provided") | ||
} | ||
|
||
docker, err := dockerapi.NewClient(getopt("DOCKER_HOST", "unix:///var/run/docker.sock")) | ||
assert(err) | ||
docker, err := dockerapi.NewClient(getopt("DOCKER_HOST", "unix:///var/run/docker.sock")) | ||
assert(err) | ||
|
||
hosts := NewHosts(docker, hostsFile, *domainName) | ||
hosts := NewHosts(docker, hostsFile, *domainName) | ||
|
||
// set up to handle events early, so we don't miss anything while doing the | ||
// initial population | ||
events := make(chan *dockerapi.APIEvents) | ||
assert(docker.AddEventListener(events)) | ||
// set up to handle events early, so we don't miss anything while doing the | ||
// initial population | ||
events := make(chan *dockerapi.APIEvents) | ||
assert(docker.AddEventListener(events)) | ||
|
||
containers, err := docker.ListContainers(dockerapi.ListContainersOptions{}) | ||
assert(err) | ||
containers, err := docker.ListContainers(dockerapi.ListContainersOptions{}) | ||
assert(err) | ||
|
||
for _, listing := range containers { | ||
go hosts.Add(listing.ID) | ||
} | ||
for _, listing := range containers { | ||
go hosts.Add(listing.ID) | ||
} | ||
|
||
log.Println("docker-hosts: Listening for Docker events...") | ||
for msg := range events { | ||
switch msg.Status { | ||
case "start": | ||
go hosts.Add(msg.ID) | ||
log.Println("docker-hosts: Listening for Docker events...") | ||
for msg := range events { | ||
switch msg.Status { | ||
case "start": | ||
go hosts.Add(msg.ID) | ||
|
||
case "die": | ||
go hosts.Remove(msg.ID) | ||
} | ||
} | ||
case "die": | ||
go hosts.Remove(msg.ID) | ||
} | ||
} | ||
|
||
log.Fatal("docker-hosts: docker event loop closed") // todo: reconnect? | ||
log.Fatal("docker-hosts: docker event loop closed") // todo: reconnect? | ||
} |