-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
128 lines (107 loc) · 3.11 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
define check_url
curl -k -s -o /dev/null \
--write-out %{http_code} \
$(1) \
| grep 200 \
|| exit 1
endef
# get envvars from tfvars
name = $(shell grep -e name infrastructure/terraform.tfvars | cut -d '=' -f 2 | tr -d '[:space:]' | tr -d '\"')
env = $(shell grep -e env infrastructure/terraform.tfvars | cut -d '=' -f 2 | tr -d '[:space:]' | tr -d '\"')
image_tag = $(shell grep -e IMAGE_VERSION code/Dockerfile | cut -d '=' -f 2 | tr -d '[:space:]' | tr -d '\"')
region = $(shell grep -e region infrastructure/terraform.tfvars | cut -d '=' -f 2 | tr -d '[:space:]' | tr -d '\"')
# ensure requirements are installed
checkdependencies:
which aws \
&& which curl \
&& which docker \
&& which python3 \
&& which terraform \
|| exit 1
# ensure variables are set
checkvariables:
ifndef AWS_ACCOUNT_NUMBER
$(error AWS_ACCOUNT_NUMBER is undefined)
endif
ifeq (, $(name))
$(error name var not set)
endif
ifeq (, $(env))
$(error env var not set)
endif
ifeq (, $(image_tag))
$(error image_tag var not set)
endif
full_image_path := $(AWS_ACCOUNT_NUMBER).dkr.ecr.$(region).amazonaws.com/$(name)-$(env):$(image_tag)
repo_path := $(AWS_ACCOUNT_NUMBER).dkr.ecr.$(region).amazonaws.com
# login to ECR
codelogin:
pushd code \
&& aws ecr get-login-password --region $(region) | docker login --username AWS --password-stdin $(repo_path) \
|| exit 1; \
popd
# build docker image
codebuild:
pushd code \
&& docker build -t $(full_image_path) . \
|| exit 1; \
popd
# push docker image
codepush:
pushd code \
&& docker push $(full_image_path) \
|| exit 1; \
popd
# basic linting
tffmt:
pushd infrastructure \
&& terraform fmt -recursive -list=true -check=true \
|| exit 1; \
popd
# init terraform
tfinit:
pushd infrastructure \
&& terraform init --reconfigure \
|| exit 1; \
popd
# validate terraform
tfvalidate:
pushd infrastructure \
&& terraform validate \
|| exit 1; \
popd
# apply ECR terraform only, prompting user for confirmation
# this is ugly but required since ECR has to exist before the docker image can be pushed up
tfecrapply:
pushd infrastructure \
&& terraform apply -target="aws_ecr_repository.ecr" \
|| exit 1; \
popd
# apply terraform, prompting user for confirmation
tfapply:
pushd infrastructure \
&& terraform apply \
|| exit 1; \
popd
# destroy all terraform infrastructure, prompting user for confirmation
tfdestroy:
pushd infrastructure \
&& terraform destroy \
|| exit 1; \
popd
test:
$(call check_url,$(shell cat infrastructure/.alb_dns_name))
prep: checkdependencies checkvariables
codetest: prep codebuild
docker run -it --entrypoint='' $(full_image_path) black --diff --check . \
&& docker run -it --entrypoint='' $(full_image_path) flake8 --exclude .git,__pycache__,.venv . \
&& docker run -it --entrypoint='' $(full_image_path) bandit -r . \
&& docker run -d -p 5000:5000 --name flasklocaltest $(full_image_path) \
&& sleep 5 \
&& $(call check_url,127.0.0.1:5000) \
&& docker stop flasklocaltest \
&& docker rm flasklocaltest
wait:
sleep 15
destroy: prep tfinit tfdestroy
all: prep tffmt tfinit tfvalidate tfecrapply codetest codepush tfapply wait test