Implement typed .env and more features

This commit is contained in:
sn0w 2018-08-21 03:19:42 +02:00
parent 30ef913c40
commit 4dfee6cff3
5 changed files with 80 additions and 43 deletions

View File

@ -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

View File

@ -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

View File

@ -129,11 +129,11 @@ define(<env_inline_fb>, <${upcase($1):-$2}>)
"traefik.fe.port=4000",
"traefik.fe.protocol=http",
"traefik.fe.entryPoints=http,https",
"traefik.fe.frontend.rule=Host:env_inline(<pleroma_url>)",
"traefik.fe.frontend.rule=Host:patsubst(__PLEROMA_URL, <string:>, <>)",
"traefik.cache.port=4000",
"traefik.cache.protocol=http",
"traefik.cache.entryPoints=http,https",
"traefik.cache.frontend.rule=Host:env_inline(<pleroma_media_proxy_url>)"
"traefik.cache.frontend.rule=Host:patsubst(patsubst(__PLEROMA_MEDIA_PROXY_URL, <http.*?//>, <>), <string:>, <>)"
>)
]
}

View File

@ -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

View File

@ -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
}