-
Notifications
You must be signed in to change notification settings - Fork 8
/
Makefile
executable file
·1151 lines (973 loc) · 37.1 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env make
SHELL=/bin/bash
.SHELLFLAGS = -e -c
.ONESHELL:
MAKEFLAGS += --no-print-directory
ifeq ($(shell [[ ( $(shell echo $(MAKE_VERSION) | cut -f1 -d.) -ge 4) ]] || echo 1),1)
$(error "Unsupported Make version. The build system does not work properly\
with GNU Make $(MAKE_VERSION),please use GNU Make 4 or above.")
endif
# You can change the password in .env
PRJ?=shaystack
HAYSTACK_PROVIDER?=shaystack.providers.db
HAYSTACK_DB?=sample/carytown.zinc
REFRESH=15
USE_OKTA?=N
AWS_PROFILE?=default
AWS_REGION?=eu-west-3
AWS_CLUSTER_NAME=haystack
AWS_STAGE?=$(STAGE)
LOGLEVEL?=WARNING
PG_PASSWORD?=password
PGADMIN_USER?=$(USER)@domain.com
PGADMIN_PASSWORD?=password
MYSQL_USER?=mysql
MYSQL_PASSWORD?=password
MONGO_USER?=mongo
MONGO_PASSWORD?=password
TLS_VERIFY=False
TERM?=dumb
# Default parameter for make [aws-]api-read
READ_PARAMS?=?filter=his&limit=5
# Default parameter for make [aws-]api-hisRead
HISREAD_PARAMS?=?id=@id1
# Override project variables
ifneq (,$(wildcard .env))
include .env
endif
PYTHON_SRC=$(shell find . -name '*.py')
PYTHON_VERSION?=3.10
PRJ_PACKAGE:=$(PRJ)
VENV ?= $(PRJ)
CONDA ?=conda
CONDA_PYTHON_EXE?=/opt/conda/bin/conda
CONDA_EXE?=$(shell which conda)
CONDA_BASE:=$(shell $(CONDA_EXE) info --base || '/opt/conda')
CONDA_PACKAGE:=$(CONDA_PREFIX)/lib/python$(PYTHON_VERSION)/site-packages
CONDA_PYTHON:=$(CONDA_PREFIX)/bin/python
CONDA_ARGS?=
GIT_CLONE_URL=$(shell git remote get-url origin)
FLASK_DEBUG?=1
STAGE=dev
AWS_STAGE?=$(STAGE)
ZAPPA_ENV=zappa_venv
DOCKER_REPOSITORY=$(USER)
PORT?=3000
INPUT_NETWORK?=localhost
HOST_API?=localhost
COOKIE_SECRET_KEY?=2d1a12a6-3232-4328-9365-b5b65e64a68f
TWINE_USERNAME?=__token__
SIGN_IDENTITY?=$(USER)
URL_PREFIX=
PIP_PACKAGE:=$(CONDA_PACKAGE)/$(PRJ_PACKAGE).egg-link
AWS_API_HOME=$(shell zappa status $(AWS_STAGE) --json | jq -r '."API Gateway URL"')
# For minio
MINIO_HOME=$(HOME)/.minio
AWS_ACCESS_KEY=$(shell aws configure --profile $(AWS_PROFILE) get aws_access_key_id)
AWS_SECRET_KEY=$(shell aws configure --profile $(AWS_PROFILE) get aws_secret_access_key)
# Export all project variables
export PRJ
export HAYSTACK_PROVIDER
export HAYSTACK_DB
export LOGLEVEL
export AWS_PROFILE
export AWS_REGION
export PYTHON_VERSION
export READ_PARAMS
export HISREAD_PARAMS
export FLASK_DEBUG
export COOKIE_SECRET_KEY
export PYTHON_VERSION
export TLS_VERIFY
export TWINE_USERNAME
# Calculate the make extended parameter
# Keep only the unknown target
#ARGS = `ARGS="$(filter-out $@,$(MAKECMDGOALS))" && echo $${ARGS:-${1}}`
# Hack to ignore unknown target. May be used to calculate parameters
#%:
# @:
define BROWSER
python -c '
import os, sys, webbrowser
from urllib.request import pathname2url
webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1])), autoraise=True)
sys.exit(0)
'
endef
CHECK_VENV=if [[ $(VENV) != "$(CONDA_DEFAULT_ENV)" ]] ; \
then ( echo -e "$(green)Use: $(cyan)conda activate $(VENV)$(green) before using $(cyan)make$(normal)"; exit 1 ) ; fi
ACTIVATE_VENV=source $(CONDA_BASE)/etc/profile.d/conda.sh && conda activate $(VENV) $(CONDA_ARGS)
DEACTIVATE_VENV=source $(CONDA_BASE)/etc/profile.d/conda.sh && conda deactivate
VALIDATE_VENV?=$(CHECK_VENV)
ifneq ($(TERM),)
normal:=$(shell tput sgr0)
bold:=$(shell tput bold)
red:=$(shell tput setaf 1)
green:=$(shell tput setaf 2)
yellow:=$(shell tput setaf 3)
blue:=$(shell tput setaf 4)
purple:=$(shell tput setaf 5)
cyan:=$(shell tput setaf 6)
white:=$(shell tput setaf 7)
gray:=$(shell tput setaf 8)
endif
.PHONY: help
.DEFAULT: help
## Print all majors target
help:
@echo "$(bold)Available rules:$(normal)"
echo
sed -n -e "/^## / { \
h; \
s/.*//; \
:doc" \
-e "H; \
n; \
s/^## //; \
t doc" \
-e "s/:.*//; \
G; \
s/\\n## /---/; \
s/\\n/ /g; \
p; \
}" ${MAKEFILE_LIST} \
| LC_ALL='C' sort --ignore-case \
| awk -F '---' \
-v ncol=$$(tput cols) \
-v indent=20 \
-v col_on="$$(tput setaf 6)" \
-v col_off="$$(tput sgr0)" \
'{ \
printf "%s%*s%s ", col_on, -indent, $$1, col_off; \
n = split($$2, words, " "); \
line_length = ncol - indent; \
for (i = 1; i <= n; i++) { \
line_length -= length(words[i]) + 1; \
if (line_length <= 0) { \
line_length = ncol - indent - length(words[i]) - 1; \
printf "\n%*s ", -indent, " "; \
} \
printf "%s ", words[i]; \
} \
printf "\n"; \
}' \
| more $(shell test $(shell uname) = Darwin && echo '--no-init --raw-control-chars')
echo -e "Use '$(cyan)make -jn ...$(normal)' for Parallel run"
echo -e "Use '$(cyan)make -B ...$(normal)' to force the target"
echo -e "Use '$(cyan)make -n ...$(normal)' to simulate the build"
# --------------------------- Info
## Print all URL
info: api url-pg aws-api
conda-info:
conda info
.PHONY: dump-*
# Tools to dump makefile variable (make dump-AWS_API_HOME)
dump-%:
@if [ "${${*}}" = "" ]; then
echo "Environment variable $* is not set";
exit 1;
else
echo "$*=${${*}}";
fi
## Print project variables
dump-params:
@echo -e "$(green)HAYSTACK_PROVIDER=$(HAYSTACK_PROVIDER)$(normal)"
echo -e "$(green)HAYSTACK_DB=$(HAYSTACK_DB)$(normal)"
echo -e "$(green)AWS_PROFILE=$(AWS_PROFILE)$(normal)"
echo -e "$(green)STAGE=$(STAGE)$(normal)"
echo -e "$(green)PORT=$(PORT)$(normal)"
echo -e "$(green)PIP_INDEX_URL=$(PIP_INDEX_URL)$(normal)"
echo -e "$(green)PIP_EXTRA_INDEX_URL=$(PIP_EXTRA_INDEX_URL)$(normal)"
echo -e "$(green)READ_PARAMS=$${READ_PARAMS}$(normal)"
echo -e "$(green)HISREAD_PARAMS=$${HISREAD_PARAMS}$(normal)"
.PHONY: shell bash env
# Start a child shell
shell bash zsh:
@$(SHELL)
env:
env
# -------------------------------------- GIT
.env:
@touch .env
.git/config: | .git .git/hooks/pre-push # Configure git
@git config --local core.autocrlf input
# Set tabulation to 4 when use 'git diff'
git config --local core.page 'less -x4'
git config --local push.followTags true
# Rule to add a validation before pushing in master branch.
# Use FORCE=y git push to override this validation.
.git/hooks/pre-push: | .git
@# Add a hook to validate the project before a git push
cat >>.git/hooks/pre-push <<PRE-PUSH
#!/usr/bin/env sh
# Validate the project before a push
if test -t 1; then
ncolors=$$(tput colors)
if test -n "\$$ncolors" && test \$$ncolors -ge 8; then
normal="\$$(tput sgr0)"
red="\$$(tput setaf 1)"
green="\$$(tput setaf 2)"
yellow="\$$(tput setaf 3)"
fi
fi
branch="\$$(git branch | grep \* | cut -d ' ' -f2)"
if [ "\$${branch}" = "master" ] || [[ "\$${branch}" = release/* ]] && [ "\$${FORCE}" != y ] ; then
printf "\$${green}Validate the project before push the commit... (\$${yellow}make validate\$${green})\$${normal}\n"
make validate
ERR=\$$?
if [ \$${ERR} -ne 0 ] ; then
printf "\$${red}'\$${yellow}make validate\$${red}' failed before git push.\$${normal}\n"
printf "Use \$${yellow}FORCE=y git push\$${normal} to force.\n"
exit \$${ERR}
fi
fi
PRE-PUSH
chmod +x .git/hooks/pre-push
fetch:
@git fetch
pull:
@git pull
## Push the source code to git and the tag simultaneously
push:
@git push --atomic origin develop $(shell git describe --tag)
# -------------------------------------- Initialize Virtual env
.PHONY: configure _configure _check_configure
## Prepare the working environment (conda venv, ...)
configure: _check_configure _configure
_check_configure:
@if [[ "$(CONDA_DEFAULT_ENV)" != "" && "$(CONDA_DEFAULT_ENV)" != "base" ]] ; \
then echo -e "$(red)Use $(cyan)conda deactivate$(red) before using $(cyan)make configure$(normal)"; exit ; fi
# Configure with the check. Use to be called inside a docker, with a mapping /opt/conda
# to create an environment via the docker.
_configure:
@if [[ "$(VENV)" != "base" ]]
then
$(CONDA_EXE) create --name "$(VENV)" -c conda-forge python=$(PYTHON_VERSION) -y $(CONDA_ARGS)
echo -e "Use: $(cyan)conda activate $(VENV)$(normal) $(CONDA_ARGS)"
else
$(CONDA_EXE) install -c conda-forge python=$(PYTHON_VERSION) -y $(CONDA_ARGS)
fi
# -------------------------------------- Standard requirements
# Rule to update the current venv, with the dependencies describe in `setup.py`
# supersqlite is not available for actual version of shaystack
$(PIP_PACKAGE): $(CONDA_PYTHON) setup.* requirements.txt | .git # Install pip dependencies
@$(VALIDATE_VENV)
echo -e "$(cyan)Install build dependencies ... (may take minutes)$(normal)"
ifeq ($(USE_OKTA),Y)
pip install gimme-aws-creds
endif
echo -e "$(cyan)Install binary dependencies ...$(normal)"
conda install -y -c conda-forge conda setuptools make git jq libpq curl psycopg2 md-toc
echo -e "$(cyan)Install project dependencies ...$(normal)"
echo -e "$(cyan)pip install -e .[dev,flask,graphql,lambda]$(normal)"
pip install -e '.[dev,flask,graphql,lambda]'
echo -e "$(cyan)pip install -e .$(normal)"
pip install -e .
touch $(PIP_PACKAGE)
# All dependencies of the project must be here
.PHONY: requirements dependencies
REQUIREMENTS=$(PIP_PACKAGE) .git/config
requirements: $(REQUIREMENTS)
dependencies: requirements
# Remove the current conda environment
remove-venv remove-$(VENV):
@$(DEACTIVATE_VENV)
conda env remove --name "$(VENV)" -y 2>/dev/null
echo -e "Use: $(cyan)conda deactivate$(normal)"
# Upgrade packages to last versions
upgrade-venv upgrade-$(VENV):
@$(VALIDATE_VENV)
conda update --all $(CONDA_ARGS)
pip list --format freeze --outdated | sed 's/(.*//g' | xargs -r -n1 pip install $(EXTRA_INDEX) -U
echo -e "$(cyan)After validation, upgrade the setup.py$(normal)"
# -------------------------------------- Clean
.PHONY: clean-pip
# Remove all the pip package
clean-pip:
@pip freeze | grep -v "^-e" | grep -v "@" | xargs pip uninstall -y
echo -e "$(cyan)Virtual env cleaned$(normal)"
.PHONY: clean-venv clean-$(VENV)
# Clean venv
clean-$(VENV): remove-venv
@conda create -y -q -n $(VENV) $(CONDA_ARGS)
echo -e "$(yellow)Warning: Conda virtualenv $(VENV) is empty.$(normal)"
## Clean the environment (Remove all components)
clean-venv : clean-$(VENV)
# clean-zappa
clean-zappa:
@rm -fr handler_venv $(ZAPPA_ENV) $(PRJ)-$(AWS_STAGE)-*.* handler_$(PRJ)-$(AWS_STAGE)*.zip
## Clean project
clean: async-stop clean-zappa
@rm -rf bin/* .eggs shaystack.egg-info .ipynb_checkpoints .mypy_cache .pytest_cache .start build nohup.out dist \
.make-* .pytype out.json test.db zappa_settings.json ChangeLog
mkdir dist/
.PHONY: clean-all
# Clean all environments
clean-all: clean docker-rm docker-rm-dmake remove-venv
# -------------------------------------- Build
.PHONY: dist build compile-all api api-read api-hisRead
# Compile all python files
compile-all:
@$(VALIDATE_VENV)
@echo -e "$(cyan)Compile all python file...$(normal)"
$(CONDA_PYTHON) -m compileall
# -------------------------------------- Docs
.PHONY: docs docs-tm
docs/api: $(REQUIREMENTS)
@$(VALIDATE_VENV)
pdoc -f --html -o docs/api shaystack app
docs-tm: docs/index.md docs/contributing.md docs/AWS.md docs/AppSync.md
md_toc -p github $?
## Generate the API HTML documentation
docs: docs/api docs-tm docs/*
@touch docs
## Start the pdoc server to update the docstrings
start-docs:
@$(VALIDATE_VENV)
pdoc --http : -f --html -o docs/api shaystack app
# -------------------------------------- Client API
.PHONY: api
## Print API endpoint URL
api:
@echo http://$(HOST_API):$(PORT)/haystack
.PHONY: api-*
## Invoke local API (eg. make api-about)
api-%:
@TARGET="$(HOST_API):$(PORT)"
curl -H "Accept: text/zinc" \
"$${TARGET}/haystack/$*"
api-read:
@echo -e "Use $(yellow)READ_PARAMS=$(READ_PARAMS)$(normal)"
TARGET="$(HOST_API):$(PORT)"
curl -H "Accept: text/zinc" \
"$${TARGET}/haystack/read$(READ_PARAMS)"
api-hisRead:
@echo -e "Use $(yellow)HISREAD_PARAMS=$(HISREAD_PARAMS)$(normal)"
TARGET="$(HOST_API):$(PORT)"
curl -H "Accept: text/zinc" \
"$${TARGET}/haystack/hisRead$(HISREAD_PARAMS)"
# -------------------------------------- Server API
.PHONY: start-api async-start-api async-stop-api
## Start api
start-api: $(REQUIREMENTS)
@$(VALIDATE_VENV)
[ -e .start/start-api.pid ] && $(MAKE) async-stop-api || true
echo -e "$(green)PROVIDER=$${HAYSTACK_PROVIDER}"
echo -e "$(green)DB=$${HAYSTACK_DB}"
echo -e "$(green)TS=$${HAYSTACK_TS}"
echo -e "$(green)Use http://$(HOST_API):$(PORT)/graphql or http://$(HOST_API):$(PORT)/haystack$(normal)"
FLASK_DEBUG=1 FLASK_ENV=$(STAGE) \
URL_PREFIX=$(URL_PREFIX) $(CONDA_PYTHON) -m app.main --port $(PORT) --host $(INPUT_NETWORK)
# Start local API server in background
async-start-api: $(REQUIREMENTS)
@$(VALIDATE_VENV)
[ -e .start/start-api.pid ] && echo -e "$(yellow)Local API was allready started$(normal)" && exit
mkdir -p .start
FLASK_DEBUG=1 FLASK_APP=app.run FLASK_ENV=$(STAGE) \
nohup $(CONDA_PYTHON) -m app.main --port $(PORT) >.start/start-api.log 2>&1 &
echo $$! >.start/start-api.pid
sleep 1
tail .start/start-api.log
echo -e "$(yellow)Local API started$(normal)"
echo -e "$(green)PROVIDER=$${HAYSTACK_PROVIDER}"
echo -e "$(green)DB=$${HAYSTACK_DB}"
echo -e "$(green)TS=$${HAYSTACK_TS}"
echo -e "$(green)Use http://$(HOST_API):$(PORT)/graphql or http://$(HOST_API):$(PORT)/haystack$(normal)"
# Stop the background local API server
async-stop-api:
@$(VALIDATE_VENV)
[ -e .start/start-api.pid ] && kill `cat .start/start-api.pid` 2>/dev/null >/dev/null || true && echo -e "$(green)Local API stopped$(normal)"
rm -f .start/start-api.pid
# -------------------------------------- GraphQL
.PHONY: graphql-schema graphql-api
## Print GraphQL endpoint API URL
graphql-api:
@echo "http://$(HOST_API):$(PORT)/graphql"
graphql-api-%:
@$(VALIDATE_VENV)
curl \
-X POST \
-H "Content-Type: application/json" \
--data '{ "query": "{ haystack { about { name } } }" }' \
http://$(HOST_API):$(PORT)/graphql/
schema.graphql: app/graphql_model.py app/blueprint_graphql.py
@$(VALIDATE_VENV)
python -m app.blueprint_graphql >schema.graphql
## Print haystack graphql schema
graphql-schema: schema.graphql
@cat schema.graphql
# -------------------------------------- Minio
# https://min.io/
# See https://docs.min.io/docs/how-to-use-aws-sdk-for-python-with-minio-server.html
.minio:
@mkdir -p .minio
start-minio: .minio $(REQUIREMENTS)
@docker run -p 9000:9000 \
-e "MINIO_ACCESS_KEY=$(AWS_ACCESS_KEY)" \
-e "MINIO_SECRET_KEY=$(AWS_SECRET_KEY)" \
-v "$(MINIO_HOME):/data" \
minio/minio server /data
async-stop-minio:
@$(VALIDATE_VENV)
[ -e .start/start-minio.pid ] && kill `cat .start/start-minio.pid` 2>/dev/null >/dev/null || true && echo -e "$(green)Local Minio stopped$(normal)"
rm -f .start/start-minio.pid
async-start-minio: .minio $(REQUIREMENTS)
@$(VALIDATE_VENV)
[ -e .start/start-minio.pid ] && echo -e "$(yellow)Local Minio was allready started$(normal)" && exit
mkdir -p .start
nohup docker run -p 9000:9000 \
--name minio_$(PRJ) \
-e "MINIO_ACCESS_KEY=$(AWS_ACCESS_KEY)" \
-e "MINIO_SECRET_KEY=$(AWS_SECRET_KEY)" \
-v "$(MINIO_HOME):/data" \
minio/minio server /data >.start/start-minio.log 2>&1 &
echo $$! >.start/start-minio.pid
sleep 2
tail .start/start-minio.log
echo -e "$(yellow)Local Minio was started$(normal)"
## Stop all background server
async-stop: async-stop-api async-stop-minio stop-pg stop-pgadmin stop-mysql stop-mongodb async-docker-stop
# -------------------------------------- AWS
ifeq ($(USE_OKTA),Y)
.PHONY: aws-update-token
# Update the AWS Token
aws-update-token: $(REQUIREMENTS)
@aws sts get-caller-identity >/dev/null 2>/dev/null || gimme-aws-creds --profile $(AWS_PROFILE)
else
aws-update-token:
@echo -e "$(yellow)Nothing to do to refresh the token. (Set USE_OKTA ?)$(normal)"
endif
.PHONY: aws-package aws-deploy aws-update aws-undeploy _zappa_settings
# Install a clean venv before invoking zappa
_zappa_pre_install: clean-zappa
@virtualenv -p python$(PYTHON_VERSION) $(ZAPPA_ENV)
source $(ZAPPA_ENV)/bin/activate
pip install -U pip setuptools
pip install -e '.[graphql,lambda]'
find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf
_zappa_settings: zappa_settings.json.template
@envsubst <zappa_settings.json.template >zappa_settings.json
## Build lambda package
aws-package: $(REQUIREMENTS) _zappa_pre_install compile-all _zappa_settings aws-update-token
@echo -e "$(cyan)Create lambda package...$(normal)"
source $(ZAPPA_ENV)/bin/activate
zappa package $(AWS_STAGE)
rm -Rf $(ZAPPA_ENV)
## Deploy lambda functions
aws-deploy: $(REQUIREMENTS) _zappa_pre_install _zappa_settings compile-all
@$(VALIDATE_VENV)
source $(ZAPPA_ENV)/bin/activate
zappa deploy $(AWS_STAGE)
rm -Rf $(ZAPPA_ENV)
echo -e "$(green)Lambdas are deployed$(normal)"
grep --color=never 'HAYSTACK_' zappa_settings.json
## Update lambda functions
aws-update: $(REQUIREMENTS) _zappa_pre_install _zappa_settings compile-all
@$(VALIDATE_VENV)
source $(ZAPPA_ENV)/bin/activate
zappa update $(AWS_STAGE)
rm -Rf $(ZAPPA_ENV)
echo -e "$(green)Lambdas are updated$(normal)"
## Remove AWS Stack
aws-undeploy: $(REQUIREMENTS) _zappa_settings
ifeq ($(USE_OKTA),Y)
gimme-aws-creds --profile $(AWS_PROFILE)
endif
@zappa undeploy $(AWS_STAGE) --remove-logs
.PHONY: aws-api
## Print AWS API endpoint URL
aws-api: aws-update-token
@echo $(AWS_API_HOME)/
## Print GraphQL API endpoint URL
aws-graphql-api: aws-update-token
@echo $(AWS_API_HOME)/graphql/
.PHONY: aws-api-*
## Call AWS api (ie. aws-api-about)
aws-api-%:
@$(VALIDATE_VENV)
TARGET="$(AWS_API_HOME)"
curl -H "Accept: text/zinc" \
"$${TARGET}/haystack/$*"
aws-api-read:
@$(VALIDATE_VENV)
TARGET="$(AWS_API_HOME)"
curl -H "Accept: text/zinc" \
"$${TARGET}/haystack/read?filter=point&limit=5"
aws-api-hisRead:
@$(VALIDATE_VENV)
TARGET="$(AWS_API_HOME)"
curl -H "Accept: text/zinc" \
"$${TARGET}/haystack/hisRead$(HISREAD_PARAMS)"
## Print AWS logs
aws-logs:
@$(VALIDATE_VENV)
zappa tail
_aws_check_repositoy:
@[ ! -z "$(AWS_REPOSITORY)" ] || (echo "Set AWS_REPOSITORY" ; exit 1)
~/.docker/config.json: Makefile
@aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin $(AWS_REPOSITORY)
## Push the docker image to AWS repository
aws-docker-push: _aws_check_repositoy ~/.docker/config.json docker-build aws-update-token
@docker tag $(DOCKER_REPOSITORY)/$(PRJ):latest $(AWS_REPOSITORY)/$(PRJ):latest
docker image push $(AWS_REPOSITORY)/$(PRJ):latest
aws-docker-deploy: aws-update-token ~/.docker/config.json
aws ecs run-task -task-definition task-def-name -cluster $(AWS_CLUSTER_NAME)
# -------------------------------------- Tests
.PHONY: unit-test
.make-unit-test: $(REQUIREMENTS) $(PYTHON_SRC) Makefile | .env
@$(VALIDATE_VENV)
pytest tests
date >.make-unit-test
## Run unit test
unit-test: .make-unit-test
.make-test: .make-unit-test
@date >.make-test
## Run all tests (unit and functional)
test: .make-test
.make-test-aws: aws-update-token
@$(VALIDATE_VENV)
echo -e "$(green)Running AWS tests...$(normal)"
pytest -s tests
echo -e "$(green)AWS tests done$(normal)"
date >.make-test-aws
## Run only tests with connection with AWS
test-aws: .make-test-aws
# Test local deployment with URL provider
functional-url-local: $(REQUIREMENTS)
@$(VALIDATE_VENV)
@echo -e "$(green)Test URL local...$(normal)"
@$(MAKE) async-stop-api >/dev/null
export HAYSTACK_PROVIDER=shaystack.providers.db
export HAYSTACK_DB=sample/carytown.zinc
$(MAKE) HAYSTACK_PROVIDER=$$HAYSTACK_PROVIDER HAYSTACK_DB=$$HAYSTACK_DB async-start-api
PYTHONPATH=tests:. $(CONDA_PYTHON) tests/functional_test.py
echo -e "$(green)Test with url serveur and local file OK$(normal)"
$(MAKE) async-stop-api >/dev/null
# Test local deployment with URL provider
functional-url-s3: $(REQUIREMENTS) aws-update-token
@$(VALIDATE_VENV)
@echo -e "$(green)Test URL on S3...$(normal)"
@$(MAKE) async-stop-api >/dev/null
export HAYSTACK_PROVIDER=shaystack.providers.db
export HAYSTACK_DB=s3://shaystack/carytown.zinc
$(MAKE) HAYSTACK_PROVIDER=$$HAYSTACK_PROVIDER HAYSTACK_DB=$$HAYSTACK_DB async-start-api
PYTHONPATH=tests:. $(CONDA_PYTHON) tests/functional_test_s3.py
echo -e "$(green)Test with url serveur and s3 file OK$(normal)"
$(MAKE) async-stop-api >/dev/null
#
# Clean DB, Start API, and try with SQLite
functional-db-sqlite: $(REQUIREMENTS)
@$(VALIDATE_VENV)
@echo -e "$(green)Test local SQLite...$(normal)"
@$(MAKE) async-stop-api>/dev/null
pip install supersqlite >/dev/null
rm -f test.db
export HAYSTACK_PROVIDER=shaystack.providers.db
export HAYSTACK_DB=sqlite3://localhost/test.db
$(CONDA_PYTHON) -m shaystack.providers.import_db --reset sample/carytown.zinc $${HAYSTACK_DB}
echo -e "$(green)Data imported in SQLite ($${HAYSTACK_DB})$(normal)"
$(MAKE) HAYSTACK_PROVIDER=$$HAYSTACK_PROVIDER HAYSTACK_DB=$$HAYSTACK_DB async-start-api
PYTHONPATH=tests:. $(CONDA_PYTHON) tests/functional_test.py
echo -e "$(green)Test with local SQLite serveur OK$(normal)"
$(MAKE) async-stop-api >/dev/null
# Clean DB, Start API, and try with SQLite + Time stream
functional-db-sqlite-ts: $(REQUIREMENTS)
@$(VALIDATE_VENV)
@echo -e "$(green)Test local SQLite + Timestream...$(normal)"
@$(MAKE) async-stop-api>/dev/null
pip install supersqlite boto3 >/dev/null
rm -f test.db
export HAYSTACK_PROVIDER=shaystack.providers.timestream
export HAYSTACK_DB=sqlite3://localhost/test.db
export HAYSTACK_TS='timestream://shaystack?mem_ttl=8760&mag_ttl=400'
export LOG_LEVEL=INFO
#$(CONDA_PYTHON) -m shaystack.providers.import_db --reset sample/carytown.zinc $${HAYSTACK_DB} $${HAYSTACK_TS}
echo -e "$(green)Data imported in SQLite and Time stream ($${HAYSTACK_DB})$(normal)"
$(MAKE) HAYSTACK_PROVIDER="$$HAYSTACK_PROVIDER" HAYSTACK_DB="$$HAYSTACK_DB" \
HAYSTACK_TS="$$HAYSTACK_TS" async-start-api
# PYTHONPATH=tests:. $(CONDA_PYTHON) tests/functional_test.py
# echo -e "$(green)Test with local SQLite serveur and Time Stream OK$(normal)"
$(MAKE) async-stop-api >/dev/null
# Start Postgres, Clean DB, Start API and try
functional-db-postgres: $(REQUIREMENTS) clean-pg
@$(VALIDATE_VENV)
@echo -e "$(green)Test local Postgres...$(normal)"
@$(MAKE) async-stop-api >/dev/null
pip install psycopg2 >/dev/null
$(MAKE) start-pg
PG_IP=$(shell docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' postgres)
export HAYSTACK_PROVIDER=shaystack.providers.db
export HAYSTACK_DB=postgresql://postgres:password@localhost:5432/postgres#haystack
$(CONDA_PYTHON) -m shaystack.providers.import_db --reset sample/carytown.zinc $${HAYSTACK_DB}
echo -e "$(green)Data imported in Postgres ($${HAYSTACK_DB})$(normal)"
$(MAKE) HAYSTACK_PROVIDER=$$HAYSTACK_PROVIDER HAYSTACK_DB=$$HAYSTACK_DB async-start-api
PYTHONPATH=tests:. $(CONDA_PYTHON) tests/functional_test.py
echo -e "$(green)Test with local Postgres serveur OK$(normal)"
$(MAKE) async-stop-api >/dev/null
# Start mysql, Clean DB, Start API and try
functional-db-mysql: $(REQUIREMENTS) clean-mysql
@$(VALIDATE_VENV)
@echo -e "$(green)Test local MySQL...$(normal)"
@$(MAKE) async-stop-api >/dev/null
pip install pymysql >/dev/null
$(MAKE) start-mysql
PG_IP=$(shell docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql)
export HAYSTACK_PROVIDER=shaystack.providers.db
export HAYSTACK_DB=mysql://mysql:password@localhost/haystackdb#haystack
$(CONDA_PYTHON) -m shaystack.providers.import_db --reset sample/carytown.zinc $${HAYSTACK_DB}
echo -e "$(green)Data imported in MySQL ($${HAYSTACK_DB})$(normal)"
$(MAKE) HAYSTACK_PROVIDER=$$HAYSTACK_PROVIDER HAYSTACK_DB=$$HAYSTACK_DB async-start-api
PYTHONPATH=tests:. $(CONDA_PYTHON) tests/functional_test.py
echo -e "$(green)Test with local MySQL serveur OK$(normal)"
$(MAKE) async-stop-api >/dev/null
# Start mongodb, Clean DB, Start API and try
functional-mongodb: $(REQUIREMENTS) #clean-mongodb
@$(VALIDATE_VENV)
@echo -e "$(green)Test local MongoDB...$(normal)"
$(MAKE) async-stop-api >/dev/null
pip install pymongo >/dev/null
$(MAKE) start-mongodb
PG_IP=$(shell docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mongodb)
export HAYSTACK_PROVIDER=shaystack.providers.mongodb
export HAYSTACK_DB=mongodb://localhost/haystackdb#haystack
$(CONDA_PYTHON) -m shaystack.providers.import_db --reset sample/carytown.zinc $${HAYSTACK_DB}
echo -e "$(green)Data imported in MongoDB ($${HAYSTACK_DB})$(normal)"
$(MAKE) HAYSTACK_PROVIDER=$$HAYSTACK_PROVIDER HAYSTACK_DB=$$HAYSTACK_DB async-start-api
PYTHONPATH=tests:. $(CONDA_PYTHON) tests/functional_test.py
echo -e "$(green)Test with local MongoDB serveur OK$(normal)"
$(MAKE) async-stop-api >/dev/null
.PHONY: functional-database
functional-database: $(REQUIREMENTS) start-pg start-mysql start-mongodb
@$(VALIDATE_VENV)
echo -e "$(green)Test same request with all databases...$(normal)"
pytest tests/test_provider_db.py
echo -e "$(green)Test same request with all databases OK$(normal)"
#functional-db-sqlite and functional-db-sqlite-ts not available
.make-functional-test: functional-url-local functional-db-postgres functional-db-mysql\
functional-url-s3 functional-mongodb functional-database
@touch .make-functional-test
## Test graphql client with different providers
functional-test: .make-functional-test
# -------------------------------------- Typing
mypy.cfg: $(CONDA_PREFIX)/bin/mypy
@$(VALIDATE_VENV)
[[ ! -f mypy.cfg ]] && mypy --generate-config mypy.cfg || true
touch mypy.cfg
.PHONY: typing
.make-typing: $(REQUIREMENTS) $(CONDA_PREFIX)/bin/mypy mypy.cfg $(PYTHON_SRC)
@$(VALIDATE_VENV)
echo -e "$(cyan)Check mypy...$(normal)"
MYPYPATH=stubs mypy --config-file=mypy.cfg -p shaystack -p app -p tests
touch .make-typing
## Check python typing
typing: .make-typing
# -------------------------------------- Lint
.PHONY: lint
.pylintrc:
@$(VALIDATE_VENV)
pylint --generate-rcfile > .pylintrc
.make-lint: $(REQUIREMENTS) $(PYTHON_SRC) | .pylintrc .pylintrc-test
@$(VALIDATE_VENV)
echo -e "$(cyan)Check lint...$(normal)"
pylint -d duplicate-code app shaystack
echo -e "$(cyan)Check lint for tests...$(normal)"
pylint --rcfile=.pylintrc-test tests
touch .make-lint
## Lint the code
lint: .make-lint
.PHONY: validate
.make-validate: .make-typing .make-lint .make-test .make-test-aws .make-functional-test dist
@echo -e "$(green)The project is validated$(normal)"
date >.make-validate
## Validate the project
validate: .make-validate
# ------------- Postgres database
## Print sqlite db url connection
sqlite-url:
@echo "sqlite3://test.db#haystack"
# ------------- Postgres database
## Start Postgres database in background
start-pg:
@docker start postgres || docker run \
--name postgres \
--hostname postgres \
-e POSTGRES_PASSWORD=password \
-p 5432:5432 \
-d postgres
echo -e "$(yellow)Postgres started$(normal)"
## Stop postgres database
stop-pg:
@docker stop postgres 2>/dev/null >/dev/null || true
echo -e "$(green)Postgres stopped$(normal)"
## Print Postgres db url connection
url-pg: start-pg
@IP=$$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' postgres)
echo "postgres://postgres:password@$$IP:5432/postgres#haystack"
pg-shell:
@IP=$$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' postgres)
docker exec -e PGPASSWORD=$(PG_PASSWORD) -it postgres psql -U postgres -h $$IP
clean-pg: start-pg
@IP=$$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' postgres)
docker exec -e PGPASSWORD=$(PG_PASSWORD) -t postgres psql -U postgres -h $$IP \
-c 'drop table if exists haystack;drop table if exists haystack_meta_datas;drop table if exists haystack_ts;'
## Start PGAdmin in background
start-pgadmin:
@docker start pgadmin || docker run \
--name pgadmin \
-p 8082:80 \
--link postgres \
-e 'PGADMIN_DEFAULT_EMAIL=$(PGADMIN_USER)' \
-e 'PGADMIN_DEFAULT_PASSWORD=$(PGADMIN_PASSWORD)' \
-d dpage/pgadmin4
echo -e "$(yellow)PGAdmin started (http://$(HOST_API):8082). Use $(cyan)$(PGADMIN_USER)$(yellow) and $(cyan)$(PGADMIN_PASSWORD)$(yellow) $(normal)"
## Stop PGAdmin
stop-pgadmin:
@docker stop pgadmin 2>/dev/null >/dev/null || true
echo -e "$(green)PGAdmin stopped$(normal)"
# ------------- MySQL database
## Start MySQL database in background
start-mysql:
@docker start mysql || docker run \
--name mysql \
--hostname mysql \
-e MYSQL_DATABASE="haystackdb" \
-e MYSQL_PASS="password" \
-e MYSQL_ROOT_PASSWORD=$(MYSQL_PASSWORD) \
-e MYSQL_USER=$(MYSQL_USER) \
-e MYSQL_PASSWORD=$(MYSQL_PASSWORD) \
-p 3306:3306 \
-d mysql
sleep 2
echo -e "$(yellow)MySQL started$(normal)"
## Stop MySQL database
stop-mysql:
@docker stop mysql 2>/dev/null >/dev/null || true
echo -e "$(green)MySQL stopped$(normal)"
## Print Postgres db url connection
url-mysql: start-mysql
@IP=$$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' mysql)
echo "mysql://mysql:password@$$IP:3306/haystackdb#haystack"
# Start a shell for MySQL
mysql-shell:
docker exec -it mysql mysql --user=root --password=$(MYSQL_PASSWORD) -h localhost haystackdb
# Clean MySQL database
clean-mysql: start-mysql
@IP=$$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' mysql)
docker exec -t mysql mysql --user=root --password=$(MYSQL_PASSWORD) -h localhost haystackdb \
--execute='drop table if exists haystack ; drop table if exists haystack_meta_datas ; drop table if exists haystack_ts;'
# ------------- Mongo database
## Start Mongo database in background
start-mongodb:
@docker start mongodb || docker run \
--name mongodb \
--hostname mongodb \
-e MONGO_INITDB_ROOT_USERNAME=$(MONGO_USER) \
-e MONGO_INITDB_ROOT_PASSWORD=$(MONGO_PASSWORD) \
--authenticationDatabase admin \
-p 27017-27019:27017-27019 \
-d haystackdb
sleep 2
echo -e "$(yellow)MongoDB started$(normal)"
## Stop mongo database
stop-mongodb:
@docker stop mongodb 2>/dev/null >/dev/null || true
echo -e "$(green)MongoDB stopped$(normal)"
## Start Mongo CI
mongo: start-mongodb
docker exec -it mongodb mongo mongodb://localhost/haystackdb
# Start shell in mongodo docker container
mongodb-shell:
docker exec -it mongodb mongosh
## Print Postgres db url connection
url-mongo: start-mongodb
@IP=$$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' mongodb)
echo "mongo://$$IP:3306/haystackdb#haystack"
# Clean Mongo database
clean-mongodb: start-mongodb
@docker exec -t mongodb mongosh mongodb://localhost/haystackdb \
--quiet --eval 'db.haystack.drop();db.haystack_ts.drop();db.haystack_meta_datas.drop();' >/dev/null
# --------------------------- Docker
## Build a Docker image with the project and current Haystack parameter (see `make dump-params`)
docker-build:
@echo "Build image with"
echo -e "$(green)PROVIDER=$${HAYSTACK_PROVIDER}"
echo -e "$(green)DB=$${HAYSTACK_DB}"
echo -e "$(green)TS=$${HAYSTACK_TS}"
@docker build \
--build-arg PORT='$(PORT)' \
--build-arg HAYSTACK_PROVIDER='$(HAYSTACK_PROVIDER)' \
--build-arg HAYSTACK_DB='$(HAYSTACK_DB)' \
--build-arg HAYSTACK_TS='$(HAYSTACK_TS)' \
--build-arg REFRESH='$(REFRESH)' \
--build-arg STAGE='$(STAGE)' \
--build-arg PIP_INDEX_URL='$(PIP_INDEX_URL)' \
--build-arg PIP_EXTRA_INDEX_URL='$(PIP_EXTRA_INDEX_URL)' \
--tag '$(DOCKER_REPOSITORY)/$(PRJ)' \
-f docker/Dockerfile .
echo -e "$(green)Docker image '$(yellow)$(DOCKER_REPOSITORY)/$(PRJ)$(green)' build with$(normal)"
$(MAKE) dump-params
## Run the docker image
docker-run: async-docker-stop docker-rm docker-inspect
docker run --rm \
-it \
--name '$(PRJ)' \
-p $(PORT):$(PORT) \
$(DOCKER_REPOSITORY)/$(PRJ)
# Print environment variables inside the docker image
docker-inspect:
@echo -e "$(green)Parameter inside the docker image:$(normal)"
docker inspect --format '{{join .Config.Env "\n" }}' $(DOCKER_REPOSITORY)/$(PRJ)
## Run the docker with a Flask server in background
async-docker-start: docker-rm
@docker run -dp $(PORT):$(PORT) --name '$(PRJ)' $(DOCKER_REPOSITORY)/$(PRJ)
echo -e "$(green)shift-4-haystack in docker is started$(normal)"
## Stop the background docker with a Flask server
async-docker-stop:
@docker stop '$(PRJ)' 2>/dev/null >/dev/null || true
echo -e "$(green)'$(DOCKER_REPOSITORY)/$(PRJ)' docker stopped$(normal)"
## Remove the docker image
docker-rm: async-docker-stop
@docker rm '$(PRJ)' >/dev/null || true
echo -e "$(green)'$(DOCKER_REPOSITORY)/$(PRJ)' docker removed$(normal)"
# Start the docker image with current shell
docker-run-shell:
docker run --rm \
-it \
--name '$(PRJ)' \
-p $(PORT):$(PORT) \
--entrypoint $(SHELL) \
$(DOCKER_REPOSITORY)/$(PRJ)
# --------------------------- Docker Make
.PHONY: docker-build-dmake docker-alias-dmake docker-inspect-dmake \
docker-configure-dmake docker-exec-dmake docker-inspect-dmake docker-rm-dmake
#-v "$(CONDA_BASE):/opt/conda"
_DOCKER_RUN_PARAMS=\
--rm \
-v "$(CONDA_BASE)/envs/:/opt/conda/envs" \
-v "$(PWD):/$(PRJ)" \