-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete_resource.sh
executable file
·312 lines (285 loc) · 9.83 KB
/
delete_resource.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/bash
#instance image volume securitygroup tenant router network floatingip
#This file is aimed at delete thinkstack server resource, you can specify the
#Provide mysql or openstack command methods to collect rally resource
date_file=`date "+%Y_%m_%d_%H_%M_%S"`
rm -rf /tmp/simth*
source /root/openrc
function_get_resourcefrommysql()
{
mysql << EOF
select sleep(2);
use neutron;
select id,floating_ip_address,status from floatingips into outfile '/tmp/simth_$date_file.floatingip';
select * from routers where name not like "router04" into outfile '/tmp/simth_$date_file.routerdb';
select * from securitygroups into outfile '/tmp/simth_$date_file.securitygroups';
select id,network_id,device_owner,device_id from ports into outfile '/tmp/simth_$date_file.portlist';
select id,name from networks into outfile '/tmp/simth_$date_file.networks';
use nova;
select display_name,uuid from instances where vm_state not like "deleted" into outfile '/tmp/simth_$date_file.instance1';
select display_name,uuid from instances where vm_state not like "deleted" into outfile '/tmp/simth_$date_file.instance';
use glance;
select name,id from images where deleted not like "1" into outfile '/tmp/simth_$date_file.image';
use cinder;
select display_name,id,attach_status from volumes where deleted not like "1" into outfile '/tmp/simth_$date_file.volume';
select id,display_name,status from snapshots where deleted=0 into outfile '/tmp/simth_$date_file.snapshotnotdeleted';
use keystone;
select id,name from project into outfile '/tmp/simth_$date_file.project';
select id,name from user into outfile '/tmp/simth_$date_file.user';
select id,name from role into outfile '/tmp/simth_$date_file.role';
EOF
}
#Retrive data from openstack command
function_get_resoure_fromcommnad()
{
echo floatingip is:
neutron floatingip-list
echo router is:
neutron router-list
echo Instance is:
nova list --all-tenant
nova list --all-tenant
echo image is:
glance image-list --all-tenant
echo volume is:
nova volume-list --all-tenant
echo securitygroup is:
neutron security-group-list
echo userlist is:
openstack user list
echo project list is:
openstack project list
echo network list
neutron net-list
}
function_delete_floatingip()
{
function_listrally_instance
cat /tmp/simth_$date_file.floatingip | while read myline_floatingip
do
# if floating ip is DOWN state, recycle
echo "FloatingIP in down status: "$myline_floatingip|grep DOWN
if [ "$?" -eq 0 ]
then
neutron floatingip-delete `echo $myline_floatingip|awk '{print $1}'`
else
# if floating ip belongs to rally instances, recycle
cat /tmp/simth_$date_file.instancerally | while read myline_instance
do
floating_ipdelete=`echo $myline_floatingip|awk '{print $2}'`
floating_id=`echo $myline_floatingip|awk '{print $1}'`
deassociate_instance=`echo $myline_instance|awk '{print $2}'`
echo `nova show $deassociate_instance`|grep $floating_ipdelete >> /tmp/simth.log
if [ "$?" -eq 0 ]
then
echo "FloatingIP used by rally: "$myline_floatingip
neutron floatingip-delete $floating_id >> /tmp/simth_$date_file_final.log
fi
done
fi
done
}
#$6=gatewayid $2=id $3=name
#Use neutron router-gateway-clear to clear gateway
#Use router-interface-delete to delete intfaces from router
#Use router-delete to delete router
#$6=gatewayid $2=id $3=name
#neutron router-gateway-clear "Router"(check name with neutron router-list)
#neutron router-interface-delete "Router" "SubNet"
#neutron router-delete "Router"
#neutron subnet-delete "SubNet"
function_delete_router()
{
cat /tmp/simth_$date_file.routerdb |awk '{print $2}'
cat /tmp/simth_$date_file.routerdb | while read myline
do
echo $myline|grep rally
if [ "$?" -eq 0 ]
then
router_todelete=`echo $myline|awk '{print $2}'`
neutron router-gateway-clear `echo $myline|awk '{print $2}'`
neutron router-port-list $router_todelete|awk -F "\"" '{print $4}' >>/tmp/tempinterface.log
cat /tmp/tempinterface.log |grep -v "^$" |while read tempinterface
do
neutron router-interface-delete $router_todelete $tempinterface
done
rm -rf /tmp/tempinterface.log
neutron router-delete $router_todelete
fi
done
}
function_listrally_instance()
{
cat /tmp/simth_$date_file.instance1 | while read myline
do
echo $myline|grep rally >> /tmp/simth_$date_file.instancerally
done
}
function_delete_instance()
{
cat /tmp/simth_$date_file.instance | while read myline
do
echo $myline|grep rally
if [ "$?" -eq 0 ]
then
nova delete `echo $myline|awk '{print $2}'`
fi
done
}
function_delete_image()
{
cat /tmp/simth_$date_file.image | while read myline
do
echo $myline|grep rally
if [ "$?" -eq 0 ]
then
glance image-delete `echo $myline|awk '{print $2}'`
fi
done
}
#volume attached should be deattched then delete
#volume datach format nova volume server_id volume_id
function_delete_volume()
{
cat /tmp/simth_$date_file.volume | while read myline
do
echo $myline|grep rally
if [ "$?" -eq 0 ]
then
echo $myline|grep attached
volume_id=`echo $myline|awk '{print $2}'`
if [ "$?" -eq 0 ]
then
# volume_id=`echo $myline|awk '{print $2}'`
echo "volume id is:" $volume_id
server_id=`nova volume-show $volume_id|grep attachments|awk -F "\"" '{print $4}'`
echo "server_id is:" $server_id
nova volume-detach $server_id $volume_id
fi
cinder reset-state $volume_id
#cinder delete `echo $myline|awk '{print $2}'`
cinder delete $volume_id
fi
done
}
function_delete_volume_snapshot()
{
cat /tmp/simth_$date_file.snapshotnotdeleted | while read myline
#cat /tmp/ddd | while read myline
do
echo $myline|grep rally
if [ "$?" -eq 0 ]
then
mysql << EOF
use cinder;
update snapshots set deleted=1 where id="`echo $myline|awk '{print $1}'`";
update snapshots set status="deleted" where id="`echo $myline|awk '{print $1}'`";
EOF
echo $myline
fi
done
}
#2id 3 name
function_delete_securitygroups()
{
#Ensure Instance does not use security group
#method1: collect all rally related security
#method2: collect all rally related instnace and then check if security is bonding to rally related security groups
cat /tmp/simth_$date_file.securitygroups |while read myline_securitygroups
do
echo $myline_securitygroups|grep rally >> /tmp/simth.log
if [ "$?" -eq 0 ]
then
cat /tmp/simth_$date_file.instance1 | while read rallyinstances
do
echo rally instance is: $rallyinstances
echo security group is: $myline_securitygroups|awk '{print $2}'
nova remove-secgroup `echo $rallyinstances|awk '{print $2}'` `echo $myline_securitygroups|awk '{print $2}'`
done
nova secgroup-delete `echo $myline_securitygroups|awk '{print $2}'`
fi
done
}
function_delete_network()
{
# id,network_id,device_owner,device_id
#detach vm port from network
#nova interface-detach instanceid portid
#1./tmp/port_list: Use sql collect netid&portid&porttype select id,network_id,device_owner from ports;
#2.chose which network you want to delete
#3.fileter the router portid and compute portid deviceid from /tmp/port_list
#4.detach compute port: nova interface-detach instanceid(deviceid) portid
#5.detach router port:
#6.delete network
cat /tmp/simth_$date_file.networks | while read myline_network
do
echo $myline_network|grep rally >> /tmp/simth.log
if [ "$?" -eq 0 ]
then
echo network is: $myline_network
myline_network_id=`echo $myline_network|awk '{print $1}'`
cat /tmp/simth_$date_file.portlist |while read myline_portlist
do
#myline_network_id=`echo $myline_network|awk '{print $1}'`
#echo $myline_network_id
echo $myline_portlist| grep $myline_network_id| grep compute >> /tmp/simth.log
if [ "$?" -eq 0 ]
then
nova interface-detach `echo $myline_portlist|awk '{print $4, $1}'`
#nova interface-detach instanceid portid
fi
echo $myline_portlist| grep $myline_network_id| grep compute >> /tmp/simth.log
if [ "$?" -eq 0 ]
then
nova interface-detach `echo $myline_portlist|awk '{print $4, $1}'`
#nova interface-detach instanceid portid
fi
echo $myline_portlist| grep $myline_network_id| grep router_interface >> /tmp/simth.log
if [ "$?" -eq 0 ]
then
echo myline_network_id $myline_network_id
router_id=`echo $myline_portlist|awk '{print $4}'`
echo router_id $router_id
net_id=`echo $myline_portlist|awk '{print $2}'`
echo net_id $net_id
subnet_net_id=`neutron net-show $net_id|grep subnets|awk '{print $4}'`
echo subnet_net $subnet_net_id
#echo subnet_id $subnet_id
neutron router-interface-delete $router_id $subnet_net_id
fi
done
echo trying to delete $myline_network_id
neutron net-delete $myline_network_id
fi
done
}
function_delete_prj_user_role()
{
cat /tmp/simth_$date_file.$1 | while read myline
do
echo $myline|grep rally >> /tmp/simth.log
if [ "$?" -eq 0 ]
then
id=`echo $myline|awk '{print $1}'`
echo trying to delete $1 $id
openstack $1 delete $id
fi
done
}
function_get_resourcefrommysql
#function_delete_instance
#function_delete_router
#function_delete_image
#function_delete_volume
#function_delete_volume_snapshot
function_delete_floatingip
#function_listrally_instance
#function_delete_securitygroups
#function_delete_network
#function_delete_prj_usr_role
#function_delete_user
#function_delete_userrole
#function_delete_prj_user_role project
#function_delete_prj_user_role user
#function_delete_prj_user_role role
tar -zcf $date_file.tar.gz /tmp/simth*