-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
137 lines (119 loc) · 6.81 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
.DEFAULT_GOAL := help
SHELL := /usr/bin/env bash
MAKEFLAGS += --no-print-directory
BP_AGENT_USER := bp-agent
CI ?= false
MKFILEDIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
MSG_INFO := "\033[36m[INFO] %s\033[0m\n"
MSG_WARN := "\033[0;33m[WARN] %s\033[0m\n"
MSG_ERROR := "\033[0;31m[ERROR] %s\033[0m\n"
#https://developer.hashicorp.com/terraform/internals/debugging
export TF_LOG=INFO
export TF_LOG_PATH=$(MKFILEDIR)/blueprints/terraform.log
define confirmation
@if [ $(CI) == false ]; then \
echo -n "Asking for your confirmation to $(1) [yes/No]" && read ans && [ $${ans:-No} = yes ] ; fi
endef
#https://aws-ia.github.io/terraform-aws-eks-blueprints/getting-started/#deploy
define deploy
@printf $(MSG_INFO) "Deploying CloudBees CD Blueprint $(1) ..."
$(call confirmation,Deploy $(1))
@terraform -chdir=$(MKFILEDIR)/blueprints/$(1) init
@terraform -chdir=$(MKFILEDIR)/blueprints/$(1) apply -target="module.vpc" -auto-approve
@terraform -chdir=$(MKFILEDIR)/blueprints/$(1) apply -target="module.eks" -auto-approve
@terraform -chdir=$(MKFILEDIR)/blueprints/$(1) apply -auto-approve
@terraform -chdir=$(MKFILEDIR)/blueprints/$(1) output > $(MKFILEDIR)/blueprints/$(1)/terraform.output
endef
#https://aws-ia.github.io/terraform-aws-eks-blueprints/getting-started/#destroy
define destroy
@printf $(MSG_INFO) "Destroying CloudBees CD Blueprint $(1) ..."
$(call confirmation,Destroy $(1))
@terraform -chdir=$(MKFILEDIR)/blueprints/$(1) destroy -target=module.eks_blueprints_addon_cbcd -auto-approve
@terraform -chdir=$(MKFILEDIR)/blueprints/$(1) destroy -target=module.eks_blueprints_addons -auto-approve
@terraform -chdir=$(MKFILEDIR)/blueprints/$(1) destroy -target=module.eks -auto-approve
@terraform -chdir=$(MKFILEDIR)/blueprints/$(1) destroy -auto-approve
@rm -f $(MKFILEDIR)/blueprints/$(1)/terraform.output
endef
define validate
@printf $(MSG_INFO) "Validating CloudBees CD availability for $(1) ..."
$(call confirmation,Validate $(1))
@source blueprints/helpers.sh && probes-common $(1)
@if [ "$(1)" == "01-getting-started" ]; then \
source blueprints/helpers.sh && probes-bp01 ; fi
@if [ "$(1)" == "02-at-scale" ]; then \
source blueprints/helpers.sh && probes-bp02 ; fi
endef
define clean
@cd blueprints/$(ROOT) && find -name ".terraform" -type d | xargs rm -rf
@cd blueprints/$(ROOT) && find -name ".terraform.lock.hcl" -type f | xargs rm -f
@cd blueprints/$(ROOT) && find -name "kubeconfig_*.yaml" -type f | xargs rm -f
@cd blueprints/$(ROOT) && find -name "terraform.output" -type f | xargs rm -f
@cd blueprints && find -name terraform.log -type f | xargs rm -f
endef
.PHONY: dRun
dRun: ## Build (if not locally present) and Run the Blueprint Agent using Bash as Entrypoint. It is ideal starting point for all targets. Example: make dRun
dRun:
$(eval IMAGE := $(shell docker image ls | grep -c local.cloudbees/bp-agent-cd))
@if [ "$(IMAGE)" == "0" ]; then \
printf $(MSG_INFO) "Building Docker Image local.cloudbees/bp-agent-cd:latest" && \
docker build . --file $(MKFILEDIR)/blueprints/Dockerfile --tag local.cloudbees/bp-agent-cd:latest; \
fi
docker run --rm -it --name bp-agent-cd \
-v $(MKFILEDIR):/$(BP_AGENT_USER)/cbcd-eks-addon -v $(HOME)/.aws:/$(BP_AGENT_USER)/.aws \
local.cloudbees/bp-agent-cd:latest
.PHONY: tfpreFlightChecks
tfpreFlightChecks: ## Run preflight checks for terraform according to getting-started/README.md . Example: ROOT=02-at-scale make tfpreFlightChecks
tfpreFlightChecks: guard-ROOT
@if [ "$(shell whoami)" != "$(BP_AGENT_USER)" ]; then printf $(MSG_WARN) "$(BP_AGENT_USER) user is not detected. Note that blueprints validations use the companion Blueprint Docker Agent available via: make dRun"; fi
@if [ ! -f blueprints/$(ROOT)/.auto.tfvars ]; then printf $(MSG_ERROR) "blueprints/$(ROOT)/.auto.tfvars file does not exist and it is required to store your own values"; exit 1; fi
@if ([ ! -f blueprints/$(ROOT)/k8s/flow_db_secrets-values.yml ] && [ $(ROOT) == "02-at-scale" ]); then printf $(MSG_ERROR) "blueprints/$(ROOT)/flow_db_secrets-values.yml file does not exist and it is required to store your secrets"; exit 1; fi
$(eval USER_ID := $(shell aws sts get-caller-identity | grep UserId | cut -d"," -f 1 | xargs ))
@if [ "$(USER_ID)" == "" ]; then printf $(MSG_ERROR) "AWS Authention for CLI is not configured" && exit 1; fi
@printf $(MSG_INFO) "Preflight Checks OK for $(USER_ID)"
.PHONY: deploy
deploy: ## Deploy Terraform Blueprint passed as parameter. Example: ROOT=02-at-scale make deploy
deploy: guard-ROOT tfpreFlightChecks
$(call deploy,$(ROOT))
.PHONY: destroy
destroy: ## Destroy Terraform Blueprint passed as parameter. Example: ROOT=02-at-scale make destroy
destroy: guard-ROOT tfpreFlightChecks
ifneq ("$(wildcard blueprints/$(ROOT)/terraform.output)","")
$(call destroy,$(ROOT))
else
@printf $(MSG_ERROR) "Blueprint $(ROOT) did not complete the Deployment target. It is not Ready for Destroy target but it is possible to destroy manually https://aws-ia.github.io/terraform-aws-eks-blueprints/getting-started/#destroy"
endif
.PHONY: clean
clean: ## Clean Blueprint passed as parameter. Example: ROOT=02-at-scale make clean
clean: guard-ROOT tfpreFlightChecks
$(call clean,$(ROOT))
.PHONY: tfAction
tfAction: ## Any Terraform Action for Blueprint passed as parameters. Usage: ROOT=02-at-scale ACTION="status list" make tf_action
tfAction: guard-ROOT guard-ACTION tfpreFlightChecks
terraform -chdir=blueprints/$(ROOT) $(ACTION)
.PHONY: validate
validate: ## Validate CloudBees CD Blueprint deployment passed as parameter. Example: ROOT=02-at-scale make validate
validate: guard-ROOT tfpreFlightChecks
ifneq ("$(wildcard blueprints/$(ROOT)/terraform.output)","")
$(call validate,$(ROOT))
else
@printf $(MSG_ERROR) "Blueprint $(ROOT) did not complete the Deployment target thus it is not Ready to be validated."
endif
blueprints/$(ROOT)/terraform.output: deploy
.PHONY: test
test: blueprints/$(ROOT)/terraform.output ## Runs a test for blueprint passed as parameters throughout their Terraform Lifecycle. Example: ROOT=02-at-scale make test
@printf $(MSG_INFO) "Running Test for $(ROOT) blueprint ..."
$(call validate,$(ROOT))
$(call destroy,$(ROOT))
$(call clean,$(ROOT))
.PHONY: test-all
test-all: ## Runs test for all blueprints throughout their Terraform Lifecycle. Example: make test
test-all:
@printf $(MSG_INFO) "Running Test for all blueprints ..."
@source $(MKFILEDIR)/blueprints/helpers.sh && test-all
.PHONY: help
help: ## Makefile Help Page
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n\nTargets:\n"} /^[\/\%a-zA-Z_-]+:.*?##/ { printf " \033[36m%-21s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST) 2>/dev/null
@printf "\nDebug: Use -d flag with targets. Example: ROOT=02-at-scale make -d validate \n\n"
.PHONY: guard-%
guard-%:
@if [[ "${${*}}" == "" ]]; then printf "\033[0;31m[ERROR]\033[0m %s\n" "Environment variable $* not set."; exit 1; fi