lock_command.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2016 The etcd Authors
  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. "os"
  19. "os/exec"
  20. "os/signal"
  21. "syscall"
  22. "github.com/coreos/etcd/clientv3"
  23. "github.com/coreos/etcd/clientv3/concurrency"
  24. "github.com/spf13/cobra"
  25. "golang.org/x/net/context"
  26. )
  27. // NewLockCommand returns the cobra command for "lock".
  28. func NewLockCommand() *cobra.Command {
  29. c := &cobra.Command{
  30. Use: "lock <lockname> [exec-command arg1 arg2 ...]",
  31. Short: "Acquires a named lock",
  32. Run: lockCommandFunc,
  33. }
  34. return c
  35. }
  36. func lockCommandFunc(cmd *cobra.Command, args []string) {
  37. if len(args) == 0 {
  38. ExitWithError(ExitBadArgs, errors.New("lock takes a lock name argument and an optional command to execute."))
  39. }
  40. c := mustClientFromCmd(cmd)
  41. if err := lockUntilSignal(c, args[0], args[1:]); err != nil {
  42. ExitWithError(ExitError, err)
  43. }
  44. }
  45. func lockUntilSignal(c *clientv3.Client, lockname string, cmdArgs []string) error {
  46. s, err := concurrency.NewSession(c)
  47. if err != nil {
  48. return err
  49. }
  50. m := concurrency.NewMutex(s, lockname)
  51. ctx, cancel := context.WithCancel(context.TODO())
  52. // unlock in case of ordinary shutdown
  53. donec := make(chan struct{})
  54. sigc := make(chan os.Signal, 1)
  55. signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
  56. go func() {
  57. <-sigc
  58. cancel()
  59. close(donec)
  60. }()
  61. if err := m.Lock(ctx); err != nil {
  62. return err
  63. }
  64. if len(cmdArgs) > 0 {
  65. cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
  66. cmd.Env = append(environLockResponse(m), os.Environ()...)
  67. cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
  68. err := cmd.Run()
  69. unlockErr := m.Unlock(context.TODO())
  70. if err != nil {
  71. return err
  72. }
  73. return unlockErr
  74. }
  75. k, kerr := c.Get(ctx, m.Key())
  76. if kerr != nil {
  77. return kerr
  78. }
  79. if len(k.Kvs) == 0 {
  80. return errors.New("lock lost on init")
  81. }
  82. display.Get(*k)
  83. select {
  84. case <-donec:
  85. return m.Unlock(context.TODO())
  86. case <-s.Done():
  87. }
  88. return errors.New("session expired")
  89. }
  90. func environLockResponse(m *concurrency.Mutex) []string {
  91. return []string{
  92. "ETCD_LOCK_KEY=" + m.Key(),
  93. fmt.Sprintf("ETCD_LOCK_REV=%d", m.Header().Revision),
  94. }
  95. }