-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdelete_cpod.sh
executable file
·155 lines (127 loc) · 3.44 KB
/
delete_cpod.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
154
155
#!/bin/bash
. ./env
[ "$1" == "" ] && echo "usage: $0 <name_of_cpod> <owner's email alias (ex: bdereims)>" && exit 1
if [ "${2}" == "" ]; then
OWNER="admin"
else
OWNER="${2}"
fi
DNSMASQ=/etc/dnsmasq.conf
HOSTS=/etc/hosts
mutex() {
while ! mkdir lock 2>&1 > /dev/null
do
echo "Waiting (PID $$)..."
sleep 2
done
}
network_delete() {
case "${BACKEND_NETWORK}" in
NSX-V)
${NETWORK_DIR}/delete_logicalswitch.sh ${1} ${2}
;;
NSX-T)
${NETWORK_DIR}/delete_logicalswitch.sh ${1} ${2}
;;
VLAN)
${NETWORK_DIR}/delete_vlan_portgroup.sh ${2}
;;
esac
}
respool_delete() {
${COMPUTE_DIR}/delete_resourcepool.sh ${1}
}
modify_dnsmasq() {
echo "Modifying '${DNSMASQ}' and '${HOSTS}'."
IPTRANSIT=$( ${COMPUTE_DIR}/cpod_ip.sh ${1} ontransit )
sed -i "/${1}\./d" ${DNSMASQ}
sed -i "/srv-host=_aserv._tcp,${IPTRANSIT},9100/d" ${DNSMASQ}
sed -i "/\t${1}\t/d" ${HOSTS}
systemctl stop dnsmasq
systemctl start dnsmasq
}
bgp_delete_peer() {
./network/delete_bgp_neighbour.sh edge-6 ${1}
}
bgp_delete_peer_vtysh() {
./network/delete_bgp_peer_vtysh.sh ${1} ${2}
}
release_mutex() {
rmdir lock
}
exit_gate() {
exit $1
}
test_owner() {
LINE=$( sed -n "/${CPOD_NAME_LOWER}\t/p" /etc/hosts | cut -f3 | sed "s/#//" | head -1 )
if [ "${LINE}" != "" ] && [ "${LINE}" != "${OWNER}" ]; then
echo "Error: Not Ok for deletion"
./extra/post_slack.sh ":wow: *${OWNER}* you're not allowed to delete *${NAME_HIGH}*"
exit 1
fi
}
main() {
CPOD_NAME="cpod-$1"
CPOD_NAME_HIGH=$( echo ${CPOD_NAME} | tr '[:lower:]' '[:upper:]' )
CPOD_NAME_LOWER=$( echo ${CPOD_NAME} | tr '[:upper:]' '[:lower:]' )
NAME_HIGH=$( echo $1 | tr '[:lower:]' '[:upper:]' )
test_owner ${2}
#test if more than 1 cpod with same header.
CPODCOUNT=$( cat ${HOSTS} | grep -c ${CPOD_NAME_LOWER})
if [[ $CPODCOUNT -gt 0 ]]
then
if [[ $CPODCOUNT -gt 1 ]]
then
echo "more than 1 cpod found. please check names"
echo "Please select CPOD instance to delete"
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
CPODLIST=$(cat ${HOSTS} | grep ${CPOD_NAME_LOWER} |awk '{print $1 " " $2}')
select CHOSENCPOD in ${CPODLIST}; do
if [ "${TEMPLATE}" = "Quit" ]; then
exit
fi
echo "you selected cpod : ${CHOSENCPOD}"
break
done
IP=$( echo ${CHOSENCPOD} | cut -d " " -f1 )
CPOD_NAME=$( echo ${CHOSENCPOD} | cut -d " " -f2 )
NAME_HIGH=${CPOD_NAME#"cpod-"}
CPOD_NAME_LOWER=$( echo ${CPOD_NAME} | tr '[:upper:]' '[:lower:]' )
IFS=$SAVEIFS
else
#check name
FOUNDNAME=$(cat ${HOSTS} | grep ${CPOD_NAME_LOWER} | cut -f2)
if [[ "${FOUNDNAME}" == "${CPOD_NAME_LOWER}" ]]
then
IP=$( cat ${HOSTS} | grep ${CPOD_NAME_LOWER} | cut -f1 )
else
echo "no cpod found with that name"
exit 1
fi
fi
else
echo "no cpod found with that name"
exit 1
fi
./extra/post_slack.sh "Deleting cPod *${NAME_HIGH}*"
echo "=== Deleting cPod called '${NAME_HIGH}'."
# IP=$( cat ${HOSTS} | grep ${CPOD_NAME_LOWER} | cut -f1 )
TMP=$( echo ${IP} | cut -d"." -f4 )
ASN=$( expr ${ASN} + ${TMP} )
echo "$NAME_HIGH - $IP - $TMP - $ASN"
mutex
#bgp_delete_peer ${IP}
bgp_delete_peer_vtysh ${IP} ${ASN}
modify_dnsmasq ${CPOD_NAME_LOWER}
release_mutex
respool_delete ${NAME_HIGH}
sleep 5
network_delete ${NSX_TRANSPORTZONE} ${CPOD_NAME_LOWER}
./cpod_lease.sh delete ${1} ${OWNER}
echo "=== Deletion is finished."
./extra/post_slack.sh ":thumbsup: cPod *${NAME_HIGH}* has been deleted"
exit_gate 0
}
main $1