start-build-env.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env bash
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. set -e # exit on error
  17. cd "$(dirname "$0")" # connect to root
  18. DOCKER_DIR=dev-support/docker
  19. DOCKER_FILE="${DOCKER_DIR}/Dockerfile"
  20. CPU_ARCH=$(echo "$MACHTYPE" | cut -d- -f1)
  21. if [[ "$CPU_ARCH" == "aarch64" || "$CPU_ARCH" == "arm64" ]]; then
  22. DOCKER_FILE="${DOCKER_DIR}/Dockerfile_aarch64"
  23. fi
  24. docker build -t hadoop-build -f $DOCKER_FILE $DOCKER_DIR
  25. USER_NAME=${SUDO_USER:=$USER}
  26. USER_ID=$(id -u "${USER_NAME}")
  27. if [ "$(uname -s)" = "Darwin" ]; then
  28. GROUP_ID=100
  29. fi
  30. if [ "$(uname -s)" = "Linux" ]; then
  31. GROUP_ID=$(id -g "${USER_NAME}")
  32. # man docker-run
  33. # When using SELinux, mounted directories may not be accessible
  34. # to the container. To work around this, with Docker prior to 1.7
  35. # one needs to run the "chcon -Rt svirt_sandbox_file_t" command on
  36. # the directories. With Docker 1.7 and later the z mount option
  37. # does this automatically.
  38. if command -v selinuxenabled >/dev/null && selinuxenabled; then
  39. DCKR_VER=$(docker -v|
  40. awk '$1 == "Docker" && $2 == "version" {split($3,ver,".");print ver[1]"."ver[2]}')
  41. DCKR_MAJ=${DCKR_VER%.*}
  42. DCKR_MIN=${DCKR_VER#*.}
  43. if [ "${DCKR_MAJ}" -eq 1 ] && [ "${DCKR_MIN}" -ge 7 ] ||
  44. [ "${DCKR_MAJ}" -gt 1 ]; then
  45. V_OPTS=:z
  46. else
  47. for d in "${PWD}" "${HOME}/.m2"; do
  48. ctx=$(stat --printf='%C' "$d"|cut -d':' -f3)
  49. if [ "$ctx" != svirt_sandbox_file_t ] && [ "$ctx" != container_file_t ]; then
  50. printf 'INFO: SELinux is enabled.\n'
  51. printf '\tMounted %s may not be accessible to the container.\n' "$d"
  52. printf 'INFO: If so, on the host, run the following command:\n'
  53. printf '\t# chcon -Rt svirt_sandbox_file_t %s\n' "$d"
  54. fi
  55. done
  56. fi
  57. fi
  58. fi
  59. # Set the home directory in the Docker container.
  60. DOCKER_HOME_DIR=${DOCKER_HOME_DIR:-/home/${USER_NAME}}
  61. docker build -t "hadoop-build-${USER_ID}" - <<UserSpecificDocker
  62. FROM hadoop-build
  63. RUN rm -f /var/log/faillog /var/log/lastlog
  64. RUN groupadd --non-unique -g ${GROUP_ID} ${USER_NAME}
  65. RUN useradd -g ${GROUP_ID} -u ${USER_ID} -k /root -m ${USER_NAME} -d "${DOCKER_HOME_DIR}"
  66. RUN echo "${USER_NAME} ALL=NOPASSWD: ALL" > "/etc/sudoers.d/hadoop-build-${USER_ID}"
  67. ENV HOME "${DOCKER_HOME_DIR}"
  68. UserSpecificDocker
  69. #If this env varible is empty, docker will be started
  70. # in non interactive mode
  71. DOCKER_INTERACTIVE_RUN=${DOCKER_INTERACTIVE_RUN-"-i -t"}
  72. # By mapping the .m2 directory you can do an mvn install from
  73. # within the container and use the result on your normal
  74. # system. And this also is a significant speedup in subsequent
  75. # builds because the dependencies are downloaded only once.
  76. docker run --rm=true $DOCKER_INTERACTIVE_RUN \
  77. -v "${PWD}:${DOCKER_HOME_DIR}/hadoop${V_OPTS:-}" \
  78. -w "${DOCKER_HOME_DIR}/hadoop" \
  79. -v "${HOME}/.m2:${DOCKER_HOME_DIR}/.m2${V_OPTS:-}" \
  80. -v "${HOME}/.gnupg:${DOCKER_HOME_DIR}/.gnupg${V_OPTS:-}" \
  81. -u "${USER_ID}" \
  82. "hadoop-build-${USER_ID}" "$@"