mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Make /etc/hosts records consistent
Fixes #8972 Signed-off-by: Alexandr Morozov <lk4d4@docker.com>
This commit is contained in:
parent
a61c4dc959
commit
6cbe1fa726
2 changed files with 37 additions and 23 deletions
|
@ -420,7 +420,7 @@ func (container *Container) buildHostsFiles(IP string) error {
|
|||
}
|
||||
container.HostsPath = hostsPath
|
||||
|
||||
extraContent := make(map[string]string)
|
||||
var extraContent []etchosts.Record
|
||||
|
||||
children, err := container.daemon.Children(container.Name)
|
||||
if err != nil {
|
||||
|
@ -429,15 +429,15 @@ func (container *Container) buildHostsFiles(IP string) error {
|
|||
|
||||
for linkAlias, child := range children {
|
||||
_, alias := path.Split(linkAlias)
|
||||
extraContent[alias] = child.NetworkSettings.IPAddress
|
||||
extraContent = append(extraContent, etchosts.Record{Hosts: alias, IP: child.NetworkSettings.IPAddress})
|
||||
}
|
||||
|
||||
for _, extraHost := range container.hostConfig.ExtraHosts {
|
||||
parts := strings.Split(extraHost, ":")
|
||||
extraContent[parts[0]] = parts[1]
|
||||
extraContent = append(extraContent, etchosts.Record{Hosts: parts[0], IP: parts[1]})
|
||||
}
|
||||
|
||||
return etchosts.Build(container.HostsPath, IP, container.Config.Hostname, container.Config.Domainname, &extraContent)
|
||||
return etchosts.Build(container.HostsPath, IP, container.Config.Hostname, container.Config.Domainname, extraContent)
|
||||
}
|
||||
|
||||
func (container *Container) buildHostnameAndHostsFiles(IP string) error {
|
||||
|
|
|
@ -3,40 +3,54 @@ package etchosts
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var defaultContent = map[string]string{
|
||||
"localhost": "127.0.0.1",
|
||||
"localhost ip6-localhost ip6-loopback": "::1",
|
||||
"ip6-localnet": "fe00::0",
|
||||
"ip6-mcastprefix": "ff00::0",
|
||||
"ip6-allnodes": "ff02::1",
|
||||
"ip6-allrouters": "ff02::2",
|
||||
type Record struct {
|
||||
Hosts string
|
||||
IP string
|
||||
}
|
||||
|
||||
func Build(path, IP, hostname, domainname string, extraContent *map[string]string) error {
|
||||
func (r Record) WriteTo(w io.Writer) (int64, error) {
|
||||
n, err := fmt.Fprintf(w, "%s\t%s\n", r.IP, r.Hosts)
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
var defaultContent = []Record{
|
||||
{Hosts: "localhost", IP: "127.0.0.1"},
|
||||
{Hosts: "localhost ip6-localhost ip6-loopback", IP: "::1"},
|
||||
{Hosts: "ip6-localnet", IP: "fe00::0"},
|
||||
{Hosts: "ip6-mcastprefix", IP: "ff00::0"},
|
||||
{Hosts: "ip6-allnodes", IP: "ff02::1"},
|
||||
{Hosts: "ip6-allrouters", IP: "ff02::2"},
|
||||
}
|
||||
|
||||
func Build(path, IP, hostname, domainname string, extraContent []Record) error {
|
||||
content := bytes.NewBuffer(nil)
|
||||
if IP != "" {
|
||||
var mainRec Record
|
||||
mainRec.IP = IP
|
||||
if domainname != "" {
|
||||
content.WriteString(fmt.Sprintf("%s\t%s.%s %s\n", IP, hostname, domainname, hostname))
|
||||
mainRec.Hosts = fmt.Sprintf("%s.%s %s", hostname, domainname, hostname)
|
||||
} else {
|
||||
content.WriteString(fmt.Sprintf("%s\t%s\n", IP, hostname))
|
||||
mainRec.Hosts = hostname
|
||||
}
|
||||
}
|
||||
|
||||
for hosts, ip := range defaultContent {
|
||||
if _, err := content.WriteString(fmt.Sprintf("%s\t%s\n", ip, hosts)); err != nil {
|
||||
if _, err := mainRec.WriteTo(content); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if extraContent != nil {
|
||||
for hosts, ip := range *extraContent {
|
||||
if _, err := content.WriteString(fmt.Sprintf("%s\t%s\n", ip, hosts)); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, r := range defaultContent {
|
||||
if _, err := r.WriteTo(content); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, r := range extraContent {
|
||||
if _, err := r.WriteTo(content); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue