tendermint/cmd/debora/main.go

333 lines
7.7 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"
2015-05-05 13:22:03 -07:00
"net/url"
2015-04-15 20:22:03 -07:00
"os"
2015-05-05 13:22:03 -07:00
"regexp"
"strings"
2015-04-21 17:42:08 -07:00
"sync"
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"
cfg "github.com/tendermint/tendermint/config"
2015-04-14 04:14:18 -07:00
)
2015-05-05 13:22:03 -07:00
func remoteNick(remote string) string {
u, err := url.Parse(remote)
if err != nil {
return regexp.MustCompile(`[[:^alnum:]]`).ReplaceAllString(remote, "_")
} else {
return regexp.MustCompile(`[[:^alnum:]]`).ReplaceAllString(u.Host, "_")
}
}
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())
// Apply bare tendermint/* configuration.
cfg.ApplyConfig(cfg.MapConfig(map[string]interface{}{"log_level": "info"}))
rootDir := os.Getenv("DEBROOT")
if rootDir == "" {
rootDir = os.Getenv("HOME") + "/.debora"
}
var (
2015-04-21 17:42:08 -07:00
groupFlag = cli.StringFlag{
Name: "group",
Value: "default",
Usage: "uses ~/.debora/<group>.cfg",
}
2015-04-24 13:12:53 -07:00
labelFlag = cli.StringFlag{
Name: "label",
Value: "_",
Usage: "label of the process, or _ by default",
}
2015-04-21 17:42:08 -07:00
bgFlag = cli.BoolFlag{
Name: "bg",
Usage: "if set, runs as a background daemon",
}
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-21 17:42:08 -07:00
groupFlag,
2015-04-15 23:41:13 -07:00
}
app.Before = func(c *cli.Context) error {
2015-04-21 17:42:08 -07:00
configFile := rootDir + "/" + c.String("group") + ".cfg"
fmt.Printf("Using configuration from %v\n", configFile)
ReadConfig(configFile)
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",
2015-05-21 14:04:38 -07:00
Action: cliStartProcess,
2015-04-16 09:46:35 -07:00
Flags: []cli.Flag{
2015-04-24 13:12:53 -07:00
labelFlag,
2015-04-21 17:42:08 -07:00
bgFlag,
2015-04-16 09:46:35 -07:00
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,
},
cli.Command{
Name: "open",
Usage: "open listener",
Action: cliOpenListener,
},
cli.Command{
Name: "close",
Usage: "close listener",
Action: cliCloseListener,
},
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-21 17:42:08 -07:00
wg := sync.WaitGroup{}
2015-04-16 16:35:51 -07:00
for _, remote := range Config.Remotes {
2015-04-21 17:42:08 -07:00
wg.Add(1)
go func(remote string) {
defer wg.Done()
response, err := GetStatus(remote)
if err != nil {
fmt.Printf("%v failure. %v\n", remote, err)
} else {
fmt.Printf("%v success. %v\n", remote, response)
}
}(remote)
}
2015-04-21 17:42:08 -07:00
wg.Wait()
}
2015-05-21 14:04:38 -07:00
func cliStartProcess(c *cli.Context) {
2015-04-16 09:46:35 -07:00
args := c.Args()
2015-04-24 13:12:53 -07:00
if len(args) < 1 {
Exit("Must specify <execPath> <args...>")
2015-04-16 09:46:35 -07:00
}
2015-04-24 13:12:53 -07:00
execPath := args[0]
args = args[1:]
2015-05-21 14:04:38 -07:00
command := btypes.CommandStartProcess{
2015-04-21 17:42:08 -07:00
Wait: !c.Bool("bg"),
2015-04-24 13:12:53 -07:00
Label: c.String("label"),
2015-04-16 09:46:35 -07:00
ExecPath: execPath,
Args: args,
Input: c.String("input"),
}
2015-04-21 17:42:08 -07:00
wg := sync.WaitGroup{}
2015-04-16 16:35:51 -07:00
for _, remote := range Config.Remotes {
2015-04-21 17:42:08 -07:00
wg.Add(1)
go func(remote string) {
defer wg.Done()
2015-05-21 14:04:38 -07:00
response, err := StartProcess(Config.PrivKey, remote, command)
2015-04-21 17:42:08 -07:00
if err != nil {
fmt.Printf("%v failure. %v\n", remote, err)
} else {
2015-04-21 17:42:08 -07:00
fmt.Printf("%v success.\n", remote)
if response.Output != "" {
fmt.Println("--------------------------------------------------------------------------------")
fmt.Println(response.Output)
fmt.Println("--------------------------------------------------------------------------------")
} else {
fmt.Println("(no output)")
}
}
2015-04-21 17:42:08 -07:00
}(remote)
2015-04-15 23:41:13 -07:00
}
2015-04-21 17:42:08 -07:00
wg.Wait()
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-21 17:42:08 -07:00
wg := sync.WaitGroup{}
2015-04-16 16:35:51 -07:00
for _, remote := range Config.Remotes {
2015-04-21 17:42:08 -07:00
wg.Add(1)
go func(remote string) {
defer wg.Done()
response, err := StopProcess(Config.PrivKey, remote, command)
if err != nil {
fmt.Printf("%v failure. %v\n", remote, err)
} else {
fmt.Printf("%v success. %v\n", remote, response)
}
}(remote)
2015-04-15 23:41:13 -07:00
}
2015-04-21 17:42:08 -07:00
wg.Wait()
2015-04-15 23:41:13 -07:00
}
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-21 17:42:08 -07:00
wg := sync.WaitGroup{}
2015-04-16 16:35:51 -07:00
for _, remote := range Config.Remotes {
2015-04-21 17:42:08 -07:00
wg.Add(1)
go func(remote string) {
defer wg.Done()
response, err := ListProcesses(Config.PrivKey, remote, command)
if err != nil {
fmt.Printf("%v failure. %v\n", Blue(remote), Red(err))
} else {
fmt.Printf("%v processes:\n", Blue(remote))
for _, proc := range response.Processes {
2015-05-05 13:22:03 -07:00
fmt.Printf(" \"%v\" => `%v %v` (%v)\n", Yellow(proc.Label), proc.ExecPath, strings.Join(proc.Args, ","), proc.Pid)
2015-04-24 13:12:53 -07:00
fmt.Printf(" started at %v", proc.StartTime.String())
if proc.EndTime.IsZero() {
fmt.Printf(", running still\n")
} else {
endTimeStr := proc.EndTime.String()
fmt.Printf(", stopped at %v\n", Yellow(endTimeStr))
2015-04-21 17:42:08 -07:00
}
2015-04-24 13:12:53 -07:00
fmt.Printf(" stdout/stderr goes to %v\n", proc.OutputPath)
2015-04-19 16:20:00 -07:00
}
2015-04-16 09:46:35 -07:00
}
2015-04-21 17:42:08 -07:00
}(remote)
2015-04-15 20:22:03 -07:00
}
2015-04-21 17:42:08 -07:00
wg.Wait()
2015-04-14 04:14:18 -07:00
}
2015-04-20 14:47:59 -07:00
func cliOpenListener(c *cli.Context) {
args := c.Args()
if len(args) < 1 {
Exit("Must specify <listenAddr e.g. 0.0.0.0:46660>")
}
listenAddr := args[0]
command := btypes.CommandOpenListener{
Addr: listenAddr,
}
wg := sync.WaitGroup{}
for _, remote := range Config.Remotes {
wg.Add(1)
go func(remote string) {
defer wg.Done()
response, err := OpenListener(Config.PrivKey, remote, command)
if err != nil {
fmt.Printf("%v failure. %v\n", remote, err)
} else {
fmt.Printf("%v opened %v.\n", remote, response.Addr)
}
}(remote)
}
wg.Wait()
}
func cliCloseListener(c *cli.Context) {
args := c.Args()
if len(args) == 0 {
Exit("Must specify listenAddr to stop")
}
listenAddr := args[0]
command := btypes.CommandCloseListener{
Addr: listenAddr,
}
wg := sync.WaitGroup{}
for _, remote := range Config.Remotes {
wg.Add(1)
go func(remote string) {
defer wg.Done()
response, err := CloseListener(Config.PrivKey, remote, command)
if err != nil {
fmt.Printf("%v failure. %v\n", remote, err)
} else {
fmt.Printf("%v success. %v\n", remote, response)
}
}(remote)
}
wg.Wait()
}
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,
}
2015-05-05 13:22:03 -07:00
2015-04-21 17:42:08 -07:00
wg := sync.WaitGroup{}
2015-05-05 13:22:03 -07:00
for _, remote := range Config.Remotes {
2015-04-21 17:42:08 -07:00
wg.Add(1)
2015-05-04 11:18:21 -07:00
go func(remote string, localPath string) {
2015-04-21 17:42:08 -07:00
defer wg.Done()
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)
}
2015-05-05 13:22:03 -07:00
}(remote, Fmt("%v_%v", localPathPrefix, remoteNick(remote)))
2015-04-20 14:47:59 -07:00
}
2015-04-21 17:42:08 -07:00
wg.Wait()
2015-04-20 14:47:59 -07:00
}