lease_command.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  21. "github.com/coreos/etcd/clientv3"
  22. "github.com/coreos/etcd/lease"
  23. )
  24. // NewLeaseCommand returns the cobra command for "lease".
  25. func NewLeaseCommand() *cobra.Command {
  26. lc := &cobra.Command{
  27. Use: "lease",
  28. Short: "lease is used to manage leases.",
  29. }
  30. lc.AddCommand(NewLeaseCreateCommand())
  31. lc.AddCommand(NewLeaseRevokeCommand())
  32. lc.AddCommand(NewLeaseKeepAliveCommand())
  33. return lc
  34. }
  35. // NewLeaseCreateCommand returns the cobra command for "lease create".
  36. func NewLeaseCreateCommand() *cobra.Command {
  37. lc := &cobra.Command{
  38. Use: "create",
  39. Short: "create is used to create leases.",
  40. Run: leaseCreateCommandFunc,
  41. }
  42. return lc
  43. }
  44. // leaseCreateCommandFunc executes the "lease create" command.
  45. func leaseCreateCommandFunc(cmd *cobra.Command, args []string) {
  46. if len(args) != 1 {
  47. ExitWithError(ExitBadArgs, fmt.Errorf("lease create 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. c := mustClient(cmd)
  54. l := clientv3.NewLease(c)
  55. resp, err := l.Create(context.TODO(), ttl)
  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. c := mustClient(cmd)
  81. l := clientv3.NewLease(c)
  82. _, err = l.Revoke(context.TODO(), lease.LeaseID(id))
  83. if err != nil {
  84. fmt.Fprintf(os.Stderr, "failed to revoke lease (%v)\n", err)
  85. return
  86. }
  87. fmt.Printf("lease %016x revoked\n", id)
  88. }
  89. // NewLeaseKeepAliveCommand returns the cobra command for "lease keep-alive".
  90. func NewLeaseKeepAliveCommand() *cobra.Command {
  91. lc := &cobra.Command{
  92. Use: "keep-alive",
  93. Short: "keep-alive is used to keep leases alive.",
  94. Run: leaseKeepAliveCommandFunc,
  95. }
  96. return lc
  97. }
  98. // leaseKeepAliveCommandFunc executes the "lease keep-alive" command.
  99. func leaseKeepAliveCommandFunc(cmd *cobra.Command, args []string) {
  100. if len(args) != 1 {
  101. ExitWithError(ExitBadArgs, fmt.Errorf("lease keep-alive command needs lease ID as argument"))
  102. }
  103. id, err := strconv.ParseInt(args[0], 16, 64)
  104. if err != nil {
  105. ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
  106. }
  107. c := mustClient(cmd)
  108. l := clientv3.NewLease(c)
  109. respc, kerr := l.KeepAlive(context.TODO(), lease.LeaseID(id))
  110. if kerr != nil {
  111. ExitWithError(ExitBadConnection, kerr)
  112. }
  113. for resp := range respc {
  114. fmt.Printf("lease %016x keepalived with TTL(%d)\n", resp.ID, resp.TTL)
  115. }
  116. fmt.Printf("lease %016x expired or revoked.\n", id)
  117. }