tendermint/cmd/debora/main.go

218 lines
5.1 KiB
Go
Raw Normal View History

2015-04-14 04:14:18 -07:00
package main
import (
"fmt"
2015-04-15 20:22:03 -07:00
"github.com/codegangsta/cli"
"io/ioutil"
"os"
acm "github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
2015-04-15 23:41:13 -07:00
btypes "github.com/tendermint/tendermint/cmd/barak/types"
2015-04-15 20:22:03 -07:00
. "github.com/tendermint/tendermint/common"
2015-04-14 04:14:18 -07:00
)
2015-04-16 16:35:51 -07:00
var Config = struct {
Remotes []string
PrivKey acm.PrivKey
}{}
2015-04-14 04:14:18 -07:00
func main() {
2015-04-15 20:22:03 -07:00
fmt.Printf("New Debora Process (PID: %d)\n", os.Getpid())
rootDir := os.Getenv("DEBROOT")
if rootDir == "" {
rootDir = os.Getenv("HOME") + "/.debora"
}
var (
configFlag = cli.StringFlag{
Name: "config-file",
Value: rootDir + "/config.json",
Usage: "config file",
}
waitFlag = cli.BoolFlag{
Name: "wait",
Usage: "whether to wait for termination",
}
inputFlag = cli.StringFlag{
Name: "input",
Value: "",
Usage: "input to the program (e.g. stdin)",
}
)
2015-04-15 20:22:03 -07:00
app := cli.NewApp()
app.Name = "debora"
app.Usage = "summons commands to barak"
app.Version = "0.0.1"
app.Email = "ethan@erisindustries.com,jae@tendermint.com"
2015-04-15 23:41:13 -07:00
app.Flags = []cli.Flag{
2015-04-16 16:35:51 -07:00
configFlag,
2015-04-15 23:41:13 -07:00
}
app.Before = func(c *cli.Context) error {
2015-04-16 16:35:51 -07:00
ReadConfig(c.String("config-file"))
2015-04-15 23:41:13 -07:00
return nil
}
2015-04-15 20:22:03 -07:00
app.Commands = []cli.Command{
cli.Command{
Name: "status",
Usage: "shows remote status",
Action: cliGetStatus,
},
2015-04-15 23:41:13 -07:00
cli.Command{
Name: "run",
Usage: "run process",
Action: cliRunProcess,
2015-04-16 09:46:35 -07:00
Flags: []cli.Flag{
waitFlag,
inputFlag,
2015-04-15 23:41:13 -07:00
},
},
cli.Command{
Name: "stop",
Usage: "stop process",
Action: cliStopProcess,
},
2015-04-15 20:22:03 -07:00
cli.Command{
Name: "list",
Usage: "list processes",
Action: cliListProcesses,
},
2015-04-20 14:47:59 -07:00
cli.Command{
Name: "download",
Usage: "download file <remote-path> <local-path-prefix>",
Action: cliDownloadFile,
},
2015-04-15 20:22:03 -07:00
}
app.Run(os.Args)
}
2015-04-16 16:35:51 -07:00
func ReadConfig(configFilePath string) {
configJSONBytes, err := ioutil.ReadFile(configFilePath)
2015-04-15 20:22:03 -07:00
if err != nil {
2015-04-16 16:35:51 -07:00
Exit(Fmt("Failed to read config file %v. %v\n", configFilePath, err))
2015-04-15 20:22:03 -07:00
}
2015-04-16 16:35:51 -07:00
binary.ReadJSON(&Config, configJSONBytes, &err)
2015-04-15 20:22:03 -07:00
if err != nil {
2015-04-16 16:35:51 -07:00
Exit(Fmt("Failed to parse config. %v", err))
2015-04-15 20:22:03 -07:00
}
}
func cliGetStatus(c *cli.Context) {
args := c.Args()
if len(args) != 0 {
fmt.Println("BTW, status takes no arguments.")
}
2015-04-16 16:35:51 -07:00
for _, remote := range Config.Remotes {
response, err := GetStatus(remote)
if err != nil {
fmt.Printf("%v failure. %v\n", remote, err)
} else {
fmt.Printf("%v success. %v\n", remote, response)
}
}
}
2015-04-15 23:41:13 -07:00
func cliRunProcess(c *cli.Context) {
2015-04-16 09:46:35 -07:00
args := c.Args()
if len(args) < 2 {
Exit("Must specify <label> <execPath> <args...>")
}
label := args[0]
execPath := args[1]
args = args[2:]
command := btypes.CommandRunProcess{
Wait: c.Bool("wait"),
Label: label,
ExecPath: execPath,
Args: args,
Input: c.String("input"),
}
2015-04-16 16:35:51 -07:00
for _, remote := range Config.Remotes {
response, err := RunProcess(Config.PrivKey, remote, command)
2015-04-15 23:41:13 -07:00
if err != nil {
fmt.Printf("%v failure. %v\n", remote, err)
} else {
2015-04-17 11:17:45 -07:00
fmt.Printf("%v success.\n", remote)
if response.Output != "" {
2015-04-17 11:17:45 -07:00
fmt.Println("--------------------------------------------------------------------------------")
fmt.Println(response.Output)
2015-04-17 11:17:45 -07:00
fmt.Println("--------------------------------------------------------------------------------")
} else {
fmt.Println("(no output)")
}
2015-04-15 23:41:13 -07:00
}
}
}
func cliStopProcess(c *cli.Context) {
2015-04-16 09:46:35 -07:00
args := c.Args()
if len(args) == 0 {
Exit("Must specify label to stop")
}
label := args[0]
command := btypes.CommandStopProcess{
Label: label,
2015-04-16 18:21:19 -07:00
Kill: true,
2015-04-16 09:46:35 -07:00
}
2015-04-16 16:35:51 -07:00
for _, remote := range Config.Remotes {
response, err := StopProcess(Config.PrivKey, remote, command)
2015-04-15 23:41:13 -07:00
if err != nil {
fmt.Printf("%v failure. %v\n", remote, err)
} else {
fmt.Printf("%v success. %v\n", remote, response)
}
}
}
2015-04-15 20:22:03 -07:00
func cliListProcesses(c *cli.Context) {
/*
args := c.Args()
if len(args) == 0 {
log.Fatal("Must specify application name")
}
app := args[0]
*/
2015-04-15 23:41:13 -07:00
command := btypes.CommandListProcesses{}
2015-04-16 16:35:51 -07:00
for _, remote := range Config.Remotes {
response, err := ListProcesses(Config.PrivKey, remote, command)
2015-04-15 20:22:03 -07:00
if err != nil {
2015-04-15 23:41:13 -07:00
fmt.Printf("%v failure. %v\n", remote, err)
2015-04-15 20:22:03 -07:00
} else {
2015-04-16 09:46:35 -07:00
fmt.Printf("%v processes:\n", remote)
for _, proc := range response.Processes {
2015-04-19 16:20:00 -07:00
startTimeStr := Green(proc.StartTime.String())
endTimeStr := proc.EndTime.String()
if !proc.EndTime.IsZero() {
endTimeStr = Red(endTimeStr)
}
fmt.Printf(" %v start:%v end:%v output:%v\n",
RightPadString(Fmt("\"%v\" => `%v` (%v)", proc.Label, proc.ExecPath, proc.Pid), 40),
startTimeStr, endTimeStr, proc.OutputPath)
2015-04-16 09:46:35 -07:00
}
2015-04-15 20:22:03 -07:00
}
}
2015-04-14 04:14:18 -07:00
}
2015-04-20 14:47:59 -07:00
func cliDownloadFile(c *cli.Context) {
args := c.Args()
if len(args) != 2 {
Exit("Must specify <remote-path> <local-path-prefix>")
}
remotePath := args[0]
localPathPrefix := args[1]
command := btypes.CommandServeFile{
Path: remotePath,
}
for i, remote := range Config.Remotes {
localPath := Fmt("%v_%v", localPathPrefix, i)
n, err := DownloadFile(Config.PrivKey, remote, command, localPath)
if err != nil {
fmt.Printf("%v failure. %v\n", remote, err)
} else {
fmt.Printf("%v success. Wrote %v bytes to %v\n", remote, n, localPath)
}
}
}