-
Notifications
You must be signed in to change notification settings - Fork 12
/
azuredevops-delete-offline-build-agents.sh
executable file
·115 lines (90 loc) · 3.51 KB
/
azuredevops-delete-offline-build-agents.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
#!/usr/bin/env bash
################################################################################
# Script Name : azuredevops-delete-offline-build-agents.sh
# Description : Used to batch delete offline build agents from a specified agent pool
# Args : PERSONAL_ACCESS_TOKEN ORGANIZATION_NAME AGENT_POOL_NAME [API_VERSION]
# Author : Wellington Ozorio <[email protected]>
################################################################################
set -o errexit
set -o pipefail
set -o nounset
function log() {
local MESSAGE="${1}"
echo "${MESSAGE}" 1>&2
}
function usage() {
log "Usage: ${0} PERSONAL_ACCESS_TOKEN ORGANIZATION_NAME AGENT_POOL_NAME [API_VERSION]"
exit 1
}
function check_azure_devops_access() {
local RESPONSE
RESPONSE=$(
curl --silent --header "${HEADER}" "${AGENT_POOLS_URI}" -o /dev/null --write-out "%{http_code}"
)
if [[ ${RESPONSE} -lt 200 || ${RESPONSE} -gt 299 ]]; then
log "ERROR: Failed accessing Azure DevOps API with HTTP code ${RESPONSE}"
exit 1
fi
}
function get_agent_pool_id() {
local AGENT_POOL
AGENT_POOL=$(
curl \
--silent \
--header "${HEADER}" "${AGENT_POOLS_URI}" |
jq --arg AGENT_POOL_NAME "$AGENT_POOL_NAME" '.value[] | select(.name == $AGENT_POOL_NAME)'
)
if [[ -z "$AGENT_POOL" ]]; then
log "ERROR: ${AGENT_POOL_NAME} agent pool not found in ${ORGANIZATION_NAME} organization"
exit 1
fi
local AGENT_POOL_ID
AGENT_POOL_ID=$(jq --raw-output '.id' <<<"${AGENT_POOL}")
echo "${AGENT_POOL_ID}"
}
function get_offline_agents() {
local AGENT_POOL_ID="${1}"
local AGENTS_URI="https://dev.azure.com/${ORGANIZATION_NAME}/_apis/distributedtask/pools/${AGENT_POOL_ID}/agents?api-version=${API_VERSION}"
local OFFLINE_AGENTS
OFFLINE_AGENTS=$(curl --silent --header "${HEADER}" "${AGENTS_URI}" | jq '.value[] | select(.status == "offline")')
if [[ -z "${OFFLINE_AGENTS}" ]]; then
log "INFO: No offline agents found in ${AGENT_POOL_NAME} agent pool"
exit 0
fi
echo "${OFFLINE_AGENTS}"
}
function delete_offline_agent() {
local AGENT="${1}"
local AGENT_POOL_ID="${2}"
log "WARN: Deleting offline agent ID ${AGENT} from ${AGENT_POOL_NAME} agent pool in ${ORGANIZATION_NAME} organization"
curl \
--request DELETE \
--url "https://dev.azure.com/${ORGANIZATION_NAME}/_apis/distributedtask/pools/${AGENT_POOL_ID}/agents/${AGENT}?api-version=${API_VERSION}" \
--header "${HEADER}" \
--silent \
--fail >/dev/null || {
log "ERROR: Failed deleting offline agent ID ${AGENT} from ${AGENT_POOL_NAME} agent pool in ${ORGANIZATION_NAME} organization"
exit 1
}
}
function main() {
if [[ "${#}" -ne 3 && "${#}" -ne 4 ]]; then
usage
fi
PERSONAL_ACCESS_TOKEN="${1}"
ORGANIZATION_NAME="${2}"
AGENT_POOL_NAME="${3}"
API_VERSION="${4:-"7.2-preview.1"}"
AGENT_POOLS_URI="https://dev.azure.com/${ORGANIZATION_NAME}/_apis/distributedtask/pools?api-version=${API_VERSION}"
BASE64_PAT=$(printf "%s" ":${PERSONAL_ACCESS_TOKEN}" | base64)
HEADER="Authorization: Basic ${BASE64_PAT}"
check_azure_devops_access
local AGENT_POOL_ID
AGENT_POOL_ID=$(get_agent_pool_id)
local OFFLINE_AGENTS
OFFLINE_AGENTS=$(get_offline_agents "${AGENT_POOL_ID}")
jq --raw-output '.id' <<<"${OFFLINE_AGENTS}" | while IFS= read -r AGENT; do
delete_offline_agent "${AGENT}" "${AGENT_POOL_ID}"
done
}
main "${@}"