From 4dfee6cff318edd08fe54b30c5d451973174b083 Mon Sep 17 00:00:00 2001 From: sn0w Date: Tue, 21 Aug 2018 03:19:42 +0200 Subject: [PATCH] Implement typed .env and more features --- .env.dist | 54 +++++++++++++++++++++++++++++++++-------------- Dockerfile | 21 +++++++----------- docker-compose.m4 | 4 ++-- docker-config.exs | 35 ++++++++++++++++++++++++------ pleroma | 9 ++++---- 5 files changed, 80 insertions(+), 43 deletions(-) diff --git a/.env.dist b/.env.dist index 8e394c3..51013f6 100644 --- a/.env.dist +++ b/.env.dist @@ -1,3 +1,17 @@ +# +# Note: The values of this file that are passed directly to +# pleroma need type hints to be applied correctly. +# The implemented types are int, bool, and string. +# Typed variables look like this: KEY=type:VALUE. +# Not specifying a type implies a string. +# +# In general: Your instance should work as expected if you leave the types as they are. +# That is: don't remove them, don't add them, don't change them. +# Always just change the values. +# +# You don't need to escape colons in your strings. +# PLEROMA_NAME=string:std::pleroma::coolinstance will work as expected. +# ######################### # Script settings # @@ -30,7 +44,7 @@ SCRIPT_BIND_IP=0.0.0.0 # traefik-container is connected to or a network that is interconnected # with traefik's network. In manual, apache or nginx mode this value may be empty or set to any other alphanumeric value. # (Defaults to something like "pleroma_docker_1" if empty) -DOCKER_NETWORK=pleroma +DOCKER_NETWORK=prod # The directory where all containers store their data. # Can be a relative path, "~/...", or absolute. @@ -38,7 +52,9 @@ DOCKER_NETWORK=pleroma DOCKER_DATADIR=./data # The uid/gid used by pleroma. -# custom.d will be automatically chown'ed to this. +# You should probably set this to the same +# uid/guid that cloned the pleroma-docker repo. +# This way modding pleroma becomes a lot easier. DOCKER_UID=1000 DOCKER_GID=1000 @@ -54,7 +70,7 @@ POSTGRES_IP= POSTGRES_DB=pleroma POSTGRES_USER=pleroma POSTGRES_PASSWORD=pleroma -PLEROMA_DB_POOL_SIZE=10 +PLEROMA_DB_POOL_SIZE=int:16 ########################## # Pleroma Settings # @@ -68,42 +84,48 @@ PLEROMA_VERSION=develop # The loglevel to use # (error/warn/info/debug) -PLEROMA_LOGLEVEL=warn +PLEROMA_LOGLEVEL=error # The domain/scheme where pleroma will be hosted # URL is a bare TLD # SCHEME is the protocol without "://" # PORT is the *external* port (ie that of your reverse proxy) -PLEROMA_URL=example.com +PLEROMA_URL=coolsite.moe PLEROMA_SCHEME=https -PLEROMA_PORT=443 +PLEROMA_PORT=int:443 # The seed for your secret keys # (Enter something as random as possible) -# (On linux you can try something like "dd if=/dev/urandom bs=1 count=128 2>/dev/null | base64 -w 0 | rev | cut -b 2- | rev") +# (On linux you can try something like "dd if=/dev/urandom bs=1 count=64 2>/dev/null | base64 -w 0 | rev | cut -b 2- | rev") PLEROMA_SECRET_KEY_BASE= # The name of your instance # (This is displayed in the top-left in pleroma-fe) -PLEROMA_NAME= +PLEROMA_NAME=string:coolInstance # Your contact info -PLEROMA_ADMIN_EMAIL= +PLEROMA_ADMIN_EMAIL=admin@coolsite.moe # How many chars a notice may have at max. -PLEROMA_MAX_NOTICE_CHARS=500 +PLEROMA_MAX_NOTICE_CHARS=int:500 # Whether your instance accepts new users or not (true/false) -PLEROMA_REGISTRATIONS_OPEN=true +PLEROMA_REGISTRATIONS_OPEN=bool:true -# Enable media proxy? -PLEROMA_MEDIA_PROXY_ENABLED=false +# Enable media proxy (true/false)? +PLEROMA_MEDIA_PROXY_ENABLED=bool:false # The url of your media proxy (if enabled) [with "http(s)://"] -PLEROMA_MEDIA_PROXY_URL=https://cdn.example.com +PLEROMA_MEDIA_PROXY_URL=string:https://cdn.coolsite.moe # Redirect to source on cache fail? -PLEROMA_MEDIA_PROXY_REDIRECT_ON_FAILURE=true +PLEROMA_MEDIA_PROXY_REDIRECT_ON_FAILURE=bool:true # Whether to enable the chat feature or not -PLEROMA_CHAT_ENABLED=true +PLEROMA_CHAT_ENABLED=bool:true + +# Where to store uploads. +# This is only relevant inside the container. +# The host path is always $DOCKER_DATADIR/uploads. +# So, you probably don't need to change this. +PLEROMA_UPLOADS_PATH=/uploads diff --git a/Dockerfile b/Dockerfile index b391beb..abefe4d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -43,18 +43,13 @@ RUN \ USER pleroma WORKDIR /home/pleroma -# Inject runtime config helper -COPY --chown=pleroma:pleroma ./docker-config.exs /docker-config.exs - # Get pleroma RUN git clone --progress https://git.pleroma.social/pleroma/pleroma.git ./pleroma - WORKDIR /home/pleroma/pleroma +# Get rebar/hex RUN \ - ln -s /docker-config.exs config/prod.secret.exs \ - && ln -s /docker-config.exs config/dev.secret.exs \ - && mix local.hex --force \ + mix local.hex --force \ && mix local.rebar --force # Bust the build cache @@ -69,13 +64,13 @@ RUN \ && git checkout $PLEROMA_VERSION \ && git pull --rebase --autostash -# Insert overrides -COPY --chown=pleroma:pleroma ./custom.d /pleroma +# Insert overrides and config helper +COPY --chown=pleroma:pleroma ./docker-config.exs /docker-config.exs +COPY --chown=pleroma:pleroma ./custom.d /home/pleroma/pleroma # Precompile RUN \ - mix deps.get \ + ln -s /docker-config.exs config/prod.secret.exs \ + && ln -s /docker-config.exs config/dev.secret.exs \ + && mix deps.get \ && mix compile - -# Disable dev-mode -ENV MIX_ENV=prod diff --git a/docker-compose.m4 b/docker-compose.m4 index b06a1cd..1a1b155 100644 --- a/docker-compose.m4 +++ b/docker-compose.m4 @@ -129,11 +129,11 @@ define(, <${upcase($1):-$2}>) "traefik.fe.port=4000", "traefik.fe.protocol=http", "traefik.fe.entryPoints=http,https", - "traefik.fe.frontend.rule=Host:env_inline()", + "traefik.fe.frontend.rule=Host:patsubst(__PLEROMA_URL, , <>)", "traefik.cache.port=4000", "traefik.cache.protocol=http", "traefik.cache.entryPoints=http,https", - "traefik.cache.frontend.rule=Host:env_inline()" + "traefik.cache.frontend.rule=Host:patsubst(patsubst(__PLEROMA_MEDIA_PROXY_URL, , <>), , <>)" >) ] } diff --git a/docker-config.exs b/docker-config.exs index 6970e52..5207e38 100644 --- a/docker-config.exs +++ b/docker-config.exs @@ -1,16 +1,37 @@ use Mix.Config defmodule Docker do - def env(name, verbatim \\ false) do - env_name = (if verbatim, do: "", else: "pleroma_") <> Atom.to_string(name) |> String.upcase - env_var = System.get_env(env_name) + def env(shortname, verbatim \\ false) do + # Get var + name = ((if verbatim, do: "", else: "pleroma_") <> Atom.to_string(shortname)) |> String.upcase() + raw_var = System.get_env(name) - if env_var == nil do - raise "Could not find #{env_name} in environment. Please define it and try again." + if raw_var == nil do + raise "Could not find #{name} in environment. Please define it and try again." end - System.put_env(env_name, "") - env_var + # Match type and cast if needed + if String.contains?(raw_var, ":") do + var_parts = String.split(raw_var, ":", parts: 2) + + type = Enum.at(var_parts, 0) + var = Enum.at(var_parts, 1) + + func = case type do + "int" -> fn(x) -> Integer.parse(x) |> elem(0) end + "bool" -> fn(x) -> x == "true" end + "string" -> fn(x) -> x end + _ -> if verbatim do + fn(x) -> x end + else + raise "Unknown type #{type} used in variable #{raw_var}." + end + end + + func.(var) + else + raw_var + end end end diff --git a/pleroma b/pleroma index 1b73d0c..26aa0c1 100755 --- a/pleroma +++ b/pleroma @@ -70,7 +70,6 @@ docker_compose() { docker-compose \ -f <(render_template) \ --project-directory . \ - --project-name "${PLEROMA_NAME:-pleroma}" \ "$@" } @@ -116,7 +115,7 @@ action__ps() { action__status; } # To persist the changes, the user still needs to rebuild the container. ### action__debug() { - debug_mounts="-v $(pwd)/custom.d:/custom.d -v $(pwd)/debug.d/build:/pleroma/_build -v $(pwd)/debug.d/deps:/pleroma/deps" + debug_mounts="-v $(pwd)/custom.d:/custom.d -v $(pwd)/debug.d/build:/home/pleroma/pleroma/_build -v $(pwd)/debug.d/deps:/home/pleroma/pleroma/deps" if [[ ! -d ./debug.d ]]; then mkdir -p ./debug.d/{build,deps} @@ -124,15 +123,15 @@ action__debug() { action__stop - docker_compose run --rm -u pleroma -w /home/pleroma/pleroma $debug_mounts server bash -c 'mix deps.get && mix compile' + docker_compose run --rm -u pleroma -w /home/pleroma/pleroma $debug_mounts server bash -c 'cp -rvf /custom.d/* /home/pleroma/pleroma && mix deps.get' x_flags="" if [[ $NO_X_FORWARDING != 1 ]]; then - x_flags="-e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -v $HOME/.Xauthority:/pleroma/.Xauthority" + x_flags="-e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix" fi [[ $NO_X_FORWARDING == 1 ]] || xhost +local:root - docker_compose run --rm -u pleroma -w /home/pleroma/pleroma $debug_mounts $x_flags server bash -c "cp -rf /custom.d/* /pleroma && $*" + docker_compose run --rm -u pleroma -w /home/pleroma/pleroma $debug_mounts $x_flags server bash -c "cp -rvf /custom.d/* /home/pleroma/pleroma && $*" [[ $NO_X_FORWARDING == 1 ]] || xhost -local:root }