-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssh_command.go
185 lines (154 loc) · 3.2 KB
/
ssh_command.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
package ssh
import (
"bytes"
"errors"
"io"
"io/ioutil"
"strings"
"time"
"golang.org/x/crypto/ssh"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers"
)
type Client struct {
Config
Stdout io.Writer
Stderr io.Writer
ConnectRetries int
client *ssh.Client
}
type Command struct {
Environment []string
Command []string
Stdin string
Abort chan interface{}
}
type ExitError struct {
Inner error
}
func (e *ExitError) Error() string {
if e.Inner == nil {
return "error"
}
return e.Inner.Error()
}
func (s *Client) getSSHKey(identityFile string) (key ssh.Signer, err error) {
buf, err := ioutil.ReadFile(identityFile)
if err != nil {
return nil, err
}
key, err = ssh.ParsePrivateKey(buf)
return key, err
}
func (s *Client) getSSHAuthMethods() ([]ssh.AuthMethod, error) {
var methods []ssh.AuthMethod
methods = append(methods, ssh.Password(s.Password))
if s.IdentityFile != "" {
key, err := s.getSSHKey(s.IdentityFile)
if err != nil {
return nil, err
}
methods = append(methods, ssh.PublicKeys(key))
}
return methods, nil
}
func (s *Client) Connect() error {
if s.Host == "" {
s.Host = "localhost"
}
if s.User == "" {
s.User = "root"
}
if s.Port == "" {
s.Port = "22"
}
methods, err := s.getSSHAuthMethods()
if err != nil {
return err
}
config := &ssh.ClientConfig{
User: s.User,
Auth: methods,
}
connectRetries := s.ConnectRetries
if connectRetries == 0 {
connectRetries = 3
}
var finalError error
for i := 0; i < connectRetries; i++ {
client, err := ssh.Dial("tcp", s.Host+":"+s.Port, config)
if err == nil {
s.client = client
return nil
}
time.Sleep(sshRetryInterval * time.Second)
finalError = err
}
return finalError
}
func (s *Client) Exec(cmd string) error {
if s.client == nil {
return errors.New("Not connected")
}
session, err := s.client.NewSession()
if err != nil {
return err
}
session.Stdout = s.Stdout
session.Stderr = s.Stderr
err = session.Run(cmd)
session.Close()
return err
}
func (s *Command) fullCommand() string {
var arguments []string
// TODO: This method is compatible only with Bjourne compatible shells
for _, part := range s.Command {
arguments = append(arguments, helpers.ShellEscape(part))
}
return strings.Join(arguments, " ")
}
func (s *Client) Run(cmd Command) error {
if s.client == nil {
return errors.New("Not connected")
}
session, err := s.client.NewSession()
if err != nil {
return err
}
defer session.Close()
var envVariables bytes.Buffer
for _, keyValue := range cmd.Environment {
envVariables.WriteString("export " + helpers.ShellEscape(keyValue) + "\n")
}
session.Stdin = io.MultiReader(
&envVariables,
bytes.NewBufferString(cmd.Stdin),
)
session.Stdout = s.Stdout
session.Stderr = s.Stderr
err = session.Start(cmd.fullCommand())
if err != nil {
return err
}
waitCh := make(chan error)
go func() {
err := session.Wait()
if _, ok := err.(*ssh.ExitError); ok {
err = &ExitError{Inner: err}
}
waitCh <- err
}()
select {
case <-cmd.Abort:
session.Signal(ssh.SIGKILL)
session.Close()
return <-waitCh
case err := <-waitCh:
return err
}
}
func (s *Client) Cleanup() {
if s.client != nil {
s.client.Close()
}
}