forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host.go
289 lines (270 loc) · 7.66 KB
/
host.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package host
import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
docker "github.com/fsouza/go-dockerclient"
"github.com/golang/glog"
tarhelper "github.com/openshift/source-to-image/pkg/tar"
"github.com/openshift/origin/pkg/bootstrap/docker/errors"
"github.com/openshift/origin/pkg/bootstrap/docker/run"
)
const (
cmdTestNsenterMount = "nsenter --mount=/rootfs/proc/1/ns/mnt findmnt"
cmdCreateVolumesDir = "if [ ! -d %[1]s ]; then mkdir -p %[1]s; fi"
cmdCreateVolumesDirBindMount = "cat /rootfs/proc/1/mountinfo | grep /var/lib/origin || " +
"nsenter --mount=/rootfs/proc/1/ns/mnt mount -o bind %[1]s %[1]s"
cmdCreateVolumesDirShare = "cat /rootfs/proc/1/mountinfo | grep %[1]s | grep shared || " +
"nsenter --mount=/rootfs/proc/1/ns/mnt mount --make-shared %[1]s"
)
// HostHelper contains methods to help check settings on a Docker host machine
// using a privileged container
type HostHelper struct {
runHelper *run.RunHelper
client *docker.Client
image string
volumesDir string
}
// NewHostHelper creates a new HostHelper
func NewHostHelper(client *docker.Client, image, volumesDir string) *HostHelper {
return &HostHelper{
runHelper: run.NewRunHelper(client),
client: client,
image: image,
volumesDir: volumesDir,
}
}
// CanUseNsenterMounter returns true if the Docker host machine can execute findmnt through nsenter
func (h *HostHelper) CanUseNsenterMounter() (bool, error) {
rc, err := h.runner().
Image(h.image).
DiscardContainer().
Privileged().
Bind("/:/rootfs:ro").
Entrypoint("/bin/bash").
Command("-c", cmdTestNsenterMount).Run()
return err == nil && rc == 0, nil
}
// EnsureVolumeShare ensures that the host Docker machine has a shared directory that can be used
// for OpenShift volumes
func (h *HostHelper) EnsureVolumeShare() error {
if err := h.ensureVolumesDir(); err != nil {
return err
}
if err := h.ensureVolumesDirBindMount(); err != nil {
return err
}
if err := h.ensureVolumesDirShare(); err != nil {
return err
}
return nil
}
func catHostFile(hostFile string) string {
file := path.Join("/rootfs", hostFile)
return fmt.Sprintf("cat %s", file)
}
// CopyFromHost copies a set of files from the Docker host to the local file system
func (h *HostHelper) CopyFromHost(sourceDir, destDir string) error {
container, err := h.runner().
Image(h.image).
Bind(fmt.Sprintf("%[1]s:%[1]s:ro", sourceDir)).
Create()
if err != nil {
return err
}
defer func() {
errors.LogError(h.client.RemoveContainer(docker.RemoveContainerOptions{ID: container}))
}()
localTarFile, err := ioutil.TempFile("", "local-copy-tar-")
if err != nil {
return err
}
localTarClosed := false
defer func() {
if !localTarClosed {
errors.LogError(localTarFile.Close())
}
errors.LogError(os.Remove(localTarFile.Name()))
}()
glog.V(4).Infof("Downloading from host path %s to local tar file: %s", sourceDir, localTarFile.Name())
err = h.client.DownloadFromContainer(container, docker.DownloadFromContainerOptions{
Path: sourceDir,
OutputStream: localTarFile,
})
if err != nil {
return err
}
if err = localTarFile.Close(); err != nil {
return err
}
localTarClosed = true
inputTar, err := os.Open(localTarFile.Name())
if err != nil {
return err
}
defer func() {
errors.LogError(inputTar.Close())
}()
tarHelper := tarhelper.New()
tarHelper.SetExclusionPattern(nil)
glog.V(4).Infof("Extracting temporary tar %s to directory %s", inputTar.Name(), destDir)
var tarLog io.Writer
if glog.V(5) {
tarLog = os.Stderr
}
return tarHelper.ExtractTarStreamWithLogging(destDir, inputTar, tarLog)
}
// makeTempCopy creates a temporary directory and places a copy of the source file
// in it. It returns the directory where the temporary copy was made.
func makeTempCopy(file string) (string, error) {
tempDir, err := ioutil.TempDir("", "")
if err != nil {
return "", err
}
destPath := filepath.Join(tempDir, filepath.Base(file))
destFile, err := os.Create(destPath)
if err != nil {
return "", err
}
defer func() {
errors.LogError(destFile.Close())
}()
sourceFile, err := os.Open(file)
if err != nil {
return "", err
}
defer func() {
errors.LogError(sourceFile.Close())
}()
_, err = io.Copy(destFile, sourceFile)
return tempDir, err
}
// CopyMasterConfigToHost copies a local file to the Docker host
func (h *HostHelper) CopyMasterConfigToHost(sourceFile, destDir string) error {
localDir, err := makeTempCopy(sourceFile)
if err != nil {
return err
}
tarHelper := tarhelper.New()
tarHelper.SetExclusionPattern(nil)
var tarLog io.Writer
if glog.V(5) {
tarLog = os.Stderr
}
localTarFile, err := ioutil.TempFile("", "master-config")
if err != nil {
return err
}
localTarClosed := false
defer func() {
if !localTarClosed {
errors.LogError(localTarFile.Close())
}
}()
glog.V(4).Infof("Creating temporary tar %s to upload to %s", localTarFile.Name(), destDir)
err = tarHelper.CreateTarStreamWithLogging(localDir, false, localTarFile, tarLog)
if err != nil {
return err
}
err = localTarFile.Close()
if err != nil {
return err
}
localTarClosed = true
localTarInputClosed := false
localTarInput, err := os.Open(localTarFile.Name())
if err != nil {
return err
}
defer func() {
if !localTarInputClosed {
localTarInput.Close()
}
}()
bind := fmt.Sprintf("%s:/var/lib/origin/openshift.local.config:z", destDir)
container, err := h.runner().
Image(h.image).
Bind(bind).Create()
_ = container
if err != nil {
return err
}
defer func() {
errors.LogError(h.client.RemoveContainer(docker.RemoveContainerOptions{ID: container}))
}()
glog.V(4).Infof("Uploading tar file %s to remote dir: %s", localTarFile.Name(), destDir)
err = h.client.UploadToContainer(container, docker.UploadToContainerOptions{
InputStream: localTarInput,
Path: "/var/lib/origin/openshift.local.config/master",
})
if err != nil {
glog.V(4).Infof("An error occurred uploading the file: %v", err)
} else {
// If the upload succeeded the local input stream will be closed automatically
localTarInputClosed = true
glog.V(4).Infof("Successfully uploaded file.")
}
return err
}
// Hostname retrieves the FQDN of the Docker host machine
func (h *HostHelper) Hostname() (string, error) {
hostname, _, _, err := h.runner().
Image(h.image).
HostNetwork().
HostPid().
DiscardContainer().
Privileged().
Entrypoint("/bin/bash").
Command("-c", "uname -n").Output()
if err != nil {
return "", err
}
return strings.ToLower(strings.TrimSpace(hostname)), nil
}
func (h *HostHelper) ensureVolumesDir() error {
cmd := fmt.Sprintf(cmdCreateVolumesDir, path.Join("/rootfs", h.volumesDir))
rc, err := h.runner().
Image(h.image).
DiscardContainer().
Privileged().
Bind("/:/rootfs").
Entrypoint("/bin/bash").
Command("-c", cmd).Run()
if err != nil || rc != 0 {
return errors.NewError("cannot create host volumes directory").WithCause(err)
}
return nil
}
func (h *HostHelper) hostPidCmd(cmd string) (int, error) {
return h.runner().
Image(h.image).
DiscardContainer().
HostPid().
Privileged().
Bind("/:/rootfs:ro").
Entrypoint("/bin/bash").
Command("-c", cmd).Run()
}
func (h *HostHelper) ensureVolumesDirBindMount() error {
cmd := fmt.Sprintf(cmdCreateVolumesDirBindMount, h.volumesDir)
rc, err := h.hostPidCmd(cmd)
if err != nil || rc != 0 {
return errors.NewError("cannot create volumes dir mount").WithCause(err)
}
return nil
}
func (h *HostHelper) ensureVolumesDirShare() error {
cmd := fmt.Sprintf(cmdCreateVolumesDirShare, h.volumesDir)
rc, err := h.hostPidCmd(cmd)
if err != nil || rc != 0 {
return errors.NewError("cannot create volumes dir share").WithCause(err)
}
return nil
}
func (h *HostHelper) runner() *run.Runner {
return h.runHelper.New()
}