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.

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