github.com/cuiweixie/go-ethereum@v1.8.2-0.20180303084001-66cd41af1e38/swarm/dev/scripts/stop-cluster.sh (about)

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