stop-cluster.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/bash
  2. #
  3. # A script to shutdown a dev swarm cluster.
  4. set -e
  5. ROOT="$(cd "$(dirname "$0")/../../.." && pwd)"
  6. source "${ROOT}/swarm/dev/scripts/util.sh"
  7. DEFAULT_BASE_DIR="${ROOT}/swarm/dev/cluster"
  8. usage() {
  9. cat >&2 <<USAGE
  10. usage: $0 [options]
  11. Shutdown a dev swarm cluster.
  12. OPTIONS:
  13. -d, --dir DIR Base directory [default: ${DEFAULT_BASE_DIR}]
  14. -h, --help Show this message
  15. USAGE
  16. }
  17. main() {
  18. local base_dir="${DEFAULT_BASE_DIR}"
  19. parse_args "$@"
  20. local pid_dir="${base_dir}/pids"
  21. stop_swarm_nodes
  22. stop_node "geth"
  23. stop_node "bootnode"
  24. delete_network
  25. }
  26. parse_args() {
  27. while true; do
  28. case "$1" in
  29. -h | --help)
  30. usage
  31. exit 0
  32. ;;
  33. -d | --dir)
  34. if [[ -z "$2" ]]; then
  35. fail "--dir flag requires an argument"
  36. fi
  37. base_dir="$2"
  38. shift 2
  39. ;;
  40. *)
  41. break
  42. ;;
  43. esac
  44. done
  45. if [[ $# -ne 0 ]]; then
  46. usage
  47. fail "ERROR: invalid arguments: $@"
  48. fi
  49. }
  50. stop_swarm_nodes() {
  51. for name in $(ls "${pid_dir}" | grep -oP 'swarm\d+'); do
  52. stop_node "${name}"
  53. done
  54. }
  55. stop_node() {
  56. local name=$1
  57. local pid_file="${pid_dir}/${name}.pid"
  58. if [[ -e "${pid_file}" ]]; then
  59. info "stopping ${name}"
  60. start-stop-daemon \
  61. --stop \
  62. --pidfile "${pid_file}" \
  63. --remove-pidfile \
  64. --oknodo \
  65. --retry 15
  66. fi
  67. if ip netns list | grep -qF "${name}"; then
  68. ip netns delete "${name}"
  69. fi
  70. if ip link show "veth${name}0" &>/dev/null; then
  71. ip link delete dev "veth${name}0"
  72. fi
  73. }
  74. delete_network() {
  75. if ip link show "swarmbr0" &>/dev/null; then
  76. ip link delete dev "swarmbr0"
  77. fi
  78. }
  79. main "$@"