Skip to content

Instantly share code, notes, and snippets.

@eth-p
Last active May 10, 2018 16:35
Show Gist options
  • Save eth-p/e96f5214a59f5d2694853da6739580b0 to your computer and use it in GitHub Desktop.
Save eth-p/e96f5214a59f5d2694853da6739580b0 to your computer and use it in GitHub Desktop.
mwoffliner (Mac OS X)
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# https://gist.github.com/eth-p/e96f5214a59f5d2694853da6739580b0
# Copyright (C) 2018 eth-p
#
# SUMMARY:
# This is a hackjob script to get mwoffliner (openzim/mwoffliner) working on
# Mac OS X without going through the pain of compiling zimwriterfs and all of
# its dependencies.
#
# REQUIREMENTS:
# - Docker (brew cask install docker)
# - Redis (brew install redis)
#
# USAGE:
# mwoffliner --mwUrl="https://.../" --adminEmail="[email protected]"
#
# NOTES:
# A Redis instance and a Docker container will be created automatically.
# Assuming all goes well, both will be destroyed automatically, too.
#
# CAVEATS:
# The mwoffliner --redis option cannot be used.
#
# -----------------------------------------------------------------------------
# Generate
CONTAINER="mwoffliner-$RANDOM"
REDIS_PORT="15${RANDOM}050"
REDIS_PORT="${REDIS_PORT:0:5}"
REDIS_PASS="$CONTAINER--$RANDOM"
# -----------------------------------------------------------------------------
# Get IP Address
IP=""
IF="en"
IFn=0
while true; do
# Test Interface
if ! ifconfig "${IF}${IFn}" &>/dev/null; then
break
fi
# Extract IP
IP="$(ipconfig getpacket en1 | grep '^yiaddr = [0-9\.]\+' | cut -d' ' -f3)"
if [ $? -eq 0 ] && [ -n "$IP" ]; then
break
fi
# Next Interface
((IFn++))
done
# -----------------------------------------------------------------------------
# Try Function
try() {
"$@"
if [ "$?" -ne 0 ]; then
printf "\033[31mAN ERROR HAS OCCURRED:\n%s\n\033[31m%s\n%s\n%s\n%s\n" \
"COMMAND: $*" \
"CONTAINER = $CONTAINER" \
"REDIS_PORT = $REDIS_PORT" \
"REDIS_PID = $REDIS_PID" \
"REDIS_PASS = $REDIS_PASS" \
>&2
fi
}
# -----------------------------------------------------------------------------
# Cleanup Function
cleanup() {
if kill -s 0 "$REDIS_PID" &>/dev/null; then
try kill "$REDIS_PID" >/dev/null
fi
try docker container rm "$CONTAINER" >/dev/null
}
trap 'cleanup' SIGINT SIGTERM
# -----------------------------------------------------------------------------
# Run Redis
redis-server - >/dev/null <<END &
save ""
appendfsync no
protected-mode no
port $REDIS_PORT
requirepass "$REDIS_PASS"
pidfile /dev/null
END
REDIS_PID=$!
# -----------------------------------------------------------------------------
# Run Container
docker run \
--name="$CONTAINER" \
--mount src="$(pwd)",target="/pwd",type=bind \
--workdir="/pwd" \
openzim/mwoffliner \
mwoffliner --redis "redis://:$REDIS_PASS@$IP:$REDIS_PORT" "$@"
result="$?"
cleanup
exit $result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment