Automating Redash Setup for Efficient Development

Hello there! If you’re tired of constantly running make up, make down, or make compose_build commands, you're not alone.

Compiling Redash can be frustrating, especially with the long wait times just to see your code changes. After enduring the waiting times and terminal commands, I found that my database was wiped clean, forcing me to re-register with email and password each time. I also had to reconnect the data source by entering passwords and other fields that I could barely remember.

I decided to streamline this repetitive process by automating these tasks with code. Here’s a guide to automate the setup of your Redash LLM Chatbot so you can focus on more productive aspects of your project.

Automating Redash Setup with Python

We need two files: a Python script to run commands and create the DataSource, and an SQL file for setup.

Python Script

First, create a Python script to automate the setup process:

from redashAPI import RedashAPIClient
import os

# Terminal command to run
# Customize the setup.sql to change default login credentials and API key
# TODO: Generate setup.sql file based on given arguments
command = "make down && make compose_build && make up && docker compose exec -u postgres -T postgres psql postgres < setup.sql"

# Run the command in the terminal of the current working directory
# Ensure your current working directory is the Redash source code directory
# TODO: Update to add a path to the Redash source code directory
os.system(command)

Redash = RedashAPIClient("uwgC6V8ipibQbru2Y02R4bnjqjeOLwkcQc1pSECs", "http://localhost:5001/")
data_source = Redash.post('data_sources', {
    "name": "__YOUR_DATA_SOURCE_NAME__",  # Update to your data source name
    "type": "pg",  # Update to your data source type
    "options": {
        "dbname": "__DB_NAME__",  # Update to your database name
        "host": "__HOST__",  # Update to your database host
        "user": "__USER__",  # Update to your database user
        "passwd": "__PASSWORD__",  # Update to your database password
        "port": 5432  # Update to your database port
    }
})
print(data_source)

Before running the script, make sure you have the setup.sql file in the same folder as the Redash source. You can place them anywhere, but ensure the working directory is set to the Redash source because we will be running make up and other commands to compile Redash. Also, install the Redash API client using pip install redash-api-client.

The API key uwgC6V8ipibQbru2Y02R4bnjqjeOLwkcQc1pSECs is used here because it will be added to the setup.sql configuration, ensuring the API key is available when initializing the RedashAPIClient.

Setup SQL File

Next, create the setup.sql file with the following content:

INSERT INTO public.organizations (updated_at, created_at, id, name, slug, settings)
    VALUES (now(), now(), 1, '_ORGANIZATION_', 'default',
            '{"settings": {"beacon_consent": false}}');

INSERT INTO public.groups (id, org_id, type, name, permissions, created_at)
    VALUES (1, 1, 'builtin', 'admin', '{admin,super_admin}', now());

INSERT INTO public.groups (id, org_id, type, name, permissions, created_at)
    VALUES (2, 1, 'builtin', 'default', '{create_dashboard,create_query,edit_dashboard,edit_query,view_query,view_source,execute_query,list_users,schedule_query,list_dashboards,list_alerts,list_data_sources}',
            '2024-04-21 23:25:13.827925+00');

INSERT INTO public.users (updated_at, created_at, id, org_id, name, email, password_hash, groups,
                          api_key, details)
    VALUES (now(), now(), 1, 1, '_USER_NAME_', '_EMAIL_', 
    '$6$rounds=656000$eU6sYb5n7K.K6hZ0$tNLgO3gqYkCJnjVvdGiW7TluHeWv/3fB5J29byOmnVU66LRYuEzAoXmh/2LSRDReXdfzSg1t7pFncP03os49r0', 
    -- password: test123
    '{1,2}', 'uwgC6V8ipibQbru2Y02R4bnjqjeOLwkcQc1pSECs', format('{"active_at": "%s"}', now())::json);

Customize _ORGANIZATION_, _USER_NAME_, and _EMAIL_ with your preferred configurations. This setup ensures that your first user or admin is created automatically. The default password is set to test123, but you can change it by hashing your preferred password using the following method from Redash documentation:

# Generate hashed password
from passlib.apps import custom_app_context as pwd_context
pwd_context.hash("your_password")

Conclusion

By automating these tasks, you can significantly reduce the manual overhead and streamline your Redash development process. This allows us to focus on building your Redash LLM Chatbot without the hassle of repetitive setup tasks.

Limitations

While this automation simplifies the setup process, it assumes a static configuration that might need adjustments for different environments or more complex setups. Additionally, the script does not handle errors or edge cases, so further enhancements may be required to ensure robustness and flexibility.

For more insights and code, visit my GitHub here. Connect with me on LinkedIn here.