From 99f9c491514303dbe3bd42f37ffbf9ace6006156 Mon Sep 17 00:00:00 2001 From: sn0w Date: Sun, 8 Apr 2018 22:49:07 +0200 Subject: [PATCH] Update structure --- .dockerignore | 2 +- .env.dist | 28 +++++++++++- .gitignore | 71 +++++++++++++++++++++++++++++ .gitmodules | 6 --- README.md | 64 ++++++++++++++++++++++++++ custom.d/.gitkeep | 0 docker-compose.m4 | 16 +++---- entrypoint.ash | 72 +++++++++++------------------- initdb.sql | 1 + pleroma | 109 ++++++++++++++++++++++++++++++++++++++++++++- pleroma.dockerfile | 19 ++++++-- pleroma.sh | 109 --------------------------------------------- 12 files changed, 319 insertions(+), 178 deletions(-) delete mode 100644 .gitmodules create mode 100644 README.md create mode 100644 custom.d/.gitkeep create mode 100644 initdb.sql mode change 160000 => 100755 pleroma delete mode 100755 pleroma.sh diff --git a/.dockerignore b/.dockerignore index 1269488..8fce603 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1 @@ -data +data/ diff --git a/.env.dist b/.env.dist index f74f5f2..550107c 100644 --- a/.env.dist +++ b/.env.dist @@ -1,26 +1,50 @@ +### Docker settings ### + # The docker network to bind to. # (Useful for reverse-proxies outside of this compose). +# (Defaults to "pleroma_docker_1") DOCKER_NETWORK= # The directory where all containers store their data. # Can be a relative path, "~/...", or absolute. DOCKER_DATADIR= -# Database settings +### Database settings ### POSTGRES_DB= POSTGRES_USER= POSTGRES_PASSWORD= -# Pleroma Settings +### Pleroma Settings ### + +# The env to use +MIX_ENV= + +# The git tag or branch to check out on build +PLEROMA_VERSION= + +# The domain/scheme/port where pleroma will be hosted PLEROMA_URL= PLEROMA_SCHEME= PLEROMA_PORT= + +# The seed for your secret keys +# (Enter something as random as possible) PLEROMA_SECRET_KEY_BASE= + +# The name of your instance PLEROMA_NAME= + +# Your contact info PLEROMA_ADMIN_EMAIL= + +# User settings PLEROMA_USER_LIMIT= PLEROMA_REGISTRATIONS_OPEN= + +# Media proxy PLEROMA_MEDIA_PROXY_ENABLED= PLEROMA_MEDIA_PROXY_REDIRECT_ON_FAILURE= PLEROMA_MEDIA_PROXY_URL= + +# DB PLEROMA_DB_POOL_SIZE= diff --git a/.gitignore b/.gitignore index 6c5156b..800446a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,73 @@ data .env +custom.d/ +!custom.d/.gitkeep +docker-compose.yml + +# Created by https://www.gitignore.io/api/osx,linux,windows + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### OSX ### +*.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk + + +# End of https://www.gitignore.io/api/osx,linux,windows diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 239bf03..0000000 --- a/.gitmodules +++ /dev/null @@ -1,6 +0,0 @@ -[submodule "pleroma"] - path = pleroma - url = git@git.pleroma.social:pleroma/pleroma.git -[submodule "camo"] - path = camo - url = https://github.com/atmos/camo.git diff --git a/README.md b/README.md new file mode 100644 index 0000000..7c6e0c4 --- /dev/null +++ b/README.md @@ -0,0 +1,64 @@ +# Pleroma-Docker + +[Pleroma](https://pleroma.social/) is a selfhosted social network that uses OStatus/ActivityPub. + +This repository is my attempt to dockerize it for easier deployment. + +## Features + +- 100% generic +- Everything is customizable +- Everything is configurable through `.env` files +- Zero special host dependencies +- "It just works" + +## Assumptions + +This repo assumes that you're using Træfik as your auto-configuring reverse proxy. + +## Tutorial + +- Make sure that `m4` and `docker-compose` are installed +- Check out this repo +- Create your env file (`cp .env.dist .env`) +- Edit the env values +- Run `./pleroma build` +- Run `./pleroma run` +- ... +- Profit! + +## Building an image + +Since this setup injects code into pleroma that moves it's configuration into the environment (ref ["The Twelve-Factor App"](https://12factor.net/)), the image is 100% reusable and can be shared/replicated across multiple hosts. To do that just run `./pleroma build` as usual and then tag your image to whatever you want. Just make sure to start the container with `env_file:` or all required `-e` pairs. + +## Customizing Pleroma + +Just add your customizations (and their folder structure) to `custom.d`.
+They will be copied into the right place when the container starts.
+You can even replace/patch pleroma's code with this, because the project is recompiled at startup. + +In general: Prepending `custom.d` to pleroma's customization guides should work all the time.
+Check them out in the [official pleroma wiki](https://git.pleroma.social/pleroma/pleroma/wikis/home). + +Here are a few customization examples: + +- I want to have a custom thumbnail + - Save it in `custom.d/priv/static/instance/thumbnail.jpeg` + +- I want to change the `config.json`. + - Just use [the template](https://git.pleroma.social/pleroma/pleroma/blob/develop/priv/static/static/config.json) and save it in `custom.d/priv/static/static/config.json` + +- I want to change the background + - Throw an image into `custom.d/priv/static/static` and then edit the config from above + +- I want a custom logo + - See above + +- I need blobs. Give me emojis. + - Save them in `custom.d/priv/static/emoji`. Then create and/or edit `custom.d/config/custom_emoji.txt`. + +- I want custom ToS + - Throw a HTML document to `priv/static/static/terms-of-service.html` + +You get the gist.
+Pretty basic stuff. diff --git a/custom.d/.gitkeep b/custom.d/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/docker-compose.m4 b/docker-compose.m4 index 308731c..c9ae90a 100644 --- a/docker-compose.m4 +++ b/docker-compose.m4 @@ -3,6 +3,7 @@ define(`upcase', `translit($1, `a-z', `A-Z')') define(`env', `upcase($1): ${upcase($1):?upcase($1)}') define(`env_fb', `upcase($1): ${upcase($1):-$2}') define(`env_inline', `${upcase($1):?upcase($1)}') +define(`env_inline_fb', `${upcase($1):-$2}') divert(1)dnl version: "3" @@ -10,7 +11,7 @@ version: "3" networks: default: external: - name: env_inline(`docker_network') + name: env_inline_fb(`docker_network', `pleroma_docker_1') services: db: @@ -22,11 +23,15 @@ services: env(`postgres_password') volumes: - env_inline(`docker_datadir')/db:/var/lib/postgresql/data + - ./initdb.sql:/docker-entrypoint-initdb.d/pleroma.sql server: build: context: . dockerfile: ./pleroma.dockerfile + args: + env(`pleroma_version') + env_fb(`mix_env', `prod') restart: unless-stopped links: - db @@ -49,16 +54,9 @@ services: env(`pleroma_media_proxy_url') env(`pleroma_db_pool_size') - env_fb(`pleroma_workspace', `/pleroma') - env_fb(`mix_archives', `/mix/archives') - env_fb(`mix_home', `/mix/home') env_fb(`mix_env', `prod') volumes: - - ./pleroma:/pleroma - - env_inline(`docker_datadir')/pleroma:/data - - env_inline(`docker_datadir')/mix:/mix - - env_inline(`docker_datadir')/misc/cache:/root/.cache - - env_inline(`docker_datadir')/misc/meta:/meta + - ./custom.d:/custom.d labels: traefik.enable: "true" traefik.fe.port: "4000" diff --git a/entrypoint.ash b/entrypoint.ash index c571b5c..17d6590 100755 --- a/entrypoint.ash +++ b/entrypoint.ash @@ -2,54 +2,32 @@ set -e -function action__build { - mix local.hex --force - mix local.rebar --force - mix deps.get - mix compile -} - -function action__configure { - mix generate_config -} - -function action__run { - if [[ ! -f /meta/ECTO_REPO_CREATED ]]; then - mix ecto.create - touch /meta/ECTO_REPO_CREATED - fi - - mix ecto.migrate - exec mix phx.server -} - -if [[ -z "$1" ]]; then - echo "No action provided." - exit 1 +# Ensure that the environment is clean +if [[ -d /pleroma-runtime ]]; then + rm -rf /pleroma-runtime fi +mkdir /pleroma-runtime -if [[ -z "$PLEROMA_WORKSPACE" ]]; then - echo "Please set the PLEROMA_WORKSPACE variable to your pleroma root." - exit 1 -fi +# Copy sources +rsync -azI /pleroma/ /pleroma-runtime/ -cd $PLEROMA_WORKSPACE -if [[ ! -L config/prod.secret.exs ]]; then - rm -f config/prod.secret.exs - ln -s /docker-config.exs config/prod.secret.exs -fi -if [[ ! -L config/dev.secret.exs ]]; then - rm -f config/dev.secret.exs - ln -s /docker-config.exs config/dev.secret.exs -fi +# Copy overrides +rsync -azI /custom.d/ /pleroma-runtime/ -case "$1" in -build) action__build;; -configure) action__configure;; -run) action__run;; -*) - echo "The action '$1' is invalid." - exit 1 -;; -esac -shift +# Go to runtime workspace +cd /pleroma-runtime + +# Make sure that the tooling is present +mix local.hex --force +mix local.rebar --force + +# Recompile +mix deps.get +mix clean && mix compile + +# Prepare DB +mix ecto.create +mix ecto.migrate + +# Liftoff o/ +exec mix phx.server diff --git a/initdb.sql b/initdb.sql new file mode 100644 index 0000000..1d8aef0 --- /dev/null +++ b/initdb.sql @@ -0,0 +1 @@ +CREATE EXTENSION IF NOT EXISTS citext; diff --git a/pleroma b/pleroma deleted file mode 160000 index 8f9bcc4..0000000 --- a/pleroma +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 8f9bcc4ab0b16efa9168f49f6e2cc14bc424d398 diff --git a/pleroma b/pleroma new file mode 100755 index 0000000..cbc46c6 --- /dev/null +++ b/pleroma @@ -0,0 +1,108 @@ +#!/bin/bash + +set -e + +function log_generic { # $1: color, $2: prefix, $3: message # + echo -e "[$(tput setaf $1)$(tput bold)$2$(tput sgr0)] $3" +} + +function log_error { # $1: message # + log_generic 1 ERR "$1" +} + +function log_ok { # $1: message # + log_generic 2 "OK " "$1" +} + +function log_info { # $1: message # + log_generic 4 INF "$1" +} + +function print_help { + echo " +Pleroma Maintenance Script + +Usage: + $0 [action] [flags] + +Actions: + start Start pleroma and sibling services + stop Stop pleroma and sibling services + restart Executes #stop and #start respectively. + logs Show the current container logs + enter Enter the pleroma container for debugging/maintenance +" +} + +function run_dockerized { + log_info "Stopping existing containers (if any)" + docker-compose down + + log_info "Rebuilding images" + docker-compose build + + log_info "Running action '$1'" + docker-compose run server $1 + + log_info "Cleaning up.." + docker-compose down +} + +function action__start { + log_info "Booting pleroma" + docker-compose up --remove-orphans -d + log_ok "Done" +} + +function action__stop { + log_info "Stopping pleroma" + docker-compose down + log_ok "Done" +} + +function action__logs { + docker-compose logs -f +} + +function action__build { + docker-compose build +} + +function action__enter { + docker-compose exec server ash +} + +function prepare { + log_info "Preparing script" + m4 docker-compose.m4 > docker-compose.yml +} + +function cleanup { + log_info "Cleaning up" + rm docker-compose.yml +} + +trap "cleanup" INT TERM EXIT + +if [[ -z "$1" ]]; then + log_error "No action provided." + print_help + exit 1 +fi + +prepare + +case "$1" in +build) action__build;; +start) action__start;; +stop) action__stop;; +restart) action__start; action__stop; ;; +logs) action__logs;; +enter) action__enter;; +*) + log_error "The action '$1' is invalid." + print_help + exit 1 +;; +esac +shift diff --git a/pleroma.dockerfile b/pleroma.dockerfile index 4221877..f738eef 100644 --- a/pleroma.dockerfile +++ b/pleroma.dockerfile @@ -1,12 +1,25 @@ FROM elixir:1.6-alpine -RUN apk add --no-cache --virtual .build alpine-sdk git +WORKDIR /pleroma +# Prepare system +RUN apk add --no-cache --virtual .build alpine-sdk git rsync + +# Perform a clone that can be cached +RUN git clone https://git.pleroma.social/pleroma/pleroma.git . + +# Prepare pleroma ADD ./docker-config.exs /docker-config.exs +ARG PLEROMA_VERSION +RUN \ + git checkout $PLEROMA_VERSION && \ + git pull --rebase --autostash && \ + ln -s /docker-config.exs config/prod.secret.exs && \ + ln -s /docker-config.exs config/dev.secret.exs +# Register entrypoint ADD ./entrypoint.ash / RUN chmod +x /entrypoint.ash -ENTRYPOINT ["/entrypoint.ash"] -CMD ["run"] +CMD ["/entrypoint.ash"] EXPOSE 4000 diff --git a/pleroma.sh b/pleroma.sh deleted file mode 100755 index 52c314d..0000000 --- a/pleroma.sh +++ /dev/null @@ -1,109 +0,0 @@ -#!/bin/bash - -set -e - -function log_generic { # $1: color, $2: prefix, $3: message # - echo -e "[$(tput setaf $1)$(tput bold)$2$(tput sgr0)] $3" -} - -function log_error { # $1: message # - log_generic 1 ERR "$1" -} - -function log_ok { # $1: message # - log_generic 2 "OK " "$1" -} - -function log_info { # $1: message # - log_generic 4 INF "$1" -} - -function print_help { - echo " -Pleroma Maintenance Script - -Usage: - $0 [action] [flags] - -Actions: - build Build the pleroma container and all dependencies - configure Runs the interactive configuration script - run Start pleroma and sibling services - stop Stop pleroma and sibling services - logs Show the current container logs -" -} - -function run_dockerized { - log_info "Stopping existing containers (if any)" - docker-compose down - - log_info "Rebuilding images" - docker-compose build - - log_info "Running action '$1'" - docker-compose run server $1 - - log_info "Cleaning up.." - docker-compose down -} - -function action__build { - run_dockerized "build" - log_ok "Done" -} - -function action__configure { - run_dockerized "configure" - log_ok "Done" -} - -function action__run { - log_info "Booting pleroma" - docker-compose up --remove-orphans -d - log_ok "Done" -} - -function action__stop { - log_info "Stopping pleroma" - docker-compose down - log_ok "Done" -} - -function action__logs { - docker-compose logs -f -} - -function prepare { - log_info "Preparing script" - m4 docker-compose.m4 > docker-compose.yml -} - -function cleanup { - log_info "Cleaning up" - rm docker-compose.yml -} - -trap "cleanup" INT TERM EXIT - -if [[ -z "$1" ]]; then - log_error "No action provided." - print_help - exit 1 -fi - -prepare - -case "$1" in -build) action__build;; -configure) action__configure;; -run) action__run;; -stop) action__stop;; -logs) action__logs;; -*) - log_error "The action '$1' is invalid." - print_help - exit 1 -;; -esac -shift