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.

395 lines
8.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
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/tendermint/tendermint/Godeps/_workspace/src/github.com/codegangsta/cli"
  5. "io/ioutil"
  6. "net/url"
  7. "os"
  8. "regexp"
  9. "strings"
  10. "sync"
  11. acm "github.com/tendermint/tendermint/account"
  12. "github.com/tendermint/tendermint/binary"
  13. btypes "github.com/tendermint/tendermint/cmd/barak/types"
  14. . "github.com/tendermint/tendermint/common"
  15. cfg "github.com/tendermint/tendermint/config"
  16. )
  17. func remoteNick(remote string) string {
  18. u, err := url.Parse(remote)
  19. if err != nil {
  20. return regexp.MustCompile(`[[:^alnum:]]`).ReplaceAllString(remote, "_")
  21. } else {
  22. return regexp.MustCompile(`[[:^alnum:]]`).ReplaceAllString(u.Host, "_")
  23. }
  24. }
  25. var Config = struct {
  26. Remotes []string
  27. PrivKey acm.PrivKey
  28. }{}
  29. func main() {
  30. fmt.Printf("New Debora Process (PID: %d)\n", os.Getpid())
  31. // Apply bare tendermint/* configuration.
  32. cfg.ApplyConfig(cfg.MapConfig(map[string]interface{}{"log_level": "info"}))
  33. rootDir := os.Getenv("DEBROOT")
  34. if rootDir == "" {
  35. rootDir = os.Getenv("HOME") + "/.debora"
  36. }
  37. var (
  38. groupFlag = cli.StringFlag{
  39. Name: "group",
  40. Value: "default",
  41. Usage: "uses ~/.debora/<group>.cfg",
  42. }
  43. labelFlag = cli.StringFlag{
  44. Name: "label",
  45. Value: "_",
  46. Usage: "label of the process, or _ by default",
  47. }
  48. bgFlag = cli.BoolFlag{
  49. Name: "bg",
  50. Usage: "if set, runs as a background daemon",
  51. }
  52. inputFlag = cli.StringFlag{
  53. Name: "input",
  54. Value: "",
  55. Usage: "input to the program (e.g. stdin)",
  56. }
  57. )
  58. app := cli.NewApp()
  59. app.Name = "debora"
  60. app.Usage = "summons commands to barak"
  61. app.Version = "0.0.1"
  62. app.Email = "ethan@erisindustries.com,jae@tendermint.com"
  63. app.Flags = []cli.Flag{
  64. groupFlag,
  65. }
  66. app.Before = func(c *cli.Context) error {
  67. configFile := rootDir + "/" + c.String("group") + ".cfg"
  68. fmt.Printf("Using configuration from %v\n", configFile)
  69. ReadConfig(configFile)
  70. return nil
  71. }
  72. app.Commands = []cli.Command{
  73. cli.Command{
  74. Name: "status",
  75. Usage: "shows remote status",
  76. Action: cliGetStatus,
  77. },
  78. cli.Command{
  79. Name: "run",
  80. Usage: "run process",
  81. Action: cliStartProcess,
  82. Flags: []cli.Flag{
  83. labelFlag,
  84. bgFlag,
  85. inputFlag,
  86. },
  87. },
  88. cli.Command{
  89. Name: "stop",
  90. Usage: "stop process",
  91. Action: cliStopProcess,
  92. },
  93. cli.Command{
  94. Name: "list",
  95. Usage: "list processes",
  96. Action: cliListProcesses,
  97. },
  98. cli.Command{
  99. Name: "open",
  100. Usage: "open barak listener",
  101. Action: cliOpenListener,
  102. },
  103. cli.Command{
  104. Name: "close",
  105. Usage: "close barka listener",
  106. Action: cliCloseListener,
  107. },
  108. cli.Command{
  109. Name: "download",
  110. Usage: "download file <remote-path> <local-path-prefix>",
  111. Action: cliDownloadFile,
  112. },
  113. cli.Command{
  114. Name: "quit",
  115. Usage: "quit barak",
  116. Action: cliQuit,
  117. },
  118. }
  119. app.Run(os.Args)
  120. }
  121. func ReadConfig(configFilePath string) {
  122. configJSONBytes, err := ioutil.ReadFile(configFilePath)
  123. if err != nil {
  124. Exit(Fmt("Failed to read config file %v. %v\n", configFilePath, err))
  125. }
  126. binary.ReadJSON(&Config, configJSONBytes, &err)
  127. if err != nil {
  128. Exit(Fmt("Failed to parse config. %v", err))
  129. }
  130. }
  131. func cliGetStatus(c *cli.Context) {
  132. args := c.Args()
  133. if len(args) != 0 {
  134. fmt.Println("BTW, status takes no arguments.")
  135. }
  136. wg := sync.WaitGroup{}
  137. failed := 0
  138. for _, remote := range Config.Remotes {
  139. wg.Add(1)
  140. go func(remote string) {
  141. defer wg.Done()
  142. response, err := GetStatus(remote)
  143. if err != nil {
  144. failed++
  145. fmt.Printf("%v failure. %v\n", remote, err)
  146. } else {
  147. fmt.Printf("%v success. %v\n", remote, response)
  148. }
  149. }(remote)
  150. }
  151. wg.Wait()
  152. if 0 < failed {
  153. os.Exit(1)
  154. }
  155. }
  156. func cliStartProcess(c *cli.Context) {
  157. args := c.Args()
  158. if len(args) < 1 {
  159. Exit("Must specify <execPath> <args...>")
  160. }
  161. execPath := args[0]
  162. args = args[1:]
  163. command := btypes.CommandStartProcess{
  164. Wait: !c.Bool("bg"),
  165. Label: c.String("label"),
  166. ExecPath: execPath,
  167. Args: args,
  168. Input: c.String("input"),
  169. }
  170. wg := sync.WaitGroup{}
  171. failed := 0
  172. for _, remote := range Config.Remotes {
  173. wg.Add(1)
  174. go func(remote string) {
  175. defer wg.Done()
  176. response, err := StartProcess(Config.PrivKey, remote, command)
  177. if err != nil {
  178. failed++
  179. fmt.Printf("%v failure. %v\n", remote, err)
  180. } else {
  181. fmt.Printf("%v success.\n", remote)
  182. if response.Output != "" {
  183. fmt.Println("--------------------------------------------------------------------------------")
  184. fmt.Println(response.Output)
  185. fmt.Println("--------------------------------------------------------------------------------")
  186. } else {
  187. fmt.Println("(no output)")
  188. }
  189. }
  190. }(remote)
  191. }
  192. wg.Wait()
  193. if 0 < failed {
  194. os.Exit(1)
  195. }
  196. }
  197. func cliStopProcess(c *cli.Context) {
  198. args := c.Args()
  199. if len(args) == 0 {
  200. Exit("Must specify label to stop")
  201. }
  202. label := args[0]
  203. command := btypes.CommandStopProcess{
  204. Label: label,
  205. Kill: true,
  206. }
  207. wg := sync.WaitGroup{}
  208. failed := 0
  209. for _, remote := range Config.Remotes {
  210. wg.Add(1)
  211. go func(remote string) {
  212. defer wg.Done()
  213. response, err := StopProcess(Config.PrivKey, remote, command)
  214. if err != nil {
  215. failed++
  216. fmt.Printf("%v failure. %v\n", remote, err)
  217. } else {
  218. fmt.Printf("%v success. %v\n", remote, response)
  219. }
  220. }(remote)
  221. }
  222. wg.Wait()
  223. if 0 < failed {
  224. os.Exit(1)
  225. }
  226. }
  227. func cliListProcesses(c *cli.Context) {
  228. /*
  229. args := c.Args()
  230. if len(args) == 0 {
  231. log.Fatal("Must specify application name")
  232. }
  233. app := args[0]
  234. */
  235. command := btypes.CommandListProcesses{}
  236. wg := sync.WaitGroup{}
  237. failed := 0
  238. for _, remote := range Config.Remotes {
  239. wg.Add(1)
  240. go func(remote string) {
  241. defer wg.Done()
  242. response, err := ListProcesses(Config.PrivKey, remote, command)
  243. if err != nil {
  244. failed++
  245. fmt.Printf("%v failure. %v\n", Blue(remote), Red(err))
  246. } else {
  247. fmt.Printf("%v processes:\n", Blue(remote))
  248. for _, proc := range response.Processes {
  249. fmt.Printf(" \"%v\" => `%v %v` (%v)\n", Yellow(proc.Label), proc.ExecPath, strings.Join(proc.Args, ","), proc.Pid)
  250. fmt.Printf(" started at %v", proc.StartTime.String())
  251. if proc.EndTime.IsZero() {
  252. fmt.Printf(", running still\n")
  253. } else {
  254. endTimeStr := proc.EndTime.String()
  255. fmt.Printf(", stopped at %v\n", Yellow(endTimeStr))
  256. }
  257. fmt.Printf(" stdout/stderr goes to %v\n", proc.OutputPath)
  258. }
  259. }
  260. }(remote)
  261. }
  262. wg.Wait()
  263. if 0 < failed {
  264. os.Exit(1)
  265. }
  266. }
  267. func cliOpenListener(c *cli.Context) {
  268. args := c.Args()
  269. if len(args) < 1 {
  270. Exit("Must specify <listenAddr e.g. [::]:46661>")
  271. }
  272. listenAddr := args[0]
  273. command := btypes.CommandOpenListener{
  274. Addr: listenAddr,
  275. }
  276. wg := sync.WaitGroup{}
  277. failed := 0
  278. for _, remote := range Config.Remotes {
  279. wg.Add(1)
  280. go func(remote string) {
  281. defer wg.Done()
  282. response, err := OpenListener(Config.PrivKey, remote, command)
  283. if err != nil {
  284. failed++
  285. fmt.Printf("%v failure. %v\n", remote, err)
  286. } else {
  287. fmt.Printf("%v opened %v.\n", remote, response.Addr)
  288. }
  289. }(remote)
  290. }
  291. wg.Wait()
  292. if 0 < failed {
  293. os.Exit(1)
  294. }
  295. }
  296. func cliCloseListener(c *cli.Context) {
  297. args := c.Args()
  298. if len(args) == 0 {
  299. Exit("Must specify listenAddr to stop")
  300. }
  301. listenAddr := args[0]
  302. command := btypes.CommandCloseListener{
  303. Addr: listenAddr,
  304. }
  305. wg := sync.WaitGroup{}
  306. failed := 0
  307. for _, remote := range Config.Remotes {
  308. wg.Add(1)
  309. go func(remote string) {
  310. defer wg.Done()
  311. response, err := CloseListener(Config.PrivKey, remote, command)
  312. if err != nil {
  313. failed++
  314. fmt.Printf("%v failure. %v\n", remote, err)
  315. } else {
  316. fmt.Printf("%v success. %v\n", remote, response)
  317. }
  318. }(remote)
  319. }
  320. wg.Wait()
  321. if 0 < failed {
  322. os.Exit(1)
  323. }
  324. }
  325. func cliDownloadFile(c *cli.Context) {
  326. args := c.Args()
  327. if len(args) != 2 {
  328. Exit("Must specify <remote-path> <local-path-prefix>")
  329. }
  330. remotePath := args[0]
  331. localPathPrefix := args[1]
  332. command := btypes.CommandServeFile{
  333. Path: remotePath,
  334. }
  335. wg := sync.WaitGroup{}
  336. failed := 0
  337. for _, remote := range Config.Remotes {
  338. wg.Add(1)
  339. go func(remote string, localPath string) {
  340. defer wg.Done()
  341. n, err := DownloadFile(Config.PrivKey, remote, command, localPath)
  342. if err != nil {
  343. failed++
  344. fmt.Printf("%v failure. %v\n", remote, err)
  345. } else {
  346. fmt.Printf("%v success. Wrote %v bytes to %v\n", remote, n, localPath)
  347. }
  348. }(remote, Fmt("%v_%v", localPathPrefix, remoteNick(remote)))
  349. }
  350. wg.Wait()
  351. if 0 < failed {
  352. os.Exit(1)
  353. }
  354. }
  355. func cliQuit(c *cli.Context) {
  356. command := btypes.CommandQuit{}
  357. wg := sync.WaitGroup{}
  358. failed := 0
  359. for _, remote := range Config.Remotes {
  360. wg.Add(1)
  361. go func(remote string) {
  362. defer wg.Done()
  363. response, err := Quit(Config.PrivKey, remote, command)
  364. if err != nil {
  365. failed++
  366. fmt.Printf("%v failure. %v\n", remote, err)
  367. } else {
  368. fmt.Printf("%v success. %v\n", remote, response)
  369. }
  370. }(remote)
  371. }
  372. wg.Wait()
  373. if 0 < failed {
  374. os.Exit(1)
  375. }
  376. }