-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMakefile
executable file
·235 lines (196 loc) · 8.43 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
SHELL:=/bin/bash
PROJECT_DIR = $(shell pwd)
include $(PROJECT_DIR)/build/admin.mk
include $(PROJECT_DIR)/build/help.mk
MODULE_NAME = "GLOBAL"
ENV_PATH = $(PROJECT_DIR)/env/$(ENV)
GLOBAL_ENV_FILE = $(ENV_PATH)/global.env
SECRET_ENV_FILE = $(ENV_PATH)/secret.env
TF_PATH = $(PROJECT_DIR)/tf/caendr
MODULE_PATH = $(PROJECT_DIR)/src/modules
-include $(GLOBAL_ENV_FILE)
LOAD_GLOBAL_ENV=export $$(cat $(GLOBAL_ENV_FILE) | sed $(WHITESPACE_REGEX) | sed $(COMMENT_REGEX) | xargs)
LOAD_GLOBAL_ENV=export $$(cat $(GLOBAL_ENV_FILE) | sed $(WHITESPACE_REGEX) | sed $(COMMENT_REGEX) | xargs)
LOAD_TF_VAR=export $$(cat $(GLOBAL_ENV_FILE) | sed $(WHITESPACE_REGEX) | sed $(COMMENT_REGEX) | sed $(TF_VAR_PREFIX_REGEX) | xargs)
LOAD_SECRET_TF_VAR=export $$(cat $(SECRET_ENV_FILE) | sed $(WHITESPACE_REGEX) | sed $(COMMENT_REGEX) | sed $(TF_VAR_PREFIX_REGEX) | xargs)
TF_SELECT_WORKSPACE=(terraform workspace new $(ENV) || (echo "Switching to existing workspace \"$(ENV)\"" && terraform workspace select $(ENV)))
all: help
targets: configure cloud-resource-plan cloud-resource-deploy cloud-resource-destroy
.PHONY : targets
.DEFAULT : help
#~
configure: #~
#~ If using Ubuntu for the development environment, use this target to install
#~ required system packages for testing and deploying project modules
#~ including docker, google cloud sdk, terraform, etc...
configure:
ifeq ($(getend group admin),)
else
@echo -e "\n$(COLOR_B)Creating docker user group...$(COLOR_N)" && \
sudo groupadd docker
endif
@echo -e "\n$(COLOR_B)Installing system packages...$(COLOR_N)"
sudo apt-get update && sudo apt-get install \
apt-transport-https \
build-essential \
ca-certificates \
curl \
fuse \
gawk \
git \
gnupg \
graphviz \
gunicorn \
gzip \
libbz2-dev \
libgraphviz-dev \
liblzma-dev \
libncursesw5-dev \
libncurses5-dev \
libxml2 \
libxml2-dev \
libxmlsec1-dev \
libxmlsec1-openssl \
make \
pkg-config \
python3 \
python3-dev \
python3-venv \
python3-virtualenv \
software-properties-common \
tabix \
virtualenv \
vim \
wget \
xmlsec1 \
zip \
zlib1g-dev
@echo -e "\n$(COLOR_B)Installing docker.io...$(COLOR_N)"
sudo apt-get install docker.io
@echo -e "\n$(COLOR_B)Adding current USER:$(USER) to docker group...$(COLOR_N)"
sudo usermod -aG docker $(USER)
@echo -e "\n$(COLOR_B)Installing Terraform...$(COLOR_N)" && \
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add - && \
sudo apt-add-repository "deb [arch=$$(dpkg --print-architecture) ] https://apt.releases.hashicorp.com $$(lsb_release -cs) main" && \
sudo apt-get update && sudo apt-get install terraform
@echo -e "\n$(COLOR_B)Installing Google Cloud SDK...$(COLOR_N)" && \
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add - && \
sudo apt-get update && sudo apt-get install google-cloud-sdk
@echo -e "\n$(COLOR_B)Installing Google Cloud SQL Proxy...$(COLOR_N)" && \
wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy && \
chmod +x cloud_sql_proxy && \
mkdir /cloudql
@echo -e "\n$(COLOR_B)Configuring Google Cloud SDK...$(COLOR_N)" && \
gcloud init && \
gcloud auth login && \
gcloud auth application-default login && \
gcloud auth configure-docker
#~
terraform-shell: #~
#~ Initializes terraform providers and loads the backend.hcl config
#~ if it exists in the environment directory, or use a local backend otherwise.
terraform-shell:
@echo -e "\n$(COLOR_B)Initializing Terraform...$(COLOR_N)"
ifneq ("$(wildcard $(ENV_PATH)/backend.hcl)","")
$(LOAD_GLOBAL_ENV) && $(LOAD_TF_VAR) && $(LOAD_SECRET_TF_VAR) && cd $(TF_PATH) && \
echo terraform init -reconfigure -backend-config=$(ENV_PATH)/backend.hcl && \
$(TF_SELECT_WORKSPACE) && \
$(SHELL)
else
$(LOAD_GLOBAL_ENV) && $(LOAD_TF_VAR) && $(LOAD_SECRET_TF_VAR) && cd $(TF_PATH) && \
echo terraform init && \
$(TF_SELECT_WORKSPACE) && \
$(SHELL)
endif
#~
cloud-resource-init: #~
#~ Initializes terraform providers and loads the backend.hcl config
#~ if it exists in the environment directory, or use a local backend otherwise.
cloud-resource-init:
@echo -e "\n$(COLOR_B)Initializing Terraform...$(COLOR_N)"
ifneq ("$(wildcard $(ENV_PATH)/backend.hcl)","")
$(LOAD_GLOBAL_ENV) && $(LOAD_TF_VAR) && $(LOAD_SECRET_TF_VAR) && cd $(TF_PATH) && \
terraform init -reconfigure -backend-config=$(ENV_PATH)/backend.hcl
else
$(LOAD_GLOBAL_ENV) && $(LOAD_TF_VAR) && $(LOAD_SECRET_TF_VAR) && cd $(TF_PATH) && \
terraform init
endif
#~
cloud-resource-plan: #~
#~ Generates a terraform plan for the infrastructure described in ./env/[environment]/terraform
#~ including any service-specific terraform modules that are required
cloud-resource-plan: cloud-resource-init
@echo -e "\n$(COLOR_B)Creating Terraform plan for changes to cloud infrastructure...$(COLOR_N)" && \
$(LOAD_GLOBAL_ENV) && $(LOAD_TF_VAR) && $(LOAD_SECRET_TF_VAR) && \
cd $(TF_PATH) && rm -rf tf_plan && \
$(TF_SELECT_WORKSPACE) && \
terraform plan -out tf_plan
@echo -e "$(COLOR_G)DONE!$(COLOR_N)\n"
@echo -e "\n\nRun this command to apply the terraform plan: $(COLOR_G)'make cloud-resource-deploy ENV=$(ENV)'$(COLOR_N)\n"
.PHONY: docker-daemon
docker-daemon:
@echo "Checking if Docker Daemon is running..."
@docker info > /dev/null
@echo "OK"
#~
cloud-resource-refresh: #~
#~ Executes the generated terraform plan for deploying infrastructure described
#~ in ./env/[environment]/terraform including any service-specific terraform modules that are required
cloud-resource-refresh: cloud-resource-init docker-daemon
@echo -e "\n$(COLOR_B)Deploying the Terraform cloud resource plan...$(COLOR_N)" && \
$(LOAD_GLOBAL_ENV) && $(LOAD_TF_VAR) && $(LOAD_SECRET_TF_VAR) && \
cd $(TF_PATH) && \
rm -rf tf_plan && \
$(TF_SELECT_WORKSPACE) && \
terraform apply -refresh-only -auto-approve
@echo -e "$(COLOR_G)DONE!$(COLOR_N)\n"
#~
cloud-resource-deploy: #~
#~ Executes the generated terraform plan for deploying infrastructure described
#~ in ./env/[environment]/terraform including any service-specific terraform modules that are required
cloud-resource-deploy: cloud-resource-init docker-daemon
@echo -e "\n$(COLOR_B)Deploying the Terraform cloud resource plan...$(COLOR_N)" && \
$(LOAD_GLOBAL_ENV) && $(LOAD_TF_VAR) && $(LOAD_SECRET_TF_VAR) && \
cd $(TF_PATH) && \
rm -rf tf_plan && \
$(TF_SELECT_WORKSPACE) && \
terraform apply -refresh-only -auto-approve && \
terraform plan -out tf_plan && \
$(MAKE) -C $(PROJECT_DIR) confirm --no-print-directory && \
terraform apply "tf_plan"
@echo -e "$(COLOR_Y)GAE -- Login to GCP and delete all the older versions of GAE no longer in use!$(COLOR_N)\n"
@echo -e "$(COLOR_G)DONE!$(COLOR_N)\n"
container-site-v2:
cd src/modules/site-v2 && make clean && (echo | make container)
container-db-operations:
cd src/modules/site-v2 && make clean && (echo | make container)
container-api-pipeline:
cd src/modules/site-v2 && make clean && (echo | make container)
#~
containers: #~
#~ Builds containers for the site-v2, db-operations, and api-pipeline
containers: container-site-v2 container-db-operations container-api-pipeline
publish-container-site-v2:
cd src/modules/site-v2 && make clean && (echo | make publish)
publish-container-db-operations:
cd src/modules/site-v2 && make clean && (echo | make publish)
publish-container-api-pipeline:
cd src/modules/api/pipeline-task && make clean && (echo | make publish)
#~
publish-containers: #~
#~ Publishes the containers for the site-v2, db-operations, and api-pipeline
publish-containers: publish-container-site-v2 publish-container-db-operations publish-container-api-pipeline
#~
cloud-resource-destroy: #~
#~ Destroys all cloud resources provisioned by terraform
cloud-resource-destroy: cloud-resource-init
@echo -e "\n$(COLOR_B)DESTROYING ALL TERRAFORM PROVISIONED CLOUD RESOURCES.\nARE YOU SURE YOU WANT TO DO THIS?$(COLOR_N)"
@$(LOAD_GLOBAL_ENV) && $(LOAD_TF_VAR) && $(LOAD_SECRET_TF_VAR) && \
$(MAKE) -C $(PROJECT_DIR) confirm --no-print-directory && \
cd $(TF_PATH) && rm -rf tf_plan && \
$(TF_SELECT_WORKSPACE) && \
terraform destroy
@echo -e "$(COLOR_G)DONE!$(COLOR_N)\n"
%:
@: