lease_command.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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",
  26. Short: "lease is used to manage leases.",
  27. }
  28. lc.AddCommand(NewLeaseGrantCommand())
  29. lc.AddCommand(NewLeaseRevokeCommand())
  30. lc.AddCommand(NewLeaseKeepAliveCommand())
  31. return lc
  32. }
  33. // NewLeaseGrantCommand returns the cobra command for "lease grant".
  34. func NewLeaseGrantCommand() *cobra.Command {
  35. lc := &cobra.Command{
  36. Use: "grant <ttl>",
  37. Short: "grant is used to create leases.",
  38. Run: leaseGrantCommandFunc,
  39. }
  40. return lc
  41. }
  42. // leaseGrantCommandFunc executes the "lease grant" command.
  43. func leaseGrantCommandFunc(cmd *cobra.Command, args []string) {
  44. if len(args) != 1 {
  45. ExitWithError(ExitBadArgs, fmt.Errorf("lease grant command needs TTL argument."))
  46. }
  47. ttl, err := strconv.ParseInt(args[0], 10, 64)
  48. if err != nil {
  49. ExitWithError(ExitBadArgs, fmt.Errorf("bad TTL (%v)", err))
  50. }
  51. ctx, cancel := commandCtx(cmd)
  52. resp, err := mustClientFromCmd(cmd).Grant(ctx, ttl)
  53. cancel()
  54. if err != nil {
  55. ExitWithError(ExitError, fmt.Errorf("failed to grant lease (%v)\n", err))
  56. }
  57. fmt.Printf("lease %016x granted with TTL(%ds)\n", resp.ID, resp.TTL)
  58. }
  59. // NewLeaseRevokeCommand returns the cobra command for "lease revoke".
  60. func NewLeaseRevokeCommand() *cobra.Command {
  61. lc := &cobra.Command{
  62. Use: "revoke <leaseID>",
  63. Short: "revoke is used to revoke leases.",
  64. Run: leaseRevokeCommandFunc,
  65. }
  66. return lc
  67. }
  68. // leaseRevokeCommandFunc executes the "lease grant" command.
  69. func leaseRevokeCommandFunc(cmd *cobra.Command, args []string) {
  70. if len(args) != 1 {
  71. ExitWithError(ExitBadArgs, fmt.Errorf("lease revoke command needs 1 argument"))
  72. }
  73. id, err := strconv.ParseInt(args[0], 16, 64)
  74. if err != nil {
  75. ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
  76. }
  77. ctx, cancel := commandCtx(cmd)
  78. _, err = mustClientFromCmd(cmd).Revoke(ctx, v3.LeaseID(id))
  79. cancel()
  80. if err != nil {
  81. ExitWithError(ExitError, fmt.Errorf("failed to revoke lease (%v)\n", err))
  82. }
  83. fmt.Printf("lease %016x revoked\n", id)
  84. }
  85. // NewLeaseKeepAliveCommand returns the cobra command for "lease keep-alive".
  86. func NewLeaseKeepAliveCommand() *cobra.Command {
  87. lc := &cobra.Command{
  88. Use: "keep-alive <leaseID>",
  89. Short: "keep-alive is used to keep leases alive.",
  90. Run: leaseKeepAliveCommandFunc,
  91. }
  92. return lc
  93. }
  94. // leaseKeepAliveCommandFunc executes the "lease keep-alive" command.
  95. func leaseKeepAliveCommandFunc(cmd *cobra.Command, args []string) {
  96. if len(args) != 1 {
  97. ExitWithError(ExitBadArgs, fmt.Errorf("lease keep-alive command needs lease ID as argument"))
  98. }
  99. id, err := strconv.ParseInt(args[0], 16, 64)
  100. if err != nil {
  101. ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
  102. }
  103. respc, kerr := mustClientFromCmd(cmd).KeepAlive(context.TODO(), v3.LeaseID(id))
  104. if kerr != nil {
  105. ExitWithError(ExitBadConnection, kerr)
  106. }
  107. for resp := range respc {
  108. fmt.Printf("lease %016x keepalived with TTL(%d)\n", resp.ID, resp.TTL)
  109. }
  110. fmt.Printf("lease %016x expired or revoked.\n", id)
  111. }