func (sb *sandbox) buildHostsFile() error { if sb.config.hostsPath == "" { sb.config.hostsPath = defaultPrefix + "/" + sb.id + "/hosts" } dir, _ := filepath.Split(sb.config.hostsPath) if err := createBasePath(dir); err != nil { return err } // This is for the host mode networking if sb.config.originHostsPath != "" { if err := copyFile(sb.config.originHostsPath, sb.config.hostsPath); err != nil && !os.IsNotExist(err) { return types.InternalErrorf("could not copy source hosts file %s to %s: %v", sb.config.originHostsPath, sb.config.hostsPath, err) } return nil } extraContent := make([]etchosts.Record, 0, len(sb.config.extraHosts)) for _, extraHost := range sb.config.extraHosts { extraContent = append(extraContent, etchosts.Record{Hosts: extraHost.name, IP: extraHost.IP}) } return etchosts.Build(sb.config.hostsPath, "", sb.config.hostName, sb.config.domainName, extraContent) }
func (ep *endpoint) buildHostsFiles() error { var extraContent []etchosts.Record ep.Lock() container := ep.container joinInfo := ep.joinInfo ifaces := ep.iFaces ep.Unlock() if container == nil { return ErrNoContainer{} } if container.config.hostsPath == "" { container.config.hostsPath = defaultPrefix + "/" + container.id + "/hosts" } dir, _ := filepath.Split(container.config.hostsPath) err := createBasePath(dir) if err != nil { return err } if joinInfo != nil && joinInfo.hostsPath != "" { content, err := ioutil.ReadFile(joinInfo.hostsPath) if err != nil && !os.IsNotExist(err) { return err } if err == nil { return ioutil.WriteFile(container.config.hostsPath, content, 0644) } } name := container.config.hostName if container.config.domainName != "" { name = name + "." + container.config.domainName } for _, extraHost := range container.config.extraHosts { extraContent = append(extraContent, etchosts.Record{Hosts: extraHost.name, IP: extraHost.IP}) } IP := "" if len(ifaces) != 0 && ifaces[0] != nil { IP = ifaces[0].addr.IP.String() } return etchosts.Build(container.config.hostsPath, IP, container.config.hostName, container.config.domainName, extraContent) }
func (sb *sandbox) updateHostsFile(ifaceIP string, svcRecords []etchosts.Record) error { // Rebuild the hosts file accounting for the passed interface IP and service records extraContent := make([]etchosts.Record, 0, len(sb.config.extraHosts)+len(svcRecords)) for _, extraHost := range sb.config.extraHosts { extraContent = append(extraContent, etchosts.Record{Hosts: extraHost.name, IP: extraHost.IP}) } for _, svc := range svcRecords { extraContent = append(extraContent, svc) } return etchosts.Build(sb.config.hostsPath, ifaceIP, sb.config.hostName, sb.config.domainName, extraContent) }
func (sb *sandbox) updateHostsFile(ifaceIP string, svcRecords []etchosts.Record) error { var err error if sb.config.originHostsPath != "" { return nil } max := func(a, b int) int { if a < b { return b } return a } extraContent := make([]etchosts.Record, 0, max(len(sb.config.extraHosts), len(svcRecords))) sb.hostsOnce.Do(func() { // Rebuild the hosts file accounting for the passed // interface IP and service records for _, extraHost := range sb.config.extraHosts { extraContent = append(extraContent, etchosts.Record{Hosts: extraHost.name, IP: extraHost.IP}) } err = etchosts.Build(sb.config.hostsPath, ifaceIP, sb.config.hostName, sb.config.domainName, extraContent) }) if err != nil { return err } extraContent = extraContent[:0] for _, svc := range svcRecords { extraContent = append(extraContent, svc) } sb.addHostsEntries(extraContent) return nil }