lease_command.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/Godeps/_workspace/src/google.golang.org/grpc"
  22. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  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. 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 arguement."))
  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. endpoint, err := cmd.Flags().GetString("endpoint")
  53. if err != nil {
  54. ExitWithError(ExitError, err)
  55. }
  56. conn, err := grpc.Dial(endpoint)
  57. if err != nil {
  58. ExitWithError(ExitBadConnection, err)
  59. }
  60. lease := pb.NewLeaseClient(conn)
  61. req := &pb.LeaseCreateRequest{TTL: ttl}
  62. resp, err := lease.LeaseCreate(context.Background(), req)
  63. if err != nil {
  64. fmt.Fprintf(os.Stderr, "failed to create lease (%v)\n", err)
  65. return
  66. }
  67. fmt.Printf("lease %016x created with TTL(%ds)\n", resp.ID, resp.TTL)
  68. }
  69. // NewLeaseRevokeCommand returns the cobra command for "lease revoke".
  70. func NewLeaseRevokeCommand() *cobra.Command {
  71. lc := &cobra.Command{
  72. Use: "revoke",
  73. Short: "revoke is used to revoke leases.",
  74. Run: leaseRevokeCommandFunc,
  75. }
  76. return lc
  77. }
  78. // leaseRevokeCommandFunc executes the "lease create" command.
  79. func leaseRevokeCommandFunc(cmd *cobra.Command, args []string) {
  80. if len(args) != 1 {
  81. ExitWithError(ExitBadArgs, fmt.Errorf("lease revoke command needs 1 argument"))
  82. }
  83. id, err := strconv.ParseInt(args[0], 16, 64)
  84. if err != nil {
  85. ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
  86. }
  87. endpoint, err := cmd.Flags().GetString("endpoint")
  88. if err != nil {
  89. ExitWithError(ExitError, err)
  90. }
  91. conn, err := grpc.Dial(endpoint)
  92. if err != nil {
  93. ExitWithError(ExitBadConnection, err)
  94. }
  95. lease := pb.NewLeaseClient(conn)
  96. req := &pb.LeaseRevokeRequest{ID: id}
  97. _, err = lease.LeaseRevoke(context.Background(), req)
  98. if err != nil {
  99. fmt.Fprintf(os.Stderr, "failed to revoke lease (%v)\n", err)
  100. return
  101. }
  102. fmt.Printf("lease %016x revoked\n", id)
  103. }