Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add: Manage grafana content/assets via terraform #913

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f0d8cf0
wip
mrnicegyu11 Sep 19, 2024
e906b41
Merge remote-tracking branch 'upstream/main' into main
mrnicegyu11 Oct 23, 2024
14c751d
Merge remote-tracking branch 'upstream/main' into main
mrnicegyu11 Oct 23, 2024
293f63c
Add csi-s3 and have portainer use it
mrnicegyu11 Oct 24, 2024
f7f72ec
Change request @hrytsuk 1GB max portainer volume size
mrnicegyu11 Oct 25, 2024
94cfb76
t push
mrnicegyu11 Oct 28, 2024
509c717
Merge remote-tracking branch 'upstream/main'
mrnicegyu11 Oct 29, 2024
1a65ecf
Merge remote-tracking branch 'upstream/main'
mrnicegyu11 Nov 13, 2024
77ee45e
Merge remote-tracking branch 'upstream/main'
mrnicegyu11 Nov 25, 2024
c9c70d6
Arch Linux Certificates Customization
mrnicegyu11 Dec 3, 2024
7b8be53
Merge remote-tracking branch 'upstream/main'
mrnicegyu11 Dec 5, 2024
bcd61cd
Merge remote-tracking branch 'upstream/main'
mrnicegyu11 Dec 12, 2024
58e1030
Merge remote-tracking branch 'upstream/main'
mrnicegyu11 Dec 13, 2024
5b1c3fb
Add grafana terrform tooling
mrnicegyu11 Dec 16, 2024
00bc0c7
Make osparc-config dotenv-precommit pass: Use all caps env-vars
mrnicegyu11 Dec 16, 2024
2e009bc
Refactoring: jinja2 takes .env file path as explicit argument (like i…
mrnicegyu11 Dec 16, 2024
8903007
Make CI_ENV_FILE vailable in makefile
mrnicegyu11 Dec 16, 2024
f520e7c
Refactor makefile targets
mrnicegyu11 Dec 16, 2024
764bb22
Add grafana terraform gitignore
mrnicegyu11 Dec 16, 2024
ead277a
Rename envvar: TF_STATE_S3_GRAFANAKEY
mrnicegyu11 Dec 16, 2024
13f92ba
Remove old scripts, makefile targets
mrnicegyu11 Dec 18, 2024
ebe66c0
Remove unused files
mrnicegyu11 Dec 18, 2024
fcab094
undue arch style commit
mrnicegyu11 Dec 18, 2024
2ba8070
Remove references to Tempo
mrnicegyu11 Dec 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions scripts/common.Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ $(if $(REPO_CONFIG_LOCATION),,$(error The location of the repo.config file given
$(if $(shell cat $(REPO_CONFIG_LOCATION)),,$(error The location of the repo.config file given in .config.location is invalid. Aborting))
$(if $(shell wc -l $(REPO_BASE_DIR)/.config.location | grep 1),,$(error The .config.location file has more than one path specified. Only one path is allowed. Aborting))

# Extract DEPLOYMENT_FQDN using Make functions
DEPLOYMENT_FQDN := $(notdir $(patsubst %/,%, $(dir $(REPO_CONFIG_LOCATION))))
# Construct CI_ENV_FILE using Make functions
CI_ENV_FILE := $(realpath $(dir $(REPO_CONFIG_LOCATION))/../../.gitlab/pipelines/1_configurations/$(DEPLOYMENT_FQDN)/ci.env)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if hardcoding assumptions about the other repo here is sustainable.

We can brainstorm together what can be done and if we can avoid using ci.env file here

$(if $(CI_ENV_FILE),,$(error The location of the repo.config file given in .config.location is invalid. Cannot find the ci.env file. Aborting))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not going to work in CI where we do not have ci.env file

$(if $(shell cat $(CI_ENV_FILE)),,$(error The location of the repo.config file given in .config.location is invalid. Cannot find the ci.env file. Aborting))

ifeq ($(_yq),)
_yq = docker run --rm -i -v $${PWD}:/workdir mikefarah/yq:4.30.4
endif
Expand Down Expand Up @@ -253,13 +260,13 @@ venv: $(REPO_BASE_DIR)/.venv/bin/activate ## Creates a python virtual environmen
ifeq ($(shell test -f j2cli_customization.py && echo -n yes),yes)

define jinja
$(REPO_BASE_DIR)/.venv/bin/j2 --format=env $(1) .env -o $(2) --customize j2cli_customization.py
$(REPO_BASE_DIR)/.venv/bin/j2 --format=env $(1) $(2) -o $(3) --customize j2cli_customization.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if for a single corner case we need to repeat .env call in all places except the corner case

Ideas:

  1. use default value .env if only 2 arguments are passed
  2. define special / different jinja function for grafana terraform
Suggested change
$(REPO_BASE_DIR)/.venv/bin/j2 --format=env $(1) $(2) -o $(3) --customize j2cli_customization.py
$(REPO_BASE_DIR)/.venv/bin/j2 --format=env $(1) $(if $(strip $(3)),$(2),.env) -o $(if $(strip $(3)),$(3),$(2)) --customize j2cli_customization.py

endef

else

define jinja
$(REPO_BASE_DIR)/.venv/bin/j2 --format=env $(1) .env -o $(2)
$(REPO_BASE_DIR)/.venv/bin/j2 --format=env $(1) $(2) -o $(3)
endef

endif
Expand Down
16 changes: 16 additions & 0 deletions scripts/tf_helper_list_json_files_in_folder.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
set -e

DIRECTORY=$1

# Find all JSON files within the directory
FILES=$(find "$DIRECTORY" -mindepth 1 -maxdepth 1 -type f -name '*.json')

# Create a JSON object where each file's basename is the key, with full paths as values
JSON_OBJECT=$(echo "$FILES" | while read -r FILE; do
BASENAME=$(basename "$FILE" .json)
echo "{\"$BASENAME\": \"$FILE\"}"
done | jq -s 'add')

# Output the JSON map
jq -n --argjson files "$JSON_OBJECT" '$files'
16 changes: 16 additions & 0 deletions scripts/tf_helper_list_subfolders.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
set -e

DIRECTORY=$1

# Use `find` to get the directories' base names
SUBFOLDERS=$(find "$DIRECTORY" -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)

# Convert the subfolder names into a JSON object with jq, where each is paired with itself
JSON_OBJECT=$(echo "$SUBFOLDERS" | tr ' ' '\n' | jq -Rn '
[inputs] |
map(select(. != "")) |
map({key: ., value: .}) |
from_entries')
# Output the JSON map
jq -n --argjson subfolders "$JSON_OBJECT" '$subfolders'
2 changes: 1 addition & 1 deletion services/maintenance-page/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ up-master: up

.PHONY: docker-compose.yml
docker-compose.yml: .venv .env
@$(call jinja, docker-compose.yml.j2, docker-compose.yml.unlinted) && \
@$(call jinja, docker-compose.yml.j2, .env, docker-compose.yml.unlinted) && \
$(_yq) docker-compose.yml.unlinted > docker-compose.yml; \
rm docker-compose.yml.unlinted >/dev/null 2>&1;

Expand Down
29 changes: 11 additions & 18 deletions services/monitoring/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ ${TEMP_COMPOSE}-local: docker-compose.yml docker-compose.letsencrypt.dns.yml con
@${REPO_BASE_DIR}/scripts/docker-stack-config.bash -e .env $< docker-compose.letsencrypt.dns.yml > $@

docker-compose.yml: docker-compose.yml.j2 .env .venv pgsql_query_exporter_config.yaml
$(call jinja,$<,$@)
$(call jinja,$<,.env,$@)

.PHONY: update.grafana.pwd
update.grafana.pwd: .env ## Change grafana pwd
Expand All @@ -84,24 +84,17 @@ update.grafana.pwd: .env ## Change grafana pwd
grafanacontainerid=$$(docker ps | grep grafana | awk '{print $$1;}');\
docker exec -ti $$grafanacontainerid grafana-cli admin reset-admin-password $$TRAEFIK_PASSWORD


.PHONY: grafana-export
grafana-export: .venv## Export the remote grafana dashboards and datasources TO YOUR LOCAL MACHINE
@cd grafana/scripts;\
source ${REPO_BASE_DIR}/.venv/bin/activate;\
pip install -r requirements.txt > /dev/null 2>&1;\
python3 export.py;

.PHONY: grafana-import
grafana-import: grafana/assets .venv ## Imports AND OVERWRITES the remote grafana dashboards and datasources FROM YOUR LOCAL MACHINE
@cd grafana/scripts;\
source ${REPO_BASE_DIR}/.venv/bin/activate;\
pip install -r requirements.txt > /dev/null 2>&1;\
python3 import.py
grafana-import: grafana/assets ## Imports the remote grafana dashboards and datasources FROM YOUR LOCAL MACHINE
@pushd ${REPO_BASE_DIR}/services/monitoring/grafana && \
$(MAKE) terraform-plan && \
$(MAKE) terraform-apply; \
popd > /dev/null


.PHONY: config.grafana.dashboards
config.grafana.dashboards: grafana/templates-provisioning/dashboards/simcore/Metrics-dashboard.json.j2 .venv #Configure dashboards for aws or dalco clusters
$(call jinja, $<, grafana/provisioning/dashboards/simcore/Metrics-dashboard.json)
$(call jinja, $<, .env, grafana/provisioning/dashboards/simcore/Metrics-dashboard.json)

.PHONY: config.monitoring
config.monitoring: grafana/template-config.monitoring ${REPO_CONFIG_LOCATION}
Expand Down Expand Up @@ -133,7 +126,7 @@ config.prometheus.ceph.simcore: ${REPO_CONFIG_LOCATION} .venv
@set -o allexport; \
source $<; \
set +o allexport; \
$(call jinja, prometheus/prometheus-ceph.yml.j2, prometheus/prometheus-ceph.yml); \
$(call jinja, prometheus/prometheus-ceph.yml.j2, .env, prometheus/prometheus-ceph.yml); \
cat prometheus/prometheus-base.yml | $(_yq) '. *+ load("prometheus/prometheus-simcore.yml")' | cat | \
$(_yq) '. *+ load("prometheus/prometheus-ceph.yml")' > prometheus/prometheus.yml; \
envsubst < prometheus/prometheus.yml > prometheus/prometheus.temp.yml; \
Expand All @@ -148,10 +141,10 @@ config.prometheus: ${REPO_CONFIG_LOCATION} .venv
mv prometheus/prometheus.temp.yml prometheus/prometheus.yml

pgsql_query_exporter_config.yaml: pgsql_query_exporter_config.yaml.j2 ${REPO_CONFIG_LOCATION} .env .venv
$(call jinja, $<, $@);
$(call jinja, $<, .env, $@);

smokeping_prober_config.yaml: smokeping_prober_config.yaml.j2 ${REPO_CONFIG_LOCATION} .env .venv
$(call jinja, $<, $@);
$(call jinja, $<, .env, $@);

.PHONY: grafana/assets
grafana/assets: ${REPO_CONFIG_LOCATION}
Expand Down
2 changes: 0 additions & 2 deletions services/monitoring/docker-compose.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ configs:
file: ./prometheus/prometheus-federation.yml
prometheus_rules:
file: ./prometheus/prometheus.rules.yml
grafana_image_renderer_config:
file: ./grafana-image-renderer/config.json
pgsql_query_exporter_config:
file: ./pgsql_query_exporter_config.yaml
smokeping_prober_config:
Expand Down
7 changes: 0 additions & 7 deletions services/monitoring/grafana-image-renderer/README.md

This file was deleted.

25 changes: 0 additions & 25 deletions services/monitoring/grafana-image-renderer/config.json

This file was deleted.

58 changes: 58 additions & 0 deletions services/monitoring/grafana/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
.DEFAULT_GOAL := help
REPO_BASE_DIR := $(shell git rev-parse --show-toplevel)
include ${REPO_BASE_DIR}/scripts/common.Makefile



# Internal VARIABLES ------------------------------------------------
TF_STATE_FILE := terraform/.terraform/terraform.tfstate

terraform/main.tf: terraform/main.tf.j2 .venv $(CI_ENV_FILE)
# generate $@
@$(call jinja, $<, $(CI_ENV_FILE), $@)
# validate and format $@
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it a redundant line?


terraform-init: $(TF_STATE_FILE) ## init terraform

$(TF_STATE_FILE): $(CI_ENV_FILE) terraform/main.tf
# terraform init
@set -a; source $<; set +a; \
if [ "$${TF_STATE_BACKEND_TYPE}" = "local" ]; then \
terraform -chdir=./terraform init; \
else \
terraform -chdir=./terraform init -backend-config="access_key=$${TF_BACKEND_ACCESS_KEY}" -backend-config="secret_key=$${TF_BACKEND_SECRET_KEY}"; \
fi

terraform/plan.cache:
@echo "$@ file not found. Run 'make terraform-plan' to generate it."
@exit 1

.PHONY: terraform-plan
terraform-plan: $(CI_ENV_FILE) $(TF_STATE_FILE) ensure-grafana-online ## terraform plan
# terraform plan
@set -a; source $<; source $${REPO_CONFIG_LOCATION}; set +a; \
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

.PHONY: terraform-apply
terraform-apply: $(CI_ENV_FILE) terraform/plan.cache $(TF_STATE_FILE) ensure-grafana-online ## terraform apply
# terraform apply
@set -a; source $<; set +a; \
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

.PHONY: ensure-grafana-online
ensure-grafana-online:
@set -o allexport; \
source $(REPO_CONFIG_LOCATION); \
set +o allexport; \
url=$${TF_VAR_GRAFANA_URL}; \
echo "Waiting for grafana at $$url to become reachable..."; \
while true; do \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we add a timeout here to avoid waiting forever?

status_code=$$(curl -k -o /dev/null -s -w "%{http_code}" $$url); \
if [ "$$status_code" -ge 200 ] && [ "$$status_code" -lt 400 ]; then \
echo "Grafana is online"; \
break; \
else \
echo "Grafana still unreachable, waiting 5s for grafana to become reachable..."; \
sleep 5; \
fi; \
done;
2 changes: 0 additions & 2 deletions services/monitoring/grafana/scripts/.gitignore

This file was deleted.

26 changes: 0 additions & 26 deletions services/monitoring/grafana/scripts/README.MD

This file was deleted.

Loading
Loading