-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Makefile
184 lines (149 loc) · 5.77 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
IMAGE_NAMESPACE ?= okfn-brasil
IMAGE_NAME ?= querido-diario-api
IMAGE_TAG ?= latest
IMAGE_FORMAT ?= docker
# Opensearch ports
# Variables used to connect the app to the OpenSearch
QUERIDO_DIARIO_DATABASE_CSV ?= censo.csv
OPENSEARCH_PORT1 ?= 9200
OPENSEARCH_PORT2 ?= 9300
# Containers data
POD_NAME ?= querido-diario
DATABASE_CONTAINER_NAME ?= $(POD_NAME)-db
OPENSEARCH_CONTAINER_NAME ?= $(POD_NAME)-opensearch
# Database info user to run the tests
POSTGRES_COMPANIES_USER ?= companies
POSTGRES_COMPANIES_PASSWORD ?= companies
POSTGRES_COMPANIES_DB ?= companiesdb
POSTGRES_COMPANIES_HOST ?= localhost
POSTGRES_COMPANIES_PORT ?= 5432
POSTGRES_COMPANIES_IMAGE ?= docker.io/postgres:10
DATABASE_RESTORE_FILE ?= contrib/data/queridodiariodb.tar
# Run integration tests. Run local opensearch to validate the iteration
RUN_INTEGRATION_TESTS ?= 0
API_PORT := 8080
run-command=(podman run --rm -ti --volume $(PWD):/mnt/code:rw \
--pod $(POD_NAME) \
--env PYTHONPATH=/mnt/code \
--env RUN_INTEGRATION_TESTS=$(RUN_INTEGRATION_TESTS) \
--env-file config/current.env \
--user=$(UID):$(UID) $(IMAGE_NAMESPACE)/$(IMAGE_NAME):$(IMAGE_TAG) $1)
wait-for=(podman run --rm -ti --volume $(PWD):/mnt/code:rw \
--pod $(POD_NAME) \
--env PYTHONPATH=/mnt/code \
--user=$(UID):$(UID) \
$(IMAGE_NAMESPACE)/$(IMAGE_NAME):$(IMAGE_TAG) wait-for-it --timeout=90 $1)
.PHONY: black
black:
podman run --rm -ti --volume $(PWD):/mnt/code:rw \
--env PYTHONPATH=/mnt/code \
--user=$(UID):$(UID) $(IMAGE_NAMESPACE)/$(IMAGE_NAME):$(IMAGE_TAG) \
black .
.PHONY: build
build:
podman build --format $(IMAGE_FORMAT) --tag $(IMAGE_NAMESPACE)/$(IMAGE_NAME):$(IMAGE_TAG) \
-f Dockerfile $(PWD)
login:
podman login --username $(REGISTRY_USER) --password "$(REGISTRY_PASSWORD)" https://index.docker.io/v1/
.PHONY: publish
publish:
podman tag $(IMAGE_NAMESPACE)/$(IMAGE_NAME):${IMAGE_TAG} $(IMAGE_NAMESPACE)/$(IMAGE_NAME):$(shell date --rfc-3339=date --utc)
podman push $(IMAGE_NAMESPACE)/$(IMAGE_NAME):$(shell date --rfc-3339=date --utc)
podman push $(IMAGE_NAMESPACE)/$(IMAGE_NAME):${IMAGE_TAG}
.PHONY: publish-tag
publish-tag:
podman tag $(IMAGE_NAMESPACE)/$(IMAGE_NAME):${IMAGE_TAG} $(IMAGE_NAMESPACE)/$(IMAGE_NAME):$(shell git describe --tags)
podman push $(IMAGE_NAMESPACE)/$(IMAGE_NAME):$(shell git describe --tags)
.PHONY: destroy
destroy:
podman rmi --force $(IMAGE_NAMESPACE)/$(IMAGE_NAME):$(IMAGE_TAG)
destroy-pod:
podman pod rm --force --ignore $(POD_NAME)
create-pod: setup-environment destroy-pod
podman pod create --publish $(API_PORT):$(API_PORT) \
--publish $(POSTGRES_COMPANIES_PORT):$(POSTGRES_COMPANIES_PORT) \
--publish $(OPENSEARCH_PORT1):$(OPENSEARCH_PORT1) \
--publish $(OPENSEARCH_PORT2):$(OPENSEARCH_PORT2) \
--name $(POD_NAME)
.PHONY: setup-environment
setup-environment:
-cp --no-clobber config/sample.env config/current.env
test -f censo.csv || curl -s -O https://querido-diario.nyc3.cdn.digitaloceanspaces.com/censo/censo.csv
test -f themes_config.json || curl -s -O https://raw.githubusercontent.com/okfn-brasil/querido-diario-data-processing/main/config/themes_config.json
set-test-variables:
$(eval POD_NAME=test-$(POD_NAME))
$(eval DATABASE_CONTAINER_NAME=test-$(DATABASE_CONTAINER_NAME))
$(eval API_PORT=8088)
$(eval OPENSEARCH_PORT1=9201)
$(eval OPENSEARCH_PORT2=9301)
$(eval OPENSEARCH_CONTAINER_NAME=test-$(OPENSEARCH_CONTAINER_NAME))
$(eval QUERIDO_DIARIO_DATABASE_CSV="")
set-integration-test-variables: set-test-variables
$(eval RUN_INTEGRATION_TESTS=1)
.PHONY: test
test: set-test-variables create-pod retest
.PHONY: retest
retest: set-test-variables black
$(call run-command, python -m unittest discover tests)
.PHONY: test-all
test-all: set-integration-test-variables create-pod opensearch database retest
.PHONY: test-shell
test-shell: set-test-variables
$(call run-command, bash)
.PHONY: coverage
coverage: set-test-variables create-pod opensearch database
$(call run-command, coverage erase)
$(call run-command, coverage run -m unittest tests)
$(call run-command, coverage report -m)
.PHONY: shell
shell:
podman run --rm -ti --volume $(PWD):/mnt/code:rw \
--env PYTHONPATH=/mnt/code \
--user=$(UID):$(UID) $(IMAGE_NAMESPACE)/$(IMAGE_NAME):$(IMAGE_TAG) \
bash
.PHONY: run
run: create-pod opensearch database load-data re-run
.PHONY:load-data
load-data:
$(call run-command, python scripts/load_fake_gazettes.py)
.PHONY: re-run
re-run: setup-environment wait-opensearch wait-database
$(call run-command, python main)
.PHONY: runshell
runshell:
$(call run-command, bash)
opensearch: stop-opensearch start-opensearch wait-opensearch
start-opensearch:
podman run -d --rm -ti \
--name $(OPENSEARCH_CONTAINER_NAME) \
--pod $(POD_NAME) \
--env discovery.type=single-node \
--env plugins.security.ssl.http.enabled=false \
opensearchproject/opensearch:2.9.0
stop-opensearch:
podman rm --force --ignore $(OPENSEARCH_CONTAINER_NAME)
wait-opensearch:
$(call wait-for, localhost:9200)
.PHONY: stop-database
stop-database:
podman rm --force --ignore $(DATABASE_CONTAINER_NAME)
.PHONY: database
database: stop-database start-database wait-database
start-database:
podman run -d --rm -ti \
--name $(DATABASE_CONTAINER_NAME) \
--pod $(POD_NAME) \
-e POSTGRES_COMPANIES_PASSWORD=$(POSTGRES_COMPANIES_PASSWORD) \
-e POSTGRES_COMPANIES_USER=$(POSTGRES_COMPANIES_USER) \
-e POSTGRES_COMPANIES_DB=$(POSTGRES_COMPANIES_DB) \
$(POSTGRES_COMPANIES_IMAGE)
wait-database:
$(call wait-for, localhost:5432)
load-database:
ifneq ("$(wildcard $(DATABASE_RESTORE_FILE))","")
podman cp $(DATABASE_RESTORE_FILE) $(DATABASE_CONTAINER_NAME):/mnt/dump_file
podman exec $(DATABASE_CONTAINER_NAME) bash -c "pg_restore -v -c -h localhost -U $(POSTGRES_COMPANIES_USER) -d $(POSTGRES_COMPANIES_DB) /mnt/dump_file || true"
else
@echo "cannot restore because file does not exists '$(DATABASE_RESTORE_FILE)'"
@exit 1
endif