Make /etc/hosts records consistent
Fixes #8972 Signed-off-by: Alexandr Morozov <lk4d4@docker.com>
This commit is contained in:
parent
bf0a0e9ca7
commit
5ecbc5de38
1 changed files with 33 additions and 19 deletions
|
@ -3,40 +3,54 @@ package etchosts
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
var defaultContent = map[string]string{
|
type Record struct {
|
||||||
"localhost": "127.0.0.1",
|
Hosts string
|
||||||
"localhost ip6-localhost ip6-loopback": "::1",
|
IP string
|
||||||
"ip6-localnet": "fe00::0",
|
|
||||||
"ip6-mcastprefix": "ff00::0",
|
|
||||||
"ip6-allnodes": "ff02::1",
|
|
||||||
"ip6-allrouters": "ff02::2",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
content := bytes.NewBuffer(nil)
|
||||||
if IP != "" {
|
if IP != "" {
|
||||||
|
var mainRec Record
|
||||||
|
mainRec.IP = IP
|
||||||
if domainname != "" {
|
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 {
|
} else {
|
||||||
content.WriteString(fmt.Sprintf("%s\t%s\n", IP, hostname))
|
mainRec.Hosts = hostname
|
||||||
}
|
}
|
||||||
}
|
if _, err := mainRec.WriteTo(content); err != nil {
|
||||||
|
|
||||||
for hosts, ip := range defaultContent {
|
|
||||||
if _, err := content.WriteString(fmt.Sprintf("%s\t%s\n", ip, hosts)); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if extraContent != nil {
|
for _, r := range defaultContent {
|
||||||
for hosts, ip := range *extraContent {
|
if _, err := r.WriteTo(content); err != nil {
|
||||||
if _, err := content.WriteString(fmt.Sprintf("%s\t%s\n", ip, hosts)); err != nil {
|
return err
|
||||||
return err
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, r := range extraContent {
|
||||||
|
if _, err := r.WriteTo(content); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue