Create the admin user as a separate command (#233)

This means the migrations can always be run on startup, meaning the database won't get out of sync with the running application. It also means users who don't set the credential variables in their compose files don't keep creating admin users every time the application starts.
This commit is contained in:
Jake Howard 2020-07-28 12:11:02 +01:00 committed by GitHub
parent a3b7cb1af0
commit ff9e4b444b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 11 deletions

View File

@ -35,13 +35,13 @@ The repo supplies with a [Docker Compose](./docker-compose.yml) file and the sam
docker-compose down docker-compose down
``` ```
- After the setup, you can start plausible as -- - After the setup, you can start plausible as --
```bash ```bash
docker-compose up -d plausible docker-compose up -d plausible
``` ```
after a successful startup (can take upto 5 mins), `plausible` is available at port `80`, navigate to [`http://localhost`](http://localhost). after a successful startup (can take upto 5 mins), `plausible` is available at port `80`, navigate to [`http://localhost`](http://localhost).
- stopping plausible -- - stopping plausible --
```bash ```bash
docker-compose down docker-compose down
``` ```
@ -51,12 +51,12 @@ The repo supplies with a [Docker Compose](./docker-compose.yml) file and the sam
docker volume rm plausible_event-data -f docker volume rm plausible_event-data -f
docker volume rm plausible_db-data -f docker volume rm plausible_db-data -f
``` ```
Note: Note:
- #1 you need to stop plausible and restart plausible if you change the environment variables. - #1 you need to stop plausible and restart plausible if you change the environment variables.
- #2 With docker-compose, you need to remove the existing container and rebuild if you want your changes need to be reflected: - #2 With docker-compose, you need to remove the existing container and rebuild if you want your changes need to be reflected:
```bash ```bash
docker rmi -f plausible_plausible:latest docker rmi -f plausible_plausible:latest
docker-compose up -d plausible docker-compose up -d plausible
``` ```
### Non-docker building ### Non-docker building
It is possible to create a release artifact by running a release. It is possible to create a release artifact by running a release.
@ -78,6 +78,7 @@ After the release, these are available under `_build/prod/rel/plausible` --
```bash ```bash
_build/prod/rel/plausible/createdb.sh _build/prod/rel/plausible/createdb.sh
_build/prod/rel/plausible/init-admin.sh
_build/prod/rel/plausible/migrate.sh _build/prod/rel/plausible/migrate.sh
_build/prod/rel/plausible/rollback.sh _build/prod/rel/plausible/rollback.sh
_build/prod/rel/plausible/seed.sh _build/prod/rel/plausible/seed.sh
@ -87,6 +88,7 @@ the same is available in the docker images as follows --
```bash ```bash
docker run plausible:master-12add db createdb docker run plausible:master-12add db createdb
docker run plausible:master-12add db init-admin
docker run plausible:master-12add db migrate docker run plausible:master-12add db migrate
docker run plausible:master-12add db rollback docker run plausible:master-12add db rollback
docker run plausible:master-12add db seed docker run plausible:master-12add db seed
@ -120,7 +122,7 @@ Following are the variables that can be used to configure the availability of th
- Disables changing of subscription and removes the trial notice banner (use with caution!) _defaults to `false`_ - Disables changing of subscription and removes the trial notice banner (use with caution!) _defaults to `false`_
### Default User Generation ### Default User Generation
For self-hosting, a default user is generated during the [Database Migration](#Database Migration) to access Plausible. To be noted that, a default user is a user whose trial period expires in 100 Years ;). For self-hosting, a default user can be generated using the `db init-admin` command. To be noted that, a default user is a user whose trial period expires in 100 Years ;).
It is *highly* recommended that you configure these parameters. It is *highly* recommended that you configure these parameters.
- ADMIN_USER_NAME - ADMIN_USER_NAME

View File

@ -38,7 +38,7 @@ services:
build: build:
context: . context: .
dockerfile: ./Dockerfile dockerfile: ./Dockerfile
command: sh -c "sleep 10 && /entrypoint.sh run" command: sh -c "sleep 10 && /entrypoint.sh db migrate && /entrypoint.sh run"
depends_on: depends_on:
- plausible_db - plausible_db
- plausible_events_db - plausible_events_db
@ -57,7 +57,7 @@ services:
build: build:
context: . context: .
dockerfile: ./Dockerfile dockerfile: ./Dockerfile
command: sh -c "sleep 10 && /entrypoint.sh db createdb && /entrypoint.sh db migrate" command: sh -c "sleep 10 && /entrypoint.sh db createdb && /entrypoint.sh db migrate && /entrypoint.sh db init-admin"
depends_on: depends_on:
- plausible_db - plausible_db
- plausible_events_db - plausible_events_db

View File

@ -36,7 +36,6 @@ defmodule Plausible.Release do
def migrate do def migrate do
prepare() prepare()
Enum.each(repos(), &run_migrations_for/1) Enum.each(repos(), &run_migrations_for/1)
init_admin()
prepare_clickhouse() prepare_clickhouse()
run_migrations_for_ch() run_migrations_for_ch()
IO.puts("Migrations successful!") IO.puts("Migrations successful!")

6
rel/overlays/init-admin.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/sh
# Create an admin user
BIN_DIR=`dirname "$0"`
${BIN_DIR}/bin/plausible eval Plausible.Release.init_admin