Skip to content

Commit f520e7c

Browse files
committed
Refactor makefile targets
1 parent 8903007 commit f520e7c

File tree

3 files changed

+73
-26
lines changed

3 files changed

+73
-26
lines changed

services/monitoring/Makefile

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,6 @@ update.grafana.pwd: .env ## Change grafana pwd
8484
grafanacontainerid=$$(docker ps | grep grafana | awk '{print $$1;}');\
8585
docker exec -ti $$grafanacontainerid grafana-cli admin reset-admin-password $$TRAEFIK_PASSWORD
8686

87-
.PHONY: ensure-grafana-online
88-
ensure-grafana-online:
89-
@set -o allexport; \
90-
source $(REPO_CONFIG_LOCATION); \
91-
set +o allexport; \
92-
url=$${TF_VAR_grafana_url}; \
93-
echo "Waiting for grafana at $$url to become reachable."; \
94-
while true; do \
95-
status_code=$$(curl -k -o /dev/null -s -w "%{http_code}" $$url); \
96-
if [ "$$status_code" -ge 200 ] && [ "$$status_code" -lt 400 ]; then \
97-
echo "Grafana is online"; \
98-
break; \
99-
else \
100-
echo "Grafana still unreachable, waiting 5s for grafana to become reachable..."; \
101-
sleep 5; \
102-
fi; \
103-
done;
10487

10588
.PHONY: grafana-export
10689
grafana-export: .venv## Export the remote grafana dashboards and datasources TO YOUR LOCAL MACHINE
@@ -110,14 +93,10 @@ grafana-export: .venv## Export the remote grafana dashboards and datasources TO
11093
python3 export.py;
11194

11295
.PHONY: grafana-import
113-
grafana-import: grafana/assets ensure-grafana-online ## Imports AND OVERWRITES the remote grafana dashboards and datasources FROM YOUR LOCAL MACHINE
114-
@set -o allexport; \
115-
source $(REPO_CONFIG_LOCATION); \
116-
set +o allexport; \
117-
pushd ${REPO_BASE_DIR}/services/monitoring/grafana/terraform && \
118-
terraform init && \
119-
terraform destroy -auto-approve && \
120-
terraform apply -auto-approve; \
96+
grafana-import: grafana/assets ## Imports AND OVERWRITES the remote grafana dashboards and datasources FROM YOUR LOCAL MACHINE
97+
@pushd ${REPO_BASE_DIR}/services/monitoring/grafana && \
98+
$(MAKE) terraform-plan && \
99+
$(MAKE) terraform-apply; \
121100
popd > /dev/null
122101

123102

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
.DEFAULT_GOAL := help
2+
REPO_BASE_DIR := $(shell git rev-parse --show-toplevel)
3+
include ${REPO_BASE_DIR}/scripts/common.Makefile
4+
5+
6+
7+
# Internal VARIABLES ------------------------------------------------
8+
TF_STATE_FILE := terraform/.terraform/terraform.tfstate
9+
10+
terraform/main.tf: terraform/main.tf.j2 .venv $(CI_ENV_FILE)
11+
# generate $@
12+
@$(call jinja, $<, $(CI_ENV_FILE), $@)
13+
# validate and format $@
14+
15+
terraform-init: $(TF_STATE_FILE) ## init terraform
16+
17+
$(TF_STATE_FILE): $(CI_ENV_FILE) terraform/main.tf
18+
# terraform init
19+
@set -a; source $<; set +a; \
20+
if [ "$${TF_STATE_BACKEND_TYPE}" = "local" ]; then \
21+
terraform -chdir=./terraform init; \
22+
else \
23+
terraform -chdir=./terraform init -backend-config="access_key=$${TF_BACKEND_ACCESS_KEY}" -backend-config="secret_key=$${TF_BACKEND_SECRET_KEY}"; \
24+
fi
25+
26+
terraform/plan.cache:
27+
@echo "$@ file not found. Run 'make terraform-plan' to generate it."
28+
@exit 1
29+
30+
.PHONY: terraform-plan
31+
terraform-plan: $(CI_ENV_FILE) $(TF_STATE_FILE) ensure-grafana-online ## terraform plan
32+
# terraform plan
33+
@set -a; source $<; source $${REPO_CONFIG_LOCATION}; set +a; \
34+
TF_VAR_aws_region=$${TF_AWS_REGION} TF_VAR_aws_access_key=$${TF_AWS_ACCESS_KEY_ID} TF_VAR_aws_secret_key=$${TF_AWS_SECRET_ACCESS_KEY} terraform -chdir=./terraform plan -out=plan.cache
35+
36+
.PHONY: terraform-apply
37+
terraform-apply: $(CI_ENV_FILE) terraform/plan.cache $(TF_STATE_FILE) ensure-grafana-online ## terraform apply
38+
# terraform apply
39+
@set -a; source $<; set +a; \
40+
TF_VAR_aws_region=$${TF_AWS_REGION} TF_VAR_aws_access_key=$${TF_AWS_ACCESS_KEY_ID} TF_VAR_aws_secret_key=$${TF_AWS_SECRET_ACCESS_KEY} terraform -chdir=./terraform apply plan.cache
41+
42+
.PHONY: ensure-grafana-online
43+
ensure-grafana-online:
44+
@set -o allexport; \
45+
source $(REPO_CONFIG_LOCATION); \
46+
set +o allexport; \
47+
url=$${TF_VAR_GRAFANA_URL}; \
48+
echo "Waiting for grafana at $$url to become reachable..."; \
49+
while true; do \
50+
status_code=$$(curl -k -o /dev/null -s -w "%{http_code}" $$url); \
51+
if [ "$$status_code" -ge 200 ] && [ "$$status_code" -lt 400 ]; then \
52+
echo "Grafana is online"; \
53+
break; \
54+
else \
55+
echo "Grafana still unreachable, waiting 5s for grafana to become reachable..."; \
56+
sleep 5; \
57+
fi; \
58+
done;

services/monitoring/grafana/terraform/main.tf renamed to services/monitoring/grafana/terraform/main.tf.j2

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
terraform {
22
required_version = "~> 1.5.1"
3-
backend "local" {
3+
{% if TF_STATE_BACKEND_TYPE != "s3" %}
4+
backend "{{TF_STATE_BACKEND_TYPE}}" {
45
path = "terraform.tfstate" # Specify the path for the state file, can be a different path if needed
56
}
7+
{% else %}
8+
backend "s3" {
9+
key = "{{ TF_STATE_S3_KEY}}"
10+
dynamodb_table = "{{ TF_STATE_S3_DYNAMODB_LOCK_TABLE }}"
11+
encrypt = true
12+
bucket = "{{ TF_STATE_S3_BUCKET_NAME }}"
13+
region = "{{ TF_STATE_S3_REGION }}"
14+
}
15+
{% endif %}
616
required_providers {
717
grafana = {
818
source = "grafana/grafana"

0 commit comments

Comments
 (0)