cachethq / Docker

A Dockerized version of Cachet.
https://cachethq.io
BSD 3-Clause "New" or "Revised" License
416 stars 280 forks source link

Automatic setup (without user interaction) #264

Open leobuskin opened 6 years ago

leobuskin commented 6 years ago

I'm trying to understand how to make automatic setup with docker distribution of Cachet and it looks nearly impossible now.

I'm setting mostly all environment variables, adding settings and user to database with standalone python script (starts after database and Cachet and updates tables), but Cachet always starts (redirects) with Setup page.

docker-compose.yml env variables for cachet service


- APP_ENV=production
- APP_URL=http://localhost:8000
- APP_KEY=oZ1Z75oeVviviFx1qILoN411Q54RO548
- DEBUG=false
  - CACHE_DRIVER=apc
  - SESSION_DRIVER=apc
  - QUEUE_DRIVER=database

  - MAIL_DRIVER=smtp
  - MAIL_HOST=smtp.mailgun.org
  - MAIL_PORT=465
  - MAIL_USERNAME=user@email.com
  - MAIL_PASSWORD=somepass
  - MAIL_ADDRESS=user@email.com
  - MAIL_NAME=Status
  - MAIL_ENCRYPTION=tls

  - DB_DRIVER=pgsql
  - DB_HOST=postgresql
  - DB_PORT=5432
  - DB_DATABASE=postgres
  - DB_USERNAME=postgres
  - DB_PASSWORD=postgres
  - DB_PREFIX=chq_

  - REDIS_HOST=null
  - REDIS_DATABASE=null
  - REDIS_PORT=null

  - GITHUB_TOKEN=null

> updating cachet tables in DB (python)
def add_cachet_user():
    settings = {
        'app_name': 'sitename',
        'app_domain': 'http://localhost:8000',
        'app_timezone': 'America/San_Francisco',
        'app_locale': 'en',
        'app_incident_days': '7',
    }
    with psycopg2.connect(
        host=config.CACHET_PG_HOST,
        port=config.CACHET_PG_PORT,
        database=config.CACHET_PG_DATABASE,
        user=config.CACHET_PG_USER,
        password=config.CACHET_PG_PASSWORD
    ) as conn:
        with conn.cursor() as cursor:
            cursor.execute(
                'INSERT INTO chq_users (username, password, email, api_key, active, level, created_at, updated_at) '
                'VALUES (%s, %s, %s, %s, %s, %s, %s, %s);',
                ['user', '$xx$xx$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxx',
                 'user@email.com', 'xxxxxxxxxxxxxx', True, 1, datetime.now(), datetime.now()]
            )
        with conn.cursor() as cursor:
            for key, value in settings.items():
                cursor.execute(
                    'INSERT INTO chq_settings (name, value, created_at, updated_at) '
                    'VALUES (%s, %s, %s, %s);',
                    [key, value, datetime.now(), datetime.now()]
                )


Is any workaround here? What do I need to run Cachet without user interaction?

> Cachet 2.5.0-dev
leobuskin commented 6 years ago

I found check for setup redirection:

public function handle(Request $request, Closure $next)
    {
        if (!$this->settings->get('app_name')) {
            return cachet_redirect('setup');
        }
        return $next($request);
    }

But app_name was set up in database (I can see it in PG table chq_settings) and as environment variable too but Cachet still redirects

shuichiro-makigaki commented 6 years ago

In v2.3, I can avoid the initial setup process by putting development.php into an application bootstrap directory.

docker cp development.php $(docker-compose ps -q cachet):/var/www/html/bootstrap/cachet/development.php

development.php contains

<?php return array (
    'app_name' => 'Cachet Test',
    'app_domain' => 'http://localhost:8000',
    'app_timezone' => 'Asia/Tokyo',
    'app_locale' => 'ja',
    'app_incident_days' => '7',
    'header' => '<div class="container"><h1>Cachet Test</h1></div>',
    'app_about' => '',
    'enable_subscribers' => '0',
    'skip_subscriber_verification' => '0',
    'display_graphs' => '0',
    'show_support' => '0',
    'enable_external_dependencies' => '1',
    'show_timezone' => '1',
);

The file name should be production.php if APP_ENV was production (I think. Not yet tried on my side.) Regarding v2.4, it's worth checking the bootstrap codes after the initial setup process (by docker cp or docker exec).

lhirlimann commented 4 years ago

So I dump the SQL and import it for now most of it works.