diff --git a/stacks/setup-container/README.md b/stacks/setup-container/README.md new file mode 100644 index 0000000..910985c --- /dev/null +++ b/stacks/setup-container/README.md @@ -0,0 +1,10 @@ +## What +This docker-compose will simply start Metabase with a PostgreSQL app db and will create the admin user + +## How +A setup container runs a shell script which will wait for Metabase to be alive and then set it up with: +user: a@b.com +pass: metabot1 + +## Additions +The script will also add the PostgreSQL database with sample data to Metabase. \ No newline at end of file diff --git a/stacks/setup-container/docker-compose.yaml b/stacks/setup-container/docker-compose.yaml index 2345bbb..c1d42dd 100644 --- a/stacks/setup-container/docker-compose.yaml +++ b/stacks/setup-container/docker-compose.yaml @@ -1,31 +1,56 @@ version: '3.9' services: - metabase_setup: - image: metabase/metabase-enterprise:v1.42.3 - hostname: metabase_setup - container_name: metabase_setup + metabase: + image: metabase/metabase:v0.43.0 + container_name: metabase + hostname: metabase + volumes: + - /dev/urandom:/dev/random:ro ports: - - 3001:3000 - networks: + - 3031:3000 + environment: + - "MB_DB_TYPE=postgres" + - "MB_DB_DBNAME=metabase" + - "MB_DB_PORT=5432" + - "MB_DB_USER=metabase" + - "MB_DB_PASS=mysecretpassword" + - "MB_DB_HOST=postgres-app-db" + networks: + - metanet1 + depends_on: + - postgres-app-db + postgres-app-db: + image: postgres:14.2-alpine + container_name: postgres-app-db + hostname: postgres-app-db + ports: + - 5432:5432 + environment: + - "POSTGRES_USER=metabase" + - "POSTGRES_DB=metabase" + - "POSTGRES_PASSWORD=mysecretpassword" + volumes: + - $PWD/postgres_origin:/var/lib/postgresql/data + networks: - metanet1 setup: image: bash:5.1.16 container_name: setup + volumes: + - $PWD/setup:/tmp networks: - metanet1 - command: > - sh -c "apk add curl jq && \ - curl -L 'https://raw.githubusercontent.com/nickjj/wait-until/v0.2.0/wait-until' -o /usr/local/bin/wait-until && \ - chmod +x /usr/local/bin/wait-until && \ - wait-until \"echo 'Checking if Metabase is ready' && curl -s 'http://metabase_setup:3000/api/health' | grep -ioE \"ok\"\" 60 && \ - if curl -s 'http://metabase_setup:3000/api/session/properties' | jq -r '.\"setup-token\"' | grep -ioE \"null\"; then echo 'Instance already configured, exiting'; else \ - echo 'Setting up the instance' && \ - token=$$(curl -s http://metabase_setup:3000/api/session/properties | jq -r '.\"setup-token\"') && \ - echo 'Setup token fetched, now configuring with:' && \ - echo \"{'token':'$$token','user':{'first_name':'a','last_name':'b','email':'a@b.com','site_name':'metabot1','password':'metabot1','password_confirm':'metabot1'},'database':null,'invite':null,'prefs':{'site_name':'metabot1','site_locale':'en','allow_tracking':'false'}}\" > file.json && \ - sed 's/'\''/\"/g' file.json > file2.json && \ - cat file2.json && \ - curl -s http://metabase_setup:3000/api/setup -H 'Content-Type: application/json' --data-binary @file2.json && echo ' < Admin session token, exiting'; fi" + depends_on: + - metabase + command: sh /tmp/metabase-setup.sh metabase:3000 + postgres-data1: + image: metabase/qa-databases:postgres-sample-12 + container_name: postgres-data1 + ports: + - 5433:5432 + hostname: postgres-data1 + networks: + - metanet1 networks: metanet1: - driver: bridge \ No newline at end of file + driver: bridge diff --git a/stacks/setup-container/setup/metabase-setup.sh b/stacks/setup-container/setup/metabase-setup.sh new file mode 100644 index 0000000..5fb9c34 --- /dev/null +++ b/stacks/setup-container/setup/metabase-setup.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +echo "seting up $1" +# get deps +apk add curl jq +# get the wait-until script +curl -L https://raw.githubusercontent.com/nickjj/wait-until/v0.2.0/wait-until -o /usr/local/bin/wait-until && \ +chmod +x /usr/local/bin/wait-until +# run the script and everything else +wait-until "echo 'Checking if Metabase is ready' && curl -s http://$1/api/health | grep -ioE 'ok'" 60 && \ +if curl -s http://$1/api/session/properties | jq -r '."setup-token"' | grep -ioE "null"; then echo 'Instance already configured, exiting (or file.json && \ +sed 's/'\''/\"/g' file.json > file2.json && \ +cat file2.json && \ +sessionToken=$(curl -s http://$1/api/setup -H 'Content-Type: application/json' --data-binary @file2.json | jq -r '.id') && echo ' < Admin session token, exiting' && \ +# creating a postgres +curl -s -X POST http://$1/api/database -H 'Content-Type: application/json' --cookie "metabase.SESSION=$sessionToken" --data '{"engine":"postgres","name":"pg","details":{"host":"postgres-data1","port":"5432","dbname":"sample","user":"metabase","password":"metasample123","schema-filters-type":"all","ssl":false,"tunnel-enabled":false,"advanced-options":false},"is_full_sync":true}'; fi + +# curl -s http://$1/api/database -H 'Content-Type: application/json' --cookie "metabase.SESSION=$sessionToken" --data {'engine':'postgres','name':'pg','details':{'host':'postgres-data1','port':null,'dbname':'sample','user':'metabase','password':'metasample123','schema-filters-type':'all','ssl':'false','tunnel-enabled':'false','advanced-options':'false'},'is_full_sync':'true'} \ No newline at end of file