Support for docker based self-hosting (#64)
* first commit with test and compile job Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * adding 'prepare' stage Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * updated ci script to include "test" compile phase Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * adding environment variables for connecting to postgresql Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * updated ci config for postgres Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * using non-alpine version of elixir Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * re-using the 'compile' artifacts and added explict env variables for testing Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * removing redundant deps fetching from common code Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * formatting using mix.format -- beware no-code changes! Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * added release config Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * adding consistent env variable for Database Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * more cleaning up of environment variables Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * Adding releases config for enabling releases Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * cleaning up env configs Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * Cleaned up config and prepared config for releases Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * updated CI script with new config for test Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * Added Dockerfile for creating production docker image Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * Adding "docker" build job yay! Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * using non-slim version of debian and installing webpack Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * Adding overlays for migrations on releases Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * restricting the docker built to master branch only Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * typo fix Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * adding "Hosting.md" to explain hosting instructions Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * removed the default comments Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * Added documentation related to env variables * updated documentation and fixed typo Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * updated documentation * Bumping up elixir version as `overlays` are only supported in latest version read release notes: https://github.com/elixir-lang/elixir/releases/tag/v1.10.0 Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * Adding tarball assembly during release Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * updated HOSTING.md Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * Added support for db migration Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * minor corrections Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * initializing admin user Admin user has been added in the "migration" phase. A default user is automatically created in the process. One can provide the related env variables, else a new one will be automatically created for you. Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * Initial base domain update - phase#1 These changes are only meant for correct operating it under self-hosting. There are many other cosmetic changes, that require updates to email, site and other places where the original website and author is used. Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * Using dedicated config variable `base_domain` instead Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * adding base_domain to releases config Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * removing the dedicated config "base_domain", relying on endpoint host Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * Removed the usage of "Mix" in code! It is bad practice to use "mix" module inside the code as in actual release this module is unavailable. Replacing this with a config environment variable Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * Added support for SMTP via Bamboo Smtp Adapter Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * Capturing SMTP errors via Sentry Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * Minor updates Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * Adding junit formatter -- useful for generating test reports Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * adding documentation for default user * Resolve "Gitlab Adoption: Add supported services in "Security & Compliance"" * bumping up the debian version to fix issues fixing some vulnerabilities identified by the scanning tools * More updates for self-hosting Changes in most of the places to suit self-hosting. Although, there are some which have been left-off. Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * quick-dirty-fix! * bumping up the db connect timeout Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * bumping up the db connect timeout Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * bumping up the db connect timeout Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * bumping up timeout - skipping MRs :-/ * removing restrictions on watching for changes this stuff isn't working * Update HOSTING.md * renamed the module name * reverting formatting-whitespace changes Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * reverting the name to release Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * adding docker-compose.yml and related instructions Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * using `plausible_url` instead of assuming `https` this is because, it is much to test in local dev machines and in most cases there's already a layer above which is capable for `https` termination and http -> https upgrade Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * WIP: merging changes from upstream Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * wip: more changes * Pushing in changes from upstream Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * changes to ci for testing Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * cleaning up and finishing clickhouse integration Signed-off-by: Chandra Tungathurthi <tckb@tgrthi.me> * updating readme with hosting detailspull/68/head
parent
89e807b469
commit
5eb8929929
@ -0,0 +1,125 @@
|
||||
include:
|
||||
- template: Container-Scanning.gitlab-ci.yml
|
||||
- template: License-Scanning.gitlab-ci.yml
|
||||
- template: SAST.gitlab-ci.yml
|
||||
|
||||
stages:
|
||||
- prepare
|
||||
- compile
|
||||
- test
|
||||
- build
|
||||
- postbuild
|
||||
|
||||
.commons: &elixir-commons
|
||||
image: elixir:1.10.3
|
||||
cache:
|
||||
key: ${CI_COMMIT_REF_SLUG}
|
||||
paths:
|
||||
- $CI_PROJECT_DIR/.mix
|
||||
- $CI_PROJECT_DIR/priv/plts
|
||||
- ~/.hex/
|
||||
before_script:
|
||||
- mkdir -p $CI_PROJECT_DIR/priv/plts/
|
||||
- mix local.hex --force && mix local.rebar --force
|
||||
- chmod +x .gitlab/build-scripts/*
|
||||
- source .gitlab/build-scripts/docker.gitlab.sh
|
||||
|
||||
deps:
|
||||
<<: *elixir-commons
|
||||
stage: prepare
|
||||
variables:
|
||||
MIX_HOME: $CI_PROJECT_DIR/.mix
|
||||
script:
|
||||
- mix deps.get
|
||||
dependencies: []
|
||||
artifacts:
|
||||
paths:
|
||||
- mix.lock
|
||||
- deps
|
||||
|
||||
compile:
|
||||
<<: *elixir-commons
|
||||
stage: compile
|
||||
script:
|
||||
- mix compile
|
||||
dependencies:
|
||||
- deps
|
||||
artifacts:
|
||||
paths:
|
||||
- mix.lock
|
||||
- _build
|
||||
- deps
|
||||
|
||||
|
||||
license_scanning:
|
||||
stage: compile
|
||||
dependencies:
|
||||
- deps
|
||||
|
||||
sast:
|
||||
stage: compile
|
||||
|
||||
test:ex_unit:
|
||||
<<: *elixir-commons
|
||||
services:
|
||||
- postgres
|
||||
- name: yandex/clickhouse-server:20.3.9.70
|
||||
alias: clickhouse
|
||||
stage: test
|
||||
variables:
|
||||
POSTGRES_PASSWORD: postgres
|
||||
POSTGRES_USER: postgres
|
||||
DATABASE_URL: postgres://postgres:postgres@postgres:5432/plausible_test?currentSchema=default
|
||||
CLICKHOUSE_DATABASE_HOST: clickhouse
|
||||
CLICKHOUSE_DATABASE_NAME: plausible_test
|
||||
MIX_HOME: $CI_PROJECT_DIR/.mix
|
||||
before_script:
|
||||
- apt update && apt install -y clickhouse-client
|
||||
- clickhouse-client --host clickhouse --query "CREATE DATABASE IF NOT EXISTS plausible_test"
|
||||
script:
|
||||
- mix test --cover
|
||||
coverage: '/\[TOTAL\]\s+(\d+\.\d+)%/'
|
||||
dependencies:
|
||||
- compile
|
||||
artifacts:
|
||||
reports:
|
||||
junit: plausible-report.xml
|
||||
|
||||
build:docker:
|
||||
<<: *elixir-commons
|
||||
image:
|
||||
name: gcr.io/kaniko-project/executor:debug
|
||||
entrypoint: [""]
|
||||
stage: build
|
||||
variables:
|
||||
MIX_ENV: prod
|
||||
MIX_HOME: $CI_PROJECT_DIR/.mix/
|
||||
APP_VERSION: $CI_COMMIT_SHORT_SHA
|
||||
before_script:
|
||||
- chmod +x .gitlab/build-scripts/*
|
||||
- source .gitlab/build-scripts/docker.gitlab.sh
|
||||
- docker_create_config
|
||||
script:
|
||||
- docker_build_image
|
||||
dependencies:
|
||||
- compile
|
||||
only:
|
||||
- master
|
||||
|
||||
deploy:plausible:
|
||||
stage: postbuild
|
||||
script:
|
||||
- "curl -X POST -F token=$PLAUSIBLE_DEPLOY_TOKEN -F ref=master -F variables[IMAGE_TAG]=${CI_COMMIT_REF_SLUG}-${CI_COMMIT_SHORT_SHA} $PLAUSIBLE_DEPLOY_PROJECT"
|
||||
only:
|
||||
- master
|
||||
|
||||
container_scanning:
|
||||
stage: postbuild
|
||||
image: registry.gitlab.com/gitlab-org/security-products/analyzers/klar:$CS_MAJOR_VERSION
|
||||
variables:
|
||||
CS_MAJOR_VERSION: 2
|
||||
KLAR_TRACE: "true"
|
||||
CLAIR_TRACE: "true"
|
||||
CLAIR_OUTPUT: "medium"
|
||||
CI_APPLICATION_REPOSITORY: $CI_REGISTRY_IMAGE
|
||||
CI_APPLICATION_TAG: ${CI_COMMIT_REF_SLUG}-$CI_COMMIT_SHORT_SHA
|
||||
@ -0,0 +1,17 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
chmod a+x /app/*.sh
|
||||
|
||||
if [[ "$1" = 'run' ]]; then
|
||||
exec gosu plausibleuser /app/bin/plausible start
|
||||
|
||||
elif [[ "$1" = 'db' ]]; then
|
||||
exec gosu plausibleuser /app/"$2".sh
|
||||
else
|
||||
exec "$@"
|
||||
|
||||
fi
|
||||
|
||||
exec "$@"
|
||||
|
||||
@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
############################
|
||||
function docker_create_config() {
|
||||
############################
|
||||
mkdir -p /kaniko/.docker/
|
||||
echo "###############"
|
||||
echo "Logging into GitLab Container Registry with CI credentials for kaniko..."
|
||||
echo "###############"
|
||||
echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
|
||||
echo ""
|
||||
|
||||
}
|
||||
|
||||
|
||||
############################
|
||||
function docker_build_image() {
|
||||
############################
|
||||
if [[ -f Dockerfile ]]; then
|
||||
echo "###############"
|
||||
echo "Building Dockerfile-based application..."
|
||||
echo "###############"
|
||||
|
||||
/kaniko/executor \
|
||||
--cache=true \
|
||||
--context ${CI_PROJECT_DIR} \
|
||||
--dockerfile ${CI_PROJECT_DIR}/Dockerfile \
|
||||
--destination ${CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_SLUG}-${CI_COMMIT_SHORT_SHA} \
|
||||
--destination ${CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_SLUG}-latest \
|
||||
\
|
||||
"$@"
|
||||
|
||||
else
|
||||
echo "No Dockerfile found."
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
@ -0,0 +1,82 @@
|
||||
# we can not use the pre-built tar because the distribution is
|
||||
# platform specific, it makes sense to build it in the docker
|
||||
|
||||
#### Builder
|
||||
FROM elixir:1.10.3 as buildcontainer
|
||||
|
||||
# preparation
|
||||
ARG APP_VER=0.0.1
|
||||
ENV GOSU_VERSION 1.11
|
||||
ENV MIX_ENV=prod
|
||||
ENV NODE_ENV=production
|
||||
ENV APP_VERSION=$APP_VER
|
||||
|
||||
RUN mkdir /app
|
||||
WORKDIR /app
|
||||
|
||||
# install build dependencies
|
||||
RUN apt-get update && \
|
||||
apt-get install -y git build-essential nodejs yarn python npm --no-install-recommends && \
|
||||
npm install npm@latest -g && \
|
||||
npm install -g webpack
|
||||
|
||||
RUN apt-get install -y --no-install-recommends ca-certificates wget \
|
||||
&& apt-get install -y --install-recommends gnupg2 dirmngr
|
||||
|
||||
# grab gosu for easy step-down from root
|
||||
RUN set -x \
|
||||
&& dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')" \
|
||||
&& wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch" \
|
||||
&& wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc" \
|
||||
&& export GNUPGHOME="$(mktemp -d)" \
|
||||
&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \
|
||||
&& gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \
|
||||
&& command -v gpgconf && gpgconf --kill all || : \
|
||||
&& rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc \
|
||||
&& chmod +x /usr/local/bin/gosu \
|
||||
&& gosu --version \
|
||||
&& gosu nobody true
|
||||
|
||||
COPY config ./config
|
||||
COPY assets ./assets
|
||||
COPY priv ./priv
|
||||
COPY lib ./lib
|
||||
COPY mix.exs ./
|
||||
COPY mix.lock ./
|
||||
RUN mix local.hex --force && \
|
||||
mix local.rebar --force && \
|
||||
mix deps.get --only prod && \
|
||||
mix deps.compile
|
||||
|
||||
RUN npm audit fix --prefix ./assets && \
|
||||
npm install --prefix ./assets && \
|
||||
npm run deploy --prefix ./assets && \
|
||||
mix phx.digest priv/static
|
||||
|
||||
WORKDIR /app
|
||||
COPY rel rel
|
||||
RUN mix release plausible
|
||||
|
||||
|
||||
# Main Docker Image
|
||||
FROM debian:bullseye
|
||||
LABEL maintainer="tckb <tckb@tgrthi.me>"
|
||||
ENV LANG=C.UTF-8
|
||||
|
||||
RUN apt-get update && \
|
||||
apt-get install -y bash openssl --no-install-recommends&& \
|
||||
apt-get clean autoclean && \
|
||||
apt-get autoremove --yes && \
|
||||
rm -rf /var/lib/{apt,dpkg,cache,log}/
|
||||
|
||||
COPY .gitlab/build-scripts/docker-entrypoint.sh /entrypoint.sh
|
||||
|
||||
RUN chmod a+x /entrypoint.sh && \
|
||||
useradd -d /app -u 1000 -s /bin/bash -m plausibleuser
|
||||
|
||||
COPY --from=buildcontainer /usr/local/bin/gosu /usr/local/bin/gosu
|
||||
COPY --from=buildcontainer /app/_build/prod/rel/plausible /app
|
||||
RUN chown -R plausibleuser:plausibleuser /app
|
||||
WORKDIR /app
|
||||
ENTRYPOINT ["/entrypoint.sh"]
|
||||
CMD ["run"]
|
||||
@ -0,0 +1,152 @@
|
||||
# Plausible Analytics
|
||||
Self-hosting is possible based on the docker images and are automatically pushed into [Gitlab hosted docker](registry.gitlab.com/tckb-public/plausible) registry for all commits on `master` branch.
|
||||
All `master-*` tags are considered to be stable and are persisted. Any other tag in the registry is considered to be for development purposes and/or unstable and are auto-deleted after a week.
|
||||
|
||||
|
||||
### Building Docker image
|
||||
Besides the GitlabCI, one can build docker image from [Dockerfile](./Dockerfile).
|
||||
|
||||
#### Up and Running
|
||||
The repo supplies with a [Docker Compose](./docker-compose.yml) file, this serves as a sample for running Plausible with Docker.
|
||||
In this sample, the db migration is done by default on startup, so you need to clean the data up every time you run:
|
||||
|
||||
First run
|
||||
```bash
|
||||
$ docker-compose up
|
||||
```
|
||||
|
||||
subsequent runs--
|
||||
```bash
|
||||
$ docker-compose down
|
||||
$ docker volume rm plausible_db-data -f
|
||||
$ docker-compose up
|
||||
```
|
||||
|
||||
### Non-docker building
|
||||
It is possible to create a release artifact by running a release.
|
||||
|
||||
```elixir
|
||||
MIX_ENV=prod mix release plausible
|
||||
```
|
||||
the release will create the pre-packed artifact at `_build/prod/rel/plausible/bin/plausible`, the release will also create a tarball at `_build/prod/` for convenience.
|
||||
|
||||
Note, that you have to feed in the related environment variables (see below `Environment Variables`)
|
||||
|
||||
## Database Migration
|
||||
On the initial setup, a migration step is necessary to create database and table schemas needed for initial bootup.
|
||||
Normally, this done by mix aliases like `ecto.setup` defined in the `mix.exs`. As this not available in "released" artifact, [`plausible_migration.ex`](./lib/plausible_migration.ex) facilitates this process.
|
||||
The overlay [scripts](./rel/overlays) take care of these.
|
||||
|
||||
After the release, these are available under `_build/prod/rel/plausible` --
|
||||
|
||||
|
||||
```bash
|
||||
_build/prod/rel/plausible/createdb.sh
|
||||
_build/prod/rel/plausible/migrate.sh
|
||||
_build/prod/rel/plausible/rollback.sh
|
||||
_build/prod/rel/plausible/seed.sh
|
||||
```
|
||||
|
||||
the same is available in the docker images as follows --
|
||||
|
||||
```bash
|
||||
docker run plausible:master-12add db createdb
|
||||
docker run plausible:master-12add db migrate
|
||||
docker run plausible:master-12add db rollback
|
||||
docker run plausible:master-12add db seed
|
||||
```
|
||||
|
||||
|
||||
## Environment Variables
|
||||
Plausible relies on the several services for operating, the expected environment variables are explaiend below.
|
||||
|
||||
### Server
|
||||
Following are the variables that can be used to configure the availability of the server.
|
||||
|
||||
- HOST (*String*)
|
||||
- The hosting address of the server. For running on local system, this can be set to **localhost**. In production systems, this can be your ingress host.
|
||||
- PORT (*Number*)
|
||||
- The port on which the server is available.
|
||||
- SECRET_KEY_BASE (*String*)
|
||||
- An internal secret key used by [Phoenix Framework](https://www.phoenixframework.org/). Follow the [instructions](https://hexdocs.pm/phoenix/Mix.Tasks.Phx.Gen.Secret.html#content) to generate one.
|
||||
- ENVIRONMENT (*String*)
|
||||
- The current running environment. _defaults to **prod**_
|
||||
- APP_VERSION (*String*)
|
||||
- The version of the app running. _defaults to current docker tag_
|
||||
|
||||
### 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 ;).
|
||||
It is *highly* recommended that you configure these parameters.
|
||||
|
||||
- ADMIN_USER_NAME
|
||||
- The default ("admin") username. _if not provided, one will be generated for you_
|
||||
- ADMIN_USER_EMAIL
|
||||
- The default ("admin") user email. _if not provided, one will be generated for you_
|
||||
- ADMIN_USER_PWD
|
||||
- The default ("admin") user password. _if not provided, one will be generated for you_
|
||||
|
||||
### Mailer/SMTP Setup
|
||||
|
||||
- MAILER_ADAPTER (*String*)
|
||||
- The adapter used for sending out e-mails. Available: `Bamboo.PostmarkAdapter` / `Bamboo.SMTPAdapter`
|
||||
- MAILER_EMAIL (*String*)
|
||||
- The email id to use for as _from_ address of all communications from Plausible.
|
||||
|
||||
In case of `Bamboo.SMTPAdapter` you need to supply the following variables:
|
||||
|
||||
- SMTP_HOST_ADDR (*String*)
|
||||
- The host address of your smtp server.
|
||||
- SMTP_HOST_PORT (*Number*)
|
||||
- The port of your smtp server.
|
||||
- SMTP_USER_NAME (*String*)
|
||||
- The username/email for smtp auth.
|
||||
- SMTP_USER_PWD (*String*)
|
||||
- The password for smtp auth.
|
||||
- SMTP_HOST_SSL_ENABLED (*Boolean String*)
|
||||
- If ssl is enabled for connecting to Smtp, _defaults to `false`_
|
||||
- SMTP_RETRIES (*Number*)
|
||||
- Number of retries to make until mailer gives up. _defaults to `2`_
|
||||
- SMTP_MX_LOOKUPS_ENABLED (*Boolean String*)
|
||||
- If MX lookups should be done before sending out emails. _defaults to `false`_
|
||||
|
||||
### Database
|
||||
|
||||
Plausible uses postgresql as database for storing all the user-data. Use the following the variables to configure it.
|
||||
|
||||
- DATABASE_URL (*String*)
|
||||
- The repo Url as dictated [here](https://hexdocs.pm/ecto/Ecto.Repo.html#module-urls)
|
||||
- DATABASE_POOL_SIZE (*Number*)
|
||||
- A default pool size for connecting to the database, defaults to *10*, a higher number is recommended for a production system.
|
||||
- DATABASE_TLS_ENABLED (*Boolean String*)
|
||||
- A flag that says whether to connect to the database via TLS, read [here](https://www.postgresql.org/docs/10/ssl-tcp.html)
|
||||
|
||||
For performance reasons, all the analytics events are stored in clickhouse:
|
||||
|
||||
- CLICKHOUSE_DATABASE_HOST (*String*)
|
||||
- CLICKHOUSE_DATABASE_NAME (*String*)
|
||||
- CLICKHOUSE_DATABASE_USER (*String*)
|
||||
- CLICKHOUSE_DATABASE_PASSWORD (*String*)
|
||||
- CLICKHOUSE_DATABASE_POOLSIZE (*Number*)
|
||||
- A default pool size for connecting to the database, defaults to *10*, a higher number is recommended for a production system.
|
||||
|
||||
### External Services
|
||||
|
||||
- [Google Client](https://developers.google.com/api-client-library)
|
||||
- GOOGLE_CLIENT_ID
|
||||
- GOOGLE_CLIENT_SECRET
|
||||
- [Sentry](https://sentry.io/)
|
||||
- SENTRY_DSN
|
||||
- [Paddle](https://paddle.com/)
|
||||
- PADDLE_VENDOR_AUTH_CODE
|
||||
- [PostMark](https://postmarkapp.com/), only in case of `Bamboo.PostmarkAdapter` mail adapter.
|
||||
- POSTMARK_API_KEY
|
||||
|
||||
Apart from these, there are also the following integrations
|
||||
|
||||
- [Twitter](https://developer.twitter.com/en/docs)
|
||||
- TWITTER_CONSUMER_KEY
|
||||
- TWITTER_CONSUMER_SECRET
|
||||
- TWITTER_ACCESS_TOKEN
|
||||
- TWITTER_ACCESS_TOKEN_SECRET
|
||||
- [Slack](https://api.slack.com/messaging/webhooks)
|
||||
- SLACK_WEBHOOK
|
||||
@ -1,105 +1,7 @@
|
||||
use Mix.Config
|
||||
|
||||
# For production, don't forget to configure the url host
|
||||
# to something meaningful, Phoenix uses this information
|
||||
# when generating URLs.
|
||||
#
|
||||
# Note we also include the path to a cache manifest
|
||||
# containing the digested version of static files. This
|
||||
# manifest is generated by the `mix phx.digest` task,
|
||||
# which you should run after static files are built and
|
||||
# before starting your production server.
|
||||
config :plausible, PlausibleWeb.Endpoint,
|
||||
http: [:inet6, port: System.get_env("PORT") || 4000],
|
||||
url: [host: System.get_env("HOST"), scheme: "https", port: 443],
|
||||
cache_static_manifest: "priv/static/cache_manifest.json"
|
||||
# For the actual-production deployments we will use releases,
|
||||
# i.e., "releases.exs" is the _actual_ production config
|
||||
# see "releases.exs"
|
||||
|
||||
# Do not print debug messages in production
|
||||
config :logger, level: :info
|
||||
|
||||
# ## SSL Support
|
||||
#
|
||||
# To get SSL working, you will need to add the `https` key
|
||||
# to the previous section and set your `:url` port to 443:
|
||||
#
|
||||
# config :plausible, PlausibleWeb.Endpoint,
|
||||
# ...
|
||||
# url: [host: "example.com", port: 443],
|
||||
# https: [
|
||||
# :inet6,
|
||||
# port: 443,
|
||||
# cipher_suite: :strong,
|
||||
# keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"),
|
||||
# certfile: System.get_env("SOME_APP_SSL_CERT_PATH")
|
||||
# ]
|
||||
#
|
||||
# The `cipher_suite` is set to `:strong` to support only the
|
||||
# latest and more secure SSL ciphers. This means old browsers
|
||||
# and clients may not be supported. You can set it to
|
||||
# `:compatible` for wider support.
|
||||
#
|
||||
# `:keyfile` and `:certfile` expect an absolute path to the key
|
||||
# and cert in disk or a relative path inside priv, for example
|
||||
# "priv/ssl/server.key". For all supported SSL configuration
|
||||
# options, see https://hexdocs.pm/plug/Plug.SSL.html#configure/1
|
||||
#
|
||||
# We also recommend setting `force_ssl` in your endpoint, ensuring
|
||||
# no data is ever sent via http, always redirecting to https:
|
||||
#
|
||||
# config :plausible, PlausibleWeb.Endpoint,
|
||||
# force_ssl: [hsts: true]
|
||||
#
|
||||
# Check `Plug.SSL` for all available options in `force_ssl`.
|
||||
|
||||
# ## Using releases (distillery)
|
||||
#
|
||||
# If you are doing OTP releases, you need to instruct Phoenix
|
||||
# to start the server for all endpoints:
|
||||
#
|
||||
# config :phoenix, :serve_endpoints, true
|
||||
#
|
||||
# Alternatively, you can configure exactly which server to
|
||||
# start per endpoint:
|
||||
#
|
||||
# config :plausible, PlausibleWeb.Endpoint, server: true
|
||||
#
|
||||
# Note you can't rely on `System.get_env/1` when using releases.
|
||||
# See the releases documentation accordingly.
|
||||
|
||||
# Finally import the config/prod.secret.exs which should be versioned
|
||||
# separately.
|
||||
config :plausible, PlausibleWeb.Endpoint,
|
||||
secret_key_base: System.get_env("SECRET_KEY_BASE")
|
||||
|
||||
config :plausible, :clickhouse,
|
||||
hostname: System.get_env("CLICKHOUSE_DATABASE_HOST"),
|
||||
database: System.get_env("CLICKHOUSE_DATABASE_NAME"),
|
||||
username: System.get_env("CLICKHOUSE_DATABASE_USER"),
|
||||
password: System.get_env("CLICKHOUSE_DATABASE_PASSWORD"),
|
||||
pool_size: 30
|
||||
|
||||
# Configure your database
|
||||
config :plausible, Plausible.Repo,
|
||||
adapter: Ecto.Adapters.Postgres,
|
||||
url: System.get_env("AVIEN_DATABASE_URL"),
|
||||
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
|
||||
timeout: 10_000,
|
||||
ssl: true
|
||||
|
||||
config :plausible, :google,
|
||||
client_id: System.get_env("GOOGLE_CLIENT_ID"),
|
||||
client_secret: System.get_env("GOOGLE_CLIENT_SECRET")
|
||||
|
||||
config :plausible, :slack,
|
||||
webhook: System.get_env("SLACK_WEBHOOK")
|
||||
|
||||
config :plausible, Plausible.Mailer,
|
||||
adapter: Bamboo.PostmarkAdapter,
|
||||
api_key: System.get_env("POSTMARK_API_KEY")
|
||||
|
||||
config :plausible, :twitter, [
|
||||
consumer_key: System.get_env("TWITTER_CONSUMER_KEY"),
|
||||
consumer_secret: System.get_env("TWITTER_CONSUMER_SECRET"),
|
||||
token: System.get_env("TWITTER_ACCESS_TOKEN"),
|
||||
token_secret: System.get_env("TWITTER_ACCESS_TOKEN_SECRET")
|
||||
]
|
||||
import_config "releases.exs"
|
||||
|
||||
@ -0,0 +1,128 @@
|
||||
import Config
|
||||
|
||||
### Mandatory params Start
|
||||
# it is highly recommended to change this parameters in production systems
|
||||
# params are made optional to facilitate smooth release
|
||||
|
||||
port = System.get_env("PORT") || 8000
|
||||
host = System.get_env("HOST", "localhost")
|
||||
|
||||
secret_key_base =
|
||||
System.get_env(
|
||||
"SECRET_KEY_BASE",
|
||||
"/NJrhNtbyCVAsTyvtk1ZYCwfm981Vpo/0XrVwjJvemDaKC/vsvBRevLwsc6u8RCg"
|
||||
)
|
||||
|
||||
db_pool_size = String.to_integer(System.get_env("DATABASE_POOL_SIZE", "10"))
|
||||
|
||||
db_url =
|
||||
System.get_env(
|
||||
"DATABASE_URL",
|
||||
"postgres://postgres:postgres@127.0.0.1:5432/plausible_test?currentSchema=default"
|
||||
)
|
||||
|
||||
db_tls_enabled? = String.to_existing_atom(System.get_env("DATABASE_TLS_ENABLED", "false"))
|
||||
admin_user = System.get_env("ADMIN_USER_NAME")
|
||||
admin_email = System.get_env("ADMIN_USER_EMAIL")
|
||||
admin_pwd = System.get_env("ADMIN_USER_PWD")
|
||||
env = System.get_env("ENVIRONMENT", "prod")
|
||||
mailer_adapter = System.get_env("MAILER_ADAPTER", "Bamboo.PostmarkAdapter")
|
||||
mailer_email = System.get_env("MAILER_EMAIL", "hello@plausible.local")
|
||||
app_version = System.get_env("APP_VERSION", "0.0.1")
|
||||
ck_host = System.get_env("CLICKHOUSE_DATABASE_HOST", "localhost")
|
||||
ck_db = System.get_env("CLICKHOUSE_DATABASE_NAME", "plausible_dev")
|
||||
ck_db_user = System.get_env("CLICKHOUSE_DATABASE_USER")
|
||||
ck_db_pwd = System.get_env("CLICKHOUSE_DATABASE_PASSWORD")
|
||||
ck_db_pool = System.get_env("CLICKHOUSE_DATABASE_POOLSIZE") || 10
|
||||
### Mandatory params End
|
||||
|
||||
sentry_dsn = System.get_env("SENTRY_DSN")
|
||||
paddle_auth_code = System.get_env("PADDLE_VENDOR_AUTH_CODE")
|
||||
google_cid = System.get_env("GOOGLE_CLIENT_ID")
|
||||
google_secret = System.get_env("GOOGLE_CLIENT_SECRET")
|
||||
slack_hook_url = System.get_env("SLACK_WEBHOOK")
|
||||
twitter_consumer_key = System.get_env("TWITTER_CONSUMER_KEY")
|
||||
twitter_consumer_secret = System.get_env("TWITTER_CONSUMER_SECRET")
|
||||
twitter_token = System.get_env("TWITTER_ACCESS_TOKEN")
|
||||
twitter_token_secret = System.get_env("TWITTER_ACCESS_TOKEN_SECRET")
|
||||
postmark_api_key = System.get_env("POSTMARK_API_KEY")
|
||||
|
||||
config :plausible,
|
||||
admin_user: admin_user,
|
||||
admin_email: admin_email,
|
||||
admin_pwd: admin_pwd,
|
||||
environment: env,
|
||||
mailer_email: mailer_email
|
||||
|
||||
config :plausible, PlausibleWeb.Endpoint,
|
||||
url: [host: host, port: port],
|
||||
http: [
|
||||
port: port
|
||||
],
|
||||
secret_key_base: secret_key_base,
|
||||
cache_static_manifest: "priv/static/cache_manifest.json",
|
||||
check_origin: false,
|
||||
load_from_system_env: true,
|
||||
server: true,
|
||||
code_reloader: false
|
||||
|
||||
config :plausible,
|
||||
Plausible.Repo,
|
||||
pool_size: db_pool_size,
|
||||
url: db_url,
|
||||
adapter: Ecto.Adapters.Postgres,
|
||||
ssl: db_tls_enabled?
|
||||
|
||||
config :sentry,
|
||||
dsn: sentry_dsn,
|
||||
environment_name: env,
|
||||
release: app_version,
|
||||
tags: %{app_version: app_version}
|
||||
|
||||
config :plausible, :paddle, vendor_auth_code: paddle_auth_code
|
||||
|
||||
config :plausible, :google,
|
||||
client_id: google_cid,
|
||||
client_secret: google_secret
|
||||
|
||||
config :plausible, :slack, webhook: slack_hook_url
|
||||
|
||||
config :plausible, :clickhouse,
|
||||
hostname: ck_host,
|
||||
database: ck_db,
|
||||
username: ck_db_user,
|
||||
password: ck_db_pwd,
|
||||
pool_size: ck_db_pool
|
||||
|
||||
case mailer_adapter do
|
||||
"Bamboo.PostmarkAdapter" ->
|
||||
config :plausible, Plausible.Mailer,
|
||||
adapter: :"Elixir.#{mailer_adapter}",
|
||||
api_key: System.get_env("POSTMARK_API_KEY")
|
||||
|
||||
"Bamboo.SMTPAdapter" ->
|
||||
config :plausible, Plausible.Mailer,
|
||||
adapter: :"Elixir.#{mailer_adapter}",
|
||||
server: System.fetch_env!("SMTP_HOST_ADDR"),
|
||||
hostname: System.get_env("HOST", "localhost"),
|
||||
port: System.fetch_env!("SMTP_HOST_PORT"),
|
||||
username: System.fetch_env!("SMTP_USER_NAME"),
|
||||
password: System.fetch_env!("SMTP_USER_PWD"),
|
||||
tls: :if_available,
|
||||
allowed_tls_versions: [:tlsv1, :"tlsv1.1", :"tlsv1.2"],
|
||||
ssl: System.get_env("SMTP_HOST_SSL_ENABLED") || true,
|
||||
retries: System.get_env("SMTP_RETRIES") || 2,
|
||||
no_mx_lookups: System.get_env("SMTP_MX_LOOKUPS_ENABLED") || true,
|
||||
auth: :always
|
||||
|
||||
_ ->
|
||||
raise "Unknown mailer_adapter; expected SMTPAdapter or PostmarkAdapter"
|
||||
end
|
||||
|
||||
config :plausible, :twitter,
|
||||
consumer_key: twitter_consumer_key,
|
||||
consumer_secret: twitter_consumer_secret,
|
||||
token: twitter_token,
|
||||
token_secret: twitter_token_secret
|
||||
|
||||
config :logger, level: :warn
|
||||
@ -0,0 +1,79 @@
|
||||
# NOTE:
|
||||
# This Docker-compose file is created as a sample and should not be used directly for production
|
||||
# You can adjust the settings as-per your environment
|
||||
version: "3.3"
|
||||
services:
|
||||
|
||||
# As it says, this service is a fake smtp server which accepts SMTP connections
|
||||
# the inbox is available at localhost:8025, in actuality, you need to use a proper smtp server
|
||||
fakesmtp_server:
|
||||
container_name: fakesmtp_server
|
||||
image: mailhog/mailhog:v1.0.0
|
||||
ports:
|
||||
- 1025:1025
|
||||
- 8025:8025
|
||||
healthcheck:
|
||||
test: echo | telnet 127.0.0.1 1025
|
||||
|
||||
|
||||
plausible_db:
|
||||
container_name: plausible_db
|
||||
image: postgres:9.4
|
||||
command: ["postgres", "-c", "log_statement=all", "-c", "log_destination=stderr"]
|
||||
volumes:
|
||||
- db-data:/var/lib/postgresql/data
|
||||
environment:
|
||||
- POSTGRES_PASSWORD=postgres
|
||||
- POSTGRES_DB=plausible_db
|
||||
- POSTGRES_USER=postgres
|
||||
ports:
|
||||
- 5432:5432
|
||||
|
||||
plausible_events_db:
|
||||
container_name: plausible_events_db
|
||||
image: yandex/clickhouse-server
|
||||
ports:
|
||||
- 8123:8123
|
||||
|
||||
plausible:
|
||||
container_name: plausible
|
||||
build:
|
||||
context: .
|
||||
dockerfile: ./Dockerfile
|
||||
command: sh -c "sleep 10 && /entrypoint.sh db createdb && /entrypoint.sh db migrate &&/entrypoint.sh run"
|
||||
environment:
|
||||
- ENVIRONMENT=production
|
||||
- PORT=8080
|
||||
- SECRET_KEY_BASE=iYb1mP5cnmY+gUxo7C/h6XMigossPhzwd8/ic6LFnQ9Y58Fl1xduSWaPq0fHDdbn
|
||||
- SIGNING_SALT=PL/THF0VMOzuv1bOcldjDzYFBLryvXNs
|
||||
- HOST=localhost
|
||||
- DATABASE_URL=postgres://postgres:postgres@plausible_db:5432/plausible
|
||||
- DATABASE_TLS_ENABLED=false
|
||||
- ADMIN_USER_NAME=admin
|
||||
- ADMIN_USER_EMAIL=admin@plausible.local
|
||||
- ADMIN_USER_PWD=admin@1234!
|
||||
- APP_VERSION=test
|
||||
- MAILER_ADAPTER=Bamboo.SMTPAdapter
|
||||
- SMTP_HOST_ADDR=fakesmtp_server
|
||||
- SMTP_HOST_PORT=1025
|
||||
- SMTP_USER_NAME=fakeuser@plausible.local
|
||||
- SMTP_USER_PWD=password
|
||||
- SMTP_HOST_SSL_ENABLED=false
|
||||
- SMTP_MX_LOOKUPS_ENABLED=false
|
||||
- CLICKHOUSE_DATABASE_HOST=plausible_events_db
|
||||
- CLICKHOUSE_DATABASE_NAME=plausible_events_db
|
||||
depends_on:
|
||||
- plausible_events_db
|
||||
- plausible_db
|
||||
- fakesmtp_server
|
||||
ports:
|
||||
- 80:8080
|
||||
links:
|
||||
- plausible_db
|
||||
- plausible_events_db
|
||||
- fakesmtp_server
|
||||
|
||||
|
||||
volumes:
|
||||
db-data:
|
||||
driver: local
|
||||
@ -1,3 +1,17 @@
|
||||
defmodule Plausible.Mailer do
|
||||
use Bamboo.Mailer, otp_app: :plausible
|
||||
|
||||
def send_email(email) do
|
||||
try do
|
||||
Plausible.Mailer.deliver_now(email)
|
||||
rescue
|
||||
error ->
|
||||
Sentry.capture_exception(error,
|
||||
stacktrace: __STACKTRACE__,
|
||||
extra: %{extra: "Error while sending email"}
|
||||
)
|
||||
|
||||
raise error
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -0,0 +1,234 @@
|
||||
defmodule Plausible.Release do
|
||||
use Plausible.Repo
|
||||
@app :plausible
|
||||
@start_apps [
|
||||
:postgrex,
|
||||
:ecto
|
||||
]
|
||||
alias Mix.Tasks.HydrateClickhouse, as: Clickhouse
|
||||
|
||||
def init_admin do
|
||||
{admin_email, admin_user, admin_pwd} =
|
||||
validate_admin(
|
||||
{Application.get_env(:plausible, :admin_email),
|
||||
Application.get_env(:plausible, :admin_user),
|
||||
Application.get_env(:plausible, :admin_pwd)}
|
||||
)
|
||||
|
||||
{:ok, admin} = Plausible.Auth.create_user(admin_user, admin_email)
|
||||
# set the password
|
||||
{:ok, admin} = Plausible.Auth.User.set_password(admin, admin_pwd) |> Repo.update()
|
||||
# bump-up the trail period
|
||||
admin
|
||||
|> Ecto.Changeset.cast(%{trial_expiry_date: Timex.today() |> Timex.shift(years: 100)}, [
|
||||
:trial_expiry_date
|
||||
])
|
||||
|> Repo.update()
|
||||
|
||||
IO.puts("Admin user created successful!")
|
||||
end
|
||||
|
||||
def migrate do
|
||||
prepare()
|
||||
Enum.each(repos(), &run_migrations_for/1)
|
||||
init_admin()
|
||||
IO.puts("Migrations successful!")
|
||||
end
|
||||
|
||||
def seed do
|
||||
prepare()
|
||||
# Run seed script
|
||||
Enum.each(repos(), &run_seeds_for/1)
|
||||
|
||||
# Signal shutdown
|
||||
IO.puts("Success!")
|
||||
end
|
||||
|
||||
def createdb do
|
||||
prepare()
|
||||
do_create_db()
|
||||
IO.puts("Creation of Db successful!")
|
||||
end
|
||||
|
||||
def rollback do
|
||||
prepare()
|
||||
|
||||
get_step =
|
||||
IO.gets("Enter the number of steps: ")
|
||||
|> String.trim()
|
||||
|> Integer.parse()
|
||||
|
||||
case get_step do
|
||||
{int, _trailing} ->
|
||||
Enum.each(repos(), fn repo -> run_rollbacks_for(repo, int) end)
|
||||
IO.puts("Rollback successful!")
|
||||
|
||||
:error ->
|
||||
IO.puts("Invalid integer")
|
||||
end
|
||||
end
|
||||
|
||||
##############################
|
||||
|
||||
defp validate_admin({nil, nil, nil}) do
|
||||
random_user = :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8)
|
||||
random_pwd = :crypto.strong_rand_bytes(20) |> Base.encode64() |> binary_part(0, 20)
|
||||
random_email = "#{random_user}@#{System.get_env("HOST")}"
|
||||
IO.puts("generated admin user/password: #{random_email} / #{random_pwd}")
|
||||
{random_email, random_user, random_pwd}
|
||||
end
|
||||
|
||||
defp validate_admin({admin_email, admin_user, admin_password}) do
|
||||
{admin_email, admin_user, admin_password}
|
||||
end
|
||||
|
||||
defp repos do
|
||||
Application.fetch_env!(@app, :ecto_repos)
|
||||
end
|
||||
|
||||
defp run_seeds_for(repo) do
|
||||
# Run the seed script if it exists
|
||||
seed_script = seeds_path(repo)
|
||||
|
||||
if File.exists?(seed_script) do
|
||||
IO.puts("Running seed script..")
|
||||
Code.eval_file(seed_script)
|
||||
end
|
||||
end
|
||||
|
||||
defp run_migrations_for(repo) do
|
||||
app = Keyword.get(repo.config, :otp_app)
|
||||
IO.puts("Running migrations for #{app}")
|
||||
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
|
||||
end
|
||||
|
||||
defp do_create_db do
|
||||
for repo <- repos() do
|
||||
:ok = ensure_repo_created(repo)
|
||||
end
|
||||
do_create_ch_db()
|
||||
end
|
||||
|
||||
|
||||
defp do_create_ch_db() do
|
||||
db_to_create = Keyword.get(Application.get_env(:plausible, :clickhouse),:database)
|
||||
|
||||
IO.puts("create #{inspect(db_to_create)} clickhouse database/tables if it doesn't exist")
|
||||
|
||||
Clickhousex.query(:clickhouse, "CREATE DATABASE IF NOT EXISTS #{db_to_create}", [])
|
||||
|
||||
tb_events = """
|
||||
CREATE TABLE IF NOT EXISTS #{db_to_create}.events (
|
||||
timestamp DateTime,
|
||||
name String,
|
||||
domain String,
|
||||
user_id UInt64,
|
||||
session_id UInt64,
|
||||
hostname String,
|
||||
pathname String,
|
||||
referrer String,
|
||||
referrer_source String,
|
||||
initial_referrer String,
|
||||
initial_referrer_source String,
|
||||
country_code LowCardinality(FixedString(2)),
|
||||
screen_size LowCardinality(String),
|
||||
operating_system LowCardinality(String),
|
||||
browser LowCardinality(String)
|
||||
) ENGINE = MergeTree()
|
||||
PARTITION BY toYYYYMM(timestamp)
|
||||
ORDER BY (name, domain, user_id, timestamp)
|
||||
SETTINGS index_granularity = 8192
|
||||
"""
|
||||
|
||||
Clickhousex.query(:clickhouse, tb_events, [])
|
||||
|
||||
tb_sessions = """
|
||||
CREATE TABLE IF NOT EXISTS #{db_to_create}.sessions (
|
||||
session_id UInt64,
|
||||
sign Int8,
|
||||
domain String,
|
||||
user_id UInt64,
|
||||
hostname String,
|
||||
timestamp DateTime,
|
||||
start DateTime,
|
||||
is_bounce UInt8,
|
||||
entry_page String,
|
||||
exit_page String,
|
||||
pageviews Int32,
|
||||
events Int32,
|
||||
duration UInt32,
|
||||
referrer String,
|
||||
referrer_source String,
|
||||
country_code LowCardinality(FixedString(2)),
|
||||
screen_size LowCardinality(String),
|
||||
operating_system LowCardinality(String),
|
||||
browser LowCardinality(String)
|
||||
) ENGINE = CollapsingMergeTree(sign)
|
||||
PARTITION BY toYYYYMM(start)
|
||||
ORDER BY (domain, user_id, session_id, start)
|
||||
SETTINGS index_granularity = 8192
|
||||
"""
|
||||
|
||||
Clickhousex.query(:clickhouse, tb_sessions, [])
|
||||
end
|
||||
|
||||
|
||||
defp ensure_repo_created(repo) do
|
||||
IO.puts("create #{inspect(repo)} database if it doesn't exist")
|
||||
|
||||
case repo.__adapter__.storage_up(repo.config) do
|
||||
:ok -> :ok
|
||||
{:error, :already_up} -> :ok
|
||||
{:error, term} -> {:error, term}
|
||||
end
|
||||
end
|
||||
|
||||
defp run_rollbacks_for(repo, step) do
|
||||
app = Keyword.get(repo.config, :otp_app)
|
||||
IO.puts("Running rollbacks for #{app} (STEP=#{step})")
|
||||
|
||||
{:ok, _, _} =
|
||||
Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, all: false, step: step))
|
||||
end
|
||||
|
||||
defp prepare do
|
||||
IO.puts("Loading #{@app}..")
|
||||
# Load the code for myapp, but don't start it
|
||||
:ok = Application.load(@app)
|
||||
|
||||
prepare_clickhouse()
|
||||
|
||||
IO.puts("Starting dependencies..")
|
||||
# Start apps necessary for executing migrations
|
||||
Enum.each(@start_apps, &Application.ensure_all_started/1)
|
||||
|
||||
|
||||
# Start the Repo(s) for myapp
|
||||
IO.puts("Starting repos..")
|
||||
Enum.each(repos(), & &1.start_link(pool_size: 2))
|
||||
|
||||
end
|
||||
|
||||
defp prepare_clickhouse do
|
||||
Application.ensure_all_started(:db_connection)
|
||||
Application.ensure_all_started(:hackney)
|
||||
Clickhousex.start_link([
|
||||
scheme: :http,
|
||||
port: 8123,
|
||||
name: :clickhouse,
|
||||
database: "default",
|
||||
hostname: Keyword.get(Application.get_env(:plausible,:clickhouse),:hostname)
|
||||
])
|
||||
end
|
||||
|
||||
defp seeds_path(repo), do: priv_path_for(repo, "seeds.exs")
|
||||
|
||||
defp priv_path_for(repo, filename) do
|
||||
app = Keyword.get(repo.config, :otp_app)
|
||||
IO.puts("App: #{app}")
|
||||
repo_underscore = repo |> Module.split() |> List.last() |> Macro.underscore()
|
||||
Path.join([priv_dir(app), repo_underscore, filename])
|
||||
end
|
||||
|
||||
defp priv_dir(app), do: "#{:code.priv_dir(app)}"
|
||||
end
|
||||
@ -1,4 +1,4 @@
|
||||
<%= if !@conn.assigns[:skip_plausible_tracking] do %>
|
||||
<script async defer src="https://plausible.io/js/plausible.js"></script>
|
||||
<script async defer src="<%="#{plausible_url()}/js/plausible.js"%>"></script>
|
||||
<script>window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }</script>
|
||||
<% end %>
|
||||
<% end %>
|
||||
@ -1,3 +1,15 @@
|
||||
defmodule PlausibleWeb.PageView do
|
||||
use PlausibleWeb, :view
|
||||
|
||||
def admin_email do
|
||||
Application.get_env(:plausible, :admin_email)
|
||||
end
|
||||
|
||||
def base_domain do
|
||||
PlausibleWeb.Endpoint.host()
|
||||
end
|
||||
|
||||
def plausible_url do
|
||||
PlausibleWeb.Endpoint.clean_url()
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,11 +1,12 @@
|
||||
defmodule Plausible.Repo.Migrations.AddPublicSites do
|
||||
use Ecto.Migration
|
||||
@host Application.get_env(:plausible, :url, :host)
|
||||
|
||||
def change do
|
||||
alter table(:sites) do
|
||||
add :public, :boolean, null: false, default: false
|
||||
end
|
||||
|
||||
execute "update sites set public=true where domain='plausible.io'"
|
||||
execute "update sites set public=true where domain='#{@host}'"
|
||||
end
|
||||
end
|
||||
|
||||
@ -0,0 +1,6 @@
|
||||
@echo off
|
||||
rem Set the release to work across nodes. If using the long name format like
|
||||
rem the one below (my_app@127.0.0.1), you need to also uncomment the
|
||||
rem RELEASE_DISTRIBUTION variable below. Must be "sname", "name" or "none".
|
||||
rem set RELEASE_DISTRIBUTION=name
|
||||
rem set RELEASE_NODE=<%= @release.name %>@127.0.0.1
|
||||
@ -0,0 +1,18 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Sets and enables heart (recommended only in daemon mode)
|
||||
# case $RELEASE_COMMAND in
|
||||
# daemon*)
|
||||
# HEART_COMMAND="$RELEASE_ROOT/bin/$RELEASE_NAME $RELEASE_COMMAND"
|
||||
# export HEART_COMMAND
|
||||
# export ELIXIR_ERL_OPTIONS="-heart"
|
||||
# ;;
|
||||
# *)
|
||||
# ;;
|
||||
# esac
|
||||
|
||||
# Set the release to work across nodes. If using the long name format like
|
||||
# the one below (my_app@127.0.0.1), you need to also uncomment the
|
||||
# RELEASE_DISTRIBUTION variable below. Must be "sname", "name" or "none".
|
||||
# export RELEASE_DISTRIBUTION=name
|
||||
# export RELEASE_NODE=<%= @release.name %>@127.0.0.1
|
||||
@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
# Creates the database if needed
|
||||
|
||||
|
||||
BIN_DIR=`dirname "$0"`
|
||||
|
||||
${BIN_DIR}/bin/plausible eval Plausible.Release.createdb
|
||||
@ -0,0 +1,6 @@
|
||||
#!/bin/sh
|
||||
# starts the db migration
|
||||
|
||||
BIN_DIR=`dirname "$0"`
|
||||
|
||||
${BIN_DIR}/bin/plausible eval Plausible.Release.migrate
|
||||
@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
|
||||
BIN_DIR=`dirname "$0"`
|
||||
|
||||
${BIN_DIR}/bin/plausible eval Plausible.Release.rollback
|
||||
@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
|
||||
BIN_DIR=`dirname "$0"`
|
||||
|
||||
${BIN_DIR}/bin/plausible eval Plausible.Release.seed
|
||||
@ -0,0 +1,11 @@
|
||||
## Customize flags given to the VM: http://erlang.org/doc/man/erl.html
|
||||
## -mode/-name/-sname/-setcookie are configured via env vars, do not set them here
|
||||
|
||||
## Number of dirty schedulers doing IO work (file, sockets, and others)
|
||||
##+SDio 5
|
||||
|
||||
## Increase number of concurrent ports/sockets
|
||||
##+Q 65536
|
||||
|
||||
## Tweak GC to run more often
|
||||
##-env ERL_FULLSWEEP_AFTER 10
|
||||
Loading…
Reference in New Issue