lease_command.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2016 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. "fmt"
  17. "io"
  18. "os"
  19. "strconv"
  20. "time"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  23. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  24. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  25. )
  26. // NewLeaseCommand returns the cobra command for "lease".
  27. func NewLeaseCommand() *cobra.Command {
  28. lc := &cobra.Command{
  29. Use: "lease",
  30. Short: "lease is used to manage leases.",
  31. }
  32. lc.AddCommand(NewLeaseCreateCommand())
  33. lc.AddCommand(NewLeaseRevokeCommand())
  34. lc.AddCommand(NewLeaseKeepAliveCommand())
  35. return lc
  36. }
  37. // NewLeaseCreateCommand returns the cobra command for "lease create".
  38. func NewLeaseCreateCommand() *cobra.Command {
  39. lc := &cobra.Command{
  40. Use: "create",
  41. Short: "create is used to create leases.",
  42. Run: leaseCreateCommandFunc,
  43. }
  44. return lc
  45. }
  46. // leaseCreateCommandFunc executes the "lease create" command.
  47. func leaseCreateCommandFunc(cmd *cobra.Command, args []string) {
  48. if len(args) != 1 {
  49. ExitWithError(ExitBadArgs, fmt.Errorf("lease create command needs TTL argument."))
  50. }
  51. ttl, err := strconv.ParseInt(args[0], 10, 64)
  52. if err != nil {
  53. ExitWithError(ExitBadArgs, fmt.Errorf("bad TTL (%v)", err))
  54. }
  55. endpoint, err := cmd.Flags().GetString("endpoint")
  56. if err != nil {
  57. ExitWithError(ExitError, err)
  58. }
  59. conn, err := grpc.Dial(endpoint)
  60. if err != nil {
  61. ExitWithError(ExitBadConnection, err)
  62. }
  63. lease := pb.NewLeaseClient(conn)
  64. req := &pb.LeaseCreateRequest{TTL: ttl}
  65. resp, err := lease.LeaseCreate(context.Background(), req)
  66. if err != nil {
  67. fmt.Fprintf(os.Stderr, "failed to create lease (%v)\n", err)
  68. return
  69. }
  70. fmt.Printf("lease %016x created with TTL(%ds)\n", resp.ID, resp.TTL)
  71. }
  72. // NewLeaseRevokeCommand returns the cobra command for "lease revoke".
  73. func NewLeaseRevokeCommand() *cobra.Command {
  74. lc := &cobra.Command{
  75. Use: "revoke",
  76. Short: "revoke is used to revoke leases.",
  77. Run: leaseRevokeCommandFunc,
  78. }
  79. return lc
  80. }
  81. // leaseRevokeCommandFunc executes the "lease create" command.
  82. func leaseRevokeCommandFunc(cmd *cobra.Command, args []string) {
  83. if len(args) != 1 {
  84. ExitWithError(ExitBadArgs, fmt.Errorf("lease revoke command needs 1 argument"))
  85. }
  86. id, err := strconv.ParseInt(args[0], 16, 64)
  87. if err != nil {
  88. ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
  89. }
  90. endpoint, err := cmd.Flags().GetString("endpoint")
  91. if err != nil {
  92. ExitWithError(ExitError, err)
  93. }
  94. conn, err := grpc.Dial(endpoint)
  95. if err != nil {
  96. ExitWithError(ExitBadConnection, err)
  97. }
  98. lease := pb.NewLeaseClient(conn)
  99. req := &pb.LeaseRevokeRequest{ID: id}
  100. _, err = lease.LeaseRevoke(context.Background(), req)
  101. if err != nil {
  102. fmt.Fprintf(os.Stderr, "failed to revoke lease (%v)\n", err)
  103. return
  104. }
  105. fmt.Printf("lease %016x revoked\n", id)
  106. }
  107. // NewLeaseKeepAliveCommand returns the cobra command for "lease keep-alive".
  108. func NewLeaseKeepAliveCommand() *cobra.Command {
  109. lc := &cobra.Command{
  110. Use: "keep-alive",
  111. Short: "keep-alive is used to keep leases alive.",
  112. Run: leaseKeepAliveCommandFunc,
  113. }
  114. return lc
  115. }
  116. // leaseKeepAliveCommandFunc executes the "lease keep-alive" command.
  117. func leaseKeepAliveCommandFunc(cmd *cobra.Command, args []string) {
  118. if len(args) != 1 {
  119. ExitWithError(ExitBadArgs, fmt.Errorf("lease keep-alive command needs lease ID as argument"))
  120. }
  121. id, err := strconv.ParseInt(args[0], 16, 64)
  122. if err != nil {
  123. ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
  124. }
  125. endpoint, err := cmd.Flags().GetString("endpoint")
  126. if err != nil {
  127. ExitWithError(ExitError, err)
  128. }
  129. conn, err := grpc.Dial(endpoint)
  130. if err != nil {
  131. ExitWithError(ExitBadConnection, err)
  132. }
  133. lease := pb.NewLeaseClient(conn)
  134. kStream, err := lease.LeaseKeepAlive(context.TODO())
  135. if err != nil {
  136. ExitWithError(ExitBadConnection, err)
  137. }
  138. nextC := make(chan int64, 1)
  139. go leaseKeepAliveRecvLoop(kStream, nextC)
  140. req := &pb.LeaseKeepAliveRequest{ID: id}
  141. for {
  142. err := kStream.Send(req)
  143. if err != nil {
  144. ExitWithError(ExitError, fmt.Errorf("failed to keep-alive lease (%v)", err))
  145. }
  146. next := <-nextC
  147. time.Sleep(time.Duration(next/2) * time.Second)
  148. }
  149. }
  150. func leaseKeepAliveRecvLoop(kStream pb.Lease_LeaseKeepAliveClient, nextC chan int64) {
  151. for {
  152. resp, err := kStream.Recv()
  153. if err == io.EOF {
  154. os.Exit(ExitSuccess)
  155. }
  156. if err != nil {
  157. ExitWithError(ExitError, err)
  158. }
  159. fmt.Printf("lease %016x keepalived with TTL(%d)\n", resp.ID, resp.TTL)
  160. nextC <- resp.TTL
  161. }
  162. }