start-build-env.sh 3.2 KB

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