exec_watch_command.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "errors"
  17. "fmt"
  18. "io"
  19. "os"
  20. "os/exec"
  21. "os/signal"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
  23. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  24. "github.com/coreos/etcd/client"
  25. )
  26. // NewExecWatchCommand returns the CLI command for "exec-watch".
  27. func NewExecWatchCommand() cli.Command {
  28. return cli.Command{
  29. Name: "exec-watch",
  30. Usage: "watch a key for changes and exec an executable",
  31. Flags: []cli.Flag{
  32. cli.IntFlag{Name: "after-index", Value: 0, Usage: "watch after the given index"},
  33. cli.BoolFlag{Name: "recursive", Usage: "watch all values for key and child keys"},
  34. },
  35. Action: func(c *cli.Context) {
  36. execWatchCommandFunc(c, mustNewKeyAPI(c))
  37. },
  38. }
  39. }
  40. // execWatchCommandFunc executes the "exec-watch" command.
  41. func execWatchCommandFunc(c *cli.Context, ki client.KeysAPI) {
  42. args := c.Args()
  43. argslen := len(args)
  44. if argslen < 2 {
  45. handleError(ExitBadArgs, errors.New("key and command to exec required"))
  46. }
  47. key := args[argslen-1]
  48. cmdArgs := args[:argslen-1]
  49. index := 0
  50. if c.Int("after-index") != 0 {
  51. index = c.Int("after-index") + 1
  52. key = args[0]
  53. cmdArgs = args[2:]
  54. }
  55. recursive := c.Bool("recursive")
  56. if recursive != false {
  57. key = args[0]
  58. cmdArgs = args[2:]
  59. }
  60. sigch := make(chan os.Signal, 1)
  61. signal.Notify(sigch, os.Interrupt)
  62. go func() {
  63. <-sigch
  64. os.Exit(0)
  65. }()
  66. w := ki.Watcher(key, &client.WatcherOptions{AfterIndex: uint64(index), Recursive: recursive})
  67. for {
  68. resp, err := w.Next(context.TODO())
  69. if err != nil {
  70. handleError(ExitServerError, err)
  71. }
  72. if resp.Node.Dir {
  73. fmt.Fprintf(os.Stderr, "Ignored dir %s change", resp.Node.Key)
  74. continue
  75. }
  76. cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
  77. cmd.Env = environResponse(resp, os.Environ())
  78. stdout, err := cmd.StdoutPipe()
  79. if err != nil {
  80. fmt.Fprintf(os.Stderr, err.Error())
  81. os.Exit(1)
  82. }
  83. stderr, err := cmd.StderrPipe()
  84. if err != nil {
  85. fmt.Fprintf(os.Stderr, err.Error())
  86. os.Exit(1)
  87. }
  88. go func() {
  89. err := cmd.Start()
  90. if err != nil {
  91. fmt.Fprintf(os.Stderr, err.Error())
  92. os.Exit(1)
  93. }
  94. go io.Copy(os.Stdout, stdout)
  95. go io.Copy(os.Stderr, stderr)
  96. cmd.Wait()
  97. }()
  98. }
  99. }
  100. func environResponse(resp *client.Response, env []string) []string {
  101. env = append(env, "ETCD_WATCH_ACTION="+resp.Action)
  102. env = append(env, "ETCD_WATCH_MODIFIED_INDEX="+fmt.Sprintf("%d", resp.Node.ModifiedIndex))
  103. env = append(env, "ETCD_WATCH_KEY="+resp.Node.Key)
  104. env = append(env, "ETCD_WATCH_VALUE="+resp.Node.Value)
  105. return env
  106. }