mirror of
https://github.com/costela/docker-etchosts.git
synced 2025-05-11 09:55:48 +02:00
add more error reporting and fallback mode for docker
This commit is contained in:
parent
092698694f
commit
42552f4316
3 changed files with 38 additions and 9 deletions
|
@ -132,6 +132,7 @@ func waitForConnection(client dockerClienter) {
|
||||||
log.Info("retrying connection to docker")
|
log.Info("retrying connection to docker")
|
||||||
_, err := client.Ping(context.Background())
|
_, err := client.Ping(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Errorf("error pinging docker server: %s", err)
|
||||||
return fmt.Errorf("error pinging docker server: %s", err)
|
return fmt.Errorf("error pinging docker server: %s", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
44
etchosts.go
44
etchosts.go
|
@ -17,8 +17,9 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func writeToEtcHosts(ipsToNames ipsToNamesMap) error {
|
func writeToEtcHosts(ipsToNames ipsToNamesMap) error {
|
||||||
// this will fail if the file doesn't exist, which is probably ok
|
// We do not want to create the hosts file; if it's not there, we probably have the wrong path.
|
||||||
etcHosts, err := os.Open(config.EtcHostsPath)
|
// Open RW because we might have to write to it (see movePreservePerms)
|
||||||
|
etcHosts, err := os.OpenFile(config.EtcHostsPath, os.O_RDWR, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not open %s for reading: %s", config.EtcHostsPath, err)
|
return fmt.Errorf("could not open %s for reading: %s", config.EtcHostsPath, err)
|
||||||
}
|
}
|
||||||
|
@ -55,7 +56,10 @@ func writeToEtcHosts(ipsToNames ipsToNamesMap) error {
|
||||||
}
|
}
|
||||||
ip := tokens[0]
|
ip := tokens[0]
|
||||||
if names, ok := ipsToNames[ip]; ok {
|
if names, ok := ipsToNames[ip]; ok {
|
||||||
writeEntryWithBanner(tmp, ip, names)
|
err = writeEntryWithBanner(tmp, ip, names)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
delete(ipsToNames, ip) // otherwise we'll append it again below
|
delete(ipsToNames, ip) // otherwise we'll append it again below
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -63,10 +67,16 @@ func writeToEtcHosts(ipsToNames ipsToNamesMap) error {
|
||||||
fmt.Fprintf(tmp, "%s\n", line)
|
fmt.Fprintf(tmp, "%s\n", line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return fmt.Errorf("error reading %s: %s", config.EtcHostsPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
// append remaining entries to file
|
// append remaining entries to file
|
||||||
for ip, names := range ipsToNames {
|
for ip, names := range ipsToNames {
|
||||||
writeEntryWithBanner(tmp, ip, names)
|
err = writeEntryWithBanner(tmp, ip, names)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = movePreservePerms(tmp, etcHosts)
|
err = movePreservePerms(tmp, etcHosts)
|
||||||
|
@ -77,12 +87,17 @@ func writeToEtcHosts(ipsToNames ipsToNamesMap) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeEntryWithBanner(tmp io.Writer, ip string, names []string) {
|
func writeEntryWithBanner(tmp io.Writer, ip string, names []string) error {
|
||||||
if len(names) > 0 {
|
if len(names) > 0 {
|
||||||
log.Debugf("writing entry for %s (%s)", ip, names)
|
log.Debugf("writing entry for %s (%s)", ip, names)
|
||||||
fmt.Fprintf(tmp, "%s\n", banner)
|
if _, err := fmt.Fprintf(tmp, "%s\n", banner); err != nil {
|
||||||
fmt.Fprintf(tmp, "%s\t%s\n", ip, strings.Join(names, " "))
|
return fmt.Errorf("error writing entry for %s: %s", ip, err)
|
||||||
|
}
|
||||||
|
if _, err := fmt.Fprintf(tmp, "%s\t%s\n", ip, strings.Join(names, " ")); err != nil {
|
||||||
|
return fmt.Errorf("error writing entry for %s: %s", ip, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func movePreservePerms(src, dst *os.File) error {
|
func movePreservePerms(src, dst *os.File) error {
|
||||||
|
@ -93,7 +108,20 @@ func movePreservePerms(src, dst *os.File) error {
|
||||||
|
|
||||||
err = os.Rename(src.Name(), dst.Name())
|
err = os.Rename(src.Name(), dst.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not rename to %s: %s", config.EtcHostsPath, err)
|
log.Infof("could not rename to %s; falling back to less safe direct-write (%s)", config.EtcHostsPath, err)
|
||||||
|
|
||||||
|
if _, err := src.Seek(0, io.SeekStart); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := dst.Seek(0, io.SeekStart); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := dst.Truncate(0); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = io.Copy(dst, src)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure we're not running with some umask that might break things
|
// ensure we're not running with some umask that might break things
|
||||||
|
|
2
main.go
2
main.go
|
@ -65,7 +65,7 @@ func main() {
|
||||||
|
|
||||||
client, err := docker.NewEnvClient()
|
client, err := docker.NewEnvClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error starting docker client: %s", err)
|
log.Fatalf("error initializing docker client: %s", err)
|
||||||
}
|
}
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue