devcontainer.sh 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env bash
  2. # Licensed to the LF AI & Data foundation under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. SOURCE="${BASH_SOURCE[0]}"
  18. while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  19. DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  20. SOURCE="$(readlink "$SOURCE")"
  21. [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
  22. done
  23. ROOT_DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"
  24. export OS_NAME="${OS_NAME:-ubuntu20.04}"
  25. unameOut="$(uname -s)"
  26. case "${unameOut}" in
  27. Linux*) machine=Linux;;
  28. Darwin*) machine=Mac;;
  29. CYGWIN*) machine=Cygwin;;
  30. MINGW*) machine=MinGw;;
  31. *) machine="UNKNOWN:${unameOut}"
  32. esac
  33. # Attempt to run in the container with the same UID/GID as we have on the host,
  34. # as this results in the correct permissions on files created in the shared
  35. # volumes. This isn't always possible, however, as IDs less than 100 are
  36. # reserved by Debian, and IDs in the low 100s are dynamically assigned to
  37. # various system users and groups. To be safe, if we see a UID/GID less than
  38. # 500, promote it to 501. This is notably necessary on macOS Lion and later,
  39. # where administrator accounts are created with a GID of 20. This solution is
  40. # not foolproof, but it works well in practice.
  41. uid=$(id -u)
  42. gid=$(id -g)
  43. [ "$uid" -lt 500 ] && uid=501
  44. [ "$gid" -lt 500 ] && gid=$uid
  45. if [ "${1-}" = "build" ];then
  46. CHECK_BUILDER=1
  47. fi
  48. if [ "${CHECK_BUILDER:-}" == "1" ];then
  49. awk 'c&&c--{sub(/^/,"#")} /# Command/{c=3} 1' $ROOT_DIR/docker-compose.yml > $ROOT_DIR/docker-compose-devcontainer.yml
  50. else
  51. awk 'c&&c--{sub(/^/,"#")} /# Build devcontainer/{c=7} 1' $ROOT_DIR/docker-compose.yml > $ROOT_DIR/docker-compose-devcontainer.yml.tmp
  52. awk 'c&&c--{sub(/^/,"#")} /# Command/{c=3} 1' $ROOT_DIR/docker-compose-devcontainer.yml.tmp > $ROOT_DIR/docker-compose-devcontainer.yml
  53. rm $ROOT_DIR/docker-compose-devcontainer.yml.tmp
  54. fi
  55. if [ "${machine}" == "Mac" ];then
  56. sed -i '' "s/# user: {{ CURRENT_ID }}/user: \"$uid:$gid\"/g" $ROOT_DIR/docker-compose-devcontainer.yml
  57. else
  58. sed -i "s/# user: {{ CURRENT_ID }}/user: \"$uid:$gid\"/g" $ROOT_DIR/docker-compose-devcontainer.yml
  59. fi
  60. pushd "$ROOT_DIR"
  61. mkdir -p "${DOCKER_VOLUME_DIRECTORY:-.docker}/amd64-${OS_NAME}-ccache"
  62. mkdir -p "${DOCKER_VOLUME_DIRECTORY:-.docker}/amd64-${OS_NAME}-go-mod"
  63. mkdir -p "${DOCKER_VOLUME_DIRECTORY:-.docker}/amd64-${OS_NAME}-vscode-extensions"
  64. mkdir -p "${DOCKER_VOLUME_DIRECTORY:-.docker}/amd64-${OS_NAME}-conan"
  65. chmod -R 777 "${DOCKER_VOLUME_DIRECTORY:-.docker}"
  66. if [ "${1-}" = "build" ];then
  67. docker compose -f $ROOT_DIR/docker-compose-devcontainer.yml pull builder
  68. docker compose -f $ROOT_DIR/docker-compose-devcontainer.yml build builder
  69. fi
  70. if [ "${1-}" = "up" ]; then
  71. docker compose -f $ROOT_DIR/docker-compose-devcontainer.yml up -d $(docker compose config --services | grep -wv "gpubuilder")
  72. fi
  73. if [ "${1-}" = "down" ]; then
  74. docker compose -f $ROOT_DIR/docker-compose-devcontainer.yml down
  75. fi
  76. if [ "${1-}" = "gpu" -a "${2-}" = "up" ]; then
  77. docker compose -f $ROOT_DIR/docker-compose-devcontainer.yml up -d $(docker compose config --services | grep -wv "builder")
  78. fi
  79. popd