2019-06-05 03:24:16 +00:00
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BasicInteractiveShell struct {
|
|
|
|
item *Item
|
|
|
|
variables []string
|
|
|
|
stdoutCh chan string
|
|
|
|
stderrCh chan string
|
|
|
|
stdin io.WriteCloser
|
|
|
|
cmd *exec.Cmd
|
|
|
|
errCount int
|
2019-06-21 04:32:02 +00:00
|
|
|
timeout time.Duration
|
2019-06-05 03:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BasicInteractiveShell) init() error {
|
|
|
|
|
2019-06-23 04:08:24 +00:00
|
|
|
cmd := exec.Command("sh", "-c", s.item.initScripts[0])
|
2019-06-05 03:24:16 +00:00
|
|
|
enrichEnvVariables(cmd, s.variables)
|
2019-06-09 14:37:05 +00:00
|
|
|
cmd.Wait()
|
2019-06-05 03:24:16 +00:00
|
|
|
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stdoutScanner := bufio.NewScanner(stdout)
|
|
|
|
stderrScanner := bufio.NewScanner(stderr)
|
|
|
|
|
|
|
|
stdoutCh := make(chan string)
|
|
|
|
stderrCh := make(chan string)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for stdoutScanner.Scan() {
|
|
|
|
stdoutCh <- stdoutScanner.Text()
|
|
|
|
stderrCh <- stderrScanner.Text()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
s.stdoutCh = stdoutCh
|
|
|
|
s.stderrCh = stderrCh
|
|
|
|
s.stdin = stdin
|
|
|
|
s.cmd = cmd
|
|
|
|
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-23 04:08:24 +00:00
|
|
|
for i := 1; i < len(s.item.initScripts); i++ {
|
|
|
|
_, err := io.WriteString(s.stdin, fmt.Sprintf(" %s\n", s.item.initScripts[i]))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
time.Sleep(startupTimeout) // TODO wait until cmd complete
|
|
|
|
}
|
|
|
|
|
2019-06-05 03:24:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BasicInteractiveShell) execute() (string, error) {
|
|
|
|
|
2019-06-23 04:08:24 +00:00
|
|
|
_, err := io.WriteString(s.stdin, fmt.Sprintf(" %s\n", s.item.sampleScript))
|
2019-06-05 03:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
s.errCount++
|
|
|
|
if s.errCount > errorThreshold {
|
2019-06-09 14:37:05 +00:00
|
|
|
_ = s.cmd.Wait()
|
2019-06-06 03:07:34 +00:00
|
|
|
s.item.basicShell = nil // restart session
|
2019-06-05 03:24:16 +00:00
|
|
|
}
|
|
|
|
return "", errors.New(fmt.Sprintf("Failed to execute command: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout := make(chan bool, 1)
|
|
|
|
|
|
|
|
go func() {
|
2019-06-21 04:32:02 +00:00
|
|
|
time.Sleep(s.timeout)
|
2019-06-05 03:24:16 +00:00
|
|
|
timeout <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
var resultText strings.Builder
|
|
|
|
var errorText strings.Builder
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case stdout := <-s.stdoutCh:
|
|
|
|
if len(stdout) > 0 {
|
|
|
|
resultText.WriteString(stdout)
|
|
|
|
resultText.WriteString("\n")
|
|
|
|
}
|
|
|
|
case stderr := <-s.stderrCh:
|
|
|
|
if len(stderr) > 0 {
|
|
|
|
errorText.WriteString(stderr)
|
|
|
|
errorText.WriteString("\n")
|
|
|
|
}
|
|
|
|
case <-timeout:
|
|
|
|
if errorText.Len() > 0 {
|
|
|
|
return "", errors.New(errorText.String())
|
|
|
|
} else {
|
2019-06-23 00:18:06 +00:00
|
|
|
return s.item.transform(resultText.String())
|
2019-06-05 03:24:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|