github.com/outbrain/consul@v1.4.5/test/bin/cluster.bash (about) 1 #!/bin/bash 2 # 3 # Script for bringing up an N node consul cluster 4 # on the local machine on different ports. 5 # 6 # The first node is listening on the default ports 7 # so that the command line tool works. 8 # 9 # Examples: 10 # 11 # 3-node cluster: 12 # 13 # $ consul-cluster.bash 14 # $ consul-cluster.bash 3 15 # 16 # 5-node cluster with specific consul version: 17 # 18 # $ consul-cluster.bash 5 ~/consul-0.7.5/consul 19 20 config() { 21 local port=${1:-0} 22 local name="consul${port}" 23 local nodeid=$(printf "00000000-0000-0000-0000-%012d" $port) 24 local path="$DIR/${name}" 25 26 cat << EOF > "${path}/a.json" 27 { 28 "server" : true, 29 "node_id" : "${nodeid}", 30 "node_name" : "${name}", 31 "data_dir" : "${name}/data", 32 "pid_file" : "${name}/pid", 33 "bind_addr" : "127.0.0.1", 34 "retry_join" : ["127.0.0.1:8301","127.0.0.1:8304","127.0.0.1:8307"], 35 "bootstrap_expect" : ${N}, 36 "ports" : { 37 "http" : $((8500 + $port)), 38 "dns" : $((8600 + $port)), 39 "server" : $((8300 + 3*$port)), 40 "serf_lan" : $((8301 + 3*$port)), 41 "serf_wan" : $((8302 + 3*$port)), 42 "rpc" : $((8400 + $port)) 43 } 44 } 45 EOF 46 } 47 48 trap cleanup EXIT TERM KILL 49 50 jobs= 51 cleanup() { 52 [ "$jobs" == "" ] || kill $jobs 53 [ "$CLEANDIR" == "y" -a "$DIR" != "" ] && rm -rf "$DIR" 54 } 55 56 run() { 57 local port=$1 58 local name=consul${port} 59 local path="$DIR/${name}" 60 61 rm -rf "${path}" 62 mkdir -p "${path}" 63 config $port 64 ( $CONSUL agent -config-dir "${path}" 2>&1 | tee "${path}/log" ; echo "Exit code: $?" >> "${path}/log" ) & 65 jobs="$jobs $!" 66 } 67 68 N=3 69 CONSUL=$(which consul) 70 CLEANDIR=y 71 SLEEP=y 72 73 while test $# -gt 0 ; do 74 case "$1" in 75 -d|--dir) 76 shift 77 DIR=$1 78 CLEANDIR=n 79 shift 80 ;; 81 -n|--nodes) 82 shift 83 N=$1 84 shift 85 ;; 86 -q|--quick) 87 shift 88 SLEEP=n 89 ;; 90 -x|--consul) 91 shift 92 CONSUL=$1 93 shift 94 ;; 95 *) 96 echo "Usage: $(basename $0) [-n nodes] [-x consul] [-d dir]" 97 echo "" 98 echo " -h, --help brief help" 99 echo " -d, --dir temp dir path to the temp directory, default is $DIR" 100 echo " -n, --nodes nodes number of nodes to start, default is $N" 101 echo " -q, --quick do not wait during startup" 102 echo " -x, --consul consul consul binary, default is $CONSUL" 103 exit 0 104 ;; 105 esac 106 done 107 108 [ "$DIR" == "" ] && DIR=$(mktemp -d /tmp/consul-cluster-XXXXXXX) 109 110 echo "Starting $N node cluster. exe=$CONSUL data=$DIR" 111 [ "$CLEANDIR" == "y" ] && echo "Data files will be removed" 112 echo "Stop with CTRL-C" 113 echo "" 114 [ "$SLEEP" == "y" ] && sleep 3 115 116 for ((i=0 ; i < N ; i++)) ; do run $i ; done 117 118 wait