forked from elastic/elasticsearch-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
201 lines (168 loc) · 7.76 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
SHELL=/bin/bash
ELASTIC_REGISTRY ?= docker.elastic.co
export PATH := ./bin:./venv/bin:$(PATH)
# Determine the version to build. Override by setting ELASTIC_VERSION env var.
ELASTIC_VERSION := $(shell ./bin/elastic-version)
ifdef STAGING_BUILD_NUM
VERSION_TAG := $(ELASTIC_VERSION)-$(STAGING_BUILD_NUM)
else
VERSION_TAG := $(ELASTIC_VERSION)
endif
# Build different images tagged as :version-<flavor>
IMAGE_FLAVORS ?= oss basic platinum
# Which image flavor will additionally receive the plain `:version` tag
DEFAULT_IMAGE_FLAVOR ?= basic
IMAGE_TAG ?= $(ELASTIC_REGISTRY)/elasticsearch/elasticsearch
# When invoking docker-compose, use an extra config fragment to map Elasticsearch's
# listening port to the docker host.
# For the x-pack security enabled image (platinum), use the fragment we utilize for tests.
ifeq ($(DEFAULT_IMAGE_FLAVOR),platinum)
DOCKER_COMPOSE := docker-compose \
-f docker-compose-$(DEFAULT_IMAGE_FLAVOR).yml \
-f tests/docker-compose-$(DEFAULT_IMAGE_FLAVOR).yml
else
DOCKER_COMPOSE := docker-compose \
-f docker-compose-$(DEFAULT_IMAGE_FLAVOR).yml \
-f docker-compose.hostports.yml
endif
.PHONY: all dockerfile docker-compose test test-build lint clean pristine run run-single run-cluster build release-manager release-manager-snapshot push
# Default target, build *and* run tests
all: build test
# Test specified versions without building
test: lint docker-compose
$(foreach FLAVOR, $(IMAGE_FLAVORS), \
pyfiglet -w 160 -f puffy "test: $(FLAVOR) single"; \
./bin/pytest --image-flavor=$(FLAVOR) --single-node tests; \
pyfiglet -w 160 -f puffy "test: $(FLAVOR) multi"; \
./bin/pytest --image-flavor=$(FLAVOR) tests; \
)
# Build and test
test-build: lint build docker-compose
lint: venv
flake8 tests
clean:
$(foreach FLAVOR, $(IMAGE_FLAVORS), \
if [[ -f "docker-compose-$(FLAVOR).yml" ]]; then \
docker-compose -f docker-compose-$(FLAVOR).yml down && docker-compose -f docker-compose-$(FLAVOR).yml rm -f -v; \
fi; \
rm -f docker-compose-$(FLAVOR).yml; \
rm -f tests/docker-compose-$(FLAVOR).yml; \
rm -f build/elasticsearch/Dockerfile-$(FLAVOR); \
)
pristine: clean
-$(foreach FLAVOR, $(IMAGE_FLAVORS), \
docker rmi -f $(IMAGE_TAG)-$(FLAVOR):$(VERSION_TAG); \
)
-docker rmi -f $(IMAGE_TAG):$(VERSION_TAG)
# Give us an easy way to start the DEFAULT_IMAGE_FLAVOR
run: run-single
run-single: build docker-compose
$(DOCKER_COMPOSE) up elasticsearch1
run-cluster: build docker-compose
$(DOCKER_COMPOSE) up elasticsearch1 elasticsearch2
# Build docker image: "elasticsearch-$(FLAVOR):$(VERSION_TAG)"
build: clean dockerfile
$(foreach FLAVOR, $(IMAGE_FLAVORS), \
pyfiglet -f puffy -w 160 "Building: $(FLAVOR)"; \
docker build -t $(IMAGE_TAG)-$(FLAVOR):$(VERSION_TAG) -f build/elasticsearch/Dockerfile-$(FLAVOR) build/elasticsearch; \
if [[ $(FLAVOR) == $(DEFAULT_IMAGE_FLAVOR) ]]; then \
docker tag $(IMAGE_TAG)-$(FLAVOR):$(VERSION_TAG) $(IMAGE_TAG):$(VERSION_TAG); \
fi; \
)
release-manager-snapshot: clean
ARTIFACTS_DIR=$(ARTIFACTS_DIR) ELASTIC_VERSION=$(ELASTIC_VERSION)-SNAPSHOT make build-from-local-artifacts
release-manager-release: clean
ARTIFACTS_DIR=$(ARTIFACTS_DIR) ELASTIC_VERSION=$(ELASTIC_VERSION) make build-from-local-artifacts
# Build from artifacts on the local filesystem, using an http server (running
# in a container) to provide the artifacts to the Dockerfile.
build-from-local-artifacts: venv dockerfile docker-compose
docker run --rm -d --name=elasticsearch-docker-artifact-server \
--network=host -v $(ARTIFACTS_DIR):/mnt \
python:3 bash -c 'cd /mnt && python3 -m http.server'
timeout 120 bash -c 'until curl -s localhost:8000 > /dev/null; do sleep 1; done'
-$(foreach FLAVOR, $(IMAGE_FLAVORS), \
pyfiglet -f puffy -w 160 "Building: $(FLAVOR)"; \
docker build --network=host -t $(IMAGE_TAG)-$(FLAVOR):$(VERSION_TAG) -f build/elasticsearch/Dockerfile-$(FLAVOR) build/elasticsearch || \
(docker kill elasticsearch-docker-artifact-server; false); \
if [[ $(FLAVOR) == $(DEFAULT_IMAGE_FLAVOR) ]]; then \
docker tag $(IMAGE_TAG)-$(FLAVOR):$(VERSION_TAG) $(IMAGE_TAG):$(VERSION_TAG); \
fi; \
)
docker kill elasticsearch-docker-artifact-server
# Build images from the latest snapshots on snapshots.elastic.co
from-snapshot:
rm -rf snapshots
mkdir -p snapshots/elasticsearch/distribution/archives/tar/build/distributions
(cd snapshots/elasticsearch/distribution/archives/tar/build/distributions && \
wget https://snapshots.elastic.co/downloads/elasticsearch/elasticsearch-$(ELASTIC_VERSION)-SNAPSHOT.tar.gz)
mkdir -p snapshots/x-pack-elasticsearch/plugin/build/distributions
(cd snapshots/x-pack-elasticsearch/plugin/build/distributions && \
wget https://snapshots.elastic.co/downloads/elasticsearch-plugins/x-pack/x-pack-$(ELASTIC_VERSION)-SNAPSHOT.zip)
for plugin in ingest-user-agent ingest-geoip; do \
mkdir -p snapshots/elasticsearch/plugins/$$plugin/build/distributions; \
(cd snapshots/elasticsearch/plugins/$$plugin/build/distributions && \
wget https://snapshots.elastic.co/downloads/elasticsearch-plugins/$$plugin/$$plugin-$(ELASTIC_VERSION)-SNAPSHOT.zip); \
done
ARTIFACTS_DIR=$$PWD/snapshots make release-manager-snapshot
# Push the images to the dedicated push endpoint at "push.docker.elastic.co"
push: test
$(foreach FLAVOR, $(IMAGE_FLAVORS), \
docker tag $(IMAGE_TAG)-$(FLAVOR):$(VERSION_TAG) push.$(IMAGE_TAG)-$(FLAVOR):$(VERSION_TAG); \
echo; echo "Pushing $(IMAGE_TAG)-$(FLAVOR):$(VERSION_TAG)"; echo; \
docker push push.$(IMAGE_TAG)-$(FLAVOR):$(VERSION_TAG); \
docker rmi push.$(IMAGE_TAG)-$(FLAVOR):$(VERSION_TAG); \
)
# Also push the plain named image based on DEFAULT_IMAGE_FLAVOR
# e.g. elasticsearch-basic:6.0.0 and elasticsearch:6.0.0 are the same.
@if [[ -z "$$(docker images -q $(IMAGE_TAG):$(VERSION_TAG))" ]]; then\
echo;\
echo "I can't push $(IMAGE_TAG):$(VERSION_TAG)";\
echo "probably because you didn't build the \"$(DEFAULT_IMAGE_FLAVOR)\" image (check your \$$IMAGE_FLAVORS).";\
echo;\
echo "Failing here.";\
echo;\
exit 1;\
fi
docker tag $(IMAGE_TAG):$(VERSION_TAG) push.$(IMAGE_TAG):$(VERSION_TAG)
echo; echo "Pushing $(IMAGE_TAG):$(VERSION_TAG)"; echo;
docker push push.$(IMAGE_TAG):$(VERSION_TAG)
docker rmi push.$(IMAGE_TAG):$(VERSION_TAG)
# The tests are written in Python. Make a virtualenv to handle the dependencies.
venv: requirements.txt
@if [ -z $$PYTHON3 ]; then\
PY3_MINOR_VER=`python3 --version 2>&1 | cut -d " " -f 2 | cut -d "." -f 2`;\
if (( $$PY3_MINOR_VER < 5 )); then\
echo "Couldn't find python3 in \$PATH that is >=3.5";\
echo "Please install python3.5 or later or explicity define the python3 executable name with \$PYTHON3";\
echo "Exiting here";\
exit 1;\
else\
export PYTHON3="python3.$$PY3_MINOR_VER";\
fi;\
fi;\
test -d venv || virtualenv --python=$$PYTHON3 venv;\
pip install -r requirements.txt;\
touch venv;\
# Generate the Dockerfiles for each image flavor from a Jinja2 template.
dockerfile: venv templates/Dockerfile.j2
$(foreach FLAVOR, $(IMAGE_FLAVORS), \
jinja2 \
-D elastic_version='$(ELASTIC_VERSION)' \
-D staging_build_num='$(STAGING_BUILD_NUM)' \
-D artifacts_dir='$(ARTIFACTS_DIR)' \
-D image_flavor='$(FLAVOR)' \
templates/Dockerfile.j2 > build/elasticsearch/Dockerfile-$(FLAVOR); \
)
# Generate docker-compose and tests/docker-compose fragment files
# for each image flavor from a Jinja2 template.
docker-compose: venv templates/docker-compose.yml.j2 templates/docker-compose-fragment.yml.j2
$(foreach FLAVOR, $(IMAGE_FLAVORS), \
jinja2 \
-D elastic_registry='$(ELASTIC_REGISTRY)' \
-D version_tag='$(VERSION_TAG)' \
-D image_flavor='$(FLAVOR)' \
templates/docker-compose.yml.j2 > docker-compose-$(FLAVOR).yml; \
jinja2 \
-D image_flavor='$(FLAVOR)' \
templates/docker-compose-fragment.yml.j2 > tests/docker-compose-$(FLAVOR).yml; \
)