run_opengl.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/bin/bash
  2. container=opengl
  3. image=thewtex/opengl
  4. port=6080
  5. extra_run_args=""
  6. quiet=""
  7. show_help() {
  8. cat << EOF
  9. Usage: ${0##*/} [-h] [-q] [-c CONTAINER] [-i IMAGE] [-p PORT] [-r DOCKER_RUN_FLAGS]
  10. This script is a convenience script to run Docker images based on
  11. thewtex/opengl. It:
  12. - Makes sure docker is available
  13. - On Windows and Mac OSX, creates a docker machine if required
  14. - Informs the user of the URL to access the container with a web browser
  15. - Stops and removes containers from previous runs to avoid conflicts
  16. - Mounts the present working directory to /home/user/work on Linux and Mac OSX
  17. - Prints out the graphical app output log following execution
  18. - Exits with the same return code as the graphical app
  19. Options:
  20. -h Display this help and exit.
  21. -c Container name to use (default ${container}).
  22. -i Image name (default ${image}).
  23. -p Port to expose HTTP server (default ${port}). If an empty
  24. string, the port is not exposed.
  25. -r Extra arguments to pass to 'docker run'. E.g.
  26. --env="APP=glxgears"
  27. -q Do not output informational messages.
  28. EOF
  29. }
  30. while [ $# -gt 0 ]; do
  31. case "$1" in
  32. -h)
  33. show_help
  34. exit 0
  35. ;;
  36. -c)
  37. container=$2
  38. shift
  39. ;;
  40. -i)
  41. image=$2
  42. shift
  43. ;;
  44. -p)
  45. port=$2
  46. shift
  47. ;;
  48. -r)
  49. extra_run_args="$extra_run_args $2"
  50. shift
  51. ;;
  52. -q)
  53. quiet=1
  54. ;;
  55. *)
  56. show_help >&2
  57. exit 1
  58. ;;
  59. esac
  60. shift
  61. done
  62. which docker 2>&1 >/dev/null
  63. if [ $? -ne 0 ]; then
  64. echo "Error: the 'docker' command was not found. Please install docker."
  65. exit 1
  66. fi
  67. os=$(uname)
  68. if [ "${os}" != "Linux" ]; then
  69. vm=$(docker-machine active 2> /dev/null || echo "default")
  70. if ! docker-machine inspect "${vm}" &> /dev/null; then
  71. if [ -z "$quiet" ]; then
  72. echo "Creating machine ${vm}..."
  73. fi
  74. docker-machine -D create -d virtualbox --virtualbox-memory 2048 ${vm}
  75. fi
  76. docker-machine start ${vm} > /dev/null
  77. eval $(docker-machine env $vm --shell=sh)
  78. fi
  79. ip=$(docker-machine ip ${vm} 2> /dev/null || echo "localhost")
  80. url="http://${ip}:$port"
  81. cleanup() {
  82. docker stop $container >/dev/null
  83. # Attempt to delete container
  84. rm_output=$(docker rm -f $container 2>&1)
  85. rm_exit_code=$?
  86. if [[ $rm_exit_code != 0 ]]; then
  87. if [[ "$CIRCLECI" == "true" ]] && [[ $rm_output == *"Driver btrfs failed to remove"* ]]; then
  88. : # Ignore error because of https://circleci.com/docs/docker-btrfs-error/
  89. else
  90. echo "$rm_output"
  91. return $rm_exit_code
  92. fi
  93. fi
  94. }
  95. running=$(docker ps -a -q --filter "name=${container}")
  96. if [ -n "$running" ]; then
  97. if [ -z "$quiet" ]; then
  98. echo "Stopping and removing the previous session..."
  99. echo ""
  100. fi
  101. cleanup
  102. fi
  103. if [ -z "$quiet" ]; then
  104. echo ""
  105. echo "Setting up the graphical application container..."
  106. echo ""
  107. if [ -n "$port" ]; then
  108. echo "Point your web browser to ${url}"
  109. echo ""
  110. fi
  111. fi
  112. pwd_dir="`cd $(dirname $0)/../..; pwd`"
  113. mount_local=""
  114. if [ "${os}" = "Linux" ] || [ "${os}" = "Darwin" ]; then
  115. mount_local=" -v ${pwd_dir}:/usr/src/CTK "
  116. fi
  117. port_arg=""
  118. if [ -n "$port" ]; then
  119. port_arg="-p $port:6080"
  120. fi
  121. docker run \
  122. -d \
  123. --name $container \
  124. ${mount_local} \
  125. $port_arg \
  126. $extra_run_args \
  127. $image >/dev/null
  128. print_app_output() {
  129. docker cp $container:/var/log/supervisor/graphical-app-launcher.log - \
  130. | tar xO
  131. result=$(docker cp $container:/tmp/graphical-app.return_code - \
  132. | tar xO)
  133. cleanup
  134. exit $result
  135. }
  136. trap "docker stop $container >/dev/null && print_app_output" SIGINT SIGTERM
  137. docker wait $container >/dev/null
  138. print_app_output
  139. # vim: noexpandtab shiftwidth=4 tabstop=4 softtabstop=0