dockerized.sh 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. # Podman by default pull from other OS registries before trying 'docker.io', so we need to be more explicit to avoid the retries
  40. registry=docker.io/
  41. else
  42. echo "Could not find Docker client or podman"
  43. exit 1
  44. fi
  45. fi
  46. d () {
  47. if [[ $use_podman ]]; then
  48. podman "$@"
  49. else
  50. docker "$@"
  51. fi
  52. }
  53. if [[ $TRAVIS ]]; then
  54. # Workaround Travis-CI intermittent network I/O error
  55. while (! d pull "${registry}urho3d/dockerized$BuildEnvironment:$DBE_TAG"); do sleep 10; done;
  56. elif [[ $DBE_REFRESH == 1 ]]; then
  57. d pull "${registry}urho3d/dockerized$BuildEnvironment:$DBE_TAG"
  58. fi
  59. if [[ $use_podman ]] || ( [[ $(d version -f '{{.Client.Version}}') =~ ^([0-9]+)\.0*([0-9]+)\. ]] && (( BASH_REMATCH[1] * 100 + BASH_REMATCH[2] >= 1809 )) ); then
  60. # podman or newer Docker client
  61. d run -it --rm -h fishtank $run_option \
  62. -e HOST_UID="$(id -u)" -e HOST_GID="$(id -g)" -e PROJECT_DIR="$PROJECT_DIR" \
  63. --env-file "$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$mount_option \
  66. "${registry}urho3d/dockerized$BuildEnvironment:$DBE_TAG" "$@"
  67. else
  68. # Fallback workaround on older Docker CLI version
  69. d run -it --rm -h fishtank \
  70. -e HOST_UID="$(id -u)" -e HOST_GID="$(id -g)" -e PROJECT_DIR="$PROJECT_DIR" \
  71. --env-file <(perl -ne 'chomp; print "$_\n" if defined $ENV{$_}' "$PROJECT_DIR/script/.env-file") \
  72. --mount type=bind,source="$PROJECT_DIR",target="$PROJECT_DIR" \
  73. --mount type=volume,source="$(id -u).urho3d_home_dir",target=/home/urho3d \
  74. "urho3d/dockerized$BuildEnvironment:$DBE_TAG" "$@"
  75. fi
  76. # vi: set ts=4 sw=4 expandtab: