lease_command.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // TODO: enable grpc.WithTransportCredentials(creds)
  60. conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
  61. if err != nil {
  62. ExitWithError(ExitBadConnection, err)
  63. }
  64. lease := pb.NewLeaseClient(conn)
  65. req := &pb.LeaseCreateRequest{TTL: ttl}
  66. resp, err := lease.LeaseCreate(context.Background(), req)
  67. if err != nil {
  68. fmt.Fprintf(os.Stderr, "failed to create lease (%v)\n", err)
  69. return
  70. }
  71. fmt.Printf("lease %016x created with TTL(%ds)\n", resp.ID, resp.TTL)
  72. }
  73. // NewLeaseRevokeCommand returns the cobra command for "lease revoke".
  74. func NewLeaseRevokeCommand() *cobra.Command {
  75. lc := &cobra.Command{
  76. Use: "revoke",
  77. Short: "revoke is used to revoke leases.",
  78. Run: leaseRevokeCommandFunc,
  79. }
  80. return lc
  81. }
  82. // leaseRevokeCommandFunc executes the "lease create" command.
  83. func leaseRevokeCommandFunc(cmd *cobra.Command, args []string) {
  84. if len(args) != 1 {
  85. ExitWithError(ExitBadArgs, fmt.Errorf("lease revoke command needs 1 argument"))
  86. }
  87. id, err := strconv.ParseInt(args[0], 16, 64)
  88. if err != nil {
  89. ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
  90. }
  91. endpoint, err := cmd.Flags().GetString("endpoint")
  92. if err != nil {
  93. ExitWithError(ExitError, err)
  94. }
  95. // TODO: enable grpc.WithTransportCredentials(creds)
  96. conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
  97. if err != nil {
  98. ExitWithError(ExitBadConnection, err)
  99. }
  100. lease := pb.NewLeaseClient(conn)
  101. req := &pb.LeaseRevokeRequest{ID: id}
  102. _, err = lease.LeaseRevoke(context.Background(), req)
  103. if err != nil {
  104. fmt.Fprintf(os.Stderr, "failed to revoke lease (%v)\n", err)
  105. return
  106. }
  107. fmt.Printf("lease %016x revoked\n", id)
  108. }
  109. // NewLeaseKeepAliveCommand returns the cobra command for "lease keep-alive".
  110. func NewLeaseKeepAliveCommand() *cobra.Command {
  111. lc := &cobra.Command{
  112. Use: "keep-alive",
  113. Short: "keep-alive is used to keep leases alive.",
  114. Run: leaseKeepAliveCommandFunc,
  115. }
  116. return lc
  117. }
  118. // leaseKeepAliveCommandFunc executes the "lease keep-alive" command.
  119. func leaseKeepAliveCommandFunc(cmd *cobra.Command, args []string) {
  120. if len(args) != 1 {
  121. ExitWithError(ExitBadArgs, fmt.Errorf("lease keep-alive command needs lease ID as argument"))
  122. }
  123. id, err := strconv.ParseInt(args[0], 16, 64)
  124. if err != nil {
  125. ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
  126. }
  127. endpoint, err := cmd.Flags().GetString("endpoint")
  128. if err != nil {
  129. ExitWithError(ExitError, err)
  130. }
  131. // TODO: enable grpc.WithTransportCredentials(creds)
  132. conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
  133. if err != nil {
  134. ExitWithError(ExitBadConnection, err)
  135. }
  136. lease := pb.NewLeaseClient(conn)
  137. kStream, err := lease.LeaseKeepAlive(context.TODO())
  138. if err != nil {
  139. ExitWithError(ExitBadConnection, err)
  140. }
  141. nextC := make(chan int64, 1)
  142. go leaseKeepAliveRecvLoop(kStream, nextC)
  143. req := &pb.LeaseKeepAliveRequest{ID: id}
  144. for {
  145. err := kStream.Send(req)
  146. if err != nil {
  147. ExitWithError(ExitError, fmt.Errorf("failed to keep-alive lease (%v)", err))
  148. }
  149. next := <-nextC
  150. time.Sleep(time.Duration(next/2) * time.Second)
  151. }
  152. }
  153. func leaseKeepAliveRecvLoop(kStream pb.Lease_LeaseKeepAliveClient, nextC chan int64) {
  154. for {
  155. resp, err := kStream.Recv()
  156. if err == io.EOF {
  157. os.Exit(ExitSuccess)
  158. }
  159. if err != nil {
  160. ExitWithError(ExitError, err)
  161. }
  162. fmt.Printf("lease %016x keepalived with TTL(%d)\n", resp.ID, resp.TTL)
  163. nextC <- resp.TTL
  164. }
  165. }