dev-install.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/bin/bash
  2. set -euo pipefail
  3. #
  4. # This script downloads and installs development dependencies required for
  5. # bridge development on Linux.
  6. #
  7. # Tested on amd64 with CentOS 8, Debian 10 and Ubuntu 20.04. Likely works on other distros as well.
  8. #
  9. # We use this to set up our CI, but you might find it useful for your own development needs.
  10. #
  11. # This installer pulls in binaries from, and therefore trusts, a number of third-party sources:
  12. #
  13. # - k3s.io by Rancher Labs.
  14. # - Go binary distribution by Google.
  15. # - Tilt binary distribution by Tilt.
  16. #
  17. # Idempotent and safe to run multiple times for upgrades (but restarts your cluster).
  18. # Goes without saying, but this is NOT for production, or anywhere close to it.
  19. #
  20. # If you want to allow other users on the host access to the k3s cluster, look into
  21. # run the installer script with an appropriate K3S_KUBECONFIG_MODE.
  22. if [[ $EUID -ne 0 ]]; then
  23. echo "This script must be run as root"
  24. exit 1
  25. fi
  26. # TODO: https://docs.docker.com/engine/security/userns-remap/
  27. if ! docker info; then
  28. echo "Please install and configure Docker first"
  29. exit 1
  30. fi
  31. # On Ubuntu/Debian, switch to iptables-legacy
  32. # https://github.com/rancher/k3s/issues/1114
  33. if update-alternatives --set iptables /usr/sbin/iptables-legacy; then
  34. systemctl restart docker
  35. fi
  36. # Ensure that our binaries are not shadowed by the distribution.
  37. export PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin:/bin
  38. # Install Go binaries.
  39. ARCH=amd64
  40. GO=1.15.3
  41. # TODO(leo): verify checksum
  42. (
  43. if [[ -d /usr/local/go ]]; then
  44. rm -rf /usr/local/go
  45. fi
  46. TMP=$(mktemp -d)
  47. (
  48. cd "$TMP"
  49. curl -OJ "https://dl.google.com/go/go${GO}.linux-${ARCH}.tar.gz"
  50. tar -C /usr/local -xzf "go${GO}.linux-${ARCH}.tar.gz"
  51. echo 'PATH=/usr/local/go/bin:$PATH' >/etc/profile.d/local_go.sh
  52. )
  53. rm -rf "$TMP"
  54. )
  55. . /etc/profile.d/local_go.sh
  56. # Install Tilt (latest stable release by Tilt). Install script looks fine.
  57. curl -fsSL https://raw.githubusercontent.com/tilt-dev/tilt/master/scripts/install.sh | bash
  58. # Install k3s with sane defaults and make it use the local Docker daemon.
  59. curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION="v1.19.3+k3s2" INSTALL_K3S_EXEC="server
  60. --disable-cloud-controller
  61. --kube-scheduler-arg=address=127.0.0.1
  62. --kube-controller-manager-arg=address=127.0.0.1
  63. --disable traefik
  64. --docker
  65. " sh -s -
  66. cat <<'EOF' > /etc/profile.d/k3s.sh
  67. alias kc=kubectl
  68. source <(kubectl completion bash)
  69. complete -F __start_kubectl kc
  70. function use-namespace {
  71. kubectl config set-context --current --namespace=$1
  72. }
  73. # Required for tilt to find the local cluster
  74. export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
  75. EOF
  76. cat <<'EOF' > /etc/profile.d/buildkit.sh
  77. # Enable buildkit support in Docker for incremental builds
  78. export DOCKER_BUILDKIT=1
  79. EOF
  80. . /etc/profile.d/k3s.sh
  81. # Set default namespace to wormhole to make it easier to reset without deleting the cluster.
  82. ! kubectl create namespace wormhole
  83. use-namespace wormhole
  84. # Trick tilt into not pushing images by pretending to be docker-desktop
  85. # FIXME: https://github.com/tilt-dev/tilt/issues/3654
  86. sed -i 's/ name: default/ name: docker-desktop/g' $KUBECONFIG
  87. sed -i 's/current-context: default/current-context: docker-desktop/g' $KUBECONFIG
  88. sed -i 's/cluster: default/cluster: docker-desktop/g' $KUBECONFIG
  89. while ! k3s kubectl get all; do
  90. echo "Waiting for k3s..."
  91. systemctl status k3s.service
  92. sleep 5
  93. done
  94. echo "Done! You have to reopen your shell or source the new profile scripts:"
  95. echo " source /etc/profile.d/k3s.sh"
  96. echo " source /etc/profile.d/buildkit.sh"
  97. echo " source /etc/profile.d/local_go.sh"