You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
3.8 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/codegangsta/cli"
  5. "io/ioutil"
  6. "os"
  7. "strings"
  8. acm "github.com/tendermint/tendermint/account"
  9. "github.com/tendermint/tendermint/binary"
  10. btypes "github.com/tendermint/tendermint/cmd/barak/types"
  11. . "github.com/tendermint/tendermint/common"
  12. )
  13. var (
  14. remotes []string
  15. privKey acm.PrivKey
  16. remotesFlag = cli.StringFlag{
  17. Name: "remotes",
  18. Value: "http://127.0.0.1:8082",
  19. Usage: "comma separated list of remote baraks",
  20. }
  21. privKeyFlag = cli.StringFlag{
  22. Name: "privkey-file",
  23. Value: "privkey",
  24. Usage: "file containing private key json",
  25. }
  26. waitFlag = cli.BoolFlag{
  27. Name: "wait",
  28. Usage: "whether to wait for termination",
  29. }
  30. inputFlag = cli.StringFlag{
  31. Name: "input",
  32. Value: "",
  33. Usage: "input to the program (e.g. stdin)",
  34. }
  35. )
  36. func main() {
  37. fmt.Printf("New Debora Process (PID: %d)\n", os.Getpid())
  38. app := cli.NewApp()
  39. app.Name = "debora"
  40. app.Usage = "summons commands to barak"
  41. app.Version = "0.0.1"
  42. app.Email = "ethan@erisindustries.com,jae@tendermint.com"
  43. app.Flags = []cli.Flag{
  44. remotesFlag,
  45. privKeyFlag,
  46. }
  47. app.Before = func(c *cli.Context) error {
  48. remotes, privKey = ParseFlags(c)
  49. return nil
  50. }
  51. app.Commands = []cli.Command{
  52. cli.Command{
  53. Name: "run",
  54. Usage: "run process",
  55. Action: cliRunProcess,
  56. Flags: []cli.Flag{
  57. waitFlag,
  58. inputFlag,
  59. },
  60. },
  61. cli.Command{
  62. Name: "stop",
  63. Usage: "stop process",
  64. Action: cliStopProcess,
  65. Flags: []cli.Flag{
  66. //remotesFlag,
  67. //privKeyFlag,
  68. },
  69. },
  70. cli.Command{
  71. Name: "list",
  72. Usage: "list processes",
  73. Action: cliListProcesses,
  74. Flags: []cli.Flag{
  75. //remotesFlag,
  76. //privKeyFlag,
  77. },
  78. },
  79. }
  80. app.Run(os.Args)
  81. }
  82. func ParseFlags(c *cli.Context) (remotes []string, privKey acm.PrivKey) {
  83. remotesStr := c.String("remotes")
  84. remotes = strings.Split(remotesStr, ",")
  85. privkeyFile := c.String("privkey-file")
  86. privkeyJSONBytes, err := ioutil.ReadFile(privkeyFile)
  87. if err != nil {
  88. fmt.Printf("Failed to read privkey from file %v. %v", privkeyFile, err)
  89. return remotes, nil
  90. }
  91. binary.ReadJSON(&privKey, privkeyJSONBytes, &err)
  92. if err != nil {
  93. Exit(Fmt("Failed to parse privkey. %v", err))
  94. }
  95. return remotes, privKey
  96. }
  97. func cliRunProcess(c *cli.Context) {
  98. args := c.Args()
  99. if len(args) < 2 {
  100. Exit("Must specify <label> <execPath> <args...>")
  101. }
  102. label := args[0]
  103. execPath := args[1]
  104. args = args[2:]
  105. command := btypes.CommandRunProcess{
  106. Wait: c.Bool("wait"),
  107. Label: label,
  108. ExecPath: execPath,
  109. Args: args,
  110. Input: c.String("input"),
  111. }
  112. for _, remote := range remotes {
  113. response, err := RunProcess(privKey, remote, command)
  114. if err != nil {
  115. fmt.Printf("%v failure. %v\n", remote, err)
  116. } else {
  117. fmt.Printf("%v success. %v\n", remote, response)
  118. }
  119. }
  120. }
  121. func cliStopProcess(c *cli.Context) {
  122. args := c.Args()
  123. if len(args) == 0 {
  124. Exit("Must specify label to stop")
  125. }
  126. label := args[0]
  127. command := btypes.CommandStopProcess{
  128. Label: label,
  129. }
  130. for _, remote := range remotes {
  131. response, err := StopProcess(privKey, remote, command)
  132. if err != nil {
  133. fmt.Printf("%v failure. %v\n", remote, err)
  134. } else {
  135. fmt.Printf("%v success. %v\n", remote, response)
  136. }
  137. }
  138. }
  139. func cliListProcesses(c *cli.Context) {
  140. /*
  141. args := c.Args()
  142. if len(args) == 0 {
  143. log.Fatal("Must specify application name")
  144. }
  145. app := args[0]
  146. */
  147. command := btypes.CommandListProcesses{}
  148. for _, remote := range remotes {
  149. response, err := ListProcesses(privKey, remote, command)
  150. if err != nil {
  151. fmt.Printf("%v failure. %v\n", remote, err)
  152. } else {
  153. fmt.Printf("%v processes:\n", remote)
  154. for _, proc := range response.Processes {
  155. fmt.Printf(" \"%v\" => `%v` (%v) start:%v end:%v output:%v\n",
  156. proc.Label, proc.ExecPath, proc.Pid,
  157. proc.StartTime, proc.EndTime, proc.OutputPath)
  158. }
  159. }
  160. }
  161. }