github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/hostutil/host_remote.go (about) 1 package hostutil 2 3 import ( 4 "bytes" 5 "time" 6 7 "github.com/evergreen-ci/evergreen/command" 8 "github.com/evergreen-ci/evergreen/model/host" 9 "github.com/evergreen-ci/evergreen/util" 10 ) 11 12 const SSHTimeout = time.Minute * 10 13 14 // RunRemoteScript executes a shell script that already exists on the remote host, 15 // returning logs and any errors that occur. Logs may still be returned for some errors. 16 func RunRemoteScript(h *host.Host, script string, sshOptions []string) (string, error) { 17 // parse the hostname into the user, host and port 18 hostInfo, err := util.ParseSSHInfo(h.Host) 19 if err != nil { 20 return "", err 21 } 22 user := h.Distro.User 23 if hostInfo.User != "" { 24 user = hostInfo.User 25 } 26 27 // run the remote script as sudo, if appropriate 28 sudoStr := "" 29 if h.Distro.SetupAsSudo { 30 sudoStr = "sudo " 31 } 32 // run command to ssh into remote machine and execute script 33 sshCmdStd := &util.CappedWriter{ 34 Buffer: &bytes.Buffer{}, 35 MaxBytes: 1024 * 1024, // 1MB 36 } 37 cmd := &command.RemoteCommand{ 38 CmdString: sudoStr + "sh " + script, 39 Stdout: sshCmdStd, 40 Stderr: sshCmdStd, 41 RemoteHostName: hostInfo.Hostname, 42 User: user, 43 Options: []string{"-p", hostInfo.Port}, 44 Background: false, 45 } 46 // force creation of a tty if sudo 47 if h.Distro.SetupAsSudo { 48 cmd.Options = []string{"-t", "-t", "-p", hostInfo.Port} 49 } 50 cmd.Options = append(cmd.Options, sshOptions...) 51 52 // run the ssh command with given timeout 53 err = util.RunFunctionWithTimeout( 54 cmd.Run, 55 SSHTimeout, 56 ) 57 return sshCmdStd.String(), err 58 }