Skip to content

Instantly share code, notes, and snippets.

@amatsus
Last active March 13, 2018 07:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amatsus/4bdcb1498ea5a002ba41edebb122c21c to your computer and use it in GitHub Desktop.
Save amatsus/4bdcb1498ea5a002ba41edebb122c21c to your computer and use it in GitHub Desktop.
This is a shell function to handling `docker run/stop/pause/unpause` in OGS/GE
#
# docker run wrapper for OGS/GE
#
# Copyright (c) 2016 Akihiro Matsushima
# Released under the MIT license
# http://opensource.org/licenses/mit-license.php
#
function sigconthandler() {
docker unpause $cid
echo "caught sigcont, container unpaused."
wait
}
function sigusr1handler() {
docker pause $cid
echo "caught sigusr1, container paused."
wait
}
function sigusr2handler() {
if [ `docker inspect --format="{{ .State.Status }}" $cid` == "paused" ]; then
docker unpause $cid
fi
docker stop $cid
echo "caught sigusr2, container stopped."
}
function docker() {
# emulate fairly POSIX sh in zsh
$(type "emulate" >/dev/null 2>&1) && emulate -L sh
local IFS=$' \t\n'
if [ "$1" = "run" ]; then
local DOCKER_RUN_LOCALOPTS=${DOCKER_RUN_OPTS:-'-u `id -u`:`id -g` -v $PWD:$PWD -w $PWD'}
if [ -n "$JOB_ID" ]; then
# define the unique cidfile name
TEMPDIR=/var/tmp/${LOGNAME:-$USER}
CIDFILE="${TEMPDIR}/${JOB_NAME:-SOMEJOB}.o${JOB_ID}.${SGE_TASK_ID:-SOMETASK}_$(date +%Y%m%d%H%M%S%3N).cid"
if [ ! -e "$TEMPDIR" ]; then
mkdir -p "$TEMPDIR"
fi
/usr/bin/docker run $(eval echo $DOCKER_RUN_LOCALOPTS) -i --cidfile="$CIDFILE" "${@:2:($#-1)}" &
pid=$!
while [[ -d /proc/$pid && -z $cid ]]; do
sleep 1
if [ -s "$CIDFILE" ]; then
read -r cid < "$CIDFILE"
rm -f "$CIDFILE"
fi
done
trap sigconthandler SIGCONT
trap sigusr1handler SIGUSR1
trap sigusr2handler SIGUSR2
wait
else
/usr/bin/docker run $(eval echo $DOCKER_RUN_LOCALOPTS) "${@:2:($#-1)}"
fi
else
/usr/bin/docker "$@"
fi
}
if [[ ! $(readlink /proc/$$/exe) =~ "zsh" ]]; then
export -f sigconthandler sigusr1handler sigusr2handler docker
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment