lease_command.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. "fmt"
  17. "strconv"
  18. v3 "github.com/coreos/etcd/clientv3"
  19. "github.com/spf13/cobra"
  20. "golang.org/x/net/context"
  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(NewLeaseKeepAliveCommand())
  32. return lc
  33. }
  34. // NewLeaseGrantCommand returns the cobra command for "lease grant".
  35. func NewLeaseGrantCommand() *cobra.Command {
  36. lc := &cobra.Command{
  37. Use: "grant <ttl>",
  38. Short: "Creates leases",
  39. Run: leaseGrantCommandFunc,
  40. }
  41. return lc
  42. }
  43. // leaseGrantCommandFunc executes the "lease grant" command.
  44. func leaseGrantCommandFunc(cmd *cobra.Command, args []string) {
  45. if len(args) != 1 {
  46. ExitWithError(ExitBadArgs, fmt.Errorf("lease grant command needs TTL argument."))
  47. }
  48. ttl, err := strconv.ParseInt(args[0], 10, 64)
  49. if err != nil {
  50. ExitWithError(ExitBadArgs, fmt.Errorf("bad TTL (%v)", err))
  51. }
  52. ctx, cancel := commandCtx(cmd)
  53. resp, err := mustClientFromCmd(cmd).Grant(ctx, ttl)
  54. cancel()
  55. if err != nil {
  56. ExitWithError(ExitError, fmt.Errorf("failed to grant lease (%v)\n", err))
  57. }
  58. display.Grant(*resp)
  59. }
  60. // NewLeaseRevokeCommand returns the cobra command for "lease revoke".
  61. func NewLeaseRevokeCommand() *cobra.Command {
  62. lc := &cobra.Command{
  63. Use: "revoke <leaseID>",
  64. Short: "Revokes leases",
  65. Run: leaseRevokeCommandFunc,
  66. }
  67. return lc
  68. }
  69. // leaseRevokeCommandFunc executes the "lease grant" command.
  70. func leaseRevokeCommandFunc(cmd *cobra.Command, args []string) {
  71. if len(args) != 1 {
  72. ExitWithError(ExitBadArgs, fmt.Errorf("lease revoke command needs 1 argument"))
  73. }
  74. id := leaseFromArgs(args[0])
  75. ctx, cancel := commandCtx(cmd)
  76. resp, err := mustClientFromCmd(cmd).Revoke(ctx, id)
  77. cancel()
  78. if err != nil {
  79. ExitWithError(ExitError, fmt.Errorf("failed to revoke lease (%v)\n", err))
  80. }
  81. display.Revoke(id, *resp)
  82. }
  83. var timeToLiveKeys bool
  84. // NewLeaseTimeToLiveCommand returns the cobra command for "lease timetolive".
  85. func NewLeaseTimeToLiveCommand() *cobra.Command {
  86. lc := &cobra.Command{
  87. Use: "timetolive <leaseID> [options]",
  88. Short: "Get lease information",
  89. Run: leaseTimeToLiveCommandFunc,
  90. }
  91. lc.Flags().BoolVar(&timeToLiveKeys, "keys", false, "Get keys attached to this lease")
  92. return lc
  93. }
  94. // leaseTimeToLiveCommandFunc executes the "lease timetolive" command.
  95. func leaseTimeToLiveCommandFunc(cmd *cobra.Command, args []string) {
  96. if len(args) != 1 {
  97. ExitWithError(ExitBadArgs, fmt.Errorf("lease timetolive command needs lease ID as argument"))
  98. }
  99. var opts []v3.LeaseOption
  100. if timeToLiveKeys {
  101. opts = append(opts, v3.WithAttachedKeys())
  102. }
  103. resp, rerr := mustClientFromCmd(cmd).TimeToLive(context.TODO(), leaseFromArgs(args[0]), opts...)
  104. if rerr != nil {
  105. ExitWithError(ExitBadConnection, rerr)
  106. }
  107. display.TimeToLive(*resp, timeToLiveKeys)
  108. }
  109. // NewLeaseKeepAliveCommand returns the cobra command for "lease keep-alive".
  110. func NewLeaseKeepAliveCommand() *cobra.Command {
  111. lc := &cobra.Command{
  112. Use: "keep-alive <leaseID>",
  113. Short: "Keeps leases alive (renew)",
  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 := leaseFromArgs(args[0])
  124. respc, kerr := mustClientFromCmd(cmd).KeepAlive(context.TODO(), id)
  125. if kerr != nil {
  126. ExitWithError(ExitBadConnection, kerr)
  127. }
  128. for resp := range respc {
  129. display.KeepAlive(*resp)
  130. }
  131. if _, ok := (display).(*simplePrinter); ok {
  132. fmt.Printf("lease %016x expired or revoked.\n", id)
  133. }
  134. }
  135. func leaseFromArgs(arg string) v3.LeaseID {
  136. id, err := strconv.ParseInt(arg, 16, 64)
  137. if err != nil {
  138. ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
  139. }
  140. return v3.LeaseID(id)
  141. }