make_mirror_command.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "errors"
  17. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
  18. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  19. "github.com/coreos/etcd/clientv3"
  20. "github.com/coreos/etcd/clientv3/sync"
  21. "github.com/coreos/etcd/etcdserver/api/v3rpc"
  22. "github.com/coreos/etcd/storage/storagepb"
  23. )
  24. var (
  25. mmcert string
  26. mmkey string
  27. mmcacert string
  28. mmprefix string
  29. )
  30. // NewMakeMirrorCommand returns the cobra command for "makeMirror".
  31. func NewMakeMirrorCommand() *cobra.Command {
  32. c := &cobra.Command{
  33. Use: "make-mirror [options] [destination]",
  34. Short: "make-mirror makes a mirror at the destination etcd cluster",
  35. Run: makeMirrorCommandFunc,
  36. }
  37. c.Flags().StringVar(&mmprefix, "prefix", "", "the key-value prefix to mirror")
  38. // TODO: add dest-prefix to mirror a prefix to a different prefix in the destionation cluster?
  39. c.Flags().StringVar(&mmcert, "dest-cert", "", "identify secure client using this TLS certificate file for the destination cluster")
  40. c.Flags().StringVar(&mmkey, "dest-key", "", "identify secure client using this TLS key file")
  41. c.Flags().StringVar(&mmcacert, "dest-cacert", "", "verify certificates of TLS enabled secure servers using this CA bundle")
  42. return c
  43. }
  44. func makeMirrorCommandFunc(cmd *cobra.Command, args []string) {
  45. if len(args) != 1 {
  46. ExitWithError(ExitBadArgs, errors.New("make-mirror takes one destination arguement."))
  47. }
  48. dc := mustClient(args[0], mmcert, mmkey, mmcacert)
  49. c := mustClientFromCmd(cmd)
  50. err := makeMirror(context.TODO(), c, dc)
  51. ExitWithError(ExitError, err)
  52. }
  53. func makeMirror(ctx context.Context, c *clientv3.Client, dc *clientv3.Client) error {
  54. // TODO: remove the prefix of the destination cluster?
  55. dkv := clientv3.NewKV(dc)
  56. s := sync.NewSyncer(c, mmprefix, 0)
  57. rc, errc := s.SyncBase(ctx)
  58. for r := range rc {
  59. for _, kv := range r.Kvs {
  60. _, err := dkv.Put(ctx, string(kv.Key), string(kv.Value))
  61. if err != nil {
  62. return err
  63. }
  64. }
  65. }
  66. err := <-errc
  67. if err != nil {
  68. return err
  69. }
  70. wc := s.SyncUpdates(ctx)
  71. for wr := range wc {
  72. if wr.CompactRevision != 0 {
  73. return v3rpc.ErrCompacted
  74. }
  75. var rev int64
  76. ops := []clientv3.Op{}
  77. for _, ev := range wr.Events {
  78. nrev := ev.Kv.ModRevision
  79. if rev != 0 && nrev > rev {
  80. _, err := dkv.Txn(ctx).Then(ops...).Commit()
  81. if err != nil {
  82. return err
  83. }
  84. ops = []clientv3.Op{}
  85. }
  86. switch ev.Type {
  87. case storagepb.PUT:
  88. ops = append(ops, clientv3.OpPut(string(ev.Kv.Key), string(ev.Kv.Value)))
  89. case storagepb.DELETE, storagepb.EXPIRE:
  90. ops = append(ops, clientv3.OpDelete(string(ev.Kv.Key)))
  91. default:
  92. panic("unexpected event type")
  93. }
  94. }
  95. if len(ops) != 0 {
  96. _, err := dkv.Txn(ctx).Then(ops...).Commit()
  97. if err != nil {
  98. return err
  99. }
  100. }
  101. }
  102. return nil
  103. }