forked from kubernetes/minikube
/
exec_runner.go
94 lines (81 loc) · 2.79 KB
/
exec_runner.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
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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 bootstrapper
import (
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"github.com/golang/glog"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/assets"
)
// ExecRunner runs commands using the os/exec package.
//
// It implements the CommandRunner interface.
type ExecRunner struct{}
// Run starts the specified command in a bash shell and waits for it to complete.
func (*ExecRunner) Run(cmd string) error {
glog.Infoln("Run:", cmd)
c := exec.Command("/bin/bash", "-c", cmd)
if err := c.Run(); err != nil {
return errors.Wrapf(err, "running command: %s", cmd)
}
return nil
}
// CombinedOutput runs the command in a bash shell and returns its
// combined standard output and standard error.
func (*ExecRunner) CombinedOutput(cmd string) (string, error) {
glog.Infoln("Run with output:", cmd)
c := exec.Command("/bin/bash", "-c", cmd)
out, err := c.CombinedOutput()
if err != nil {
return "", errors.Wrapf(err, "running command: %s\n output: %s", cmd, out)
}
return string(out), nil
}
// Copy copies a file and its permissions
func (*ExecRunner) Copy(f assets.CopyableFile) error {
if err := os.MkdirAll(f.GetTargetDir(), os.ModePerm); err != nil {
return errors.Wrapf(err, "error making dirs for %s", f.GetTargetDir())
}
targetPath := filepath.Join(f.GetTargetDir(), f.GetTargetName())
if _, err := os.Stat(targetPath); err == nil {
if err := os.Remove(targetPath); err != nil {
return errors.Wrapf(err, "error removing file %s", targetPath)
}
}
target, err := os.Create(targetPath)
if err != nil {
return errors.Wrapf(err, "error creating file at %s", targetPath)
}
perms, err := strconv.Atoi(f.GetPermissions())
if err != nil {
return errors.Wrapf(err, "error converting permissions %s to integer", perms)
}
if err := os.Chmod(targetPath, os.FileMode(perms)); err != nil {
return errors.Wrapf(err, "error changing file permissions for %s", targetPath)
}
if _, err = io.Copy(target, f); err != nil {
return errors.Wrapf(err, `error copying file %s to target location:
do you have the correct permissions?`,
targetPath)
}
return target.Close()
}
// Remove removes a file
func (e *ExecRunner) Remove(f assets.CopyableFile) error {
cmd := getDeleteFileCommand(f)
return e.Run(cmd)
}