2018-04-08 20:49:07 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2018-12-28 00:51:44 +00:00
|
|
|
#########################################################
|
|
|
|
# Options #
|
|
|
|
#########################################################
|
|
|
|
|
2018-04-08 20:49:07 +00:00
|
|
|
set -e
|
2018-08-20 21:06:34 +00:00
|
|
|
set -o pipefail
|
2018-04-08 20:49:07 +00:00
|
|
|
|
2018-12-28 00:51:44 +00:00
|
|
|
#########################################################
|
|
|
|
# Globals #
|
|
|
|
#########################################################
|
2018-04-08 20:49:07 +00:00
|
|
|
|
2018-08-18 21:20:44 +00:00
|
|
|
flags=""
|
|
|
|
|
2018-12-28 00:51:44 +00:00
|
|
|
#########################################################
|
|
|
|
# Helpers #
|
|
|
|
#########################################################
|
|
|
|
|
2018-12-30 13:31:32 +00:00
|
|
|
has_command() {
|
|
|
|
if command -v 1>/dev/null 2>&1 "$1"; then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
require_command() {
|
|
|
|
if ! has_command "$1"; then
|
|
|
|
printf "\nError: This action requires the command '%s' in your PATH.\n" "$1"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
2018-06-03 22:11:12 +00:00
|
|
|
render_template() {
|
|
|
|
m4 $flags docker-compose.m4 | awk 'NF'
|
2018-04-08 20:49:07 +00:00
|
|
|
}
|
|
|
|
|
2018-06-03 22:11:12 +00:00
|
|
|
docker_compose() {
|
2018-08-18 21:20:44 +00:00
|
|
|
docker-compose \
|
|
|
|
-f <(render_template) \
|
|
|
|
--project-directory . \
|
|
|
|
"$@"
|
2018-04-08 20:49:07 +00:00
|
|
|
}
|
|
|
|
|
2018-06-03 22:11:12 +00:00
|
|
|
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
|
2018-04-08 20:49:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 13:31:32 +00:00
|
|
|
download_file() { # $1: source, $2: target
|
|
|
|
if has_command curl; then
|
|
|
|
curl -sSL "$1" -o "$2"
|
|
|
|
elif has_command wget; then
|
|
|
|
wget "$1" -O "$2"
|
|
|
|
else
|
|
|
|
printf "\nError: This action requires either curl or wget in your PATH.\n"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
request_file_content() { # $1: source
|
|
|
|
if has_command curl; then
|
|
|
|
curl -sSL "$1"
|
|
|
|
elif has_command wget; then
|
|
|
|
wget "$1" -O- 2>/dev/null
|
|
|
|
else
|
|
|
|
printf "\nError: This action requires either curl or wget in your PATH.\n"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
2018-12-28 00:51:44 +00:00
|
|
|
#########################################################
|
|
|
|
# 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'
|
|
|
|
}
|
2018-08-20 21:06:34 +00:00
|
|
|
|
2018-12-28 00:51:44 +00:00
|
|
|
action__logs() {
|
|
|
|
docker_compose logs -f $*
|
|
|
|
}
|
2018-08-20 21:06:34 +00:00
|
|
|
|
2018-12-28 00:51:44 +00:00
|
|
|
action__mix() {
|
|
|
|
docker_compose exec server ash -c "cd /pleroma && mix $*"
|
|
|
|
}
|
2018-08-20 21:06:34 +00:00
|
|
|
|
2018-12-28 00:51:44 +00:00
|
|
|
action__passthrough() {
|
|
|
|
docker_compose $*
|
|
|
|
}
|
2018-08-20 21:06:34 +00:00
|
|
|
|
2018-12-28 00:51:44 +00:00
|
|
|
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
|
|
|
|
}
|
2018-08-20 21:06:34 +00:00
|
|
|
|
|
|
|
action__debug() {
|
2018-08-21 01:19:42 +00:00
|
|
|
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"
|
2018-08-20 21:06:34 +00:00
|
|
|
|
|
|
|
if [[ ! -d ./debug.d ]]; then
|
|
|
|
mkdir -p ./debug.d/{build,deps}
|
2018-08-18 21:20:44 +00:00
|
|
|
fi
|
2018-08-20 21:06:34 +00:00
|
|
|
|
2018-08-21 01:47:26 +00:00
|
|
|
if [[ ! -d ./custom.d/lib ]]; then
|
|
|
|
mkdir -p ./custom.d/lib
|
|
|
|
fi
|
|
|
|
|
2018-08-20 21:06:34 +00:00
|
|
|
action__stop
|
|
|
|
|
2018-08-21 01:19:42 +00:00
|
|
|
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'
|
2018-08-20 21:06:34 +00:00
|
|
|
|
|
|
|
x_flags=""
|
|
|
|
if [[ $NO_X_FORWARDING != 1 ]]; then
|
2018-08-21 01:19:42 +00:00
|
|
|
x_flags="-e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix"
|
2018-08-20 21:06:34 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
[[ $NO_X_FORWARDING == 1 ]] || xhost +local:root
|
2018-08-21 01:19:42 +00:00
|
|
|
docker_compose run --rm -u pleroma -w /home/pleroma/pleroma $debug_mounts $x_flags server bash -c "cp -rvf /custom.d/* /home/pleroma/pleroma && $*"
|
2018-08-20 21:06:34 +00:00
|
|
|
[[ $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
|
2018-08-18 21:20:44 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 00:51:44 +00:00
|
|
|
#########################################################
|
|
|
|
# Help #
|
|
|
|
#########################################################
|
|
|
|
|
|
|
|
print_help() {
|
|
|
|
echo "
|
|
|
|
Pleroma Maintenance Script
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
$0 [action]
|
|
|
|
|
|
|
|
Actions:
|
|
|
|
build Rebuild the pleroma container.
|
|
|
|
|
|
|
|
config [file = config.yml] Print the generated pleroma config to stdout.
|
|
|
|
|
|
|
|
dump Dump the generated docker-compose.yml to stdout.
|
|
|
|
|
|
|
|
debug [bin] [args...] Launches a new pleroma container but uses \$bin instead of phx.server as entrypoint.
|
|
|
|
**Warning**: This is intended for debugging pleroma with tools like :debugger and :observer.
|
|
|
|
It thus forwards your X-Server into docker and temporarily fiddles with your xhost
|
|
|
|
access controls. If this is a security concern for you, please export NO_X_FORWARDING=1
|
|
|
|
before launching a debugger session.
|
|
|
|
|
|
|
|
enter Spawn a shell inside the container for debugging/maintenance.
|
|
|
|
This command does not link to the postgres container.
|
|
|
|
If you need that use #debug instead.
|
|
|
|
|
|
|
|
logs Show the current container logs.
|
|
|
|
|
|
|
|
mix [task] [args...] Run a mix task without entering the container.
|
|
|
|
|
|
|
|
mod [file] Creates the file in custom.d and downloads the content from pleroma.social.
|
|
|
|
The download respects your \$PLEROMA_VERSION from .env.
|
|
|
|
|
|
|
|
passthrough / p [...] Pass any custom command to docker-compose.
|
|
|
|
|
|
|
|
restart Executes #stop and #start respectively.
|
|
|
|
|
|
|
|
start / up Start pleroma and sibling services.
|
|
|
|
|
|
|
|
stop / down Stop pleroma and sibling services.
|
|
|
|
|
|
|
|
status / ps Show the current container status.
|
|
|
|
|
|
|
|
Environment:
|
|
|
|
DEBUG can be used to modify the loglevel.
|
|
|
|
DEBUG=1 prints all commands before they are executed.
|
|
|
|
DEBUG=2 prints all bash statements before they are executed (a lot).
|
|
|
|
|
|
|
|
SHOPT can be used to modify shell options.
|
|
|
|
Pass a list of options to this variable like SHOPT='-x -e'.
|
|
|
|
-e is always on unless you set it to +e.
|
|
|
|
|
|
|
|
For setting long options with -o use a colon (:) instead of a space
|
|
|
|
to seperate the option from -o. For example: SHOPT='-x -e -o:pipefail'.
|
|
|
|
|
|
|
|
Contributing:
|
|
|
|
You can report bugs or contribute to this project at:
|
|
|
|
https://glitch.sh/sn0w/pleroma-docker
|
|
|
|
"
|
|
|
|
}
|
|
|
|
|
|
|
|
#########################################################
|
|
|
|
# Main #
|
|
|
|
#########################################################
|
|
|
|
|
2018-08-18 21:20:44 +00:00
|
|
|
# Check if there is any command at all
|
2018-04-08 20:49:07 +00:00
|
|
|
if [[ -z "$1" ]]; then
|
|
|
|
print_help
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2018-08-18 21:20:44 +00:00
|
|
|
# Check for SHOPTs
|
|
|
|
if [[ ! -z "$SHOPT" ]]; then
|
|
|
|
for opt in $SHOPT; do
|
|
|
|
if [[ $opt =~ ":" ]]; then
|
|
|
|
set -o ${opt//-o:/}
|
|
|
|
else
|
|
|
|
set $opt
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check for DEBUG
|
|
|
|
if [[ ! -z "$DEBUG" ]]; then
|
|
|
|
if [[ $DEBUG == 1 ]]; then
|
|
|
|
export DEBUG_COMMANDS=1
|
|
|
|
elif [[ $DEBUG == 2 ]]; then
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Parse .env
|
2018-06-03 22:11:12 +00:00
|
|
|
load_env
|
|
|
|
|
2018-12-28 00:51:44 +00:00
|
|
|
# Guess function name of current command and call it if present
|
2018-08-18 21:20:44 +00:00
|
|
|
func="action__${1}"
|
|
|
|
|
|
|
|
if type -t $func 2>&1 1>/dev/null; then
|
|
|
|
shift
|
|
|
|
[[ $DEBUG != 1 ]] || set -x
|
|
|
|
$func $@
|
|
|
|
{ [[ $DEBUG != 1 ]] || set +x; } 2>/dev/null
|
2018-06-03 22:11:12 +00:00
|
|
|
else
|
2018-04-08 20:49:07 +00:00
|
|
|
print_help
|
|
|
|
exit 1
|
2018-06-03 22:11:12 +00:00
|
|
|
fi
|