-
Notifications
You must be signed in to change notification settings - Fork 5
/
wipeRabbitMQ.js
105 lines (92 loc) · 2.94 KB
/
wipeRabbitMQ.js
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
#!/usr/bin/env node
// Spawn process and resolve stdout contents
const execCapture = (command) =>
new Promise((resolve, reject) => {
let data = '';
const process = require('child_process').spawn(command, {
stdio: 'pipe',
shell: true,
});
process.stdout.on('data', (chunk) => {
data += chunk;
});
process.on('exit', () => resolve(data)).on('error', () => reject(data));
});
// Given a string, returns a string with a slash at the end.
const endWithSlash = (str) => (str.endsWith('/') ? str : `${str}/`);
// Make string URI safe
const makeSafe = (str) =>
encodeURIComponent(str).replace(/\(/g, '%28').replace(/\)/g, '%29');
// Receives an array of functions that return promises. Waits for results serially.
const runGettersAndReportProgress = async (promiseGetterArray) => {
await Promise.all(
promiseGetterArray.map(async (getPromise, idx) => {
await getPromise();
process.stdout.write(
` ${idx}/${promiseGetterArray.length} completed\r`,
);
}),
);
console.log(
`${promiseGetterArray.length}/${promiseGetterArray.length} completed`,
);
};
// ENV VARS
const USER = process.env.AMQP_USER || 'guest';
const PASS = process.env.AMQP_PASS || 'guest';
const AMQP_MANAGEMENT_URL = endWithSlash(
process.env.AMQP_MANAGEMENT_URL || 'http://localhost:15672',
);
const getDeleteCommand = ({ vhost, name, isQueue = true }) =>
`curl -u ${USER}:${PASS} -XDELETE ` +
`${AMQP_MANAGEMENT_URL}api/${isQueue ? 'queues' : 'exchanges'}/${makeSafe(
vhost,
)}/${makeSafe(name)}`;
execCapture(`curl -u ${USER}:${PASS} ${AMQP_MANAGEMENT_URL}api/exchanges`)
.then((body) => {
let parsed = null;
try {
parsed = JSON.parse(body);
} catch (e) {
console.log(
`Request did not return valid JSON.\n${body}\n\nIs the RabbitMQ server running?\n`,
);
process.exit(1);
}
if (!Array.isArray(parsed)) {
console.log(`Expected response to be array: \n${body}`);
process.exit(1);
}
console.log('Wiping Exchanges');
return runGettersAndReportProgress(
parsed.map(({ name, vhost }) => () =>
execCapture(getDeleteCommand({ name, vhost, isQueue: false })),
),
);
})
.then(() =>
execCapture(`curl -u ${USER}:${PASS} ${AMQP_MANAGEMENT_URL}api/queues`),
)
.then((body) => {
let parsed = null;
try {
parsed = JSON.parse(body);
} catch (e) {
console.log(
`Request did not return valid JSON.\n${body}\n\nIs the RabbitMQ server running?\n`,
);
process.exit(1);
}
if (!Array.isArray(parsed)) {
console.log(`Expected response to be array: \n${body}`);
process.exit(1);
}
console.log('Wiping Queues');
return runGettersAndReportProgress(
parsed.map(({ name, vhost }) => () =>
execCapture(getDeleteCommand({ name, vhost, isQueue: true })),
),
);
})
.then(() => console.log('Finished without issue.'))
.catch(console.error);