This repository has been archived by the owner on Aug 21, 2019. It is now read-only.
forked from blalor/docker-hosts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hosts.go
113 lines (91 loc) · 2.65 KB
/
hosts.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
dockerapi "github.com/fsouza/go-dockerclient"
"log"
"os"
"strings"
"sync"
)
type HostEntry struct {
IPAddress string
CanonicalHostname string
Aliases []string
}
type Hosts struct {
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
}
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")
}
}
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()
}
func (h *Hosts) Remove(containerId string) {
h.Lock()
defer h.Unlock()
delete(h.entries, containerId)
h.WriteFile()
}