2015-04-08 14:31:20 -07:00
|
|
|
package main
|
|
|
|
|
2015-04-14 01:14:55 -07:00
|
|
|
// A note on the origin of the name.
|
|
|
|
// http://en.wikipedia.org/wiki/Barak
|
2015-04-13 13:14:37 -07:00
|
|
|
// TODO: Nonrepudiable command log
|
|
|
|
|
2015-04-08 14:31:20 -07:00
|
|
|
import (
|
2015-04-13 13:14:37 -07:00
|
|
|
"errors"
|
2015-04-17 11:45:13 -07:00
|
|
|
"flag"
|
2015-04-08 14:31:20 -07:00
|
|
|
"fmt"
|
2015-04-10 02:12:17 -07:00
|
|
|
"io"
|
2015-04-08 14:31:20 -07:00
|
|
|
"io/ioutil"
|
2015-04-10 02:12:17 -07:00
|
|
|
"net/http"
|
2015-04-08 14:31:20 -07:00
|
|
|
"os"
|
2015-04-13 13:14:37 -07:00
|
|
|
"reflect"
|
2015-04-08 14:31:20 -07:00
|
|
|
"sync"
|
2015-04-19 15:45:06 -07:00
|
|
|
"time"
|
2015-04-08 14:31:20 -07:00
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/binary"
|
2015-04-14 04:14:18 -07:00
|
|
|
. "github.com/tendermint/tendermint/cmd/barak/types"
|
2015-04-08 14:31:20 -07:00
|
|
|
. "github.com/tendermint/tendermint/common"
|
2015-04-22 19:45:36 -07:00
|
|
|
"github.com/tendermint/tendermint/p2p"
|
2015-04-08 14:31:20 -07:00
|
|
|
pcm "github.com/tendermint/tendermint/process"
|
2015-04-10 02:12:17 -07:00
|
|
|
"github.com/tendermint/tendermint/rpc"
|
2015-04-08 14:31:20 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var Routes = map[string]*rpc.RPCFunc{
|
2015-04-15 14:49:21 -07:00
|
|
|
"status": rpc.NewRPCFunc(Status, []string{}),
|
|
|
|
"run": rpc.NewRPCFunc(Run, []string{"auth_command"}),
|
2015-04-13 13:14:37 -07:00
|
|
|
// NOTE: also, two special non-JSONRPC routes called "download" and "upload"
|
2015-04-08 14:31:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
Validators []Validator
|
|
|
|
ListenAddress string
|
2015-04-13 13:14:37 -07:00
|
|
|
StartNonce uint64
|
2015-04-22 19:45:36 -07:00
|
|
|
Registries []string
|
2015-04-08 14:31:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Global instance
|
2015-04-14 01:14:55 -07:00
|
|
|
var barak = struct {
|
2015-04-13 13:14:37 -07:00
|
|
|
mtx sync.Mutex
|
2015-04-16 14:10:20 -07:00
|
|
|
pid int
|
|
|
|
nonce uint64
|
2015-04-13 13:14:37 -07:00
|
|
|
processes map[string]*pcm.Process
|
|
|
|
validators []Validator
|
2015-04-19 15:45:06 -07:00
|
|
|
rootDir string
|
2015-04-22 19:45:36 -07:00
|
|
|
registries []string
|
2015-04-16 14:10:20 -07:00
|
|
|
}{
|
|
|
|
mtx: sync.Mutex{},
|
|
|
|
pid: os.Getpid(),
|
|
|
|
nonce: 0,
|
|
|
|
processes: make(map[string]*pcm.Process),
|
|
|
|
validators: nil,
|
2015-04-19 15:45:06 -07:00
|
|
|
rootDir: "",
|
2015-04-22 19:45:36 -07:00
|
|
|
registries: nil,
|
2015-04-16 14:10:20 -07:00
|
|
|
}
|
2015-04-08 14:31:20 -07:00
|
|
|
|
|
|
|
func main() {
|
2015-04-16 14:41:20 -07:00
|
|
|
fmt.Printf("New Barak Process (PID: %d)\n", os.Getpid())
|
2015-04-08 14:31:20 -07:00
|
|
|
|
2015-04-17 11:45:13 -07:00
|
|
|
// read flags to change options file.
|
|
|
|
var optionsBytes []byte
|
|
|
|
var optionsFile string
|
2015-04-08 14:31:20 -07:00
|
|
|
var err error
|
2015-04-17 11:45:13 -07:00
|
|
|
flag.StringVar(&optionsFile, "options-file", "", "Read options from file instead of stdin")
|
|
|
|
flag.Parse()
|
|
|
|
if optionsFile != "" {
|
|
|
|
optionsBytes, err = ioutil.ReadFile(optionsFile)
|
|
|
|
} else {
|
|
|
|
optionsBytes, err = ioutil.ReadAll(os.Stdin)
|
|
|
|
}
|
2015-04-08 14:31:20 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(Fmt("Error reading input: %v", err))
|
|
|
|
}
|
|
|
|
options := binary.ReadJSON(&Options{}, optionsBytes, &err).(*Options)
|
|
|
|
if err != nil {
|
|
|
|
panic(Fmt("Error parsing input: %v", err))
|
|
|
|
}
|
2015-04-14 01:14:55 -07:00
|
|
|
barak.nonce = options.StartNonce
|
|
|
|
barak.validators = options.Validators
|
2015-04-19 15:45:06 -07:00
|
|
|
barak.rootDir = os.Getenv("BRKROOT")
|
|
|
|
if barak.rootDir == "" {
|
|
|
|
barak.rootDir = os.Getenv("HOME") + "/.barak"
|
|
|
|
}
|
2015-04-20 18:51:20 -07:00
|
|
|
err = EnsureDir(barak.rootDir)
|
|
|
|
if err != nil {
|
|
|
|
panic(Fmt("Error creating barak rootDir: %v", err))
|
|
|
|
}
|
2015-04-08 14:31:20 -07:00
|
|
|
|
2015-04-19 16:45:10 -07:00
|
|
|
// Write pid to file.
|
2015-04-20 18:01:30 -07:00
|
|
|
err = AtomicWriteFile(barak.rootDir+"/pidfile", []byte(Fmt("%v", barak.pid)))
|
|
|
|
if err != nil {
|
|
|
|
panic(Fmt("Error writing pidfile: %v", err))
|
|
|
|
}
|
2015-04-19 16:45:10 -07:00
|
|
|
|
2015-04-08 14:31:20 -07:00
|
|
|
// Debug.
|
2015-04-13 13:14:37 -07:00
|
|
|
fmt.Printf("Options: %v\n", options)
|
2015-04-14 01:14:55 -07:00
|
|
|
fmt.Printf("Barak: %v\n", barak)
|
2015-04-08 14:31:20 -07:00
|
|
|
|
2015-04-13 13:14:37 -07:00
|
|
|
// Start rpc server.
|
2015-04-22 19:45:36 -07:00
|
|
|
listener := p2p.NewDefaultListener("tcp", options.ListenAddress, false)
|
2015-04-10 02:12:17 -07:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/download", ServeFile)
|
2015-04-22 19:45:36 -07:00
|
|
|
mux.HandleFunc("/register", Register)
|
2015-04-10 02:12:17 -07:00
|
|
|
// TODO: mux.HandleFunc("/upload", UploadFile)
|
|
|
|
rpc.RegisterRPCFuncs(mux, Routes)
|
2015-04-22 19:45:36 -07:00
|
|
|
rpc.StartHTTPServer(listener, mux)
|
|
|
|
|
|
|
|
// Register this barak with central listener
|
|
|
|
extAddress := listener.ExternalAddress().String()
|
|
|
|
for _, registry := range barak.registries {
|
|
|
|
go func(registry string) {
|
|
|
|
var response ResponseRegister
|
|
|
|
_, err = rpc.Call(registry, "register", Arr(extAddress), &response)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error registering to registry %v:\n %v\n", registry, err)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Successfully registered with registry %v\n", registry)
|
|
|
|
}
|
|
|
|
}(registry)
|
|
|
|
}
|
2015-04-08 14:31:20 -07:00
|
|
|
|
|
|
|
TrapSignal(func() {
|
2015-04-14 01:14:55 -07:00
|
|
|
fmt.Println("Barak shutting down")
|
2015-04-08 14:31:20 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2015-04-15 14:49:21 -07:00
|
|
|
// RPC functions
|
|
|
|
|
|
|
|
func Status() (*ResponseStatus, error) {
|
|
|
|
barak.mtx.Lock()
|
2015-04-16 14:10:20 -07:00
|
|
|
pid := barak.pid
|
2015-04-15 14:49:21 -07:00
|
|
|
nonce := barak.nonce
|
|
|
|
validators := barak.validators
|
|
|
|
barak.mtx.Unlock()
|
|
|
|
|
|
|
|
return &ResponseStatus{
|
2015-04-16 14:10:20 -07:00
|
|
|
Pid: pid,
|
2015-04-15 14:49:21 -07:00
|
|
|
Nonce: nonce,
|
|
|
|
Validators: validators,
|
|
|
|
}, nil
|
|
|
|
}
|
2015-04-13 13:14:37 -07:00
|
|
|
|
|
|
|
func Run(authCommand AuthCommand) (interface{}, error) {
|
|
|
|
command, err := parseValidateCommand(authCommand)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-19 15:51:43 -07:00
|
|
|
log.Info(Fmt("Run() received command %v:\n%v", reflect.TypeOf(command), command))
|
2015-04-13 13:14:37 -07:00
|
|
|
// Issue command
|
|
|
|
switch c := command.(type) {
|
|
|
|
case CommandRunProcess:
|
|
|
|
return RunProcess(c.Wait, c.Label, c.ExecPath, c.Args, c.Input)
|
|
|
|
case CommandStopProcess:
|
|
|
|
return StopProcess(c.Label, c.Kill)
|
|
|
|
case CommandListProcesses:
|
|
|
|
return ListProcesses()
|
|
|
|
default:
|
|
|
|
return nil, errors.New("Invalid endpoint for command")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseValidateCommandStr(authCommandStr string) (Command, error) {
|
|
|
|
var err error
|
|
|
|
authCommand := binary.ReadJSON(AuthCommand{}, []byte(authCommandStr), &err).(AuthCommand)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to parse auth_command")
|
|
|
|
return nil, errors.New("AuthCommand parse error")
|
|
|
|
}
|
|
|
|
return parseValidateCommand(authCommand)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseValidateCommand(authCommand AuthCommand) (Command, error) {
|
|
|
|
commandJSONStr := authCommand.CommandJSONStr
|
|
|
|
signatures := authCommand.Signatures
|
|
|
|
// Validate commandJSONStr
|
2015-04-14 01:14:55 -07:00
|
|
|
if !validate([]byte(commandJSONStr), barak.validators, signatures) {
|
2015-04-13 13:14:37 -07:00
|
|
|
fmt.Printf("Failed validation attempt")
|
|
|
|
return nil, errors.New("Validation error")
|
|
|
|
}
|
|
|
|
// Parse command
|
|
|
|
var err error
|
|
|
|
command := binary.ReadJSON(NoncedCommand{}, []byte(commandJSONStr), &err).(NoncedCommand)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to parse command")
|
|
|
|
return nil, errors.New("Command parse error")
|
|
|
|
}
|
|
|
|
// Prevent replays
|
2015-04-14 01:14:55 -07:00
|
|
|
if barak.nonce+1 != command.Nonce {
|
2015-04-13 13:14:37 -07:00
|
|
|
return nil, errors.New("Replay error")
|
|
|
|
} else {
|
2015-04-14 01:14:55 -07:00
|
|
|
barak.nonce += 1
|
2015-04-13 13:14:37 -07:00
|
|
|
}
|
|
|
|
return command.Command, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// RPC base commands
|
|
|
|
// WARNING Not validated, do not export to routes.
|
|
|
|
|
2015-04-10 02:12:17 -07:00
|
|
|
func RunProcess(wait bool, label string, execPath string, args []string, input string) (*ResponseRunProcess, error) {
|
2015-04-14 01:14:55 -07:00
|
|
|
barak.mtx.Lock()
|
2015-04-08 14:31:20 -07:00
|
|
|
|
|
|
|
// First, see if there already is a process labeled 'label'
|
2015-04-14 01:14:55 -07:00
|
|
|
existing := barak.processes[label]
|
2015-04-16 10:13:50 -07:00
|
|
|
if existing != nil && existing.EndTime.IsZero() {
|
2015-04-14 01:14:55 -07:00
|
|
|
barak.mtx.Unlock()
|
2015-04-15 21:49:14 -07:00
|
|
|
return nil, fmt.Errorf("Process already exists: %v", label)
|
2015-04-08 14:31:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, create one.
|
2015-04-19 15:45:06 -07:00
|
|
|
err := EnsureDir(barak.rootDir + "/outputs")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to create outputs dir: %v", err)
|
|
|
|
}
|
|
|
|
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)
|
2015-04-16 09:46:35 -07:00
|
|
|
if err == nil {
|
|
|
|
barak.processes[label] = proc
|
|
|
|
}
|
2015-04-14 01:14:55 -07:00
|
|
|
barak.mtx.Unlock()
|
2015-04-16 09:46:35 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-08 14:31:20 -07:00
|
|
|
|
|
|
|
if wait {
|
2015-04-16 10:54:07 -07:00
|
|
|
<-proc.WaitCh
|
2015-04-17 10:27:17 -07:00
|
|
|
output := pcm.ReadOutput(proc)
|
2015-04-17 11:11:13 -07:00
|
|
|
fmt.Println("Read output", output)
|
2015-04-16 10:54:07 -07:00
|
|
|
if proc.ExitState == nil {
|
|
|
|
return &ResponseRunProcess{
|
|
|
|
Success: true,
|
2015-04-17 10:27:17 -07:00
|
|
|
Output: output,
|
2015-04-16 10:54:07 -07:00
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
return &ResponseRunProcess{
|
|
|
|
Success: proc.ExitState.Success(), // Would be always false?
|
2015-04-17 10:27:17 -07:00
|
|
|
Output: output,
|
2015-04-16 10:54:07 -07:00
|
|
|
}, nil
|
|
|
|
}
|
2015-04-08 14:31:20 -07:00
|
|
|
} else {
|
2015-04-17 10:27:17 -07:00
|
|
|
return &ResponseRunProcess{
|
|
|
|
Success: true,
|
|
|
|
Output: "",
|
|
|
|
}, nil
|
2015-04-08 14:31:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func StopProcess(label string, kill bool) (*ResponseStopProcess, error) {
|
2015-04-14 01:14:55 -07:00
|
|
|
barak.mtx.Lock()
|
|
|
|
proc := barak.processes[label]
|
|
|
|
barak.mtx.Unlock()
|
2015-04-08 14:31:20 -07:00
|
|
|
|
|
|
|
if proc == nil {
|
2015-04-15 21:49:14 -07:00
|
|
|
return nil, fmt.Errorf("Process does not exist: %v", label)
|
2015-04-08 14:31:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
err := pcm.Stop(proc, kill)
|
|
|
|
return &ResponseStopProcess{}, err
|
|
|
|
}
|
2015-04-10 02:12:17 -07:00
|
|
|
|
2015-04-13 13:14:37 -07:00
|
|
|
func ListProcesses() (*ResponseListProcesses, error) {
|
|
|
|
var procs = []*pcm.Process{}
|
2015-04-14 01:14:55 -07:00
|
|
|
barak.mtx.Lock()
|
2015-04-16 09:46:35 -07:00
|
|
|
fmt.Println("Processes: %v", barak.processes)
|
2015-04-14 01:14:55 -07:00
|
|
|
for _, proc := range barak.processes {
|
2015-04-13 13:14:37 -07:00
|
|
|
procs = append(procs, proc)
|
|
|
|
}
|
2015-04-14 01:14:55 -07:00
|
|
|
barak.mtx.Unlock()
|
2015-04-13 13:14:37 -07:00
|
|
|
|
|
|
|
return &ResponseListProcesses{
|
|
|
|
Processes: procs,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-04-22 19:45:36 -07:00
|
|
|
// Another barak instance registering its external
|
|
|
|
// address to a remote barak.
|
|
|
|
func Register(w http.ResponseWriter, req *http.Request) {
|
|
|
|
registry, err := os.OpenFile(barak.rootDir+"/registry.log", os.O_RDWR|os.O_APPEND, 0x600)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Could not open registry file. Please contact the administrator", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO: Also check the X-FORWARDED-FOR or whatever it's called.
|
|
|
|
registry.Write([]byte(Fmt("++ %v\n", req.RemoteAddr)))
|
|
|
|
registry.Close()
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(200)
|
|
|
|
w.Write([]byte("Noted!"))
|
|
|
|
}
|
2015-04-13 13:14:37 -07:00
|
|
|
|
2015-04-22 19:45:36 -07:00
|
|
|
func ServeFile(w http.ResponseWriter, req *http.Request) {
|
2015-04-13 13:14:37 -07:00
|
|
|
authCommandStr := req.FormValue("auth_command")
|
|
|
|
command, err := parseValidateCommandStr(authCommandStr)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, Fmt("Invalid command: %v", err), 400)
|
|
|
|
}
|
|
|
|
serveCommand, ok := command.(CommandServeFile)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, "Invalid command", 400)
|
|
|
|
}
|
|
|
|
path := serveCommand.Path
|
2015-04-10 02:12:17 -07:00
|
|
|
if path == "" {
|
|
|
|
http.Error(w, "Must specify path", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, Fmt("Error opening file: %v. %v", path, err), 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = io.Copy(w, file)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, Fmt("Error serving file: %v. %v", path, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|