[docker] fix run.sh for OSX with access to X

- OSX readlink doesn't have the -f or -m options, require correct PAPARAZZI_SRC to be set already for now
- using xauth with docker on OSX doesn't work, so we use socat to forward the socket via TCP
  - see https://github.com/docker/docker/issues/8710
- no (pulse)audio on OSX...
- find on OSX doesn't have the -printf option... so use exec echo instead

this will hopefully fix #1421
This commit is contained in:
Felix Ruess
2015-11-17 00:09:46 +01:00
parent 545883421c
commit d9eb0b41f8
+74 -18
View File
@@ -10,27 +10,67 @@ else
args="$@" args="$@"
fi fi
# check if running on Linux or OSX
UNAME=$(uname -s)
############################################################
# share this paparazzi directory with the container
############################################################
# set PAPARAZZI_SRC to this tree # set PAPARAZZI_SRC to this tree
SCRIPT=$(readlink -f $0) # on OSX: readlink doesn't have the -f or -m options, try using pwd
SCRIPT_DIR=$(dirname $(readlink -f $0)) if [ $UNAME == "Linux" ]; then
PAPARAZZI_SRC=$(readlink -m $SCRIPT_DIR/..) SCRIPT=$(readlink -f $0)
SCRIPT_DIR=$(dirname $(readlink -f $0))
PAPARAZZI_SRC=$(readlink -m $SCRIPT_DIR/..)
else
PAPARAZZI_SRC=$(dirname $(pwd))
fi
# PAPARAZZI_HOME inside the container # PAPARAZZI_HOME inside the container
PPRZ_HOME_CONTAINER=/home/pprz/paparazzi PPRZ_HOME_CONTAINER=/home/pprz/paparazzi
# share the paparazzi directory and set it as working directory
SHARE_PAPARAZZI_HOME_OPTS="--volume=$PAPARAZZI_SRC:$PPRZ_HOME_CONTAINER \
--env=PAPARAZZI_HOME=$PPRZ_HOME_CONTAINER \
--env=PAPARAZZI_SRC=$PPRZ_HOME_CONTAINER \
-w $PPRZ_HOME_CONTAINER"
USER_UID=$(id -u) ############################################################
XSOCK=/tmp/.X11-unix # grant access to X-Server
XAUTH=/tmp/.docker.xauth ############################################################
touch $XAUTH if [ $UNAME == "Linux" ]; then
xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge - XSOCK=/tmp/.X11-unix
XAUTH=/tmp/.docker.xauth
touch $XAUTH
xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge -
# options to grant access to the Xserver # options to grant access to the Xserver
X_WINDOW_OPTS="--volume=$XSOCK:$XSOCK --volume=$XAUTH:$XAUTH --env=XAUTHORITY=${XAUTH} --env=DISPLAY=${DISPLAY}" X_WINDOW_OPTS="--volume=$XSOCK:$XSOCK --volume=$XAUTH:$XAUTH --env=XAUTHORITY=${XAUTH} --env=DISPLAY=${DISPLAY}"
fi
# pass audio to pulseaudio server on host # using xauth with docker on OSX doesn't work, so we use socat:
PULSE_AUDIO_OPTS="--volume=/run/user/${USER_UID}/pulse:/run/pulse" # see https://github.com/docker/docker/issues/8710
if [ $UNAME == "Darwin" ]; then
X_WINDOW_OPTS="--env=DISPLAY=192.168.99.1:0"
TCPPROXY="socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\""
fi
############################################################
# Audio
############################################################
if [ $UNAME == "Linux" ]; then
# pass audio to pulseaudio server on host
USER_UID=$(id -u)
PULSE_AUDIO_OPTS="--volume=/run/user/${USER_UID}/pulse:/run/pulse"
fi
############################################################
# USB
############################################################
# give the container access to USB, WARNING runs it as priviliged container! # give the container access to USB, WARNING runs it as priviliged container!
# use it if ENABLE_USB variable is non-empty/zero # use it if ENABLE_USB variable is non-empty/zero
if [ -n "$PRIVILEGED_USB" ]; then if [ -n "$PRIVILEGED_USB" ]; then
@@ -42,18 +82,24 @@ fi
# try to detect which USB devices to pass to the container automatically # try to detect which USB devices to pass to the container automatically
# set DISABLE_USB=1 to turn it off # set DISABLE_USB=1 to turn it off
if [ -z "$DISABLE_USB" ]; then if [ -z "$DISABLE_USB" ]; then
USB_OPTS=$(find /dev -maxdepth 1 \( -name "ttyACM?" -or -name "ttyUSB?" \) -printf "--device=%p ") # find on OSX doesn't have the -printf option... so use exec echo instead
USB_OPTS=$(find /dev -maxdepth 1 \( -name "ttyACM?" -or -name "ttyUSB?" \) -exec echo -n "--device={} " \;)
if [ -n "$USB_OPTS" ]; then if [ -n "$USB_OPTS" ]; then
echo Passing auto-detected USB devices: $USB_OPTS echo Passing auto-detected USB devices: $USB_OPTS
fi fi
fi fi
# share the paparazzi directory and set it as working directory
SHARE_PAPARAZZI_HOME_OPTS="--volume=$PAPARAZZI_SRC:$PPRZ_HOME_CONTAINER \
--env=PAPARAZZI_HOME=$PPRZ_HOME_CONTAINER \
--env=PAPARAZZI_SRC=$PPRZ_HOME_CONTAINER \
-w $PPRZ_HOME_CONTAINER"
############################################################
# Run it!
############################################################
if [ $UNAME == "Darwin" ]; then
# start socat in background to forward the X socket via TCP
$TCPPROXY &
fi
# run the docker container with all the fancy options
docker run \ docker run \
${X_WINDOW_OPTS} \ ${X_WINDOW_OPTS} \
${PULSE_AUDIO_OPTS} \ ${PULSE_AUDIO_OPTS} \
@@ -61,5 +107,15 @@ docker run \
${SHARE_PAPARAZZI_HOME_OPTS} \ ${SHARE_PAPARAZZI_HOME_OPTS} \
--rm $args --rm $args
############################################################
# cleanup after exiting from docker container
############################################################
# cleanup XAUTHORITY file again # cleanup XAUTHORITY file again
rm -f $XAUTH rm -f $XAUTH
# on OSX kill background socat process again
if [ $UNAME == "Darwin" ]; then
pkill -f "$TCPPROXY"
fi