Organize ./pleroma script

This commit is contained in:
sn0w 2018-12-28 01:51:44 +01:00
parent 3adfa6642f
commit 37a9991481
1 changed files with 146 additions and 87 deletions

233
pleroma
View File

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