datatracker/docker/run
2021-10-13 11:33:37 +00:00

118 lines
3.6 KiB
Bash
Executable file

#!/bin/bash
version=0.20
program=${0##*/}
progdir=${0%/*}
if [ "$progdir" = "$program" ]; then progdir="."; fi
if [ "$progdir" = "." ]; then progdir="$PWD"; fi
parent=$(dirname "$progdir")
if [ "$parent" = "." ]; then parent="$PWD"; fi
if [[ $(uname) =~ CYGWIN.* ]]; then parent=$(echo "$parent" | sed -e 's/^\/cygdrive\/\(.\)/\1:/'); fi
# ----------------------------------------------------------------------
function usage() {
cat <<EOF
NAME
$program - Run a docker datatracker container with suitable settings
SYNOPSIS
$program [OPTIONS] ARGS
DESCRIPTION
This is a wrapper which runs docker with suitable arguments on a
debian-based docker image which has been set up with the dependencies
needed to easily run the IETF datatracker in development mode. By
default, it expects to find the MySQL database files at
$parent/data/mysql, which is mapped inside the
container to /var/lib/mysql, and it will set up a home directory for
the current user ($USER) and map it to $HOME.
EOF
echo -e "OPTIONS"
if [ "$(uname)" = "Linux" ] || [ "$(uname)" = "Darwin" ]; then
grep -E "^[ ]+[-][A-Za-z| -]+\*?\)[ ]+[A-Za-z].+#" "$0" | tr -s "\t|" "\t," | sed -r -e 's/\)[ \t]+([A-Z]+)=\$2[^#]*#/=\1\t/' -e 's/\)[^#]*#/\t/'
else
grep -E "^[ ]+[-][A-Za-z| -]+\*?\)[ ]+[A-Za-z].+#" "$0" | sed 's/\|.*\$2[^#]*#/ /'| sed -E 's/\|.*\)[^#]*#/ /'
fi
cat <<EOF
FILES
AUTHOR
Written by Henrik Levkowetz, <henrik@levkowetz.com>
COPYRIGHT
Copyright (c) 2015 IETF Trust and the persons identified as authors of
the code. All rights reserved. License 'Simplified BSD', as specified
in http://opensource.org/licenses/BSD-3-Clause.
EOF
}
# ----------------------------------------------------------------------
function die() {
echo -e "\n$program: error: $*" >&2
exit 1
}
# ----------------------------------------------------------------------
function version() {
echo -e "$program $version"
}
# ----------------------------------------------------------------------
trap 'echo "$program($LINENO): Command failed with error code $? ([$$] $0 $*)"; exit 1' ERR
# ----------------------------------------------------------------------
# Option parsing
# Options
shortopts=hp:vVcC
longopts=help,port=,version,cached,no-cached
# Default values
MYSQLDIR=$parent/data/mysql
PORT=8000
REPO="ietf/datatracker-environment"
CACHED=':cached'
if [ "$(uname)" = "Linux" ]; then
args=$(getopt -o "$shortopts" --long "$longopts" -n '$program' -- $SV "$@")
if [ $? != 0 ] ; then die "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$args"
else
# Darwin, BSDs
args=$(getopt -o$shortopts $SV $*)
if [ $? != 0 ] ; then die "Terminating..." >&2 ; exit 1 ; fi
set -- $args
fi
while true ; do
case "$1" in
-c| --cached) CACHED=':cached';; # Use cached disk access to reduce system load
-C| --no-cached) CACHED=':consistent';; # Use fully synchronized disk access
-h| --help) usage; exit;; # Show this help, then exit
-p| --port) PORT=$2; shift;; # Bind the container's port 8000 to external port PORT
-V| --version) version; exit;; # Show program version, then exit
--) shift; break;;
*) die "Internal error, inconsistent option specification: '$1'";;
esac
shift
done
if [ -z "$TAG" ]; then
TAG=$(basename "$(svn info "$parent" | grep ^URL | awk '{print $2}')")
fi
# ----------------------------------------------------------------------
echo "Starting a docker container for '$REPO:$TAG'."
mkdir -p "$MYSQLDIR"
docker run -ti -p "$PORT":8000 -p 33306:3306 \
-v "$parent:/root/src$CACHED" \
-v "$MYSQLDIR:/var/lib/mysql:delegated" \
"$REPO:$TAG" "$@"