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.

191 lines
4.4 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
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. func main() {
  17. fmt.Printf("New Debora Process (PID: %d)\n", os.Getpid())
  18. rootDir := os.Getenv("DEBROOT")
  19. if rootDir == "" {
  20. rootDir = os.Getenv("HOME") + "/.debora"
  21. }
  22. var (
  23. configFlag = cli.StringFlag{
  24. Name: "config-file",
  25. Value: rootDir + "/config.json",
  26. Usage: "config file",
  27. }
  28. waitFlag = cli.BoolFlag{
  29. Name: "wait",
  30. Usage: "whether to wait for termination",
  31. }
  32. inputFlag = cli.StringFlag{
  33. Name: "input",
  34. Value: "",
  35. Usage: "input to the program (e.g. stdin)",
  36. }
  37. )
  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. configFlag,
  45. }
  46. app.Before = func(c *cli.Context) error {
  47. ReadConfig(c.String("config-file"))
  48. return nil
  49. }
  50. app.Commands = []cli.Command{
  51. cli.Command{
  52. Name: "status",
  53. Usage: "shows remote status",
  54. Action: cliGetStatus,
  55. },
  56. cli.Command{
  57. Name: "run",
  58. Usage: "run process",
  59. Action: cliRunProcess,
  60. Flags: []cli.Flag{
  61. waitFlag,
  62. inputFlag,
  63. },
  64. },
  65. cli.Command{
  66. Name: "stop",
  67. Usage: "stop process",
  68. Action: cliStopProcess,
  69. },
  70. cli.Command{
  71. Name: "list",
  72. Usage: "list processes",
  73. Action: cliListProcesses,
  74. },
  75. }
  76. app.Run(os.Args)
  77. }
  78. func ReadConfig(configFilePath string) {
  79. configJSONBytes, err := ioutil.ReadFile(configFilePath)
  80. if err != nil {
  81. Exit(Fmt("Failed to read config file %v. %v\n", configFilePath, err))
  82. }
  83. binary.ReadJSON(&Config, configJSONBytes, &err)
  84. if err != nil {
  85. Exit(Fmt("Failed to parse config. %v", err))
  86. }
  87. }
  88. func cliGetStatus(c *cli.Context) {
  89. args := c.Args()
  90. if len(args) != 0 {
  91. fmt.Println("BTW, status takes no arguments.")
  92. }
  93. for _, remote := range Config.Remotes {
  94. response, err := GetStatus(remote)
  95. if err != nil {
  96. fmt.Printf("%v failure. %v\n", remote, err)
  97. } else {
  98. fmt.Printf("%v success. %v\n", remote, response)
  99. }
  100. }
  101. }
  102. func cliRunProcess(c *cli.Context) {
  103. args := c.Args()
  104. if len(args) < 2 {
  105. Exit("Must specify <label> <execPath> <args...>")
  106. }
  107. label := args[0]
  108. execPath := args[1]
  109. args = args[2:]
  110. command := btypes.CommandRunProcess{
  111. Wait: c.Bool("wait"),
  112. Label: label,
  113. ExecPath: execPath,
  114. Args: args,
  115. Input: c.String("input"),
  116. }
  117. for _, remote := range Config.Remotes {
  118. response, err := RunProcess(Config.PrivKey, remote, command)
  119. if err != nil {
  120. fmt.Printf("%v failure. %v\n", remote, err)
  121. } else {
  122. fmt.Printf("%v success.\n", remote)
  123. if response.Output != "" {
  124. fmt.Println("--------------------------------------------------------------------------------")
  125. fmt.Println(response.Output)
  126. fmt.Println("--------------------------------------------------------------------------------")
  127. } else {
  128. fmt.Println("(no output)")
  129. }
  130. }
  131. }
  132. }
  133. func cliStopProcess(c *cli.Context) {
  134. args := c.Args()
  135. if len(args) == 0 {
  136. Exit("Must specify label to stop")
  137. }
  138. label := args[0]
  139. command := btypes.CommandStopProcess{
  140. Label: label,
  141. Kill: true,
  142. }
  143. for _, remote := range Config.Remotes {
  144. response, err := StopProcess(Config.PrivKey, remote, command)
  145. if err != nil {
  146. fmt.Printf("%v failure. %v\n", remote, err)
  147. } else {
  148. fmt.Printf("%v success. %v\n", remote, response)
  149. }
  150. }
  151. }
  152. func cliListProcesses(c *cli.Context) {
  153. /*
  154. args := c.Args()
  155. if len(args) == 0 {
  156. log.Fatal("Must specify application name")
  157. }
  158. app := args[0]
  159. */
  160. command := btypes.CommandListProcesses{}
  161. for _, remote := range Config.Remotes {
  162. response, err := ListProcesses(Config.PrivKey, remote, command)
  163. if err != nil {
  164. fmt.Printf("%v failure. %v\n", remote, err)
  165. } else {
  166. fmt.Printf("%v processes:\n", remote)
  167. for _, proc := range response.Processes {
  168. startTimeStr := Green(proc.StartTime.String())
  169. endTimeStr := proc.EndTime.String()
  170. if !proc.EndTime.IsZero() {
  171. endTimeStr = Red(endTimeStr)
  172. }
  173. fmt.Printf(" %v start:%v end:%v output:%v\n",
  174. RightPadString(Fmt("\"%v\" => `%v` (%v)", proc.Label, proc.ExecPath, proc.Pid), 40),
  175. startTimeStr, endTimeStr, proc.OutputPath)
  176. }
  177. }
  178. }
  179. }