|
| 1 | +# |
| 2 | +# Variables |
| 3 | +# |
| 4 | + |
| 5 | +LOAD_BALANCER_STACK_NAME := rabbit-loadbalancer |
| 6 | + |
| 7 | +MAKEFLAGS += --no-print-directory |
| 8 | + |
| 9 | +# |
| 10 | +# Helpers |
| 11 | +# |
| 12 | + |
| 13 | +define create_node_stack_name |
| 14 | +rabbit-node0$(1) |
| 15 | +endef |
| 16 | + |
| 17 | +validate-NODE_COUNT: guard-NODE_COUNT |
| 18 | + @if ! echo "$(NODE_COUNT)" | grep --quiet --extended-regexp '^[1-9]$$'; then \ |
| 19 | + echo NODE_COUNT must be a positive single digit integer; \ |
| 20 | + exit 1; \ |
| 21 | + fi |
| 22 | + |
| 23 | +validate-node-ix0%: .env |
| 24 | + @if ! echo "$*" | grep --quiet --extended-regexp '^[1-9]$$'; then \ |
| 25 | + echo "Node index $* must be a positive single digit integer"; \ |
| 26 | + exit 1; \ |
| 27 | + fi |
| 28 | + |
| 29 | + @set -o allexport; . $<; set +o allexport; \ |
| 30 | + if [ "$*" -lt 1 ] || [ "$*" -gt "$$RABBIT_CLUSTER_NODE_COUNT" ]; then \ |
| 31 | + echo "Node index $* is out of range 1..$$RABBIT_CLUSTER_NODE_COUNT"; \ |
| 32 | + exit 1; \ |
| 33 | + fi |
| 34 | + |
| 35 | +# |
| 36 | +# Cluster level |
| 37 | +# |
| 38 | + |
| 39 | +### Note: up operation is called by CI automatically |
| 40 | +### it must NOT deploy stacks if they are already running |
| 41 | +### to avoid breaking existing cluster (stopping all nodes at once) |
| 42 | +up: start-cluster |
| 43 | + |
| 44 | +down: stop-cluster |
| 45 | + |
| 46 | +start-cluster: start-all-nodes start-loadbalancer |
| 47 | + |
| 48 | +update-cluster stop-cluster: |
| 49 | + @$(error This operation may break cluster. Check README for details.) |
| 50 | + |
| 51 | +# |
| 52 | +# Load Balancer |
| 53 | +# |
| 54 | + |
| 55 | +start-loadbalancer: .stack.loadbalancer.yml |
| 56 | + @docker stack deploy --with-registry-auth --prune --compose-file $< $(LOAD_BALANCER_STACK_NAME) |
| 57 | + |
| 58 | +update-loadbalancer: start-loadbalancer |
| 59 | + |
| 60 | +stop-loadbalancer: |
| 61 | + @docker stack rm $(LOAD_BALANCER_STACK_NAME) |
| 62 | + |
| 63 | +# |
| 64 | +# Rabbit all Nodes together |
| 65 | +# |
| 66 | + |
| 67 | +.start-all-nodes: validate-NODE_COUNT |
| 68 | + @i=1; \ |
| 69 | + while [ $$i -le $(NODE_COUNT) ]; do \ |
| 70 | + $(MAKE) start-node0$$i; \ |
| 71 | + i=$$((i + 1)); \ |
| 72 | + done |
| 73 | + |
| 74 | +start-all-nodes: .env |
| 75 | + @source $<; \ |
| 76 | + $(MAKE) .start-all-nodes NODE_COUNT=$$RABBIT_CLUSTER_NODE_COUNT |
| 77 | + |
| 78 | +update-all-nodes: |
| 79 | + @$(error Updating all nodes at the same time may break the cluster \ |
| 80 | + as it may restart (i.e. stop) all nodes at the same time. \ |
| 81 | + Update one node at a time) |
| 82 | + |
| 83 | +stop-all-nodes: |
| 84 | + @$(error Stopping all nodes at the same time breaks the cluster. \ |
| 85 | + Update one node at a time. \ |
| 86 | + Read more at https://groups.google.com/g/rabbitmq-users/c/owvanX2iSqA/m/ZAyRDhRfCQAJ) |
| 87 | + |
| 88 | +# |
| 89 | +# Rabbit Node level |
| 90 | +# |
| 91 | + |
| 92 | +start-node0%: validate-node-ix0% .stack.node0%.yml |
| 93 | + @STACK_NAME=$(call create_node_stack_name,$*); \ |
| 94 | + if docker stack ls --format '{{.Name}}' | grep --silent "$$STACK_NAME"; then \ |
| 95 | + echo "Rabbit Node $* is already running, skipping"; \ |
| 96 | + else \ |
| 97 | + echo "Starting Rabbit Node $* ..."; \ |
| 98 | + docker stack deploy --with-registry-auth --prune --compose-file $(word 2,$^) $(call create_node_stack_name,$*); \ |
| 99 | + fi |
| 100 | + |
| 101 | +update-node0%: validate-node-ix0% .stack.node0%.yml |
| 102 | + @docker stack deploy --detach=false --with-registry-auth --prune --compose-file $(word 2,$^) $(call create_node_stack_name,$*) |
| 103 | + |
| 104 | +stop-node0%: validate-node-ix0% |
| 105 | + @docker stack rm --detach=false $(call create_node_stack_name,$*) |
0 commit comments