/
hosts.go
64 lines (60 loc) · 2.07 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
package proxy
import (
"bytes"
"fmt"
"io"
"net"
"path"
"strings"
)
// rewrite /etc/hosts, unlinking the file (so Docker does not modify it again) but
// leaving it with valid contents...
func (proxy *Proxy) RewriteEtcHosts(hostsPath, fqdn string, ips []*net.IPNet, extraHosts []string) error {
hostsPathDir := path.Dir(hostsPath)
mnt := "/container"
mntHosts := path.Join(mnt, path.Base(hostsPath))
var buf bytes.Buffer
writeEtcHostsContents(&buf, fqdn, ips, extraHosts)
contents := buf.String()
cmdLine := fmt.Sprintf("echo '%s' > %s && rm -f %s && echo '%s' > %s", contents, mntHosts, mntHosts, contents, mntHosts)
mounts := []string{hostsPathDir + ":" + mnt}
return proxy.runTransientContainer([]string{"sh"}, []string{"-c", cmdLine}, mounts)
}
// we assume (for compatibility with the weave script) that fqdn has a dot
// between hostname and domain. domain may be blank.
func writeEtcHostsContents(w io.Writer, fqdn string, cidrs []*net.IPNet, extraHosts []string) {
index := strings.Index(fqdn, ".")
name := fqdn[:index]
var hostnames string
if index == len(fqdn)-1 { // dot comes at the end - no domain
hostnames = name
} else {
if fqdn[len(fqdn)-1] == '.' { // strip a final trailing dot
fqdn = fqdn[:len(fqdn)-1]
}
hostnames = fqdn + " " + name
}
fmt.Fprintln(w, "# created by Weave - BEGIN")
fmt.Fprintln(w, "# container hostname")
for _, cidr := range cidrs {
fmt.Fprintf(w, "%-15s %s\n", cidr.IP, hostnames)
}
fmt.Fprintln(w)
fmt.Fprintln(w, "# static names added with --add-host")
for _, eh := range extraHosts {
parts := strings.SplitN(eh, ":", 2)
if len(parts) != 2 {
continue
}
fmt.Fprintf(w, "%-15s %s\n", parts[1], parts[0])
}
fmt.Fprintln(w)
fmt.Fprintln(w, "# default localhost entries")
fmt.Fprintln(w, "127.0.0.1 localhost")
fmt.Fprintln(w, "::1 ip6-localhost ip6-loopback")
fmt.Fprintln(w, "fe00::0 ip6-localnet")
fmt.Fprintln(w, "ff00::0 ip6-mcastprefix")
fmt.Fprintln(w, "ff02::1 ip6-allnodes")
fmt.Fprintln(w, "ff02::2 ip6-allrouters")
fmt.Fprintln(w, "# created by Weave - END")
}