-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclean_up.sh
92 lines (74 loc) · 3.8 KB
/
clean_up.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
#!/bin/bash
# Access granted under MIT Open Source License: https://en.wikipedia.org/wiki/MIT_License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions
# of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
# TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
set -o errexit
set -o pipefail
set -o nounset
# set -o xtrace # For debugging
###################
# PARAMETERS
#
# RESOURCE_GROUP_NAME_PREFIX
prefix="mdwdops"
echo "!! WARNING: !!"
echo "THIS SCRIPT WILL DELETE RESOURCES PREFIXED WITH $prefix !!"
if [[ -n $prefix ]]; then
printf "\nPIPELINES:\n"
az pipelines list -o tsv --only-show-errors | { grep "$prefix" || true; } | awk '{print $8}'
printf "\nVARIABLE GROUPS:\n"
az pipelines variable-group list -o tsv --only-show-errors | { grep "$prefix" || true; } | awk '{print $6}'
printf "\nSERVICE CONNECTIONS:\n"
az devops service-endpoint list -o tsv --only-show-errors | { grep "$prefix" || true; } | awk '{print $6}'
printf "\nSERVICE PRINCIPALS:\n"
az ad sp list --query "[?contains(appDisplayName,'$prefix')].displayName" -o tsv --show-mine
printf "\nRESOURCE GROUPS:\n"
az group list --query "[?contains(name,'$prefix') && ! contains(name,'dbw')].name" -o tsv
printf "\nEND OF SUMMARY\n"
read -r -p "Do you wish to DELETE above? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
echo "Delete pipelines the start with '$prefix' in name..."
[[ -n $prefix ]] &&
az pipelines list -o tsv |
{ grep "$prefix" || true; } |
awk '{print $4}' |
xargs -r -I % az pipelines delete --id % --yes
echo "Delete variable groups the start with '$prefix' in name..."
[[ -n $prefix ]] &&
az pipelines variable-group list -o tsv |
{ grep "$prefix" || true; } |
awk '{print $3}' |
xargs -r -I % az pipelines variable-group delete --id % --yes
echo "Delete service connections the start with '$prefix' in name..."
[[ -n $prefix ]] &&
az devops service-endpoint list -o tsv |
{ grep "$prefix" || true; } |
awk '{print $3}' |
xargs -r -I % az devops service-endpoint delete --id % --yes
echo "Delete service principal the start with '$prefix' in name, created by yourself..."
[[ -n $prefix ]] &&
az ad sp list --query "[?contains(appDisplayName,'$prefix')].appId" -o tsv --show-mine |
xargs -r -I % az ad sp delete --id %
echo "Delete resource group the start with '$prefix' in name..."
[[ -n $prefix ]] &&
az group list --query "[?contains(name,'$prefix') && ! contains(name,'dbw')].name" -o tsv |
xargs -I % az group delete --verbose --name % -y
;;
*)
exit
;;
esac
fi