dockerized.sh 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright (c) 2008-2020 the Urho3D project.
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. # THE SOFTWARE.
  22. #
  23. if [[ $# -eq 0 ]]; then echo "Usage: dockerized.sh native|mingw|android|rpi|arm|web [command [params]]"; exit 1; fi
  24. PROJECT_DIR=$(cd "${0%/*}/.." || exit 1; pwd)
  25. if [[ ! $DBE_TAG ]]; then
  26. # If the command failed or not on a tag then use 'master' by default; TRAVIS_COMMIT should be empty for non-CI usage
  27. DBE_TAG=$(git describe --exact-match "$TRAVIS_COMMIT" 2>/dev/null || echo master)
  28. fi
  29. BuildEnvironment=-$1; shift
  30. BuildEnvironment=${BuildEnvironment/-base}
  31. # Determine which tool is available to use
  32. if ! docker --version >/dev/null 2>&1; then
  33. if podman --version >/dev/null 2>&1; then
  34. use_podman=1
  35. # Disable SELinux protection in order to mount Urho3D project root directory from host to container in unprivileged mode
  36. run_option="--security-opt label=disable"
  37. # Podman mount volume as 'noexec' by default but we need 'exec' for Android build (aapt2 permission denied otherwise)
  38. mount_option=,exec
  39. else
  40. echo "Could not find Docker client or podman"
  41. exit 1
  42. fi
  43. fi
  44. d () {
  45. if [[ $use_podman ]]; then
  46. podman "$@"
  47. else
  48. docker "$@"
  49. fi
  50. }
  51. if [[ $TRAVIS ]]; then
  52. # Workaround Travis-CI intermittent network I/O error
  53. while (! d pull "urho3d/dockerized$BuildEnvironment:$DBE_TAG"); do sleep 10; done;
  54. elif [[ $DBE_REFRESH == 1 ]]; then
  55. d pull "urho3d/dockerized$BuildEnvironment:$DBE_TAG"
  56. fi
  57. if [[ $use_podman ]] || ( [[ $(d version -f '{{.Client.Version}}') =~ ^([0-9]+)\.0*([0-9]+)\. ]] && (( BASH_REMATCH[1] * 100 + BASH_REMATCH[2] >= 1809 )) ); then
  58. # podman or newer Docker client
  59. d run -it --rm -h fishtank $run_option \
  60. -e HOST_UID="$(id -u)" -e HOST_GID="$(id -g)" -e PROJECT_DIR="$PROJECT_DIR" \
  61. --env-file "$PROJECT_DIR/script/.env-file" \
  62. --mount type=bind,source="$PROJECT_DIR",target="$PROJECT_DIR" \
  63. --mount type=volume,source="$(id -u).urho3d_home_dir",target=/home/urho3d$mount_option \
  64. "urho3d/dockerized$BuildEnvironment:$DBE_TAG" "$@"
  65. else
  66. # Fallback workaround on older Docker CLI version
  67. d run -it --rm -h fishtank \
  68. -e HOST_UID="$(id -u)" -e HOST_GID="$(id -g)" -e PROJECT_DIR="$PROJECT_DIR" \
  69. --env-file <(perl -ne 'chomp; print "$_\n" if defined $ENV{$_}' "$PROJECT_DIR/script/.env-file") \
  70. --mount type=bind,source="$PROJECT_DIR",target="$PROJECT_DIR" \
  71. --mount type=volume,source="$(id -u).urho3d_home_dir",target=/home/urho3d \
  72. "urho3d/dockerized$BuildEnvironment:$DBE_TAG" "$@"
  73. fi
  74. # vi: set ts=4 sw=4 expandtab: