forked from evergreen-ci/evergreen
/
remote_command.go
91 lines (74 loc) · 2.13 KB
/
remote_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
package command
import (
"fmt"
"io"
"os/exec"
"strings"
"github.com/10gen-labs/slogger/v1"
"github.com/evergreen-ci/evergreen"
)
type RemoteCommand struct {
Id string
CmdString string
Stdout io.Writer
Stderr io.Writer
// info necessary for sshing into the remote host
RemoteHostName string
User string
Options []string
Background bool
// optional flag for hiding sensitive commands from log output
LoggingDisabled bool
// set after the command is started
Cmd *exec.Cmd
}
func (rc *RemoteCommand) Run() error {
evergreen.Logger.Logf(slogger.DEBUG, "RemoteCommand(%v) beginning Run()", rc.Id)
err := rc.Start()
if err != nil {
return err
}
if rc.Cmd != nil && rc.Cmd.Process != nil {
evergreen.Logger.Logf(slogger.DEBUG, "RemoteCommand(%v) started process %v", rc.Id, rc.Cmd.Process.Pid)
} else {
evergreen.Logger.Logf(slogger.DEBUG, "RemoteCommand(%v) has nil Cmd or Cmd.Process in Run()", rc.Id)
}
return rc.Cmd.Wait()
}
func (rc *RemoteCommand) Wait() error {
return rc.Cmd.Wait()
}
func (rc *RemoteCommand) Start() error {
// build the remote connection, in user@host format
remote := rc.RemoteHostName
if rc.User != "" {
remote = fmt.Sprintf("%v@%v", rc.User, remote)
}
// build the command
cmdArray := append(rc.Options, remote)
// set to the background, if necessary
cmdString := rc.CmdString
if rc.Background {
cmdString = fmt.Sprintf("nohup %v > /tmp/start 2>&1 &", cmdString)
}
cmdArray = append(cmdArray, cmdString)
if !rc.LoggingDisabled {
evergreen.Logger.Logf(slogger.WARN, "Remote command executing: '%#v'",
strings.Join(cmdArray, " "))
}
// set up execution
cmd := exec.Command("ssh", cmdArray...)
cmd.Stdout = rc.Stdout
cmd.Stderr = rc.Stderr
// cache the command running
rc.Cmd = cmd
return cmd.Start()
}
func (rc *RemoteCommand) Stop() error {
if rc.Cmd != nil && rc.Cmd.Process != nil {
evergreen.Logger.Logf(slogger.DEBUG, "RemoteCommand(%v) killing process %v", rc.Id, rc.Cmd.Process.Pid)
return rc.Cmd.Process.Kill()
}
evergreen.Logger.Logf(slogger.WARN, "RemoteCommand(%v) Trying to stop command but Cmd / Process was nil", rc.Id)
return nil
}