lease_command.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "os"
  18. "strconv"
  19. v3 "github.com/coreos/etcd/clientv3"
  20. "github.com/spf13/cobra"
  21. "golang.org/x/net/context"
  22. )
  23. // NewLeaseCommand returns the cobra command for "lease".
  24. func NewLeaseCommand() *cobra.Command {
  25. lc := &cobra.Command{
  26. Use: "lease",
  27. Short: "lease is used to manage leases.",
  28. }
  29. lc.AddCommand(NewLeaseGrantCommand())
  30. lc.AddCommand(NewLeaseRevokeCommand())
  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",
  38. Short: "grant is used to create 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. fmt.Fprintf(os.Stderr, "failed to grant lease (%v)\n", err)
  57. return
  58. }
  59. fmt.Printf("lease %016x granted with TTL(%ds)\n", resp.ID, resp.TTL)
  60. }
  61. // NewLeaseRevokeCommand returns the cobra command for "lease revoke".
  62. func NewLeaseRevokeCommand() *cobra.Command {
  63. lc := &cobra.Command{
  64. Use: "revoke",
  65. Short: "revoke is used to revoke 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, err := strconv.ParseInt(args[0], 16, 64)
  76. if err != nil {
  77. ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
  78. }
  79. ctx, cancel := commandCtx(cmd)
  80. _, err = mustClientFromCmd(cmd).Revoke(ctx, v3.LeaseID(id))
  81. cancel()
  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. respc, kerr := mustClientFromCmd(cmd).KeepAlive(context.TODO(), v3.LeaseID(id))
  107. if kerr != nil {
  108. ExitWithError(ExitBadConnection, kerr)
  109. }
  110. for resp := range respc {
  111. fmt.Printf("lease %016x keepalived with TTL(%d)\n", resp.ID, resp.TTL)
  112. }
  113. fmt.Printf("lease %016x expired or revoked.\n", id)
  114. }