Skip to content

Commit

Permalink
🔨(project) provision metabase instance using terraform
Browse files Browse the repository at this point in the history
Using `make bootstrap` now automates Metabase instance provisioning:

- metabase admin user is created
- QualiCharge API database is set as a datasource
  • Loading branch information
jmaupetit committed Jun 17, 2024
1 parent 41357e8 commit b0f5299
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ venv.bak/

# -- Tools
.coverage

# -- Provisioning
provisioning/.terraform*
provisioning/terraform.tfstate*
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ bootstrap: \
build \
migrate-api \
create-api-test-db \
create-metabase-db \
create-metabase-db \
seed-metabase \
seed-oidc \
create-superuser
.PHONY: bootstrap
Expand Down Expand Up @@ -131,6 +132,16 @@ create-superuser: ## create super user
--force
.PHONY: create-superuser

seed-metabase: ## seed the Metabase server
@echo "Running metabase service …"
@$(COMPOSE) up -d --wait metabase
@echo "Create metabase initial admin user…"
bin/metabase-init
@echo "Create API data source…"
$(COMPOSE_RUN) terraform init
$(COMPOSE_RUN) terraform apply -auto-approve
.PHONY: seed-metabase

seed-oidc: ## seed the OIDC provider
@echo 'Starting OIDC provider…'
@$(COMPOSE) up -d keycloak
Expand Down
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ make bootstrap
> 👉 Cette commande doit préparer votre environnement et builder les images
> Docker nécessaires au démarrage de votre environnement.
Une fois votre environnement de travail initialisé, vous pouvez lancer le projet en
utilisant :
Une fois votre environnement de travail initialisé, vous pouvez lancer le projet
en utilisant :

```
make run
make run-all
```

Les services QualiCharge doivent maintenant tourner sur votre poste :
Expand All @@ -55,6 +55,16 @@ Et enfin, pour lancer les tests du projet :
make test
```

## Explorer les données collectées avec Metabase

Si vous avez utilisé la commande `make bootstrap` pour initialiser le projet,
vous devez avoir provisionné une instance de Metabase qui est accessible depuis
un navigateur sur l'URL suivante :
[http://localhost:3000](http://localhost:3000).

> :bulb: Vous pouvez vous connecter en utilisant le login `[email protected]` et
> le mot de passe `supersecret`.
## Utilisation du client d'API et du CLI `qcc`

Voir la documentation du projet : [./src/client/](./src/client/)
Expand Down
51 changes: 51 additions & 0 deletions bin/metabase-init
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash

# This script will run at project bootstrap to initialize a metabase instance
#
# It is inspired from the metabase setup script of the metabase terraform provider:
# github.com/bnjns/terraform-provider-metabase

set -eo pipefail

# Utilities
declare COMPOSE_RUN="docker compose run --rm -T"
declare CURL="${COMPOSE_RUN} curl"
declare JQ="${COMPOSE_RUN} jq"

# Vars
declare METABASE_HOST="http://metabase:3000"
declare METABASE_EMAIL="[email protected]"
declare METABASE_PASSWORD="supersecret"

# Get the setup token
echo "⚙️ Getting setup token…"
if ! setupToken=$(${CURL} -s --fail "${METABASE_HOST}/api/session/properties" | ${JQ} -er '."setup-token"'); then
echo "Failed to extract setup token"
exit 2
fi

echo "⚙️ Configuring metabase…"
${JQ} -n "{ \
database: null, \
invite: null, \
prefs: { \
allow_tracking: false, \
site_locale: \"en\", \
site_name: \"QualiCharge\" \
}, \
user: { \
email: \"${METABASE_EMAIL}\", \
first_name: \"Admin\", \
last_name: \"User\", \
password: \"${METABASE_PASSWORD}\", \
password_confirm: \"${METABASE_PASSWORD}\", \
site_name: \"QualiCharge\" \
}, \
token: \"${setupToken}\" \
}" | ${CURL} -s --fail \
-X POST \
-H "Content-Type: application/json" \
-d @- \
"${METABASE_HOST}/api/setup"

echo "✅ all done."
24 changes: 21 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,29 @@ services:
- postgresql

# -- tools
dockerize:
image: jwilder/dockerize

curl:
image: curlimages/curl:8.8.0
user: ${DOCKER_USER:-1000}
env_file:
- env.d/metabase
volumes:
- .:/work

dockerize:
image: jwilder/dockerize

jq:
# There is no other tags than latest
image: ddev/ddev-utilities:latest
entrypoint: jq
env_file:
- env.d/metabase

terraform:
image: hashicorp/terraform:1.8.5
env_file:
- env.d/terraform
user: ${DOCKER_USER:-1000}
working_dir: /home/terraform
volumes:
- ./provisioning:/home/terraform
2 changes: 2 additions & 0 deletions env.d/metabase
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ MB_DB_PASS=pass
MB_DB_PORT=5432
MB_DB_TYPE=postgres
MB_DB_USER=qualicharge
MB_PASSWORD_COMPLEXITY=weak
MB_SEND_EMAIL_ON_FIRST_LOGIN_FROM_NEW_DEVICE=false
3 changes: 3 additions & 0 deletions env.d/terraform
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
METABASE_HOST=http://metabase:3000
[email protected]
METABASE_PASSWORD=supersecret
15 changes: 15 additions & 0 deletions provisioning/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Database
resource "metabase_database" "qualicharge" {
engine = "postgres"
name = "QualiCharge API"

details = jsonencode({
host = "postgresql"
port = 5432
dbname = "qualicharge-api"
user = "qualicharge"
})
details_secure = jsonencode({
password = "pass"
})
}
13 changes: 13 additions & 0 deletions provisioning/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
terraform {
required_providers {
metabase = {
source = "bnjns/metabase"
version = "0.9.0"
}
}
}

provider "metabase" {
# Configured using environment variables
# See env.d/terraform
}

0 comments on commit b0f5299

Please sign in to comment.