-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
370 lines (264 loc) · 10.4 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
ENV ?= dev
TEMP_ENV_FILE := $(shell mktemp -t ".env.XXXXXX")
$(foreach env, default ${ENV}, $(eval _ := $(shell \
find env/${env} -type f -name "*.env" -follow \
| xargs awk 1 \
| grep -ve "^\#" \
>> ${TEMP_ENV_FILE}) \
))
$(eval _ := $(shell \
test -f .env && \
cat .env \
| grep -ve "^\#" \
>> ${TEMP_ENV_FILE} \
))
-include ${TEMP_ENV_FILE}
ENV_KEYS := $(shell ( \
cat ${TEMP_ENV_FILE} \
| sed -E 's/^([A-Za-z][A-Za-z0-9_-]*)=(.*)/\1/' \
| sort -u \
))
export ${ENV_KEYS}
REMOVE_TEMP_ENV_FILE := $(shell unlink ${TEMP_ENV_FILE})
DOCKER_USER ?= $(shell id -u)
DOCKER_COMPOSE_COMMAND ?= docker-compose
DOCKER_COMPOSE = ${DOCKER_COMPOSE_COMMAND} $(1)
DOCKER_COMPOSE_RUN = ${DOCKER_COMPOSE_COMMAND} run --user ${DOCKER_USER} $(1)
DOCKER_COMPOSE_EXEC = ${DOCKER_COMPOSE_COMMAND} exec --user ${DOCKER_USER} $(1)
DOCKER_COMMAND ?= docker
DOCKER = ${DOCKER_COMMAND} $(1)
CONFIRM = (\
printf "$(1) [y/N]" 1>&2; printf " " 1>&2; \
read answer && ( \
test "$${answer:-N}" = "y" || \
test "$${answer:-N}" = "Y") \
)
### State management
up:
@$(call DOCKER_COMPOSE, up --detach ${MAKE_APP_SERVICES})
start:
@$(call DOCKER_COMPOSE, start ${MAKE_APP_SERVICES})
stop: down
down:
@$(call DOCKER_COMPOSE, stop ${MAKE_APP_SERVICES})
restart:
@$(call DOCKER_COMPOSE, restart)
rm:
@$(call DOCKER_COMPOSE, rm --force -v)
build:
@$(call DOCKER_COMPOSE, build ${MAKE_APP_SERVICES})
create:
@$(call DOCKER_COMPOSE, up --no-start ${MAKE_APP_SERVICES})
logs:
@$(call DOCKER_COMPOSE, logs --follow)
$(addprefix build-, ${MAKE_SERVICES}): build-%:
@$(call DOCKER_COMPOSE, build $*)
$(addprefix create-, ${MAKE_SERVICES}): create-%:
@$(call DOCKER_COMPOSE, up --no-start $*)
$(addprefix start-, ${MAKE_SERVICES}): start-%:
@$(call DOCKER_COMPOSE, up -d $*)
$(addprefix stop-, ${MAKE_SERVICES}): stop-%:
@$(call DOCKER_COMPOSE, stop $*)
$(addprefix kill-, ${MAKE_SERVICES}): kill-%:
@$(call DOCKER_COMPOSE, kill $*)
$(addprefix restart-, ${MAKE_SERVICES}): restart-%:
@$(call DOCKER_COMPOSE, restart $*)
$(addprefix rm-, ${MAKE_SERVICES}): rm-%:
@$(call DOCKER_COMPOSE, rm --force -v $*)
$(addprefix fuck-, ${MAKE_SERVICES}): fuck-%: \
stop-% rm-% build-% create-% start-%
$(addsuffix -sh, ${MAKE_SERVICES}): %-sh:
@$(call DOCKER_COMPOSE, exec $* sh)
$(addsuffix -logs, ${MAKE_SERVICES}): %-logs:
@$(call DOCKER_COMPOSE, logs --follow $*)
setup: db-migrate
@make db-import < ./db/tokens.sql
fuck: stop rm build create start
off: up logs
### Tests
test-front:
@${DOCKER_COMPOSE_COMMAND} run --rm ${DOCKER_FRONT_SERVICE} test
test-api:
@$(call DOCKER_COMPOSE_RUN,--rm ${DOCKER_API_SERVICE} test)
test-api-e2e:
@$(call DOCKER_COMPOSE_RUN,--rm ${DOCKER_API_SERVICE} test:e2e)
test: test-front test-api test-contracts
### Database
db-client:
@$(call DOCKER_COMPOSE_EXEC, \
${DOCKER_DB_SERVICE} mysql \
--user=${MYSQL_USER} \
--password=${MYSQL_PASSWORD} \
--default-character-set=utf8mb4 \
${MYSQL_DATABASE} \
)
db-import:
@$(call DOCKER_COMPOSE_EXEC, \
-T ${DOCKER_DB_SERVICE} mysql \
--host=${MYSQL_HOST} \
--user=${MYSQL_USER} \
--password=${MYSQL_PASSWORD} \
--default-character-set=utf8mb4 \
${MYSQL_DATABASE} \
)
db-upgrade: db-migrate
db-migrate: db-ormconfig
@$(call DOCKER_COMPOSE_RUN, --rm ${DOCKER_API_SERVICE} migration:run)
db-rollback: db-ormconfig
@$(call DOCKER_COMPOSE_RUN, --rm ${DOCKER_API_SERVICE} migration:revert)
db-generate-migration: db-ormconfig
@$(call DOCKER_COMPOSE_RUN, --rm ${DOCKER_API_SERVICE} migration:generate)
db-ormconfig:
@$(call DOCKER_COMPOSE_RUN, --rm ${DOCKER_API_SERVICE} ormconfig)
db-add-tokens:
@$(call DOCKER_COMPOSE_RUN, --rm ${DOCKER_API_SERVICE} add-tokens)
### Contracts
CONTRACTS = $(call DOCKER_COMPOSE_RUN,--rm ${DOCKER_CONTRACTS_SERVICE} $(1))
build-contracts:
@$(call CONTRACTS, build)
test-contracts:
@$(call CONTRACTS, test)
## Live chain
CONTRACTS_LIVE_RUN = $(call CONTRACTS,$(1) --network $(2) --chain $(2))
# : deploy
$(addprefix deploy-all-, ${ENABLED_NETWORKS}): deploy-all-%:
@$(call CONFIRM,Deploy all?)
@$(call CONTRACTS_LIVE_RUN,deploy-all,$*)
$(addprefix deploy-facet-, ${ENABLED_NETWORKS}): deploy-facet-%:
@$(call CONFIRM,Deploy facet?)
@$(call CONTRACTS_LIVE_RUN,deploy-facet --facet $(facet),$*)
$(addprefix deploy-bridge-, ${ENABLED_NETWORKS}): deploy-bridge-%:
@$(call CONFIRM,Deploy bridge?)
@$(call CONTRACTS_LIVE_RUN,deploy-bridge --bridge $(bridge),$*)
$(addprefix deploy-dex-, ${ENABLED_NETWORKS}): deploy-dex-%:
@$(call CONFIRM,Deploy dex?)
@$(call CONTRACTS_LIVE_RUN,deploy-dex --dex $(dex),$*)
# : mantain
$(addprefix verify-, ${ENABLED_NETWORKS}): verify-%:
@$(call CONFIRM,Verify diamond?)
@$(call CONTRACTS_LIVE_RUN,verify-diamond,$*)
$(addprefix retrieve-, ${ENABLED_NETWORKS}): retrieve-%:
@$(call CONFIRM,Retrieve $(amount) of $(token)?)
@$(call CONTRACTS_LIVE_RUN,retrieve --token $(token) --amount $(amount),$*)
$(addprefix update-relayer-, ${ENABLED_NETWORKS}): update-relayer-%:
@$(call CONFIRM,Update relayer?)
@$(call CONTRACTS_LIVE_RUN,update-relayer,$*)
# : loupe
$(addprefix loupe-diamond-, ${ENABLED_NETWORKS}): loupe-diamond-%:
@$(call CONTRACTS_LIVE_RUN,loupe --store diamond,$*)
$(addprefix loupe-bridge-, ${ENABLED_NETWORKS}): loupe-bridge-%:
@$(call CONTRACTS_LIVE_RUN,loupe --store bridge,$*)
$(addprefix loupe-swap-, ${ENABLED_NETWORKS}): loupe-swap-%:
@$(call CONTRACTS_LIVE_RUN,loupe --store swap,$*)
# : bulk deploy
$(addprefix dd-, ${ENABLED_NETWORKS}): dd-%: # will create new address
@$(call CONFIRM,You REALLY want to create a new diamond?)
@$(call CONTRACTS_LIVE_RUN,deploy-all,$*)
@$(call CONTRACTS_LIVE_RUN,deploy-bridge --bridge Anyswap,$*)
@$(call CONTRACTS_LIVE_RUN,deploy-dex --dex ZeroEx,$*)
$(addprefix ud-, ${ENABLED_NETWORKS}): ud-%: # will use current address
@$(call CONFIRM,You REALLY want to update the whole diamond?)
@$(call CONTRACTS_LIVE_RUN,deploy-facet --facet DiamondCutterFacet,$*)
@$(call CONTRACTS_LIVE_RUN,deploy-facet --facet RouterFacet,$*)
@$(call CONTRACTS_LIVE_RUN,deploy-facet --facet RelayerUpdaterFacet,$*)
@$(call CONTRACTS_LIVE_RUN,deploy-facet --facet ProviderUpdaterFacet,$*)
@$(call CONTRACTS_LIVE_RUN,deploy-facet --facet FeeManagerFacet,$*)
@$(call CONTRACTS_LIVE_RUN,deploy-facet --facet DiamondLoupeFacet,$*)
@$(call CONTRACTS_LIVE_RUN,deploy-bridge --bridge Anyswap,$*)
@$(call CONTRACTS_LIVE_RUN,deploy-dex --dex ZeroEx,$*)
## Forked chain
$(addprefix fork-, ${ENABLED_NETWORKS}): fork-%:
@$(call DOCKER_COMPOSE_RUN, \
--rm \
-e "FORKING=true" \
-e "FORKED_RPC_NODE=${$*_RPC_NODE}" \
-e "FORKED_CHAIN_ID=${$*_CHAIN_ID}" \
-p ${$*_EXTERNAL_PORT}:8545 \
--name "running-$*" \
${DOCKER_CONTRACTS_SERVICE} \
up \
)
CONTRACTS_DOCKER_EXEC = $(call DOCKER,exec -it "running-$(1)" $(2))
CONTRACTS_RUN = $(call CONTRACTS_DOCKER_EXEC,$(1),yarn $(2) --chain localhost --network localhost)
CONTRACTS_DEPLOY_ALL = $(call CONTRACTS_RUN,$(1),deploy-all)
CONTRACTS_DEPLOY_FACET = $(call CONTRACTS_RUN,$(1),deploy-facet --facet $(2))
CONTRACTS_DEPLOY_BRIDGE = $(call CONTRACTS_RUN,$(1),deploy-bridge --bridge $(2))
CONTRACTS_DEPLOY_DEX = $(call CONTRACTS_RUN,$(1),deploy-dex --dex $(2))
CONTRACTS_GET_TOKENS = $(call CONTRACTS_RUN,$(1),get-tokens --token $(2))
CONTRACTS_LOUPE = $(call CONTRACTS_RUN,$(1),loupe --store $(2))
# : deploy
$(addprefix deploy-all-fork-, ${ENABLED_NETWORKS}): deploy-all-fork-%:
@$(call CONTRACTS_DEPLOY_ALL,$*)
$(addprefix deploy-facet-fork-, ${ENABLED_NETWORKS}): deploy-facet-fork-%:
@$(call CONTRACTS_DEPLOY_FACET,$*,$(facet))
$(addprefix deploy-bridge-fork-, ${ENABLED_NETWORKS}): deploy-bridge-fork-%:
@$(call CONTRACTS_DEPLOY_BRIDGE,$*,$(bridge))
$(addprefix deploy-dex-fork-, ${ENABLED_NETWORKS}): deploy-dex-fork-%:
@$(call CONTRACTS_DEPLOY_DEX,$*,$(dex))
# : fake
$(addprefix get-tokens-, ${ENABLED_NETWORKS}): get-tokens-%:
@$(call CONTRACTS_GET_TOKENS,$*,$(token))
# : loupe
$(addprefix loupe-fork-diamond-, ${ENABLED_NETWORKS}): loupe-fork-diamond-%:
@$(call CONTRACTS_LOUPE,$*,diamond)
$(addprefix loupe-fork-bridge-, ${ENABLED_NETWORKS}): loupe-fork-bridge-%:
@$(call CONTRACTS_LOUPE,$*,bridge)
$(addprefix loupe-fork-swap-, ${ENABLED_NETWORKS}): loupe-fork-swap-%:
@$(call CONTRACTS_LOUPE,$*,swap)
# : bulk deploy
$(addprefix ddf-, ${ENABLED_NETWORKS}): ddf-%:
@$(call CONTRACTS_DEPLOY_ALL,$*)
@$(call CONTRACTS_DEPLOY_BRIDGE,$*,Anyswap)
@$(call CONTRACTS_DEPLOY_DEX,$*,ZeroEx)
$(addprefix udf-, ${ENABLED_NETWORKS}): udf-%:
@$(call CONTRACTS_DEPLOY_FACET,$*,RouterFacet)
@$(call CONTRACTS_DEPLOY_FACET,$*,RelayerUpdaterFacet)
@$(call CONTRACTS_DEPLOY_FACET,$*,ProviderUpdaterFacet)
@$(call CONTRACTS_DEPLOY_FACET,$*,DiamondLoupeFacet)
@$(call CONTRACTS_DEPLOY_FACET,$*,DiamondCutterFacet)
@$(call CONTRACTS_DEPLOY_BRIDGE,$*,Anyswap)
@$(call CONTRACTS_DEPLOY_DEX,$*,ZeroEx)
### Relayer
RELAYER = $(call DOCKER_COMPOSE_RUN,--rm ${DOCKER_RELAYER_SERVICE} $(1))
relayer-router-listener:
@$(call RELAYER,run:dev:router-listener)
relayer-multichain-listener:
@$(call RELAYER,run:dev:multichain-listener)
relayer-events-consumer:
@$(call RELAYER,run:dev:events-consumer)
relayer-txs-consumer:
@$(call RELAYER,run:dev:txs-consumer)
### Terraform
TERRAFORM = $(call DOCKER_COMPOSE_RUN,--rm ${DOCKER_TERRAFORM_SERVICE} $(1))
TERRAFORM_SWITCH = $(call TERRAFORM,workspace select ${ENV})
tf-init:
@$(call TERRAFORM,init \
-backend-config="access_key=${CREATOR_AWS_ACCESS_KEY}" \
-backend-config="secret_key=${CREATOR_AWS_SECRET_KEY}" \
)
tf-plan:
@$(call TERRAFORM_SWITCH)
@$(call TERRAFORM,plan)
tf-apply:
@$(call TERRAFORM_SWITCH)
@$(call TERRAFORM,apply)
tf-destroy:
@$(call TERRAFORM_SWITCH)
@$(call TERRAFORM,destroy)
### Ansible
PLAYBOOKS_PATH = infrastructure/ansible/playbooks
RUN_PLAYBOOK = ansible-playbook -i infrastructure/ansible/inventory.ini -e "nodes=$(1)" $(2)
RUN_PLAYBOOK_GLOBAL = $(call RUN_PLAYBOOK,$(1),${PLAYBOOKS_PATH}/$(2))
RUN_PLAYBOOK_ON_SERVICE = $(call RUN_PLAYBOOK,$(2)_$(1),-e "service=$(2)" ${PLAYBOOKS_PATH}/$(3))
$(addprefix setup-instances-, test prod): setup-instances-%:
@$(call RUN_PLAYBOOK_GLOBAL,$*,docker.yml)
@$(call RUN_PLAYBOOK_GLOBAL,$*,profile.yml)
@$(call RUN_PLAYBOOK_ON_SERVICE,$*,api,setup_node_files.yml)
@$(call RUN_PLAYBOOK_ON_SERVICE,$*,grafana,setup_node_files.yml)
### Grafana
grafana-setup:
@docker plugin install grafana/loki-docker-driver:latest --alias loki --grant-all-permissions
grafana-up:
@$(call DOCKER_COMPOSE, up --detach ${MAKE_GRAFANA_SERVICES})
grafana-down:
@$(call DOCKER_COMPOSE, stop ${MAKE_GRAFANA_SERVICES})