2019-04-07 23:15:01 -05:00
|
|
|
package shell
|
|
|
|
|
2019-06-04 22:20:15 -05:00
|
|
|
import "fmt"
|
2019-04-07 23:15:01 -05:00
|
|
|
import "strings"
|
|
|
|
import "time"
|
|
|
|
import "os"
|
|
|
|
import "os/exec"
|
|
|
|
import "bufio"
|
2019-06-04 22:20:15 -05:00
|
|
|
import "bytes"
|
|
|
|
import "io"
|
2019-06-05 03:28:15 -05:00
|
|
|
// import "io/ioutil"
|
2019-06-04 22:20:15 -05:00
|
|
|
|
2019-04-07 23:15:01 -05:00
|
|
|
import "github.com/davecgh/go-spew/spew"
|
|
|
|
import "github.com/svent/go-nbreader"
|
|
|
|
|
2019-05-09 19:24:25 -05:00
|
|
|
import log "github.com/sirupsen/logrus"
|
|
|
|
// import "github.com/wercker/journalhook"
|
|
|
|
|
2019-06-06 11:34:47 -05:00
|
|
|
var callback func(interface{}, int)
|
|
|
|
|
2019-05-09 17:20:26 -05:00
|
|
|
var shellStdout *os.File
|
|
|
|
var shellStderr *os.File
|
|
|
|
|
2019-05-09 19:24:25 -05:00
|
|
|
var spewOn bool = false
|
2019-05-09 20:54:36 -05:00
|
|
|
var msecDelay int = 20 // number of milliseconds to delay between reads with no data
|
2019-05-09 19:24:25 -05:00
|
|
|
|
2019-06-05 03:28:15 -05:00
|
|
|
var bytesBuffer bytes.Buffer
|
|
|
|
var bytesSplice []byte
|
2019-06-04 22:20:15 -05:00
|
|
|
|
2019-06-06 18:12:59 -05:00
|
|
|
func handleError(c interface{}, ret int) {
|
2019-06-06 11:34:47 -05:00
|
|
|
log.Println("shell.Run() Returned", ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2019-06-06 18:12:59 -05:00
|
|
|
callback = handleError
|
2019-06-06 11:34:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func InitCallback(f func(interface{}, int)) {
|
|
|
|
callback = f
|
|
|
|
}
|
|
|
|
|
2019-04-07 23:15:01 -05:00
|
|
|
func Script(cmds string) int {
|
|
|
|
// split on new lines (while we are at it, handle stupid windows text files
|
|
|
|
lines := strings.Split(strings.Replace(cmds, "\r\n", "\n", -1), "\n")
|
|
|
|
|
|
|
|
for _, line := range lines {
|
2019-06-06 17:59:49 -05:00
|
|
|
line = Chomp(line) // this is like 'chomp' in perl
|
2019-05-09 19:24:25 -05:00
|
|
|
log.Println("LINE:", line)
|
2019-04-07 23:15:01 -05:00
|
|
|
time.Sleep(1)
|
2019-04-07 23:16:56 -05:00
|
|
|
Run(line)
|
2019-04-07 23:15:01 -05:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-05-09 19:24:25 -05:00
|
|
|
/*
|
|
|
|
func UseJournalctl() {
|
|
|
|
journalhook.Enable()
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
func SpewOn() {
|
|
|
|
spewOn = true
|
|
|
|
}
|
|
|
|
|
2019-05-09 20:54:36 -05:00
|
|
|
func SetDelayInMsec(msecs int) {
|
|
|
|
msecDelay = msecs
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:20:26 -05:00
|
|
|
func SetStdout(newout *os.File) {
|
|
|
|
shellStdout = newout
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetStderr(newerr *os.File) {
|
|
|
|
shellStderr = newerr
|
|
|
|
}
|
|
|
|
|
2019-06-06 11:34:47 -05:00
|
|
|
// NOTE: this might cause problems:
|
|
|
|
// always remove the newlines at the end ?
|
2019-06-06 15:02:16 -05:00
|
|
|
func Run(cmdline string) string {
|
2019-06-05 03:28:15 -05:00
|
|
|
log.Println("shell.Run() START " + cmdline)
|
2019-04-07 23:15:01 -05:00
|
|
|
|
2019-06-06 17:59:49 -05:00
|
|
|
cmd := Chomp(cmdline) // this is like 'chomp' in perl
|
2019-04-07 23:15:01 -05:00
|
|
|
cmdArgs := strings.Fields(cmd)
|
|
|
|
if (len(cmdArgs) == 0) {
|
2019-06-06 18:12:59 -05:00
|
|
|
handleError(fmt.Errorf("cmdline == ''"), 0)
|
2019-06-05 03:28:15 -05:00
|
|
|
log.Debug("END ", cmd)
|
2019-06-06 15:02:16 -05:00
|
|
|
return "" // nothing to do
|
2019-04-07 23:15:01 -05:00
|
|
|
}
|
|
|
|
if (cmdArgs[0] == "cd") {
|
|
|
|
if (len(cmdArgs) > 1) {
|
|
|
|
log.Println("os.Chdir()", cmd)
|
|
|
|
os.Chdir(cmdArgs[1])
|
|
|
|
}
|
2019-06-06 18:12:59 -05:00
|
|
|
handleError(nil, 0)
|
2019-06-05 03:28:15 -05:00
|
|
|
log.Debug("END ", cmd)
|
2019-06-06 15:02:16 -05:00
|
|
|
return "" // nothing to do
|
2019-04-07 23:15:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
process := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...)
|
2019-05-09 17:20:26 -05:00
|
|
|
pstdout, _ := process.StdoutPipe()
|
|
|
|
pstderr, _ := process.StderrPipe()
|
|
|
|
|
2019-05-09 19:24:25 -05:00
|
|
|
if (spewOn) {
|
|
|
|
spew.Dump(pstdout)
|
|
|
|
}
|
2019-05-09 17:20:26 -05:00
|
|
|
|
2019-04-07 23:15:01 -05:00
|
|
|
process.Start()
|
|
|
|
|
2019-05-09 17:20:26 -05:00
|
|
|
if (shellStdout == nil) {
|
|
|
|
shellStdout = os.Stdout
|
|
|
|
}
|
|
|
|
|
|
|
|
f := bufio.NewWriter(shellStdout)
|
2019-04-07 23:15:01 -05:00
|
|
|
|
2019-05-09 17:20:26 -05:00
|
|
|
newreader := bufio.NewReader(pstdout)
|
2019-04-07 23:15:01 -05:00
|
|
|
nbr := nbreader.NewNBReader(newreader, 1024)
|
|
|
|
|
2019-05-09 20:54:36 -05:00
|
|
|
tmp := bufio.NewReader(pstderr)
|
|
|
|
go NonBlockingReader(tmp, shellStderr)
|
2019-04-07 23:15:01 -05:00
|
|
|
|
2019-05-09 18:48:53 -05:00
|
|
|
totalCount := 0
|
|
|
|
|
2019-05-09 20:54:36 -05:00
|
|
|
var dead bool = false
|
|
|
|
for (dead == false) {
|
|
|
|
time.Sleep(time.Duration(msecDelay) * time.Millisecond) // only check the buffer 500 times a second
|
2019-04-07 23:15:01 -05:00
|
|
|
// log.Println("sleep done")
|
|
|
|
|
2019-05-09 20:54:36 -05:00
|
|
|
var empty bool = false
|
2019-05-09 18:48:53 -05:00
|
|
|
// tight loop that reads 1K at a time until buffer is empty
|
2019-05-09 20:54:36 -05:00
|
|
|
for (empty == false) {
|
2019-05-09 18:48:53 -05:00
|
|
|
oneByte := make([]byte, 1024)
|
|
|
|
count, err := nbr.Read(oneByte)
|
|
|
|
totalCount += count
|
|
|
|
|
|
|
|
if (err != nil) {
|
2019-06-05 03:28:15 -05:00
|
|
|
log.Debug("Read() count = ", count, "err = ", err)
|
2019-05-09 18:48:53 -05:00
|
|
|
oneByte = make([]byte, 1024)
|
|
|
|
count, err = nbr.Read(oneByte)
|
2019-06-05 03:28:15 -05:00
|
|
|
log.Debug("STDOUT: count = ", count)
|
2019-05-09 21:46:27 -05:00
|
|
|
f.Write(oneByte[0:count])
|
2019-05-09 18:48:53 -05:00
|
|
|
f.Flush()
|
2019-05-09 20:54:36 -05:00
|
|
|
empty = true
|
|
|
|
dead = true
|
2019-05-09 18:48:53 -05:00
|
|
|
}
|
2019-05-09 21:46:27 -05:00
|
|
|
// f.Write([]byte(string(oneByte)))
|
2019-05-09 18:48:53 -05:00
|
|
|
if (count == 0) {
|
2019-05-09 20:54:36 -05:00
|
|
|
empty = true
|
2019-05-09 21:46:27 -05:00
|
|
|
} else {
|
2019-06-05 03:28:15 -05:00
|
|
|
log.Debug("STDOUT: count = ", count)
|
|
|
|
io.WriteString(&bytesBuffer, string(oneByte))
|
2019-05-09 21:46:27 -05:00
|
|
|
f.Write(oneByte[0:count])
|
|
|
|
f.Flush()
|
2019-05-09 18:48:53 -05:00
|
|
|
}
|
2019-04-07 23:15:01 -05:00
|
|
|
}
|
|
|
|
|
2019-05-09 18:48:53 -05:00
|
|
|
if (totalCount != 0) {
|
2019-06-05 03:28:15 -05:00
|
|
|
log.Debug("STDOUT: totalCount = ", totalCount)
|
2019-05-09 20:54:36 -05:00
|
|
|
totalCount = 0
|
2019-04-07 23:15:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := process.Wait()
|
|
|
|
|
|
|
|
if err != nil {
|
2019-05-09 19:24:25 -05:00
|
|
|
if (spewOn) {
|
2019-06-05 03:28:15 -05:00
|
|
|
// this panics: spew.Dump(err.(*exec.ExitError))
|
2019-05-09 19:24:25 -05:00
|
|
|
spew.Dump(process.ProcessState)
|
|
|
|
}
|
2019-06-05 03:28:15 -05:00
|
|
|
// stuff := err.(*exec.ExitError)
|
|
|
|
log.Debug("ERROR ", err.Error())
|
|
|
|
log.Debug("END ", cmdline)
|
2019-06-06 15:02:16 -05:00
|
|
|
// return -1, "", fmt.Errorf(err.Error())
|
2019-06-06 18:12:59 -05:00
|
|
|
// handleError(fmt.Errorf(err.Error()), -1)
|
|
|
|
handleError(err, -1)
|
2019-06-06 15:02:16 -05:00
|
|
|
return ""
|
2019-04-07 23:15:01 -05:00
|
|
|
}
|
2019-06-05 03:28:15 -05:00
|
|
|
|
|
|
|
// log.Println("shell.Run() END buf =", bytesBuffer)
|
|
|
|
// convert this to a byte array and then trip NULLs
|
|
|
|
// WTF this copies nulls with b.String() is fucking insanly stupid
|
|
|
|
byteSlice := bytesBuffer.Bytes()
|
|
|
|
b := bytes.Trim(byteSlice, "\x00")
|
|
|
|
|
|
|
|
log.Debug("shell.Run() END b =", b)
|
|
|
|
|
|
|
|
// reset the bytesBuffer
|
|
|
|
bytesBuffer.Reset()
|
2019-06-06 11:34:47 -05:00
|
|
|
|
|
|
|
// NOTE: this might cause problems:
|
|
|
|
// this removes the newlines at the end
|
|
|
|
tmp2 := string(b)
|
|
|
|
tmp2 = strings.TrimSuffix(tmp2, "\n")
|
2019-06-04 22:20:15 -05:00
|
|
|
log.Println("shell.Run() END ", cmdline)
|
2019-06-06 18:12:59 -05:00
|
|
|
log.Println("shell.Run() calling handleError() :")
|
|
|
|
handleError(nil, 0)
|
2019-06-06 17:59:49 -05:00
|
|
|
return Chomp(b)
|
2019-04-07 23:15:01 -05:00
|
|
|
}
|
2019-04-08 18:43:49 -05:00
|
|
|
|
|
|
|
func Daemon(cmdline string, timeout time.Duration) int {
|
|
|
|
for {
|
|
|
|
Run(cmdline)
|
|
|
|
time.Sleep(timeout)
|
|
|
|
}
|
|
|
|
}
|
2019-05-09 20:54:36 -05:00
|
|
|
|
|
|
|
// pass in two file handles (1 read, 1 write)
|
|
|
|
func NonBlockingReader(buffReader *bufio.Reader, writeFileHandle *os.File) {
|
|
|
|
// newreader := bufio.NewReader(readFileHandle)
|
|
|
|
|
|
|
|
// create a nonblocking GO reader
|
|
|
|
nbr := nbreader.NewNBReader(buffReader, 1024)
|
|
|
|
|
|
|
|
for {
|
|
|
|
// defer buffReader.Close()
|
|
|
|
// defer writeFileHandle.Flush()
|
|
|
|
defer writeFileHandle.Close()
|
|
|
|
totalCount := 0
|
|
|
|
for {
|
|
|
|
oneByte := make([]byte, 1024)
|
|
|
|
count, err := nbr.Read(oneByte)
|
|
|
|
if (err != nil) {
|
2019-06-05 03:28:15 -05:00
|
|
|
log.Debug("count, err =", count, err)
|
2019-06-06 18:12:59 -05:00
|
|
|
handleError(err, -1)
|
2019-05-09 20:54:36 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
totalCount += count
|
|
|
|
if (count == 0) {
|
|
|
|
time.Sleep(time.Duration(msecDelay) * time.Millisecond) // without this delay this will peg the CPU
|
|
|
|
if (totalCount != 0) {
|
2019-06-05 03:28:15 -05:00
|
|
|
log.Debug("STDERR: totalCount = ", totalCount)
|
2019-05-09 20:54:36 -05:00
|
|
|
totalCount = 0
|
|
|
|
}
|
2019-05-09 21:46:27 -05:00
|
|
|
} else {
|
2019-06-05 03:28:15 -05:00
|
|
|
log.Debug("STDERR: count = ", count)
|
2019-05-09 21:46:27 -05:00
|
|
|
writeFileHandle.Write(oneByte[0:count])
|
2019-05-09 20:54:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|