lease_command.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/lease"
  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(NewLeaseCreateCommand())
  30. lc.AddCommand(NewLeaseRevokeCommand())
  31. lc.AddCommand(NewLeaseKeepAliveCommand())
  32. return lc
  33. }
  34. // NewLeaseCreateCommand returns the cobra command for "lease create".
  35. func NewLeaseCreateCommand() *cobra.Command {
  36. lc := &cobra.Command{
  37. Use: "create",
  38. Short: "create is used to create leases.",
  39. Run: leaseCreateCommandFunc,
  40. }
  41. return lc
  42. }
  43. // leaseCreateCommandFunc executes the "lease create" command.
  44. func leaseCreateCommandFunc(cmd *cobra.Command, args []string) {
  45. if len(args) != 1 {
  46. ExitWithError(ExitBadArgs, fmt.Errorf("lease create 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. resp, err := mustClientFromCmd(cmd).Create(context.TODO(), ttl)
  53. if err != nil {
  54. fmt.Fprintf(os.Stderr, "failed to create lease (%v)\n", err)
  55. return
  56. }
  57. fmt.Printf("lease %016x created 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",
  63. Short: "revoke is used to revoke leases.",
  64. Run: leaseRevokeCommandFunc,
  65. }
  66. return lc
  67. }
  68. // leaseRevokeCommandFunc executes the "lease create" 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. _, err = mustClientFromCmd(cmd).Revoke(context.TODO(), lease.LeaseID(id))
  78. if err != nil {
  79. fmt.Fprintf(os.Stderr, "failed to revoke lease (%v)\n", err)
  80. return
  81. }
  82. fmt.Printf("lease %016x revoked\n", id)
  83. }
  84. // NewLeaseKeepAliveCommand returns the cobra command for "lease keep-alive".
  85. func NewLeaseKeepAliveCommand() *cobra.Command {
  86. lc := &cobra.Command{
  87. Use: "keep-alive",
  88. Short: "keep-alive is used to keep leases alive.",
  89. Run: leaseKeepAliveCommandFunc,
  90. }
  91. return lc
  92. }
  93. // leaseKeepAliveCommandFunc executes the "lease keep-alive" command.
  94. func leaseKeepAliveCommandFunc(cmd *cobra.Command, args []string) {
  95. if len(args) != 1 {
  96. ExitWithError(ExitBadArgs, fmt.Errorf("lease keep-alive command needs lease ID as argument"))
  97. }
  98. id, err := strconv.ParseInt(args[0], 16, 64)
  99. if err != nil {
  100. ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
  101. }
  102. respc, kerr := mustClientFromCmd(cmd).KeepAlive(context.TODO(), lease.LeaseID(id))
  103. if kerr != nil {
  104. ExitWithError(ExitBadConnection, kerr)
  105. }
  106. for resp := range respc {
  107. fmt.Printf("lease %016x keepalived with TTL(%d)\n", resp.ID, resp.TTL)
  108. }
  109. fmt.Printf("lease %016x expired or revoked.\n", id)
  110. }