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.

250 lines
5.7 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. "sync"
  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 Config = struct {
  14. Remotes []string
  15. PrivKey acm.PrivKey
  16. }{}
  17. func main() {
  18. fmt.Printf("New Debora Process (PID: %d)\n", os.Getpid())
  19. rootDir := os.Getenv("DEBROOT")
  20. if rootDir == "" {
  21. rootDir = os.Getenv("HOME") + "/.debora"
  22. }
  23. var (
  24. groupFlag = cli.StringFlag{
  25. Name: "group",
  26. Value: "default",
  27. Usage: "uses ~/.debora/<group>.cfg",
  28. }
  29. bgFlag = cli.BoolFlag{
  30. Name: "bg",
  31. Usage: "if set, runs as a background daemon",
  32. }
  33. inputFlag = cli.StringFlag{
  34. Name: "input",
  35. Value: "",
  36. Usage: "input to the program (e.g. stdin)",
  37. }
  38. )
  39. app := cli.NewApp()
  40. app.Name = "debora"
  41. app.Usage = "summons commands to barak"
  42. app.Version = "0.0.1"
  43. app.Email = "ethan@erisindustries.com,jae@tendermint.com"
  44. app.Flags = []cli.Flag{
  45. groupFlag,
  46. }
  47. app.Before = func(c *cli.Context) error {
  48. configFile := rootDir + "/" + c.String("group") + ".cfg"
  49. fmt.Printf("Using configuration from %v\n", configFile)
  50. ReadConfig(configFile)
  51. return nil
  52. }
  53. app.Commands = []cli.Command{
  54. cli.Command{
  55. Name: "status",
  56. Usage: "shows remote status",
  57. Action: cliGetStatus,
  58. },
  59. cli.Command{
  60. Name: "run",
  61. Usage: "run process",
  62. Action: cliRunProcess,
  63. Flags: []cli.Flag{
  64. bgFlag,
  65. inputFlag,
  66. },
  67. },
  68. cli.Command{
  69. Name: "stop",
  70. Usage: "stop process",
  71. Action: cliStopProcess,
  72. },
  73. cli.Command{
  74. Name: "list",
  75. Usage: "list processes",
  76. Action: cliListProcesses,
  77. },
  78. cli.Command{
  79. Name: "download",
  80. Usage: "download file <remote-path> <local-path-prefix>",
  81. Action: cliDownloadFile,
  82. },
  83. }
  84. app.Run(os.Args)
  85. }
  86. func ReadConfig(configFilePath string) {
  87. configJSONBytes, err := ioutil.ReadFile(configFilePath)
  88. if err != nil {
  89. Exit(Fmt("Failed to read config file %v. %v\n", configFilePath, err))
  90. }
  91. binary.ReadJSON(&Config, configJSONBytes, &err)
  92. if err != nil {
  93. Exit(Fmt("Failed to parse config. %v", err))
  94. }
  95. }
  96. func cliGetStatus(c *cli.Context) {
  97. args := c.Args()
  98. if len(args) != 0 {
  99. fmt.Println("BTW, status takes no arguments.")
  100. }
  101. wg := sync.WaitGroup{}
  102. for _, remote := range Config.Remotes {
  103. wg.Add(1)
  104. go func(remote string) {
  105. defer wg.Done()
  106. response, err := GetStatus(remote)
  107. if err != nil {
  108. fmt.Printf("%v failure. %v\n", remote, err)
  109. } else {
  110. fmt.Printf("%v success. %v\n", remote, response)
  111. }
  112. }(remote)
  113. }
  114. wg.Wait()
  115. }
  116. func cliRunProcess(c *cli.Context) {
  117. args := c.Args()
  118. if len(args) < 2 {
  119. Exit("Must specify <label> <execPath> <args...>")
  120. }
  121. label := args[0]
  122. execPath := args[1]
  123. args = args[2:]
  124. command := btypes.CommandRunProcess{
  125. Wait: !c.Bool("bg"),
  126. Label: label,
  127. ExecPath: execPath,
  128. Args: args,
  129. Input: c.String("input"),
  130. }
  131. wg := sync.WaitGroup{}
  132. for _, remote := range Config.Remotes {
  133. wg.Add(1)
  134. go func(remote string) {
  135. defer wg.Done()
  136. response, err := RunProcess(Config.PrivKey, remote, command)
  137. if err != nil {
  138. fmt.Printf("%v failure. %v\n", remote, err)
  139. } else {
  140. fmt.Printf("%v success.\n", remote)
  141. if response.Output != "" {
  142. fmt.Println("--------------------------------------------------------------------------------")
  143. fmt.Println(response.Output)
  144. fmt.Println("--------------------------------------------------------------------------------")
  145. } else {
  146. fmt.Println("(no output)")
  147. }
  148. }
  149. }(remote)
  150. }
  151. wg.Wait()
  152. }
  153. func cliStopProcess(c *cli.Context) {
  154. args := c.Args()
  155. if len(args) == 0 {
  156. Exit("Must specify label to stop")
  157. }
  158. label := args[0]
  159. command := btypes.CommandStopProcess{
  160. Label: label,
  161. Kill: true,
  162. }
  163. wg := sync.WaitGroup{}
  164. for _, remote := range Config.Remotes {
  165. wg.Add(1)
  166. go func(remote string) {
  167. defer wg.Done()
  168. response, err := StopProcess(Config.PrivKey, remote, command)
  169. if err != nil {
  170. fmt.Printf("%v failure. %v\n", remote, err)
  171. } else {
  172. fmt.Printf("%v success. %v\n", remote, response)
  173. }
  174. }(remote)
  175. }
  176. wg.Wait()
  177. }
  178. func cliListProcesses(c *cli.Context) {
  179. /*
  180. args := c.Args()
  181. if len(args) == 0 {
  182. log.Fatal("Must specify application name")
  183. }
  184. app := args[0]
  185. */
  186. command := btypes.CommandListProcesses{}
  187. wg := sync.WaitGroup{}
  188. for _, remote := range Config.Remotes {
  189. wg.Add(1)
  190. go func(remote string) {
  191. defer wg.Done()
  192. response, err := ListProcesses(Config.PrivKey, remote, command)
  193. if err != nil {
  194. fmt.Printf("%v failure. %v\n", Blue(remote), Red(err))
  195. } else {
  196. fmt.Printf("%v processes:\n", Blue(remote))
  197. for _, proc := range response.Processes {
  198. startTimeStr := Green(proc.StartTime.String())
  199. endTimeStr := proc.EndTime.String()
  200. if !proc.EndTime.IsZero() {
  201. endTimeStr = Red(endTimeStr)
  202. }
  203. fmt.Printf(" %v start:%v end:%v output:%v\n",
  204. RightPadString(Fmt("\"%v\" => `%v` (%v)", Yellow(proc.Label), proc.ExecPath, proc.Pid), 40),
  205. startTimeStr, endTimeStr, proc.OutputPath)
  206. }
  207. }
  208. }(remote)
  209. }
  210. wg.Wait()
  211. }
  212. func cliDownloadFile(c *cli.Context) {
  213. args := c.Args()
  214. if len(args) != 2 {
  215. Exit("Must specify <remote-path> <local-path-prefix>")
  216. }
  217. remotePath := args[0]
  218. localPathPrefix := args[1]
  219. command := btypes.CommandServeFile{
  220. Path: remotePath,
  221. }
  222. wg := sync.WaitGroup{}
  223. for i, remote := range Config.Remotes {
  224. wg.Add(1)
  225. go func(remote string) {
  226. defer wg.Done()
  227. localPath := Fmt("%v_%v", localPathPrefix, i)
  228. n, err := DownloadFile(Config.PrivKey, remote, command, localPath)
  229. if err != nil {
  230. fmt.Printf("%v failure. %v\n", remote, err)
  231. } else {
  232. fmt.Printf("%v success. Wrote %v bytes to %v\n", remote, n, localPath)
  233. }
  234. }(remote)
  235. }
  236. wg.Wait()
  237. }