From ff9e886be3fe08734b1d647aa10b4a71bc8fa269 Mon Sep 17 00:00:00 2001 From: Chris Raible Date: Tue, 8 Jul 2025 20:44:09 -0700 Subject: [PATCH 1/3] Added experimental script to automate fetching Tinybird tokens --- tinybird/Dockerfile | 2 ++ tinybird/getTokens.sh | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100755 tinybird/getTokens.sh diff --git a/tinybird/Dockerfile b/tinybird/Dockerfile index 17d589a..a0f3092 100644 --- a/tinybird/Dockerfile +++ b/tinybird/Dockerfile @@ -3,6 +3,7 @@ FROM python:3.13-slim@sha256:6544e0e002b40ae0f59bc3618b07c1e48064c4faed3a15ae2fb # Install dependencies RUN apt-get update && apt-get install -y --no-install-recommends \ curl \ + jq \ ca-certificates \ && rm -rf /var/lib/apt/lists/* @@ -10,6 +11,7 @@ WORKDIR /home/tinybird # Install Tinybird using the standard installation script COPY handleLogin.sh /usr/local/bin/tinybird-login +COPY getTokens.sh /usr/local/bin/get-tokens COPY tb-wrapper /usr/local/bin/tb-wrapper RUN curl https://tinybird.co | sh diff --git a/tinybird/getTokens.sh b/tinybird/getTokens.sh new file mode 100755 index 0000000..d292525 --- /dev/null +++ b/tinybird/getTokens.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +# Get token from .tinyb JSON file +USER_TOKEN=$(jq -r '.token' .tinyb) +echo "User token: $USER_TOKEN" + +if [ -z "$USER_TOKEN" ] || [ "$USER_TOKEN" = "null" ]; then + echo "Error: Could not find token in .tinyb file" + exit 1 +fi + +# Get host from .tinyb JSON file +HOST=$(jq -r '.host' .tinyb) +echo "Host: $HOST" + +if [ -z "$HOST" ] || [ "$HOST" = "null" ]; then + echo "Error: Could not find host in .tinyb file" + exit 1 +fi + +# Get id from .tinyb JSON file +WORKSPACE_ID=$(jq -r '.id' .tinyb) +echo "Workspace ID: $WORKSPACE_ID" + +if [ -z "$WORKSPACE_ID" ] || [ "$WORKSPACE_ID" = "null" ]; then + echo "Error: Could not find id in .tinyb file" + exit 1 +fi + +# Make GET request to tokens endpoint and parse response +RESPONSE=$(curl -s -X GET \ + "$HOST/v0/tokens" \ + -H "Authorization: Bearer $USER_TOKEN") + +# Parse tokens from response +ADMIN_TOKEN=$(echo "$RESPONSE" | jq -r '.tokens[] | select(.name == "admin token") | .token') +TRACKER_TOKEN=$(echo "$RESPONSE" | jq -r '.tokens[] | select(.name == "tracker") | .token') + +# Echo the tokens as proof of concept +echo "Admin token: $ADMIN_TOKEN" +echo "Tracker token: $TRACKER_TOKEN" \ No newline at end of file From ebe9c3040b6cd728d451ec6d208fab74ac8ef64a Mon Sep 17 00:00:00 2001 From: James Loh Date: Mon, 14 Jul 2025 17:31:23 +1000 Subject: [PATCH 2/3] Simplify TB initial setup no ref - By outputting the tokens in the format the `.env` file expects this greatly simplifies the initial setup by just makign it copy -> paste able - This will mean users don't need to load up the UI and copy/paste tokens at all and can do the whole setup (after they've signed up for TB) from the CLI --- TINYBIRD.md | 8 ++------ tinybird/getTokens.sh | 9 ++++----- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/TINYBIRD.md b/TINYBIRD.md index beae53b..2140b38 100644 --- a/TINYBIRD.md +++ b/TINYBIRD.md @@ -6,12 +6,8 @@ Note: Currently Traffic Analytics features are behind a feature flag. For now, y 1. Run `docker compose run --rm tinybird-login` to login to your Tinybird account following the steps given 1. Run `docker compose --profile=analytics up tinybird-sync`. This will copy the Tinybird files from the Ghost container into a shared volume. The service should log "Tinybird files synced into shared volume.", then exit. 1. Run `docker compose --profile=analytics up tinybird-deploy` and wait for the service to exit successfully. This will create your Tinybird datasources, pipes and API endpoints. It may take a minute or two to complete the first time. You should see "Deployment #1 is live!" in your terminal before the service exits. -1. Find your workspace's ID and events API endpoint: `docker compose run --rm tinybird-login tb --cloud info` - 1. Copy the value of "api", and add it to your `.env` file as `TINYBIRD_API_URL`. You can also find this value in your Tinybird Workspace's UI. - 1. Copy the value of "workspace_id", and add it to your `.env` file as `TINYBIRD_WORKSPACE_ID` -1. Using the UI link from the previous step, open your workspace and click on *Tokens* in the left hand menu -1. Copy your Tinybird "Workspace admin token" and add it to your `.env` file as `TINYBIRD_ADMIN_TOKEN` -1. Copy your Tinybird `tracker` token and add it to your `.env` file as `TINYBIRD_TRACKER_TOKEN` +1. Run `docker compose --profile=analytics run --rm tinybird-login get-tokens` +1. Copy and paste the values from the previous step into your `.env` file 1. Run `docker compose --profile=analytics up -d` to start all services in the background 1. Add `analytics` to `COMPOSE_PROFILES=` in the top of your `.env` file to automatically include the `analytics` profile when running `docker compose` commands 1. At this point, everything should be working. You can test it's working by visiting your site's homepage, then checking the Stats page in Ghost Admin — you should see a view recorded. diff --git a/tinybird/getTokens.sh b/tinybird/getTokens.sh index d292525..87d0df9 100755 --- a/tinybird/getTokens.sh +++ b/tinybird/getTokens.sh @@ -2,7 +2,6 @@ # Get token from .tinyb JSON file USER_TOKEN=$(jq -r '.token' .tinyb) -echo "User token: $USER_TOKEN" if [ -z "$USER_TOKEN" ] || [ "$USER_TOKEN" = "null" ]; then echo "Error: Could not find token in .tinyb file" @@ -11,7 +10,7 @@ fi # Get host from .tinyb JSON file HOST=$(jq -r '.host' .tinyb) -echo "Host: $HOST" +echo "TINYBIRD_API_URL=$HOST" if [ -z "$HOST" ] || [ "$HOST" = "null" ]; then echo "Error: Could not find host in .tinyb file" @@ -20,7 +19,7 @@ fi # Get id from .tinyb JSON file WORKSPACE_ID=$(jq -r '.id' .tinyb) -echo "Workspace ID: $WORKSPACE_ID" +echo "TINYBIRD_WORKSPACE_ID=$WORKSPACE_ID" if [ -z "$WORKSPACE_ID" ] || [ "$WORKSPACE_ID" = "null" ]; then echo "Error: Could not find id in .tinyb file" @@ -37,5 +36,5 @@ ADMIN_TOKEN=$(echo "$RESPONSE" | jq -r '.tokens[] | select(.name == "admin token TRACKER_TOKEN=$(echo "$RESPONSE" | jq -r '.tokens[] | select(.name == "tracker") | .token') # Echo the tokens as proof of concept -echo "Admin token: $ADMIN_TOKEN" -echo "Tracker token: $TRACKER_TOKEN" \ No newline at end of file +echo "TINYBIRD_ADMIN_TOKEN=$ADMIN_TOKEN" +echo "TINYBIRD_TRACKER_TOKEN=$TRACKER_TOKEN" From 56d8f47a79f002026ae8fae7cbb143a43562332c Mon Sep 17 00:00:00 2001 From: James Loh Date: Tue, 15 Jul 2025 11:39:42 +1000 Subject: [PATCH 3/3] Simplify initial setup by not include `--profile` arg no ref - As Chris notes this isn't actually required when doing the initial setup/mentioning the specific container you want to deploy - This standardises us in just doing the `--rm run` command for all of them to keep everything nice and neat --- TINYBIRD.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/TINYBIRD.md b/TINYBIRD.md index 2140b38..874e27f 100644 --- a/TINYBIRD.md +++ b/TINYBIRD.md @@ -4,9 +4,9 @@ Note: Currently Traffic Analytics features are behind a feature flag. For now, y 1. Create a Tinybird account and a Tinybird workspace at [tinybird.co](https://auth.tinybird.co/login). You can select any cloud/region you choose. 1. Run `docker compose run --rm tinybird-login` to login to your Tinybird account following the steps given -1. Run `docker compose --profile=analytics up tinybird-sync`. This will copy the Tinybird files from the Ghost container into a shared volume. The service should log "Tinybird files synced into shared volume.", then exit. -1. Run `docker compose --profile=analytics up tinybird-deploy` and wait for the service to exit successfully. This will create your Tinybird datasources, pipes and API endpoints. It may take a minute or two to complete the first time. You should see "Deployment #1 is live!" in your terminal before the service exits. -1. Run `docker compose --profile=analytics run --rm tinybird-login get-tokens` +1. Run `docker compose run --rm tinybird-sync`. This will copy the Tinybird files from the Ghost container into a shared volume. The service should log "Tinybird files synced into shared volume.", then exit. +1. Run `docker compose run --rm tinybird-deploy` and wait for the service to exit successfully. This will create your Tinybird datasources, pipes and API endpoints. It may take a minute or two to complete the first time. You should see "Deployment #1 is live!" in your terminal before the service exits. +1. Run `docker compose run --rm tinybird-login get-tokens` 1. Copy and paste the values from the previous step into your `.env` file 1. Run `docker compose --profile=analytics up -d` to start all services in the background 1. Add `analytics` to `COMPOSE_PROFILES=` in the top of your `.env` file to automatically include the `analytics` profile when running `docker compose` commands