-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
149 lines (113 loc) · 5.63 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
ROLE ?= 'default' ## make {func} ROLE=<AWS_ACCOUNT_ROLE>
REGION ?= 'us-east-1' ## make {func} REGION=<AWS_TARGET_REGION>
###############################################
# Global Variables
# - Setup and templating variables
###############################################
SHELL := /bin/bash
CHDIR_SHELL := $(SHELL)
OS := darwin
BASE_DIR := $(shell pwd)
#ACCOUNT_ID := $(shell aws sts --profile $(ROLE) get-caller-identity --output text --query 'Account')
INVENTORY := $(shell which terraform-inventory |awk '{print$3}')
STATE_DIR := $(BASE_DIR)/_states
LOGS_DIR := $(BASE_DIR)/_logs
KEYS_DIR := $(BASE_DIR)/_keys
MODULE := $(BASE_DIR)/modules
ANSIBLE := $(BASE_DIR)/ansible
## Example directories for all prerequisites
DEFAULT := $(BASE_DIR)/examples/default
INIT := $(BASE_DIR)/examples/default/init
## Default generics to test until I move it over to Rake
default: test
all: sonarqube provision
rebuild: destroy all
###############################################
# Helper functions
# - follows best practices design patterns
###############################################
define chdir
$(eval _D=$(firstword $(1) $(@D)))
$(info $(MAKE): cd $(_D)) $(eval SHELL = cd $(_D); $(CHDIR_SHELL))
endef
.check-region:
@if test "$(REGION)" = ""; then echo "REGION not set"; exit 1; fi
.check-role:
@if test "$(ROLE)" = ""; then echo "ROLE not set"; exit 1; fi
.directory-%:
$(call chdir, ${${*}})
.assert-%:
@if [ "${${*}}" = "" ]; then \
echo "[✗] Variable ${*} not set" ; exit 1 ; \
else \
echo "[√] ${*} set as: ${${*}}" ; \
fi
.roles: .directory-ANSIBLE
[[ `ls roles/*/ 2>/dev/null` ]] && rm -fr roles/* ; \
sed -e "s/<SSH_KEYFILE>/$(ROLE)/" ansible.tmpl.cfg >| ansible.cfg ; \
ansible-galaxy install -r requirements.yml
###############################################
# Generic functions
###############################################
graph: .directory-MODULE
terraform init && terraform graph |dot -Tpng >| $(LOGS_DIR)/graph.png
clean:
@rm -rf $(TERRAFORM)/.terraform
@rm -f $(LOGS_DIR)/graph.png
@rm -f $(LOGS_DIR)/*.log
globals:
@echo "REGION set to: $(REGION)"
@echo "ROLE set to: $(ROLE)"
###############################################
# Testing functions
# - follow testing design patterns
###############################################
test:
@echo 'No tests currently configured...'
###############################################
# Deployment functions
# - follows deployment patterns
###############################################
init: .directory-MODULE
terraform init
preflight-init: .directory-INIT .check-region
terraform init \
&& aws-vault exec $(ROLE) --assume-role-ttl=60m -- terraform plan \
-var region=$(REGION) \
-var key_name=$(ROLE) \
2>&1 |tee $(LOGS_DIR)/pre-plan.log ; \
\
aws-vault exec $(ROLE) --assume-role-ttl=60m -- terraform apply \
-state=$(STATE_DIR)/$(ROLE)-pre_terraform.tfstate \
-var region=$(REGION) \
-var key_name=$(ROLE) \
-auto-approve \
2>&1 |tee $(LOGS_DIR)/pre-apply.log
preflight-output:
@if [ ! -f "$(STATE_DIR)/$(ROLE)-pre_terraform.tfstate" ]; then make pre-build ROLE=$(ROLE) ; fi
export ARN=$(shell terraform output -state=$(STATE_DIR)/$(ROLE)-pre_terraform.tfstate |awk -F ' = ' '{print$$2}') \
echo $(ARN)
sonarqube: init .directory-MODULE .check-region
aws-vault exec $(ROLE) --assume-role-ttl=60m -- terraform plan \
-var region=$(REGION) \
-var key_name=$(ROLE) \
2>&1 |tee $(LOGS_DIR)/sonarqube-plan.log ; \
\
aws-vault exec $(ROLE) --assume-role-ttl=60m -- terraform apply \
-state=$(STATE_DIR)/$(ROLE)_terraform.tfstate \
-var region=$(REGION) \
-var key_name=$(ROLE) \
-auto-approve \
2>&1 |tee $(LOGS_DIR)/sonarqube-apply.log
destroy: init .directory-MODULE .check-region
@echo -e "\n\n\n\nsonarqube-destroy: $(date +"%Y-%m-%d @ %H:%M:%S")\n" \
>> $(LOGS_DIR)/sonarqube-destroy.log
aws-vault exec $(ROLE) --assume-role-ttl=60m -- terraform destroy \
-state=$(STATE_DIR)/$(ROLE)_terraform.tfstate \
-var region=$(REGION) \
-var key_name=$(ROLE) \
-auto-approve \
2>&1 |tee $(LOGS_DIR)/sonarqube-destroy.log
ssh: .directory-MODULE
exec `terraform output -state=$(STATE_DIR)/$(ROLE)_terraform.tfstate \
|head -1 |awk -F' = ' '{print$$2}' |sed 's/.\//..\//'`