dockerized.sh 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. mount_option=,exec # Podman mount volume as 'noexec' by default but we need 'exec' for Android build (aapt2 permission denied otherwise)
  36. else
  37. echo "Could not find Docker client or podman"
  38. exit 1
  39. fi
  40. fi
  41. d () {
  42. if [[ $use_podman ]]; then
  43. # Disable SELinux protection in order to mount Urho3D project root directory from host to container in unprivileged mode
  44. podman $1 --security-opt label=disable "${@:2}"
  45. else
  46. docker "$@"
  47. fi
  48. }
  49. # Workaround Travis-CI intermittent network I/O error
  50. if [[ $TRAVIS ]]; then while (! d pull "urho3d/dockerized$BuildEnvironment:$DBE_TAG"); do sleep 10; done; fi
  51. if [[ $use_podman ]] || ( [[ $(d version -f '{{.Client.Version}}') =~ ^([0-9]+)\.0*([0-9]+)\. ]] && (( BASH_REMATCH[1] * 100 + BASH_REMATCH[2] >= 1809 )) ); then
  52. # podman or newer Docker client
  53. d run -it --rm -h fishtank \
  54. -e HOST_UID="$(id -u)" -e HOST_GID="$(id -g)" -e PROJECT_DIR="$PROJECT_DIR" \
  55. --env-file "$PROJECT_DIR/script/.env-file" \
  56. --mount type=bind,source="$PROJECT_DIR",target="$PROJECT_DIR" \
  57. --mount type=volume,source="$(id -u).urho3d_home_dir",target=/home/urho3d$mount_option \
  58. "urho3d/dockerized$BuildEnvironment:$DBE_TAG" "$@"
  59. else
  60. # Fallback workaround on older Docker CLI version
  61. d run -it --rm -h fishtank \
  62. -e HOST_UID="$(id -u)" -e HOST_GID="$(id -g)" -e PROJECT_DIR="$PROJECT_DIR" \
  63. --env-file <(perl -ne 'chomp; print "$_\n" if defined $ENV{$_}' "$PROJECT_DIR/script/.env-file") \
  64. --mount type=bind,source="$PROJECT_DIR",target="$PROJECT_DIR" \
  65. --mount type=volume,source="$(id -u).urho3d_home_dir",target=/home/urho3d \
  66. "urho3d/dockerized$BuildEnvironment:$DBE_TAG" "$@"
  67. fi
  68. # vi: set ts=4 sw=4 expandtab: