-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
370 lines (325 loc) · 12.7 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
################################################################################
# Description:
# Executes testing and validation for python code and configuration files
# within a StackStorm pack.
#
# =============================================
#PACK_DIR := $(dir $(realpath $(firstword $(MAKEFILE_LIST))))
ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
PACK_DIR := $(ROOT_DIR)/../
CI_DIR ?= $(ROOT_DIR)
YAML_FILES := $(shell git ls-files '*.yaml' '*.yml')
JSON_FILES := $(shell git ls-files '*.json')
PY_FILES := $(shell git ls-files '*.py')
ROOT_VIRTUALENV ?= ""
### python 2/3 specific stuff
#PYTHON_EXE ?= python3
PYTHON_EXE ?= $(shell if python3.8 --version >/dev/null 2>&1; then echo "python3.8"; else echo "python3"; fi)
PYTHON_VERSION = $(shell $(PYTHON_EXE) --version 2>&1 | awk '{ print $$2 }')
PYTHON_NAME = python$(PYTHON_VERSION)
PYTHON_CI_DIR = $(ROOT_DIR)/$(PYTHON_NAME)
VIRTUALENV_NAME ?= virtualenv
VIRTUALENV_DIR ?= $(PYTHON_CI_DIR)/$(VIRTUALENV_NAME)
ST2_VIRTUALENV_DIR ?= "/tmp/st2-pack-tests-virtualenvs"
ST2_REPO_PATH ?= $(CI_DIR)/st2
ST2_REPO_BRANCH ?= feature/test-coverage-report
LINT_CONFIGS_BRANCH ?= master
LINT_CONFIGS_DIR ?= $(CI_DIR)/lint-configs/
PACK_NAME ?= Caller_needs_to_set_variable_PACK_NAME
export ST2_REPO_PATH ROOT_DIR
# All components are prefixed by st2
COMPONENTS := $(wildcard /tmp/st2/st2*)
.PHONY: all
# don't register right now (requires us to install stackstorm)
#all: requirements lint packs-resource-register packs-tests
all: .pythonvars virtualenv requirements lint packs-tests
# .PHONY: python2
# python2: .python2 .pythonvars all
# .PHONY: python3
# python3: .python3 .pythonvars all
.PHONY: pack-name
pack-name:
@echo $(PACK_NAME)
.PHONY: clean
clean: .pythonvars .clean-st2-repo .clean-st2-lint-repo .clean-virtualenv .clean-pack
.PHONY: lint
lint: .pythonvars virtualenv requirements .clone-st2-lint-repo flake8 pylint configs-check metadata-check
.PHONY: flake8
flake8: .pythonvars virtualenv requirements .clone-st2-lint-repo .flake8
.PHONY: pylint
pylint: .pythonvars virtualenv requirements .clone-st2-repo .clone-st2-lint-repo .pylint
.PHONY: configs-check
configs-check: .pythonvars virtualenv requirements .clone-st2-repo .copy-pack-to-subdirectory .configs-check
.PHONY: metadata-check
metadata-check: .pythonvars virtualenv requirements .metadata-check
# list all makefile targets
.PHONY: list
list:
@$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | xargs
# Task which copies pack to temporary sub-directory so we can use old-style check scripts which
# # require pack to be in a sub-directory
.PHONY: .copy-pack-to-subdirectory
.copy-pack-to-subdirectory:
mkdir -p /tmp/packs/$(PACK_NAME)
cd $(PACK_DIR); find . -name 'ci' -prune -or -name '.git' -or -type f -print | rsync -R --files-from=- ./ /tmp/packs/$(PACK_NAME)
.PHONY: .clean-pack
.clean-pack:
@echo
@echo "==================== cleaning packs ===================="
@echo
@echo "Start Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
rm -rf /tmp/packs/${PACK_DIR}
@echo "End Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
.PHONY: packs-resource-register
packs-resource-register: .pythonvars virtualenv requirements .clone-st2-repo .copy-pack-to-subdirectory .install-mongodb .packs-resource-register
.PHONY: packs-missing-tests
packs-missing-tests: .pythonvars virtualenv requirements .packs-missing-tests
.PHONY: packs-tests
packs-tests: .pythonvars virtualenv requirements .clone-st2-repo .packs-tests
.PHONY: test
test: packs-tests
.PHONY: .flake8
.flake8:
@echo
@echo "==================== flake8 ===================="
@echo
@echo "Start Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
. $(VIRTUALENV_DIR)/bin/activate; \
for py in $(PY_FILES); do \
flake8 --config $(LINT_CONFIGS_DIR)/python/.flake8 $$py || exit 1; \
done
@echo "End Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
.PHONY: .pylint
.pylint:
@echo
@echo "==================== pylint ===================="
@echo
@echo "Start Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
. $(VIRTUALENV_DIR)/bin/activate; \
REQUIREMENTS_DIR=$(CI_DIR)/ CONFIG_DIR=$(LINT_CONFIGS_DIR) ST2_REPO_PATH=${ST2_REPO_PATH} st2-check-pylint-pack $(PACK_DIR) || exit 1;
@echo "End Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
.PHONY: .clone-st2-lint-repo
.clone-st2-lint-repo:
@echo
@echo "==================== cloning st2 lint repo ===================="
@echo
@echo "Start Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
if [ ! -d "$(LINT_CONFIGS_DIR)" ]; then \
git clone https://github.com/StackStorm/lint-configs.git --depth 1 --single-branch --branch $(LINT_CONFIGS_BRANCH) $(LINT_CONFIGS_DIR); \
else \
cd "$(LINT_CONFIGS_DIR)"; \
git pull; \
fi;
@echo "End Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
.PHONY: .clean-st2-lint-repo
.clean-st2-lint-repo:
@echo
@echo "==================== cleaning st2 lint repo ===================="
@echo
@echo "Start Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
rm -rf $(LINT_CONFIGS_DIR)
@echo "End Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
.PHONY: .configs-check
.configs-check:
@echo
@echo "==================== configs-check ===================="
@echo
@echo "Start Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
. $(VIRTUALENV_DIR)/bin/activate; \
for yaml in $(YAML_FILES); do \
st2-check-validate-yaml-file $$yaml || exit 1; \
done
. $(VIRTUALENV_DIR)/bin/activate; \
for json in $(JSON_FILES); do \
st2-check-validate-json-file $$json || exit 1; \
done
@echo "End Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
@echo
@echo "==================== example config check ===================="
@echo
@echo "Start Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
. $(VIRTUALENV_DIR)/bin/activate; \
ST2_REPO_PATH=${ST2_REPO_PATH} st2-check-validate-pack-example-config /tmp/packs/$(PACK_NAME) || exit 1;
@echo "End Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
.PHONY: .metadata-check
.metadata-check:
@echo
@echo "==================== metadata-check ===================="
@echo
@echo "Start Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
. $(VIRTUALENV_DIR)/bin/activate; \
ST2_REPO_PATH=${ST2_REPO_PATH} st2-check-validate-pack-metadata-exists $(PACK_DIR) || exit 1;
@echo "End Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
.PHONY: .install-mongodb
.install-monogodb:
# @todo
# install_mongodb() {
# ST2_MONGODB_PASSWORD=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 24 ; echo '')
# # Add key and repo for the latest stable MongoDB (3.2)
# sudo rpm --import https://www.mongodb.org/static/pgp/server-3.2.asc
# sudo sh -c "cat <<EOT > /etc/yum.repos.d/mongodb-org-3.2.repo
# [mongodb-org-3.2]
# name=MongoDB Repository
# baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.2/x86_64/
# gpgcheck=1
# enabled=1
# gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
# EOT"
# sudo yum -y install mongodb-org
# # Configure MongoDB to listen on localhost only
# sudo sed -i -e "s#bindIp:.*#bindIp: 127.0.0.1#g" /etc/mongod.conf
# sudo systemctl start mongod
# sudo systemctl enable mongod
# sleep 5
# # Create admin user and user used by StackStorm (MongoDB needs to be running)
# mongo <<EOF
# use admin;
# db.createUser({
# user: "admin",
# pwd: "${ST2_MONGODB_PASSWORD}",
# roles: [
# { role: "userAdminAnyDatabase", db: "admin" }
# ]
# });
# quit();
# EOF
# mongo <<EOF
# use st2;
# db.createUser({
# user: "stackstorm",
# pwd: "${ST2_MONGODB_PASSWORD}",
# roles: [
# { role: "readWrite", db: "st2" }
# ]
# });
# quit();
# EOF
# # Require authentication to be able to acccess the database
# sudo sh -c 'echo -e "security:\n authorization: enabled" >> /etc/mongod.conf'
# # MongoDB needs to be restarted after enabling auth
# sudo systemctl restart mongod
# }
.PHONY: .packs-resource-register
.packs-resource-register:
@echo
@echo "==================== packs-resource-register ===================="
@echo
@echo "Start Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
. $(VIRTUALENV_DIR)/bin/activate; \
ST2_CONFIG_FILE=$(CI_DIR)/st2.tests.conf ST2_REPO_PATH=${ST2_REPO_PATH} st2-check-register-pack-resources /tmp/packs/$(PACK_NAME) || exit 1;
@echo "End Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
# Python 2 has different requirements for tests being that some of the newly updated modules
# only support python 3. There is a different requirements file to be used for python 2 but
# this is hardcoded in the bin file. So we can move the file around if using python 2 otherwise
# use the default module.
.PHONY: .packs-tests
.packs-tests:
@echo
@echo "==================== packs-tests ===================="
@echo
@echo "Start Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
. $(VIRTUALENV_DIR)/bin/activate; \
if [ ! -f "$(CI_DIR)/st2-requirements-installed.txt" ]; then \
$(VIRTUALENV_DIR)/bin/pip install --cache-dir $(HOME)/.pip-cache -q -r $(ST2_REPO_PATH)/requirements.txt; \
$(VIRTUALENV_DIR)/bin/pip install --cache-dir $(HOME)/.pip-cache -q -r $(PACK_DIR)requirements.txt; \
$(VIRTUALENV_DIR)/bin/pip install --cache-dir $(HOME)/.pip-cache -q -r $(PACK_DIR)requirements-tests.txt; \
touch $(CI_DIR)/st2-requirements-installed.txt; \
fi; \
ST2_REPO_PATH=${ST2_REPO_PATH} $(ST2_REPO_PATH)/st2common/bin/st2-run-pack-tests -c -t -x -j -p $(PACK_DIR) || exit 1;
@echo "End Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
.PHONY: .packs-missing-tests
.packs-missing-tests:
@echo
@echo "==================== pack-missing-tests ===================="
@echo
@echo "Start Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
. $(VIRTUALENV_DIR)/bin/activate; \
st2-check-print-pack-tests-coverage $(PACK_DIR) || exit 1;
@echo "End Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
.PHONY: .clone-st2-repo
.clone-st2-repo:
@echo
@echo "==================== cloning st2 repo ===================="
@echo
@echo "Start Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
if [ -d "/tmp/st2" ]; then \
cp -r "/tmp/st2" "$(ST2_REPO_PATH)"; \
fi; \
if [ ! -d "$(ST2_REPO_PATH)" ]; then \
git clone https://github.com/EncoreTechnologies/st2.git --depth 1 --single-branch --branch $(ST2_REPO_BRANCH) $(ST2_REPO_PATH); \
else \
cd "$(ST2_REPO_PATH)"; \
git pull; \
fi;
@echo "End Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
.PHONY: .clean-st2-repo
.clean-st2-repo:
@echo
@echo "==================== cleaning st2 repo ===================="
@echo
@echo "Start Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
rm -rf $(ST2_REPO_PATH)
@echo "End Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
.PHONY: requirements
requirements:
@echo
@echo "==================== requirements ===================="
@echo
@echo "Start Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
if [ ! -f "$(CI_DIR)/requirements-installed.txt" ]; then \
. $(VIRTUALENV_DIR)/bin/activate; \
$(VIRTUALENV_DIR)/bin/pip install --cache-dir $(HOME)/.pip-cache --upgrade "pip"; \
$(VIRTUALENV_DIR)/bin/pip install --cache-dir $(HOME)/.pip-cache -q -r $(CI_DIR)/requirements-dev.txt; \
$(VIRTUALENV_DIR)/bin/pip install --cache-dir $(HOME)/.pip-cache -q -r $(CI_DIR)/requirements-pack-tests.txt; \
touch $(CI_DIR)/requirements-installed.txt; \
fi;
@echo "End Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
.PHONY: virtualenv
virtualenv:
@echo
@echo "==================== virtualenv ===================="
@echo
@echo "Start Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
if [ ! -d "$(VIRTUALENV_DIR)" ]; then \
if [ -d "$(ROOT_VIRTUALENV)" ]; then \
$(ROOT_DIR)/bin/clonevirtualenv.py $(ROOT_VIRTUALENV) $(VIRTUALENV_DIR);\
else \
if python3.8 --version >/dev/null 2>&1; then \
python3.8 -m venv $(VIRTUALENV_DIR); \
else \
python3 -m venv $(VIRTUALENV_DIR); \
fi; \
fi; \
fi;
@echo "End Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
.PHONY: .clean-virtualenv
.clean-virtualenv:
@echo
@echo "==================== cleaning virtualenv ===================="
@echo
@echo "Start Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
rm -rf $(VIRTUALENV_DIR)
rm -rf $(CI_DIR)/python*
@echo "End Time = $(shell date '+%Y-%m-%dT%H:%M:%S.%N')"
# # setup python3 executable
# .PHONY: .python3
# .python3:
# @echo
# @echo "==================== python3 ===================="
# @echo
# $(eval PYTHON_EXE=python3)
# @echo "PYTHON_EXE=$(PYTHON_EXE)"
# initialize PYTHON_EXE dependent variables
.PHONY: .pythonvars
.pythonvars:
@echo
@echo "==================== pythonvars ===================="
@echo
$(eval PYTHON_VERSION=$(shell $(PYTHON_EXE) --version 2>&1 | awk '{ print $$2 }'))
$(eval PYTHON_NAME=python$(PYTHON_VERSION))
$(eval PYTHON_CI_DIR=$(ROOT_DIR)/$(PYTHON_NAME))
$(eval VIRTUALENV_DIR=$(PYTHON_CI_DIR)/$(VIRTUALENV_NAME))
@echo "PYTHON_VERSION=$(PYTHON_VERSION)"
@echo "PYTHON_NAME=$(PYTHON_NAME)"
@echo "PYTHON_CI_DIR=$(PYTHON_CI_DIR)"
@echo "VIRTUALENV_DIR=$(VIRTUALENV_DIR)"