exec_watch_command.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/github.com/coreos/go-etcd/etcd"
  24. )
  25. // NewExecWatchCommand returns the CLI command for "exec-watch".
  26. func NewExecWatchCommand() cli.Command {
  27. return cli.Command{
  28. Name: "exec-watch",
  29. Usage: "watch a key for changes and exec an executable",
  30. Flags: []cli.Flag{
  31. cli.IntFlag{Name: "after-index", Value: 0, Usage: "watch after the given index"},
  32. cli.BoolFlag{Name: "recursive", Usage: "watch all values for key and child keys"},
  33. },
  34. Action: func(c *cli.Context) {
  35. handleKey(c, execWatchCommandFunc)
  36. },
  37. }
  38. }
  39. // execWatchCommandFunc executes the "exec-watch" command.
  40. func execWatchCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
  41. _ = io.Copy
  42. _ = exec.Command
  43. args := c.Args()
  44. argsLen := len(args)
  45. if argsLen < 2 {
  46. return nil, errors.New("Key and command to exec required")
  47. }
  48. key := args[argsLen-1]
  49. cmdArgs := args[:argsLen-1]
  50. index := 0
  51. if c.Int("after-index") != 0 {
  52. index = c.Int("after-index") + 1
  53. key = args[0]
  54. cmdArgs = args[2:]
  55. }
  56. recursive := c.Bool("recursive")
  57. if recursive != false {
  58. key = args[0]
  59. cmdArgs = args[2:]
  60. }
  61. sigch := make(chan os.Signal, 1)
  62. signal.Notify(sigch, os.Interrupt)
  63. stop := make(chan bool)
  64. go func() {
  65. <-sigch
  66. stop <- true
  67. os.Exit(0)
  68. }()
  69. receiver := make(chan *etcd.Response)
  70. go client.Watch(key, uint64(index), recursive, receiver, stop)
  71. for {
  72. resp := <-receiver
  73. cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
  74. cmd.Env = environResponse(resp, os.Environ())
  75. stdout, err := cmd.StdoutPipe()
  76. if err != nil {
  77. fmt.Fprintf(os.Stderr, err.Error())
  78. os.Exit(1)
  79. }
  80. stderr, err := cmd.StderrPipe()
  81. if err != nil {
  82. fmt.Fprintf(os.Stderr, err.Error())
  83. os.Exit(1)
  84. }
  85. err = cmd.Start()
  86. if err != nil {
  87. fmt.Fprintf(os.Stderr, err.Error())
  88. os.Exit(1)
  89. }
  90. go io.Copy(os.Stdout, stdout)
  91. go io.Copy(os.Stderr, stderr)
  92. cmd.Wait()
  93. }
  94. }
  95. func environResponse(resp *etcd.Response, env []string) []string {
  96. env = append(env, "ETCD_WATCH_ACTION="+resp.Action)
  97. env = append(env, "ETCD_WATCH_MODIFIED_INDEX="+fmt.Sprintf("%d", resp.Node.ModifiedIndex))
  98. env = append(env, "ETCD_WATCH_KEY="+resp.Node.Key)
  99. env = append(env, "ETCD_WATCH_VALUE="+resp.Node.Value)
  100. return env
  101. }