ocboot.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/bin/bash
  2. shopt -s expand_aliases
  3. set -e
  4. DEFAULT_REPO=registry.cn-beijing.aliyuncs.com/yunionio
  5. IMAGE_REPOSITORY=${IMAGE_REPOSITORY:-$DEFAULT_REPO}
  6. VERSION=${VERSION:-v4-k3s.4}
  7. OCBOOT_IMAGE="$IMAGE_REPOSITORY/ocboot:$VERSION"
  8. CUR_DIR="$(pwd)"
  9. CONTAINER_NAME="buildah-ocboot"
  10. alias buildah="sudo buildah"
  11. ensure_buildah() {
  12. if ! [ -x "$(command -v buildah)" ]; then
  13. echo "Installing buildah ..."
  14. ./scripts/install-buildah.sh
  15. fi
  16. }
  17. buildah_from_image() {
  18. if buildah ps | grep $CONTAINER_NAME; then
  19. buildah rm $CONTAINER_NAME
  20. fi
  21. local img="$1"
  22. echo "Using buildah pull $img"
  23. buildah from --name $CONTAINER_NAME "$img"
  24. }
  25. ensure_buildah
  26. buildah_from_image "$OCBOOT_IMAGE"
  27. mkdir -p "$HOME/.ssh"
  28. ROOT_DIR='/ocboot'
  29. CMD=""
  30. is_ocboot_subcmd() {
  31. local subcmds="install upgrade add-node add-lbagent backup restore setup-container-env switch-edition clickhouse setup-ai-env"
  32. for subcmd in $subcmds; do
  33. if [[ "$1" == "$subcmd" ]]; then
  34. return 0
  35. fi
  36. done
  37. return 1
  38. }
  39. if is_ocboot_subcmd "$1"; then
  40. CMD="$ROOT_DIR/ocboot.py"
  41. fi
  42. buildah_version=$(buildah --version | awk '{print $3}')
  43. buildah_version_major=$(echo "$buildah_version" | awk -F. '{print $1}')
  44. buildah_version_minor=$(echo "$buildah_version" | awk -F. '{print $2}')
  45. buildah_extra_args=()
  46. # buildah accepts --env since 1.23
  47. echo "buildah version: $buildah_version"
  48. if [[ $buildah_version_major -eq 1 ]] && [[ "$buildah_version_minor" -gt 23 ]]; then
  49. buildah_extra_args+=(-e ANSIBLE_VERBOSITY="${ANSIBLE_VERBOSITY:-0}")
  50. buildah_extra_args+=(-e HOME="$HOME")
  51. fi
  52. cmd_extra_args=""
  53. origin_args="$@"
  54. if [[ "$1" == "run.py" ]]; then
  55. if [[ "$IMAGE_REPOSITORY" != "$DEFAULT_REPO" ]]; then
  56. cmd_extra_args="$cmd_extra_args -i $IMAGE_REPOSITORY"
  57. fi
  58. origin_args="$ROOT_DIR/$origin_args"
  59. fi
  60. mkdir -p "$HOME/.kube"
  61. buildah run --isolation chroot --user $(id -u):$(id -g) \
  62. -t "${buildah_extra_args[@]}" \
  63. --net=host \
  64. -v "$(mktemp -d):$HOME/.ansible" \
  65. -v "$HOME/.ssh:$HOME/.ssh" \
  66. -v "$HOME/.kube:$HOME/.kube" \
  67. -v "/etc/passwd:/etc/passwd:ro" \
  68. -v "/etc/group:/etc/group:ro" \
  69. -v "$(pwd):$ROOT_DIR" \
  70. -v "$(pwd)/airgap_assets/k3s-install.sh:/airgap_assets/k3s-install.sh:ro" \
  71. "$CONTAINER_NAME" $CMD $origin_args $cmd_extra_args