lease_command.go 4.4 KB

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