From 37a999148168c73ebecd367f3feb634bc25e42d2 Mon Sep 17 00:00:00 2001 From: sn0w Date: Fri, 28 Dec 2018 01:51:44 +0100 Subject: [PATCH] Organize ./pleroma script --- pleroma | 233 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 146 insertions(+), 87 deletions(-) diff --git a/pleroma b/pleroma index 62e13c6..e5781a2 100755 --- a/pleroma +++ b/pleroma @@ -1,8 +1,150 @@ #!/bin/bash +######################################################### +# Options # +######################################################### + set -e set -o pipefail +######################################################### +# Globals # +######################################################### + +flags="" + +######################################################### +# Helpers # +######################################################### + +render_template() { + m4 $flags docker-compose.m4 | awk 'NF' +} + +docker_compose() { + docker-compose \ + -f <(render_template) \ + --project-directory . \ + "$@" +} + +load_env() { + if [[ ! -f .env ]]; then + echo "Please create a .env file first" + echo "(Copy .env.dist to .env for a template)" + exit 1 + fi + + while read -r line; do + if [[ "$line" == \#* ]] || [[ -z "$line" ]]; then + continue; + fi + + export "${line?}" + flags="-D__${line?} $flags" + done < .env +} + +######################################################### +# Subcommands # +######################################################### + +action__build() { + docker_compose build --build-arg __BUST_CACHE="$(date +%s)" server +} + +action__config() { + docker run --rm -t -i -v $(pwd):/mnt ruby:alpine sh -c "cd /mnt && ruby config_parser/parser.rb ${1:-config.yml}" +} + +action__dump() { + cat <(render_template) +} + +action__enter() { + docker_compose exec server ash -c 'cd /pleroma && ash' +} + +action__logs() { + docker_compose logs -f $* +} + +action__mix() { + docker_compose exec server ash -c "cd /pleroma && mix $*" +} + +action__passthrough() { + docker_compose $* +} + +action__p() { + action__passthrough $* +} + +action__restart() { + action__stop + action__start +} + +action__start() { + docker_compose up --remove-orphans -d +} + +action__up() { + action__start +} + +action__stop() { + docker_compose down +} + +action__down() { + action__stop +} + +action__status() { + docker_compose ps +} + +action__ps() { + action__status +} + +action__debug() { + 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} + fi + + if [[ ! -d ./custom.d/lib ]]; then + mkdir -p ./custom.d/lib + fi + + action__stop + + 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" + 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 -rvf /custom.d/* /home/pleroma/pleroma && $*" + [[ $NO_X_FORWARDING == 1 ]] || xhost -local:root +} + +action__mod() { + echo "Preparing 'custom.d/$1' for modding..." + install -D <(echo '') ./custom.d/$1 + wget -O ./custom.d/$1 https://git.pleroma.social/pleroma/pleroma/raw/$PLEROMA_VERSION/$1 +} + +######################################################### +# Help # +######################################################### + print_help() { echo " Pleroma Maintenance Script @@ -62,91 +204,9 @@ Contributing: " } -flags="" - -render_template() { - m4 $flags docker-compose.m4 | awk 'NF' -} - -docker_compose() { - docker-compose \ - -f <(render_template) \ - --project-directory . \ - "$@" -} - -load_env() { - if [[ ! -f .env ]]; then - echo "Please create a .env file first" - echo "(Copy .env.dist to .env for a template)" - exit 1 - fi - - while read -r line; do - if [[ "$line" == \#* ]] || [[ -z "$line" ]]; then - continue; - fi - - export "${line?}" - flags="-D__${line?} $flags" - done < .env -} - -action__build() { docker_compose build --build-arg __BUST_CACHE="$(date +%s)" server; } -action__config() { docker run --rm -t -i -v $(pwd):/mnt ruby:alpine sh -c "cd /mnt && ruby config_parser/parser.rb ${1:-config.yml}"; } -action__dump() { cat <(render_template); } -action__enter() { docker_compose exec server ash -c 'cd /pleroma && ash'; } -action__logs() { docker_compose logs -f; } -action__mix() { docker_compose exec server ash -c "cd /pleroma && mix $*"; } -action__passthrough() { docker_compose $*; } -action__p() { action__passthrough $*; } - -action__restart() { action__stop; action__start; } - -action__start() { docker_compose up --remove-orphans -d; } -action__up() { action__start; } - -action__stop() { docker_compose down; } -action__down() { action__stop; } - -action__status() { docker_compose ps; } -action__ps() { action__status; } - -### -# This function rips out the mix caches from the container -# in order to speed up rebuilds during debugging/modding sessions. -# 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:/home/pleroma/pleroma/_build -v $(pwd)/debug.d/deps:/home/pleroma/pleroma/deps" - - if [[ ! -d ./debug.d ]]; then - mkdir -p ./debug.d/{build,deps} - fi - - if [[ ! -d ./custom.d/lib ]]; then - mkdir -p ./custom.d/lib - fi - - action__stop - - 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" - 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 -rvf /custom.d/* /home/pleroma/pleroma && $*" - [[ $NO_X_FORWARDING == 1 ]] || xhost -local:root -} - -action__mod() { - echo "Preparing 'custom.d/$1' for modding..." - install -D <(echo '') ./custom.d/$1 - wget -O ./custom.d/$1 https://git.pleroma.social/pleroma/pleroma/raw/$PLEROMA_VERSION/$1 -} +######################################################### +# Main # +######################################################### # Check if there is any command at all if [[ -z "$1" ]]; then @@ -177,8 +237,7 @@ fi # Parse .env load_env -# Guess function name of current command -# and then check for it's existance. +# Guess function name of current command and call it if present func="action__${1}" if type -t $func 2>&1 1>/dev/null; then