Skip to content

Commit 758139d

Browse files
committed
first commit
0 parents  commit 758139d

File tree

26 files changed

+2150
-0
lines changed

26 files changed

+2150
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
_dist
3+
bin

.golangci.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
run:
2+
timeout: 2m
3+
4+
linters:
5+
disable-all: true
6+
enable:
7+
- deadcode
8+
- dupl
9+
- gofmt
10+
- goimports
11+
- golint
12+
- gosimple
13+
- govet
14+
- ineffassign
15+
- misspell
16+
- nakedret
17+
- structcheck
18+
- unused
19+
- varcheck
20+
21+
linters-settings:
22+
gofmt:
23+
simplify: true
24+
goimports:
25+
local-prefixes: github.com/ebuildy/elastic-copy
26+
dupl:
27+
threshold: 400

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

Makefile

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
BINDIR := $(CURDIR)/bin
2+
DIST_DIRS := find * -type d -exec
3+
TARGETS := darwin/amd64 linux/amd64#linux/386 linux/arm linux/arm64 linux/ppc64le windows/amd64
4+
BINNAME ?= elasticcopy
5+
6+
GOPATH = $(shell go env GOPATH)
7+
DEP = $(GOPATH)/bin/dep
8+
GOX = $(GOPATH)/bin/gox
9+
GOIMPORTS = $(GOPATH)/bin/goimports
10+
NAMESPACE = github.com/ebuildy/elastic-copy
11+
12+
ACCEPTANCE_DIR:=$(GOPATH)/src/helm.sh/acceptance-testing
13+
# To specify the subset of acceptance tests to run. '.' means all tests
14+
ACCEPTANCE_RUN_TESTS=.
15+
16+
# go option
17+
PKG := ./...
18+
TAGS :=
19+
TESTS := .
20+
TESTFLAGS :=
21+
LDFLAGS := -w -s
22+
GOFLAGS :=
23+
SRC := $(shell find . -type f -name '*.go' -print)
24+
25+
# Required for globs to work correctly
26+
SHELL = /bin/bash
27+
28+
GIT_COMMIT = $(shell git rev-parse HEAD)
29+
GIT_SHA = $(shell git rev-parse --short HEAD)
30+
GIT_TAG = $(shell git describe --tags --abbrev=0 2>/dev/null)
31+
GIT_DIRTY = $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "clean")
32+
33+
ifdef VERSION
34+
BINARY_VERSION = $(VERSION)
35+
endif
36+
BINARY_VERSION ?= ${GIT_TAG}
37+
38+
# Only set Version if building a tag or VERSION is set
39+
ifneq ($(BINARY_VERSION),)
40+
LDFLAGS += -X /internal/version.version=${BINARY_VERSION}
41+
endif
42+
43+
# Clear the "unreleased" string in BuildMetadata
44+
ifneq ($(GIT_TAG),)
45+
LDFLAGS += -X ${NAMESPACE}/internal/version.metadata=
46+
endif
47+
LDFLAGS += -X ${NAMESPACE}/internal/version.gitCommit=${GIT_COMMIT}
48+
LDFLAGS += -X ${NAMESPACE}/internal/version.gitTreeState=${GIT_DIRTY}
49+
50+
.PHONY: all
51+
all: build
52+
53+
# ------------------------------------------------------------------------------
54+
# build
55+
56+
.PHONY: build
57+
build: $(BINDIR)/$(BINNAME)
58+
59+
$(BINDIR)/$(BINNAME): $(SRC)
60+
GO111MODULE=on go build $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' -o $(BINDIR)/$(BINNAME) .
61+
62+
# ------------------------------------------------------------------------------
63+
# test
64+
65+
.PHONY: test
66+
test: build
67+
test: TESTFLAGS += -race -v
68+
test: test-style
69+
test: test-unit
70+
71+
.PHONY: test-unit
72+
test-unit:
73+
@echo
74+
@echo "==> Running unit tests <=="
75+
GO111MODULE=on go test $(GOFLAGS) -run $(TESTS) $(PKG) $(TESTFLAGS)
76+
77+
.PHONY: test-coverage
78+
test-coverage:
79+
@echo
80+
@echo "==> Running unit tests with coverage <=="
81+
@ ./.ci/scripts/coverage.sh
82+
83+
.PHONY: test-acceptance
84+
test-acceptance: TARGETS = linux/amd64
85+
test-acceptance: build build-cross
86+
@if [ -d "${ACCEPTANCE_DIR}" ]; then \
87+
cd ${ACCEPTANCE_DIR} && \
88+
ROBOT_RUN_TESTS=$(ACCEPTANCE_RUN_TESTS) ROBOT_HELM_PATH=$(BINDIR) make acceptance; \
89+
else \
90+
echo "You must clone the acceptance_testing repo under $(ACCEPTANCE_DIR)"; \
91+
echo "You can find the acceptance_testing repo at https://github.com/helm/acceptance-testing"; \
92+
fi
93+
94+
.PHONY: test-acceptance-completion
95+
test-acceptance-completion: ACCEPTANCE_RUN_TESTS = shells.robot
96+
test-acceptance-completion: test-acceptance
97+
98+
.PHONY: coverage
99+
coverage:
100+
@scripts/coverage.sh
101+
102+
.PHONY: format
103+
format: $(GOIMPORTS)
104+
GO111MODULE=on go list -f '{{.Dir}}' ./... | xargs $(GOIMPORTS) -w -local ${NAMESPACE}
105+
106+
# ------------------------------------------------------------------------------
107+
# dependencies
108+
109+
# If go get is run from inside the project directory it will add the dependencies
110+
# to the go.mod file. To avoid that we change to a directory without a go.mod file
111+
# when downloading the following dependencies
112+
113+
$(GOX):
114+
(cd /; GO111MODULE=on go get -u github.com/mitchellh/gox)
115+
116+
$(GOIMPORTS):
117+
(cd /; GO111MODULE=on go get -u golang.org/x/tools/cmd/goimports)
118+
119+
# ------------------------------------------------------------------------------
120+
# release
121+
122+
.PHONY: build-cross
123+
build-cross: LDFLAGS += -extldflags "-static"
124+
build-cross: $(GOX)
125+
GO111MODULE=on CGO_ENABLED=0 $(GOX) -parallel=3 -output="_dist/{{.OS}}-{{.Arch}}/$(BINNAME)" -osarch='$(TARGETS)' $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' .
126+
127+
.PHONY: dist
128+
dist:
129+
( \
130+
cd _dist && \
131+
$(DIST_DIRS) cp ../LICENSE {} \; && \
132+
$(DIST_DIRS) cp ../README.md {} \; && \
133+
$(DIST_DIRS) tar -zcf ${BINNAME}-${BINARY_VERSION}-{}.tar.gz {} \; && \
134+
$(DIST_DIRS) zip -r ${BINNAME}-${BINARY_VERSION}-{}.zip {} \; \
135+
)
136+
137+
.PHONY: checksum
138+
checksum:
139+
for f in _dist/*.{gz,zip} ; do \
140+
shasum -a 256 "$${f}" | awk '{print $$1}' > "$${f}.sha256" ; \
141+
done
142+
143+
# ------------------------------------------------------------------------------
144+
145+
.PHONY: clean
146+
clean:
147+
@rm -rf $(BINDIR) ./_dist
148+
149+
.PHONY: info
150+
info:
151+
@echo "Version: ${BINARY_VERSION}"
152+
@echo "Git Tag: ${GIT_TAG}"
153+
@echo "Git Commit: ${GIT_COMMIT}"
154+
@echo "Git Tree State: ${GIT_DIRTY}"

commands/count.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package commands
2+
3+
import (
4+
"github.com/ebuildy/elastic-copy/pkg/action"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
func newCountCmd() *cobra.Command {
9+
action := action.NewCountAction(settings)
10+
11+
cmd := &cobra.Command{
12+
Use: "count",
13+
RunE: func(cmd *cobra.Command, args []string) error {
14+
action.Run()
15+
16+
return nil
17+
},
18+
}
19+
20+
flags := cmd.PersistentFlags()
21+
22+
flags.StringVar(&action.Source, "source", "http://localhost:9200", "source URL")
23+
flags.StringVar(&action.Query, "query", "", "")
24+
flags.StringArrayVar(&action.Indices, "indices", nil, "indices to copy")
25+
26+
return cmd
27+
}

commands/root.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"github.com/ebuildy/elastic-copy/pkg/cli"
6+
"github.com/sirupsen/logrus"
7+
"github.com/spf13/cobra"
8+
"io"
9+
)
10+
11+
var (
12+
settings = cli.New()
13+
)
14+
15+
func NewRootCmd(out io.Writer, args []string) *cobra.Command {
16+
cmd := &cobra.Command{
17+
Use: "elasticcopy",
18+
Short: "Copy data from & to elasticsearch",
19+
SilenceUsage: true,
20+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
21+
l, err := logrus.ParseLevel(settings.LogLevel)
22+
23+
if err != nil {
24+
fmt.Printf("log level \"%s\" is invalid!", settings.LogLevel)
25+
l = logrus.InfoLevel
26+
}
27+
28+
logrus.SetLevel(l)
29+
},
30+
}
31+
32+
cmd.AddCommand(
33+
newRunCmd(),
34+
newSamplerCmd(),
35+
newCountCmd(),
36+
)
37+
38+
flags := cmd.PersistentFlags()
39+
40+
flags.ParseErrorsWhitelist.UnknownFlags = true
41+
flags.Parse(args)
42+
43+
return cmd
44+
}

commands/run.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package commands
2+
3+
import (
4+
"github.com/ebuildy/elastic-copy/pkg/action"
5+
"github.com/ebuildy/elastic-copy/pkg/utils"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
func newRunCmd() *cobra.Command {
10+
action := action.NewRunAction(settings)
11+
12+
cmd := &cobra.Command{
13+
Use: "run",
14+
Aliases: []string{"gen"},
15+
RunE: func(cmd *cobra.Command, args []string) error {
16+
action.Run()
17+
18+
return nil
19+
},
20+
}
21+
22+
flags := cmd.PersistentFlags()
23+
24+
flags.StringVar(&action.Source, "source", "http://localhost:9200", "source URL")
25+
flags.StringVar(&action.Target, "target", utils.TARGET_STDOUT, "target URL")
26+
flags.StringVar(&action.Query, "query", "", "")
27+
flags.StringArrayVar(&action.Indices, "indices", nil, "indices to copy")
28+
flags.Uint64Var(&action.Count, "count", 0, "0 => all, X => count")
29+
flags.IntVar(&action.ReadBatchSize, "read_batch", 100, "how many documents to read in one scroll")
30+
flags.IntVar(&action.WriteBatchSize, "write_batch", 20, "how many documents to send to writer in one batch")
31+
flags.IntVar(&action.Threads, "threads", 5, "Number of threads in pool")
32+
33+
return cmd
34+
}

commands/sampler.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package commands
2+
3+
import (
4+
"github.com/ebuildy/elastic-copy/pkg/action"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
func newSamplerCmd() *cobra.Command {
9+
action := action.NewSampleAction(settings)
10+
11+
cmd := &cobra.Command{
12+
Use: "sample",
13+
RunE: func(cmd *cobra.Command, args []string) error {
14+
action.Run()
15+
16+
return nil
17+
},
18+
}
19+
20+
flags := cmd.PersistentFlags()
21+
22+
flags.StringVar(&action.Target, "target", "http://localhost:9200", "target URL")
23+
flags.StringVar(&action.Index, "index", "", "")
24+
flags.IntVar(&action.Count, "count", 100, "")
25+
26+
return cmd
27+
}

docker-compose.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: "3"
2+
3+
services:
4+
es2:
5+
image: elasticsearch:2.4
6+
environment:
7+
- discovery.type=single-node
8+
- bootstrap.memory_lock=true
9+
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
10+
ports:
11+
- 9200:9200
12+
13+
es7:
14+
image: docker.elastic.co/elasticsearch/elasticsearch-oss:7.4.2
15+
environment:
16+
- discovery.type=single-node
17+
- bootstrap.memory_lock=true
18+
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
19+
ports:
20+
- 9201:9200

go.mod

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module github.com/ebuildy/elastic-copy
2+
3+
go 1.13
4+
5+
require (
6+
github.com/cheggaaa/pb/v3 v3.0.4
7+
github.com/elastic/go-elasticsearch/v7 v7.5.1-0.20200210103642-33004104143c
8+
github.com/panjf2000/ants/v2 v2.3.0
9+
github.com/sirupsen/logrus v1.4.2
10+
github.com/spf13/cobra v0.0.5
11+
github.com/spf13/pflag v1.0.3
12+
github.com/stretchr/testify v1.4.0
13+
github.com/tidwall/gjson v1.5.0
14+
github.com/tidwall/pretty v1.0.1 // indirect
15+
)

0 commit comments

Comments
 (0)