start-build-env.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. OS_PLATFORM="${1:-}"
  19. [ "$#" -gt 0 ] && shift
  20. DEFAULT_OS_PLATFORM="ubuntu_20"
  21. OS_PLATFORM_SUFFIX=""
  22. if [[ -n ${OS_PLATFORM} ]] && [[ "${OS_PLATFORM}" != "${DEFAULT_OS_PLATFORM}" ]]; then
  23. # ubuntu_20 (default) platform does not have suffix in Dockerfile.
  24. OS_PLATFORM_SUFFIX="_${OS_PLATFORM}"
  25. fi
  26. DOCKER_DIR=dev-support/docker
  27. DOCKER_FILE="${DOCKER_DIR}/Dockerfile${OS_PLATFORM_SUFFIX}"
  28. CPU_ARCH=$(echo "$MACHTYPE" | cut -d- -f1)
  29. if [[ "$CPU_ARCH" == "aarch64" || "$CPU_ARCH" == "arm64" ]]; then
  30. DOCKER_FILE="${DOCKER_DIR}/Dockerfile${OS_PLATFORM_SUFFIX}_aarch64"
  31. fi
  32. if [ ! -e "${DOCKER_FILE}" ] ; then
  33. echo "'${OS_PLATFORM}' environment not available yet for '${CPU_ARCH}'"
  34. exit 1
  35. fi
  36. docker build -t hadoop-build -f $DOCKER_FILE $DOCKER_DIR
  37. USER_NAME=${SUDO_USER:=$USER}
  38. USER_ID=$(id -u "${USER_NAME}")
  39. if [ "$(uname -s)" = "Darwin" ]; then
  40. GROUP_ID=100
  41. fi
  42. if [ "$(uname -s)" = "Linux" ]; then
  43. GROUP_ID=$(id -g "${USER_NAME}")
  44. # man docker-run
  45. # When using SELinux, mounted directories may not be accessible
  46. # to the container. To work around this, with Docker prior to 1.7
  47. # one needs to run the "chcon -Rt svirt_sandbox_file_t" command on
  48. # the directories. With Docker 1.7 and later the z mount option
  49. # does this automatically.
  50. if command -v selinuxenabled >/dev/null && selinuxenabled; then
  51. DCKR_VER=$(docker -v|
  52. awk '$1 == "Docker" && $2 == "version" {split($3,ver,".");print ver[1]"."ver[2]}')
  53. DCKR_MAJ=${DCKR_VER%.*}
  54. DCKR_MIN=${DCKR_VER#*.}
  55. if [ "${DCKR_MAJ}" -eq 1 ] && [ "${DCKR_MIN}" -ge 7 ] ||
  56. [ "${DCKR_MAJ}" -gt 1 ]; then
  57. V_OPTS=:z
  58. else
  59. for d in "${PWD}" "${HOME}/.m2"; do
  60. ctx=$(stat --printf='%C' "$d"|cut -d':' -f3)
  61. if [ "$ctx" != svirt_sandbox_file_t ] && [ "$ctx" != container_file_t ]; then
  62. printf 'INFO: SELinux is enabled.\n'
  63. printf '\tMounted %s may not be accessible to the container.\n' "$d"
  64. printf 'INFO: If so, on the host, run the following command:\n'
  65. printf '\t# chcon -Rt svirt_sandbox_file_t %s\n' "$d"
  66. fi
  67. done
  68. fi
  69. fi
  70. fi
  71. # Set the home directory in the Docker container.
  72. DOCKER_HOME_DIR=${DOCKER_HOME_DIR:-/home/${USER_NAME}}
  73. docker build -t "hadoop-build${OS_PLATFORM_SUFFIX}-${USER_ID}" - <<UserSpecificDocker
  74. FROM hadoop-build
  75. RUN rm -f /var/log/faillog /var/log/lastlog
  76. RUN groupadd --non-unique -g ${GROUP_ID} ${USER_NAME}
  77. RUN useradd -g ${GROUP_ID} -u ${USER_ID} -k /root -m ${USER_NAME} -d "${DOCKER_HOME_DIR}"
  78. RUN echo "${USER_NAME} ALL=NOPASSWD: ALL" > "/etc/sudoers.d/hadoop-build-${USER_ID}"
  79. ENV HOME "${DOCKER_HOME_DIR}"
  80. UserSpecificDocker
  81. #If this env varible is empty, docker will be started
  82. # in non interactive mode
  83. DOCKER_INTERACTIVE_RUN=${DOCKER_INTERACTIVE_RUN-"-i -t"}
  84. # By mapping the .m2 directory you can do an mvn install from
  85. # within the container and use the result on your normal
  86. # system. And this also is a significant speedup in subsequent
  87. # builds because the dependencies are downloaded only once.
  88. docker run --rm=true $DOCKER_INTERACTIVE_RUN \
  89. -v "${PWD}:${DOCKER_HOME_DIR}/hadoop${V_OPTS:-}" \
  90. -w "${DOCKER_HOME_DIR}/hadoop" \
  91. -v "${HOME}/.m2:${DOCKER_HOME_DIR}/.m2${V_OPTS:-}" \
  92. -v "${HOME}/.gnupg:${DOCKER_HOME_DIR}/.gnupg${V_OPTS:-}" \
  93. -u "${USER_ID}" \
  94. --name "hadoop-build${OS_PLATFORM_SUFFIX}" \
  95. "hadoop-build${OS_PLATFORM_SUFFIX}-${USER_ID}" "$@"