|
| 1 | +/* |
| 2 | +Copyright 2020 Roblox Corporation |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package etchosts |
| 19 | + |
| 20 | +import ( |
| 21 | + "bytes" |
| 22 | + "fmt" |
| 23 | + "io" |
| 24 | + "io/ioutil" |
| 25 | + "os" |
| 26 | + "strings" |
| 27 | + |
| 28 | + "github.com/docker/docker/opts" |
| 29 | +) |
| 30 | + |
| 31 | +// Code referenced from https://github.com/moby/libnetwork/blob/master/etchosts/etchosts.go |
| 32 | + |
| 33 | +// Record Structure for a single host record |
| 34 | +type Record struct { |
| 35 | + Hosts string |
| 36 | + IP string |
| 37 | +} |
| 38 | + |
| 39 | +// WriteTo writes record to file and returns bytes written or error |
| 40 | +func (r Record) WriteTo(w io.Writer) (int64, error) { |
| 41 | + n, err := fmt.Fprintf(w, "%s\t%s\n", r.IP, r.Hosts) |
| 42 | + return int64(n), err |
| 43 | +} |
| 44 | + |
| 45 | +var ( |
| 46 | + // Default hosts config records slice |
| 47 | + defaultContent = []Record{ |
| 48 | + {Hosts: "localhost", IP: "127.0.0.1"}, |
| 49 | + {Hosts: "localhost ip6-localhost ip6-loopback", IP: "::1"}, |
| 50 | + {Hosts: "ip6-localnet", IP: "fe00::0"}, |
| 51 | + {Hosts: "ip6-mcastprefix", IP: "ff00::0"}, |
| 52 | + {Hosts: "ip6-allnodes", IP: "ff02::1"}, |
| 53 | + {Hosts: "ip6-allrouters", IP: "ff02::2"}, |
| 54 | + } |
| 55 | +) |
| 56 | + |
| 57 | +// BuildEtcHosts builds NOMAD_TASK_DIR/etc_hosts with defaults. |
| 58 | +func BuildEtcHosts(hostsFile string) error { |
| 59 | + content := bytes.NewBuffer(nil) |
| 60 | + |
| 61 | + // Write defaultContent slice to buffer |
| 62 | + for _, r := range defaultContent { |
| 63 | + if _, err := r.WriteTo(content); err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + } |
| 67 | + return ioutil.WriteFile(hostsFile, content.Bytes(), 0644) |
| 68 | +} |
| 69 | + |
| 70 | +// CopyEtcHosts copies /etc/hosts to NOMAD_TASK_DIR/etc_hosts |
| 71 | +func CopyEtcHosts(hostsFile string) error { |
| 72 | + srcFile, err := os.Open("/etc/hosts") |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + defer srcFile.Close() |
| 77 | + |
| 78 | + destFile, err := os.Create(hostsFile) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + defer destFile.Close() |
| 83 | + |
| 84 | + _, err = io.Copy(destFile, srcFile) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +// AddExtraHosts add hosts, given as name:IP to container /etc/hosts. |
| 92 | +func AddExtraHosts(hostsFile string, extraHosts []string) error { |
| 93 | + fd, err := os.OpenFile(hostsFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + defer fd.Close() |
| 98 | + |
| 99 | + for _, extraHost := range extraHosts { |
| 100 | + // allow IPv6 addresses in extra hosts; only split on first ":" |
| 101 | + if _, err := opts.ValidateExtraHost(extraHost); err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + |
| 105 | + hostnameIP := strings.SplitN(extraHost, ":", 2) |
| 106 | + msg := fmt.Sprintf("%s\t%s\n", hostnameIP[1], hostnameIP[0]) |
| 107 | + if _, err := fd.WriteString(msg); err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + } |
| 111 | + return nil |
| 112 | +} |
0 commit comments