-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrabbit_move.sh
executable file
·153 lines (120 loc) · 3.73 KB
/
rabbit_move.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env bash
#: Simple RabbitMQ shovel CLI utility using the HTTP api to save me (and possibly you) the need to install the CLI on your local machine and the need to access the web UI.
#: You can set up Shell aliases to handle periodically occuring tasks using one command from the CLI.
set -o errexit
set -o nounset
set -o pipefail
work_dir=$(dirname "$(readlink --canonicalize-existing "${0}" 2> /dev/null)")
readonly script_name="${0##*/}"
rabbitmq_hostname=""
rabbitmq_queue_source=""
rabbitmq_queue_destination=""
rabbitmq_vhost="%2f"
rabbitmq_source_hostname=""
rabbitmq_destination_hostname=""
shovel_name="move-shell-script"
trap clean_up ERR EXIT SIGINT SIGTERM
usage() {
cat <<USAGE_TEXT
Usage: ${script_name} [-h | --help] [-a <ARG>] [--abc <ARG>] [-f | --flag]
DESCRIPTION
RabbitMQ HTTP API utility to shovel messages from queue A to queue B.
OPTIONS:
-h
Hostname of RabbitMQ API, including the protocol (http(s), amqp(s)) and the port, username and password and the vhost. VHost must be percent encoded.
-s
Source AMQP server (amqp://).
-d
Destination AMQP server (amqp://).
-f
Name of the source queue (where the messages are taken from).
-t
Name of the destination queue (where the messages are sent to).
-v
VHost, defaults to / (%2f). Must be percent encoded.
-n
Name of the shovel, optional.
USAGE_TEXT
}
clean_up() {
trap - ERR EXIT SIGINT SIGTERM
}
die() {
local -r msg="${1}"
local -r code="${2:-90}"
echo "${msg}" >&2
exit "${code}"
}
parse_user_options() {
while getopts "h:f:t:s:d:v:" opt; do
case "${opt}" in
h)
rabbitmq_hostname="${OPTARG}"
;;
f)
rabbitmq_queue_source="${OPTARG}"
;;
t)
rabbitmq_queue_destination="${OPTARG}"
;;
s)
rabbitmq_source_hostname="${OPTARG}"
;;
d)
rabbitmq_destination_hostname="${OPTARG}"
;;
v)
rabbitmq_vhost="${OPTARG}"
;;
*)
usage
die "error: parsing options" 1
;;
esac
done
}
parse_user_options "${@}"
invalid_flag=0
if [ -z "${rabbitmq_hostname}" ]; then
invalid_flag=1
echo "Missing hostname, specify it with -h parameter"
fi
if [ -z "${rabbitmq_queue_source}" ]; then
invalid_flag=1
echo "Missing queue from, specify it with -f parameter"
fi
if [ -z "${rabbitmq_queue_destination}" ]; then
invalid_flag=1
echo "Missing queue to, specify it with -t parameter"
fi
if [ -z "${rabbitmq_source_hostname}" ]; then
invalid_flag=1
echo "Missing AMQP source hostname, specify it with -s parameter"
fi
if [ -z "${rabbitmq_destination_hostname}" ]; then
rabbitmq_destination_hostname="${rabbitmq_source_hostname}"
fi
if ((invalid_flag)); then
die "Cannot proceed with missing required parameters." 1
fi
if [ "$rabbitmq_queue_source" = "$rabbitmq_queue_destination" ]; then
die "Cannot shovel from messages from the same queue (${rabbitmq_queue_source} -> ${rabbitmq_queue_destination})." 1
fi
curl -v -X PUT $rabbitmq_hostname/api/parameters/shovel/$rabbitmq_vhost/$shovel_name \
-H "content-type: application/json" \
-d @- <<EOF
{
"value": {
"src-protocol": "amqp091",
"src-uri": "$rabbitmq_source_hostname",
"src-queue": "$rabbitmq_queue_source",
"dest-protocol": "amqp091",
"dest-uri": "$rabbitmq_destination_hostname",
"dest-queue": "$rabbitmq_queue_destination",
"src-delete-after": "queue-length"
}
}
EOF
#,"src-delete-after": "queue-length"
echo "Shovel created."
exit 0