build-rook.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env bash
  2. set -e
  3. #############
  4. # VARIABLES #
  5. #############
  6. rook_git_root=$(git rev-parse --show-toplevel)
  7. rook_kube_templates_dir="$rook_git_root/deploy/examples/"
  8. #############
  9. # FUNCTIONS #
  10. #############
  11. function fail_if {
  12. if ! git rev-parse --show-toplevel &> /dev/null; then
  13. echo "It looks like you are NOT in a Git repository"
  14. echo "This script should be executed from WITHIN Rook's git repository"
  15. exit 1
  16. fi
  17. }
  18. function purge_rook_pods {
  19. cd "$rook_kube_templates_dir"
  20. # Older rook versions use resource type "pool", newer versions
  21. # use resource type "cephblockpools".
  22. kubectl delete -n rook-ceph pool replicapool || true
  23. kubectl delete -n rook-ceph cephblockpools replicapool || true
  24. kubectl delete storageclass rook-ceph-block || true
  25. kubectl delete -f kube-registry.yaml || true
  26. # Older rook versions use resource type "cluster",
  27. # versions > 0.8 use resource type "cephcluster".
  28. kubectl delete -n rook-ceph cluster rook-ceph || true
  29. kubectl delete -n rook-ceph cephcluster rook-ceph || true
  30. kubectl delete crd cephclusters.ceph.rook.io cephblockpools.ceph.rook.io cephobjectstores.ceph.rook.io cephobjectstoreusers.ceph.rook.io cephfilesystems.ceph.rook.io volumes.rook.io || true
  31. kubectl delete -n rook-ceph daemonset rook-ceph-agent || true
  32. kubectl delete -f operator.yaml || true
  33. kubectl delete clusterroles rook-ceph-agent || true
  34. kubectl delete clusterrolebindings rook-ceph-agent || true
  35. kubectl delete namespace rook-ceph || true
  36. cd "$rook_git_root"
  37. }
  38. function purge_ceph_vms {
  39. instances=$(vagrant global-status | awk '/k8s-/ { print $1 }')
  40. for i in $instances; do
  41. # assuming /var/lib/rook is not ideal but it should work most of the time
  42. vagrant ssh "$i" -c "cat << 'EOF' > /tmp/purge-ceph.sh
  43. sudo rm -rf /var/lib/rook
  44. for disk in \$(sudo blkid | awk '/ROOK/ {print \$1}' | sed 's/[0-9]://' | uniq); do
  45. sudo dd if=/dev/zero of=\$disk bs=1M count=20 oflag=direct,dsync
  46. done
  47. EOF"
  48. vagrant ssh "$i" -c "bash /tmp/purge-ceph.sh"
  49. done
  50. }
  51. # shellcheck disable=SC2120
  52. function add_user_to_docker_group {
  53. sudo groupadd docker || true
  54. sudo gpasswd -a vagrant docker || true
  55. if [[ $(id -gn) != docker ]]; then
  56. exec sg docker "$0 $*"
  57. fi
  58. }
  59. function run_docker_registry {
  60. if ! docker ps | grep -sq registry; then
  61. docker run -d -p 5000:5000 --restart=always --name registry registry:2
  62. fi
  63. }
  64. function docker_import {
  65. img=$(docker images | grep -Eo '^build-[a-z0-9]{8}/ceph-[a-z0-9]+\s')
  66. # shellcheck disable=SC2086
  67. docker tag $img 172.17.8.1:5000/rook/ceph:latest
  68. docker --debug push 172.17.8.1:5000/rook/ceph:latest
  69. # shellcheck disable=SC2086
  70. docker rmi $img
  71. }
  72. function make_rook {
  73. # go to the repository root dir
  74. cd "$rook_git_root"
  75. # build rook
  76. make
  77. }
  78. function run_rook {
  79. cd "$rook_kube_templates_dir"
  80. kubectl create -f operator.yaml
  81. while ! kubectl get crd cephclusters.ceph.rook.io >/dev/null 2>&1; do
  82. echo "waiting for Rook operator"
  83. sleep 10
  84. done
  85. kubectl create -f cluster.yaml
  86. cd -
  87. }
  88. function edit_rook_cluster_template {
  89. cd "$rook_kube_templates_dir"
  90. sed -i 's|image: .*$|image: 172.17.8.1:5000/rook/ceph:latest|' operator.yaml
  91. echo "operator.yml has been edited with the new image '172.17.8.1:5000/rook/ceph:latest'"
  92. cd -
  93. }
  94. function config_kubectl {
  95. local k8s_01_vm
  96. k8s_01_vm=$(vagrant global-status | awk '/k8s-01/ { print $1 }')
  97. mkdir -p $HOME/.kube/
  98. vagrant ssh $k8s_01_vm -c "sudo cat /root/.kube/config" > $HOME/.kube/config.rook
  99. if [ -f "$HOME/.kube/config" ] && \
  100. ! diff $HOME/.kube/config $HOME/.kube/config.rook >/dev/null 2>&1 ;
  101. then
  102. echo "Backing up existing Kubernetes configuration file."
  103. mv $HOME/.kube/config $HOME/.kube/config.before.rook."$(date +%s)"
  104. ln -sf $HOME/.kube/config.rook $HOME/.kube/config
  105. fi
  106. kubectl get nodes
  107. }
  108. ########
  109. # MAIN #
  110. ########
  111. fail_if
  112. config_kubectl
  113. add_user_to_docker_group
  114. run_docker_registry
  115. # we purge rook otherwise make fails for 'use-use' image
  116. purge_rook_pods
  117. purge_ceph_vms
  118. make_rook
  119. docker_import
  120. edit_rook_cluster_template
  121. run_rook