-
Notifications
You must be signed in to change notification settings - Fork 35
/
Makefile
297 lines (236 loc) · 8.47 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
SHELL := /bin/sh
pkg_name := yeti-web
user := yeti-web
app_dir := /opt/$(user)
version_file := version.yml
app_files := bin \
app \
.bundle \
config \
config.ru \
db \
doc \
Gemfile \
Gemfile.lock \
lib \
public \
Rakefile \
vendor \
pgq-processors \
$(version_file) \
vendor/rbenv \
.ruby-version
exclude_files := config/database.yml \
config/yeti_web.yml \
config/secrets.yml \
*.o \
*.a
version = $(shell ./ci/gen_version.sh)
debian_version = $(shell echo $(version) | sed 's/_/~/' | sed 's/-master/~master/' | sed 's/-rc/~rc/')-1
commit = $(shell git rev-parse HEAD)
debian_host_release != lsb_release -sc
export DEBFULLNAME ?= YETI team
export DEBEMAIL ?= [email protected]
gems := $(CURDIR)/vendor/bundler
bundle_bin := $(gems)/bin/bundle
bundler_gems := $(CURDIR)/vendor/bundle
export GEM_PATH := $(gems):$(bundler_gems)
# must match final destination in debian package
export RBENV_ROOT := $(app_dir)/vendor/rbenv
export PATH := $(RBENV_ROOT)/shims:$(PATH)
rbenv_version = $(file < .ruby-version)
export no_proxy ?= 127.0.0.1,localhost
pgq_drop_roles := DROP ROLE IF EXISTS pgq_reader; \
DROP ROLE IF EXISTS pgq_writer; \
DROP ROLE IF EXISTS pgq_admin;
pgq_create_roles := CREATE ROLE pgq_reader; \
CREATE ROLE pgq_writer; \
CREATE ROLE pgq_admin in role pgq_reader,pgq_writer;
export YETI_DB_HOST ?= db
define info
@printf '\n\e[33m> msg \e[0m\n\n'
endef
###### Rules ######
.PHONY: all
all: docs assets pgq-processors-gems
debian/changelog:
$(info:msg=Generating changelog)
dch \
--create \
--package "$(pkg_name)" \
--newversion "$(debian_version)" \
--distribution "$(debian_host_release)" \
"Release $(version), commit: $(commit)"
version.yml: debian/changelog
$(info:msg=Create version file (version: $(version), commit: $(commit)))
@echo "version: $(version)" > $@
@echo "commit: $(commit)" >> $@
config/database.yml:
$(info:msg=Creating database.yml for build/tests)
cp config/database.build.yml config/database.yml
config/click_house.yml:
$(info:msg=Creating click_house.yml for tests)
cp config/click_house.yml.distr config/click_house.yml
config/secrets.yml:
$(info:msg=Creating secrets.yml for tests)
cp config/secrets.yml.distr config/secrets.yml
config/yeti_web.yml:
$(info:msg=Creating yeti_web.yml for build/tests)
@# explicitly raise error during tests if policy is misconfigured
sed -E 's/( +when_no_(config|policy_class): *)(.*)/\1raise/' \
config/yeti_web.yml.ci > config/yeti_web.yml
config/policy_roles.yml:
$(info:msg=Creating policy_roles.yml for build/tests)
cp config/policy_roles.yml.distr config/policy_roles.yml
$(RBENV_ROOT)/versions/$(rbenv_version):
$(info:msg=Installing ruby $(rbenv_version) into $(RBENV_ROOT))
mkdir -pv "$(RBENV_ROOT)"
curl -sSL "https://raw.githubusercontent.com/rbenv/ruby-build/master/share/ruby-build/$(rbenv_version)" \
> "debian/$(rbenv_version)"
rbenv install "debian/$(rbenv_version)"
rm "debian/$(rbenv_version)"
.PHONY: ruby
ruby: $(RBENV_ROOT)/versions/$(rbenv_version)
.PHONY: bundler
bundler: ruby
$(info:msg=Install bundler)
gem install --no-document --install-dir $(gems) bundler
$(bundle_bin) config --local clean 'true'
$(bundle_bin) config --local jobs 4
$(bundle_bin) config --local deployment 'true'
.PHONY: gems
gems: bundler
$(info:msg=Install/Update gems)
$(bundle_bin) config --local --delete with
$(bundle_bin) config --local --delete without
$(bundle_bin) config --local without 'development test'
$(bundle_bin) install
.PHONY: gems-test
gems-test: bundler
$(info:msg=Install/Update gems for tests)
$(bundle_bin) config --local --delete with
$(bundle_bin) config --local --delete without
$(bundle_bin) config --local with 'development test'
$(bundle_bin) install
$(bundle_bin) binstubs rspec-core
.PHONY: docs
docs: gems-test config/database.yml config/yeti_web.yml config/policy_roles.yml config/secrets.yml
$(info:msg=Preparing test database for docs generation)
RAILS_ENV=test $(bundle_bin) exec rake \
db:drop \
db:create \
db:schema:load \
db:migrate \
db:seed
RAILS_ENV=test $(bundle_bin) exec rake custom_seeds[network_prefixes]
$(info:msg=Generating documentation)
RAILS_ENV=test $(bundle_bin) exec rake docs:generate
RAILS_ENV=test $(bundle_bin) exec rake db:drop
.PHONY: assets
assets: gems config/database.yml config/yeti_web.yml config/policy_roles.yml config/secrets.yml
$(info:msg=Precompile assets)
RAILS_ENV=production RAILS_COMPILE_ASSETS=true $(bundle_bin) exec rake assets:precompile
.PHONY: pgq-processors-gems
pgq-processors-gems:
$(info:msg=call pgq-processors processing)
$(MAKE) -C pgq-processors
.PHONY: swagger
swagger: debian/changelog
$(info:msg=call swagger processing)
$(MAKE) -C swagger version=${version}
.PHONY: prepare-test-db
prepare-test-db: gems-test config/database.yml config/yeti_web.yml config/policy_roles.yml
$(info:msg=Preparing test database)
RAILS_ENV=test $(bundle_bin) exec rake parallel:drop
@# avoid race condition when createing pgq roles in parallel with
@# parallel:spec
@# https://github.com/pgq/pgq/blob/master/functions/pgq.upgrade_schema.sql
psql -h "$(YETI_DB_HOST)" -U postgres -c '$(pgq_drop_roles)'
psql -h "$(YETI_DB_HOST)" -U postgres -c '$(pgq_create_roles)'
RAILS_ENV=test $(bundle_bin) exec rake parallel:create
RAILS_ENV=test $(bundle_bin) exec rake parallel:load_schema
RAILS_ENV=test $(bundle_bin) exec rake parallel:rake[db:seed]
RAILS_ENV=test $(bundle_bin) exec rake parallel:rake[custom_seeds[network_prefixes]]
.PHONY: test
test: test-pgq-processors lint brakeman rspec
.PHONY: rspec
rspec: gems-test config/database.yml config/yeti_web.yml config/policy_roles.yml prepare-test-db config/click_house.yml config/secrets.yml
ifdef spec
$(info:msg=Testing spec $(spec))
RAILS_ENV=test $(bundle_bin) exec rspec "$(spec)"
else
$(info:msg=Running rspec tests)
RAILS_ENV=test $(bundle_bin) exec parallel_test \
spec/ \
--type rspec \
$(if $(TEST_GROUP),--only-group $(TEST_GROUP),) \
&& script/format_runtime_log log/parallel_runtime_rspec.log \
|| { script/format_runtime_log log/parallel_runtime_rspec.log; false; }
endif
.PHONY: rspec
database_consistency: gems-test config/database.yml config/yeti_web.yml config/policy_roles.yml config/secrets.yml prepare-test-db
$(info:msg=Check the consistency of the database constraints with the application validations)
RAILS_ENV=test $(bundle_bin) exec database_consistency
.PHONY: lint
lint: gems-test config/database.yml config/yeti_web.yml config/secrets.yml
$(info:msg=Running rubocop and bundle audit)
RAILS_ENV=test $(bundle_bin) exec rubocop -P
RAILS_ENV=test $(bundle_bin) exec rake bundle:audit
.PHONY: brakeman
brakeman: gems-test config/database.yml config/yeti_web.yml config/secrets.yml
$(info:msg=Running brakeman)
RAILS_ENV=test $(bundle_bin) exec brakeman
.PHONY: coverage_report
coverage_report: gems-test
$(info:msg=Generate coverage report)
$(bundle_bin) exec rake coverage:report
.PHONY: test-pgq-processors
test-pgq-processors: config/database.yml config/yeti_web.yml config/policy_roles.yml config/secrets.yml
$(info:msg=Preparing test database for pgq-processors)
@# PGQ_PROCESSORS_TEST is used in database_build.yml to setup separate db
@# to not interfere with main test suite when running make tasks in parallel
RAILS_ENV=test PGQ_PROCESSORS_TEST=true $(bundle_bin) exec rake \
db:drop \
db:create \
db:schema:load \
db:migrate \
db:seed
$(info:msg=Run pgq-processors tests)
$(MAKE) -C pgq-processors test
RAILS_ENV=test PGQ_PROCESSORS_TEST=true $(bundle_bin) exec rake db:drop
vendor/rbenv:
cp -r "$(RBENV_ROOT)" vendor/
.PHONY: install
install: $(app_files)
$(info:msg=install app files)
@mkdir -p $(DESTDIR)$(app_dir)
tar -c --no-auto-compress $(addprefix --exclude , $(exclude_files)) $^ | tar -x -C $(DESTDIR)$(app_dir)
@mkdir -v -p $(addprefix $(DESTDIR)$(app_dir)/, log tmp )
@install -v -m0644 -D debian/$(pkg_name).rsyslog $(DESTDIR)/etc/rsyslog.d/$(pkg_name).conf
@install -v -m0644 -d $(DESTDIR)/var/log/yeti
.PHONY: clean
clean:
$(info:msg=Cleaning)
$(MAKE) -C swagger clean
$(MAKE) -C pgq-processors clean
rm -rf public/assets \
.bundle \
doc/api
rm -fv config/database.yml \
config/yeti_web.yml \
config/policy_roles.yml \
config/secrets.yml
rm -fv bin/rspec
.PHONY: clean-all
clean-all:
$(info:msg=Cleaning everything)
-@debian/rules clean
rm -rf $(gems)
rm -rf $(bundler_gems)
rm -rf .bundle
rm -f $(version_file)
rm -f debian/changelog
.PHONY: package
package: debian/changelog
$(info:msg=Building package)
dpkg-buildpackage -uc -us -b