mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-29 04:31:44 +00:00
refactor process.Process to take files
This commit is contained in:
@ -5,21 +5,23 @@ package main
|
|||||||
// TODO: Nonrepudiable command log
|
// TODO: Nonrepudiable command log
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/tendermint/tendermint/wire"
|
|
||||||
. "github.com/tendermint/tendermint/cmd/barak/types"
|
. "github.com/tendermint/tendermint/cmd/barak/types"
|
||||||
. "github.com/tendermint/tendermint/common"
|
. "github.com/tendermint/tendermint/common"
|
||||||
cfg "github.com/tendermint/tendermint/config"
|
cfg "github.com/tendermint/tendermint/config"
|
||||||
pcm "github.com/tendermint/tendermint/process"
|
pcm "github.com/tendermint/tendermint/process"
|
||||||
"github.com/tendermint/tendermint/rpc/server"
|
"github.com/tendermint/tendermint/rpc/server"
|
||||||
|
"github.com/tendermint/tendermint/wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
const BarakVersion = "0.0.1"
|
const BarakVersion = "0.0.1"
|
||||||
@ -152,8 +154,13 @@ func StartProcess(wait bool, label string, execPath string, args []string, input
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Failed to create outputs dir: %v", err)
|
return nil, fmt.Errorf("Failed to create outputs dir: %v", err)
|
||||||
}
|
}
|
||||||
|
inFile := bytes.NewReader([]byte(input))
|
||||||
outPath := Fmt("%v/outputs/%v_%v.out", barak_.RootDir(), label, time.Now().Format("2006_01_02_15_04_05_MST"))
|
outPath := Fmt("%v/outputs/%v_%v.out", barak_.RootDir(), label, time.Now().Format("2006_01_02_15_04_05_MST"))
|
||||||
proc, err := pcm.Create(pcm.ProcessModeDaemon, label, execPath, args, input, outPath)
|
outFile, err := OpenAutoFile(outPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
proc, err := pcm.Create(label, execPath, args, inFile, outFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -161,7 +168,14 @@ func StartProcess(wait bool, label string, execPath string, args []string, input
|
|||||||
|
|
||||||
if wait {
|
if wait {
|
||||||
<-proc.WaitCh
|
<-proc.WaitCh
|
||||||
output := pcm.ReadOutput(proc)
|
|
||||||
|
// read output from outPath
|
||||||
|
outputBytes, err := ioutil.ReadFile(outPath)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Sprintf("ERROR READING OUTPUT: %v", err)
|
||||||
|
}
|
||||||
|
output := string(outputBytes)
|
||||||
|
|
||||||
// fmt.Println("Read output", output)
|
// fmt.Println("Read output", output)
|
||||||
if proc.ExitState == nil {
|
if proc.ExitState == nil {
|
||||||
return &ResponseStartProcess{
|
return &ResponseStartProcess{
|
||||||
@ -260,7 +274,7 @@ func ServeFileHandler(w http.ResponseWriter, req *http.Request) {
|
|||||||
http.Error(w, Fmt("Unknown process label: %v", path), 400)
|
http.Error(w, Fmt("Unknown process label: %v", path), 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
path = proc.OutputPath
|
path = proc.OutputFile.(*os.File).Name()
|
||||||
}
|
}
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -11,10 +11,10 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
acm "github.com/tendermint/tendermint/account"
|
acm "github.com/tendermint/tendermint/account"
|
||||||
"github.com/tendermint/tendermint/wire"
|
|
||||||
btypes "github.com/tendermint/tendermint/cmd/barak/types"
|
btypes "github.com/tendermint/tendermint/cmd/barak/types"
|
||||||
. "github.com/tendermint/tendermint/common"
|
. "github.com/tendermint/tendermint/common"
|
||||||
cfg "github.com/tendermint/tendermint/config"
|
cfg "github.com/tendermint/tendermint/config"
|
||||||
|
"github.com/tendermint/tendermint/wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
func remoteNick(remote string) string {
|
func remoteNick(remote string) string {
|
||||||
@ -268,7 +268,6 @@ func cliListProcesses(c *cli.Context) {
|
|||||||
endTimeStr := proc.EndTime.String()
|
endTimeStr := proc.EndTime.String()
|
||||||
fmt.Printf(", stopped at %v\n", Yellow(endTimeStr))
|
fmt.Printf(", stopped at %v\n", Yellow(endTimeStr))
|
||||||
}
|
}
|
||||||
fmt.Printf(" stdout/stderr goes to %v\n", proc.OutputPath)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}(remote)
|
}(remote)
|
||||||
|
@ -1,15 +1,11 @@
|
|||||||
package process
|
package process
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
. "github.com/tendermint/tendermint/common"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Process struct {
|
type Process struct {
|
||||||
@ -19,39 +15,20 @@ type Process struct {
|
|||||||
Pid int
|
Pid int
|
||||||
StartTime time.Time
|
StartTime time.Time
|
||||||
EndTime time.Time
|
EndTime time.Time
|
||||||
OutputPath string
|
|
||||||
Cmd *exec.Cmd `json:"-"`
|
Cmd *exec.Cmd `json:"-"`
|
||||||
ExitState *os.ProcessState `json:"-"`
|
ExitState *os.ProcessState `json:"-"`
|
||||||
OutputFile *AutoFile `json:"-"`
|
InputFile io.Reader `json:"-"`
|
||||||
|
OutputFile io.WriteCloser `json:"-"`
|
||||||
WaitCh chan struct{} `json:"-"`
|
WaitCh chan struct{} `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
ProcessModeStd = iota
|
|
||||||
ProcessModeDaemon
|
|
||||||
)
|
|
||||||
|
|
||||||
// execPath: command name
|
// execPath: command name
|
||||||
// args: args to command. (should not include name)
|
// args: args to command. (should not include name)
|
||||||
func Create(mode int, label string, execPath string, args []string, input string, outPath string) (*Process, error) {
|
func Create(label string, execPath string, args []string, inFile io.Reader, outFile io.WriteCloser) (*Process, error) {
|
||||||
outFile, err := OpenAutoFile(outPath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
cmd := exec.Command(execPath, args...)
|
cmd := exec.Command(execPath, args...)
|
||||||
switch mode {
|
cmd.Stdout = outFile
|
||||||
case ProcessModeStd:
|
cmd.Stderr = outFile
|
||||||
cmd.Stdout = io.MultiWriter(os.Stdout, outFile)
|
cmd.Stdin = inFile
|
||||||
cmd.Stderr = io.MultiWriter(os.Stderr, outFile)
|
|
||||||
cmd.Stdin = nil
|
|
||||||
case ProcessModeDaemon:
|
|
||||||
cmd.Stdout = outFile
|
|
||||||
cmd.Stderr = outFile
|
|
||||||
cmd.Stdin = nil
|
|
||||||
}
|
|
||||||
if input != "" {
|
|
||||||
cmd.Stdin = bytes.NewReader([]byte(input))
|
|
||||||
}
|
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -61,9 +38,9 @@ func Create(mode int, label string, execPath string, args []string, input string
|
|||||||
Args: args,
|
Args: args,
|
||||||
Pid: cmd.Process.Pid,
|
Pid: cmd.Process.Pid,
|
||||||
StartTime: time.Now(),
|
StartTime: time.Now(),
|
||||||
OutputPath: outPath,
|
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
ExitState: nil,
|
ExitState: nil,
|
||||||
|
InputFile: inFile,
|
||||||
OutputFile: outFile,
|
OutputFile: outFile,
|
||||||
WaitCh: make(chan struct{}),
|
WaitCh: make(chan struct{}),
|
||||||
}
|
}
|
||||||
@ -85,14 +62,6 @@ func Create(mode int, label string, execPath string, args []string, input string
|
|||||||
return proc, nil
|
return proc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadOutput(proc *Process) string {
|
|
||||||
output, err := ioutil.ReadFile(proc.OutputPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Sprintf("ERROR READING OUTPUT: %v", err)
|
|
||||||
}
|
|
||||||
return string(output)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Stop(proc *Process, kill bool) error {
|
func Stop(proc *Process, kill bool) error {
|
||||||
defer proc.OutputFile.Close()
|
defer proc.OutputFile.Close()
|
||||||
if kill {
|
if kill {
|
||||||
|
Reference in New Issue
Block a user