-
Notifications
You must be signed in to change notification settings - Fork 340
/
ambari_cancel_all_requests.sh
executable file
·61 lines (53 loc) · 2.11 KB
/
ambari_cancel_all_requests.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
#!/usr/bin/env bash
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2016-09-27 17:25:36 +0100 (Tue, 27 Sep 2016)
#
# https://github.com/HariSekhon/DevOps-Python-tools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#
set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x
AMBARI_HOST="${1:-${AMBARI_HOST:-localhost}}"
AMBARI_PORT="${2:-${AMBARI_PORT:-8080}}"
AMBARI_USER="${3:-${AMBARI_USER:-admin}}"
AMBARI_PASSWORD="${4:-${AMBARI_PASSWORD:-admin}}"
AMBARI_CLUSTER="${5:-${AMBARI_CLUSTER:-Sandbox}}"
PROTOCOL="http"
if [ -n "${SSL:-}" ]; then
PROTOCOL="https"
if [ "$AMBARI_PORT" = "8080" ]; then
AMBARI_PORT=8443
fi
fi
usage(){
echo "Simple script to cancel all Ambari op requests
usage: ${0##*/} <ambari_host> <ambari_port> <username> <password> <cluster_name>
Prefer using the following environment variables instead of arguments in order to not expose the Ambari admin credentials in the process list:
AMBARI_HOST (default: localhost)
AMBARI_PORT (default: 8080)
AMBARI_USER (default: admin)
AMBARI_PASSWORD (default: admin)
AMBARI_CLUSTER (default: Sandbox)
SSL (default: blank, any value enables SSL)
"
exit 1
}
if [ $# -gt 5 ]; then
usage
fi
echo "querying Ambari for request IDs"
curl -u "$AMBARI_USER:$AMBARI_PASSWORD" "$PROTOCOL://$AMBARI_HOST:$AMBARI_PORT/api/v1/clusters/$AMBARI_CLUSTER/requests" |
grep id |
awk '{print $3}' |
while read -r id; do
echo "requesting cancellation of request $id"
# Unfortunately this call returns 200 OK even if your user account doesn't have permission to do this operation, but will fail on the requests query above for wrong username/password
curl -u "$AMBARI_USER:$AMBARI_PASSWORD" -i -H "X-Requested-By: $AMBARI_USER ($USER)" -X PUT -d '{"Requests":{"request_status":"ABORTED","abort_reason":"Aborted by user"}}' "$PROTOCOL://$AMBARI_HOST:$AMBARI_PORT/api/v1/clusters/$AMBARI_CLUSTER/requests/$id"
done