forked from kubernetes/minikube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh_runner.go
122 lines (106 loc) · 3.35 KB
/
ssh_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
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
/*
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 (
"fmt"
"io"
"path"
"sync"
"github.com/golang/glog"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh"
"k8s.io/minikube/pkg/minikube/assets"
)
// SSHRunner runs commands through SSH.
//
// It implements the CommandRunner interface.
type SSHRunner struct {
c *ssh.Client
}
// NewSSHRunner returns a new SSHRunner that will run commands
// through the ssh.Client provided.
func NewSSHRunner(c *ssh.Client) *SSHRunner {
return &SSHRunner{c}
}
// Remove runs a command to delete a file on the remote.
func (s *SSHRunner) Remove(f assets.CopyableFile) error {
sess, err := s.c.NewSession()
if err != nil {
return errors.Wrap(err, "getting ssh session")
}
defer sess.Close()
cmd := getDeleteFileCommand(f)
return sess.Run(cmd)
}
// Run starts a command on the remote and waits for it to return.
func (s *SSHRunner) Run(cmd string) error {
glog.Infoln("Run:", cmd)
sess, err := s.c.NewSession()
if err != nil {
return errors.Wrap(err, "getting ssh session")
}
defer sess.Close()
return sess.Run(cmd)
}
// CombinedOutput runs the command on the remote and returns its combined
// standard output and standard error.
func (s *SSHRunner) CombinedOutput(cmd string) (string, error) {
glog.Infoln("Run with output:", cmd)
sess, err := s.c.NewSession()
if err != nil {
return "", errors.Wrap(err, "getting ssh session")
}
defer sess.Close()
out, err := sess.CombinedOutput(cmd)
if err != nil {
return "", errors.Wrapf(err, "running command: %s\n output: %s", cmd, out)
}
return string(out), nil
}
// Copy copies a file to the remote over SSH.
func (s *SSHRunner) Copy(f assets.CopyableFile) error {
deleteCmd := fmt.Sprintf("sudo rm -f %s", path.Join(f.GetTargetDir(), f.GetTargetName()))
mkdirCmd := fmt.Sprintf("sudo mkdir -p %s", f.GetTargetDir())
for _, cmd := range []string{deleteCmd, mkdirCmd} {
if err := s.Run(cmd); err != nil {
return errors.Wrapf(err, "Error running command: %s", cmd)
}
}
sess, err := s.c.NewSession()
if err != nil {
return errors.Wrap(err, "Error creating new session via ssh client")
}
w, err := sess.StdinPipe()
if err != nil {
return errors.Wrap(err, "Error accessing StdinPipe via ssh session")
}
// The scpcmd below *should not* return until all data is copied and the
// StdinPipe is closed. But let's use a WaitGroup to make it expicit.
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
defer w.Close()
header := fmt.Sprintf("C%s %d %s\n", f.GetPermissions(), f.GetLength(), f.GetTargetName())
fmt.Fprint(w, header)
io.Copy(w, f)
fmt.Fprint(w, "\x00")
}()
scpcmd := fmt.Sprintf("sudo scp -t %s", f.GetTargetDir())
out, err := sess.CombinedOutput(scpcmd)
if err != nil {
return errors.Wrapf(err, "Error running scp command: %s output: %s", scpcmd, out)
}
wg.Wait()
return nil
}