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.

180 lines
4.1 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.\n", remote)
  119. if response.Output != "" {
  120. fmt.Println("--------------------------------------------------------------------------------")
  121. fmt.Println(response.Output)
  122. fmt.Println("--------------------------------------------------------------------------------")
  123. } else {
  124. fmt.Println("(no output)")
  125. }
  126. }
  127. }
  128. }
  129. func cliStopProcess(c *cli.Context) {
  130. args := c.Args()
  131. if len(args) == 0 {
  132. Exit("Must specify label to stop")
  133. }
  134. label := args[0]
  135. command := btypes.CommandStopProcess{
  136. Label: label,
  137. Kill: true,
  138. }
  139. for _, remote := range Config.Remotes {
  140. response, err := StopProcess(Config.PrivKey, remote, command)
  141. if err != nil {
  142. fmt.Printf("%v failure. %v\n", remote, err)
  143. } else {
  144. fmt.Printf("%v success. %v\n", remote, response)
  145. }
  146. }
  147. }
  148. func cliListProcesses(c *cli.Context) {
  149. /*
  150. args := c.Args()
  151. if len(args) == 0 {
  152. log.Fatal("Must specify application name")
  153. }
  154. app := args[0]
  155. */
  156. command := btypes.CommandListProcesses{}
  157. for _, remote := range Config.Remotes {
  158. response, err := ListProcesses(Config.PrivKey, remote, command)
  159. if err != nil {
  160. fmt.Printf("%v failure. %v\n", remote, err)
  161. } else {
  162. fmt.Printf("%v processes:\n", remote)
  163. for _, proc := range response.Processes {
  164. fmt.Printf(" \"%v\" => `%v` (%v) start:%v end:%v output:%v\n",
  165. proc.Label, proc.ExecPath, proc.Pid,
  166. proc.StartTime, proc.EndTime, proc.OutputPath)
  167. }
  168. }
  169. }
  170. }