lease_command.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. "context"
  17. "fmt"
  18. "strconv"
  19. v3 "go.etcd.io/etcd/clientv3"
  20. "github.com/spf13/cobra"
  21. )
  22. // NewLeaseCommand returns the cobra command for "lease".
  23. func NewLeaseCommand() *cobra.Command {
  24. lc := &cobra.Command{
  25. Use: "lease <subcommand>",
  26. Short: "Lease related commands",
  27. }
  28. lc.AddCommand(NewLeaseGrantCommand())
  29. lc.AddCommand(NewLeaseRevokeCommand())
  30. lc.AddCommand(NewLeaseTimeToLiveCommand())
  31. lc.AddCommand(NewLeaseListCommand())
  32. lc.AddCommand(NewLeaseKeepAliveCommand())
  33. return lc
  34. }
  35. // NewLeaseGrantCommand returns the cobra command for "lease grant".
  36. func NewLeaseGrantCommand() *cobra.Command {
  37. lc := &cobra.Command{
  38. Use: "grant <ttl>",
  39. Short: "Creates leases",
  40. Run: leaseGrantCommandFunc,
  41. }
  42. return lc
  43. }
  44. // leaseGrantCommandFunc executes the "lease grant" command.
  45. func leaseGrantCommandFunc(cmd *cobra.Command, args []string) {
  46. if len(args) != 1 {
  47. ExitWithError(ExitBadArgs, fmt.Errorf("lease grant command needs TTL argument"))
  48. }
  49. ttl, err := strconv.ParseInt(args[0], 10, 64)
  50. if err != nil {
  51. ExitWithError(ExitBadArgs, fmt.Errorf("bad TTL (%v)", err))
  52. }
  53. ctx, cancel := commandCtx(cmd)
  54. resp, err := mustClientFromCmd(cmd).Grant(ctx, ttl)
  55. cancel()
  56. if err != nil {
  57. ExitWithError(ExitError, fmt.Errorf("failed to grant lease (%v)", err))
  58. }
  59. display.Grant(*resp)
  60. }
  61. // NewLeaseRevokeCommand returns the cobra command for "lease revoke".
  62. func NewLeaseRevokeCommand() *cobra.Command {
  63. lc := &cobra.Command{
  64. Use: "revoke <leaseID>",
  65. Short: "Revokes leases",
  66. Run: leaseRevokeCommandFunc,
  67. }
  68. return lc
  69. }
  70. // leaseRevokeCommandFunc executes the "lease grant" command.
  71. func leaseRevokeCommandFunc(cmd *cobra.Command, args []string) {
  72. if len(args) != 1 {
  73. ExitWithError(ExitBadArgs, fmt.Errorf("lease revoke command needs 1 argument"))
  74. }
  75. id := leaseFromArgs(args[0])
  76. ctx, cancel := commandCtx(cmd)
  77. resp, err := mustClientFromCmd(cmd).Revoke(ctx, id)
  78. cancel()
  79. if err != nil {
  80. ExitWithError(ExitError, fmt.Errorf("failed to revoke lease (%v)", err))
  81. }
  82. display.Revoke(id, *resp)
  83. }
  84. var timeToLiveKeys bool
  85. // NewLeaseTimeToLiveCommand returns the cobra command for "lease timetolive".
  86. func NewLeaseTimeToLiveCommand() *cobra.Command {
  87. lc := &cobra.Command{
  88. Use: "timetolive <leaseID> [options]",
  89. Short: "Get lease information",
  90. Run: leaseTimeToLiveCommandFunc,
  91. }
  92. lc.Flags().BoolVar(&timeToLiveKeys, "keys", false, "Get keys attached to this lease")
  93. return lc
  94. }
  95. // leaseTimeToLiveCommandFunc executes the "lease timetolive" command.
  96. func leaseTimeToLiveCommandFunc(cmd *cobra.Command, args []string) {
  97. if len(args) != 1 {
  98. ExitWithError(ExitBadArgs, fmt.Errorf("lease timetolive command needs lease ID as argument"))
  99. }
  100. var opts []v3.LeaseOption
  101. if timeToLiveKeys {
  102. opts = append(opts, v3.WithAttachedKeys())
  103. }
  104. resp, rerr := mustClientFromCmd(cmd).TimeToLive(context.TODO(), leaseFromArgs(args[0]), opts...)
  105. if rerr != nil {
  106. ExitWithError(ExitBadConnection, rerr)
  107. }
  108. display.TimeToLive(*resp, timeToLiveKeys)
  109. }
  110. // NewLeaseListCommand returns the cobra command for "lease list".
  111. func NewLeaseListCommand() *cobra.Command {
  112. lc := &cobra.Command{
  113. Use: "list",
  114. Short: "List all active leases",
  115. Run: leaseListCommandFunc,
  116. }
  117. return lc
  118. }
  119. // leaseListCommandFunc executes the "lease list" command.
  120. func leaseListCommandFunc(cmd *cobra.Command, args []string) {
  121. resp, rerr := mustClientFromCmd(cmd).Leases(context.TODO())
  122. if rerr != nil {
  123. ExitWithError(ExitBadConnection, rerr)
  124. }
  125. display.Leases(*resp)
  126. }
  127. var (
  128. leaseKeepAliveOnce bool
  129. )
  130. // NewLeaseKeepAliveCommand returns the cobra command for "lease keep-alive".
  131. func NewLeaseKeepAliveCommand() *cobra.Command {
  132. lc := &cobra.Command{
  133. Use: "keep-alive [options] <leaseID>",
  134. Short: "Keeps leases alive (renew)",
  135. Run: leaseKeepAliveCommandFunc,
  136. }
  137. lc.Flags().BoolVar(&leaseKeepAliveOnce, "once", false, "Resets the keep-alive time to its original value and exits immediately")
  138. return lc
  139. }
  140. // leaseKeepAliveCommandFunc executes the "lease keep-alive" command.
  141. func leaseKeepAliveCommandFunc(cmd *cobra.Command, args []string) {
  142. if len(args) != 1 {
  143. ExitWithError(ExitBadArgs, fmt.Errorf("lease keep-alive command needs lease ID as argument"))
  144. }
  145. id := leaseFromArgs(args[0])
  146. if leaseKeepAliveOnce {
  147. respc, kerr := mustClientFromCmd(cmd).KeepAliveOnce(context.TODO(), id)
  148. if kerr != nil {
  149. ExitWithError(ExitBadConnection, kerr)
  150. }
  151. display.KeepAlive(*respc)
  152. return
  153. }
  154. respc, kerr := mustClientFromCmd(cmd).KeepAlive(context.TODO(), id)
  155. if kerr != nil {
  156. ExitWithError(ExitBadConnection, kerr)
  157. }
  158. for resp := range respc {
  159. display.KeepAlive(*resp)
  160. }
  161. if _, ok := (display).(*simplePrinter); ok {
  162. fmt.Printf("lease %016x expired or revoked.\n", id)
  163. }
  164. }
  165. func leaseFromArgs(arg string) v3.LeaseID {
  166. id, err := strconv.ParseInt(arg, 16, 64)
  167. if err != nil {
  168. ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
  169. }
  170. return v3.LeaseID(id)
  171. }