forked from kubernetes/kops
/
hosts.go
139 lines (110 loc) · 3.21 KB
/
hosts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package hosts
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"github.com/golang/glog"
)
const GUARD_BEGIN = "# Begin host entries managed by kops - do not edit"
const GUARD_END = "# End host entries managed by kops"
func UpdateHostsFileWithRecords(p string, addrToHosts map[string][]string) error {
stat, err := os.Stat(p)
if err != nil {
return fmt.Errorf("error getting file status of %q: %v", p, err)
}
data, err := ioutil.ReadFile(p)
if err != nil {
return fmt.Errorf("error reading file %q: %v", p, err)
}
var out []string
depth := 0
for _, line := range strings.Split(string(data), "\n") {
k := strings.TrimSpace(line)
if k == GUARD_BEGIN {
depth++
}
if depth <= 0 {
out = append(out, line)
}
if k == GUARD_END {
depth--
}
}
// Ensure a single blank line
for {
if len(out) == 0 {
break
}
if out[len(out)-1] != "" {
break
}
out = out[:len(out)-1]
}
out = append(out, "")
out = append(out, GUARD_BEGIN)
for addr, hosts := range addrToHosts {
sort.Strings(hosts)
out = append(out, addr+"\t"+strings.Join(hosts, " "))
}
out = append(out, GUARD_END)
out = append(out, "")
// Note that because we are bind mounting /etc/hosts, we can't do a normal atomic file write
// (where we write a temp file and rename it)
// TODO: We should just hold the file open while we read & write it
err = ioutil.WriteFile(p, []byte(strings.Join(out, "\n")), stat.Mode().Perm())
if err != nil {
return fmt.Errorf("error writing file %q: %v", p, err)
}
return nil
}
func atomicWriteFile(filename string, data []byte, perm os.FileMode) error {
dir := filepath.Dir(filename)
tempFile, err := ioutil.TempFile(dir, ".tmp"+filepath.Base(filename))
if err != nil {
return fmt.Errorf("error creating temp file in %q: %v", dir, err)
}
mustClose := true
mustRemove := true
defer func() {
if mustClose {
if err := tempFile.Close(); err != nil {
glog.Warningf("error closing temp file: %v", err)
}
}
if mustRemove {
if err := os.Remove(tempFile.Name()); err != nil {
glog.Warningf("error removing temp file %q: %v", tempFile.Name(), err)
}
}
}()
if _, err := tempFile.Write(data); err != nil {
return fmt.Errorf("error writing temp file: %v", err)
}
if err := tempFile.Close(); err != nil {
return fmt.Errorf("error closing temp file: %v", err)
}
mustClose = false
if err := os.Chmod(tempFile.Name(), perm); err != nil {
return fmt.Errorf("error changing mode of temp file: %v", err)
}
if err := os.Rename(tempFile.Name(), filename); err != nil {
return fmt.Errorf("error moving temp file %q to %q: %v", tempFile.Name(), filename, err)
}
mustRemove = false
return nil
}