This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathforce-delete-instance
executable file
·150 lines (106 loc) · 2.96 KB
/
force-delete-instance
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
#!/bin/bash
# force delete instances
# VARs
INSTANCE_NAME=""
INSTANCE_ID=""
INSTANCE_IPv4=""
INSTANCE_NAME_TO_DEL=""
INSTANCE_ID_TO_DEL=""
INSTANCE_NAME_IN_ERROR=""
PORT_ID=""
EMPTY_PORTS="false"
ANSWER=""
ANSWER_DEL=""
# GET name
echo 'enter instance name'
read INSTANCE_NAME
# CHECK if exists
nova list | grep $INSTANCE_NAME
# > /dev/null 2>&1
# GET instance id
#if [[ $? -eq 0 ]]; then
# echo ""
# echo "instance $INSTANCE_NAME exist :-)"
INSTANCE_ID=`nova list --all-tenants=1 | grep $INSTANCE_NAME | awk '{print $2}'`
#fi
# GET ipv4
#INSTANCE_IPv4=`nova list | grep $INSTANCE_NAME | awk '{print $12}' | cut -d= -f 2`
echo $INSTANCE_IPv4
INSTANCE_IPv4=`nova show $INSTANCE_ID | grep network | awk '{print $5}'`
# GET port id
PORT_ID=`neutron port-list | grep $INSTANCE_IPv4 | awk '{print $2}'`
# CHECK if port exist's
if [[ $PORT_ID == "" || $PORT_ID == " " ]];then
PORT_ID="no ports attached!"
EMPTY_PORTS="true"
fi
# SHOW var's
echo ""
echo "instance_name = $INSTANCE_NAME"
echo "instance_id = $INSTANCE_ID"
echo "ipv4 = $INSTANCE_IPv4"
echo "port_id = $PORT_ID"
if [[ $EMPTY_PORTS == "false" ]]; then
# REMOVE port
echo ""
echo "remove port $PORT_ID from instance $INSTANCE_NAME ? ( yes/no )"
read ANSWER
if [[ $ANSWER == "yes" ]]; then
neutron port-delete $PORT_ID
else
echo ""
echo "exit"
exit 0
fi
fi
# DELETE INSTANCE
echo ""
echo "remove instance $INSTANCE_NAME, id=$INSTANCE_ID ? ( yes/no )"
read ANSWER_DEL
if [[ $ANSWER_DEL == "yes" ]]; then
# nova delete $INSTANCE_ID
echo "nova delete $INSTANCE_ID"
else
echo ""
echo "exit"
exit 0
fi
# CHECK IF instance still exit's
nova list | grep $INSTANCE_NAME
if [ $? -eq 1 ]; then
echo "instance was successfully deleted :-)"
fi
if [ $? -eq 0 ]; then
echo " instance still exits, trying to delete again ..."
# If an instance gets stuck in an intermediate state (e.g., "deleting")
echo "wait 10 seconds"
sleep 10
nova list | grep deleting
if [ $? -eq 0 ]; then
INSTANCE_ID_TO_DEL=`nova list --all-tenants=1 | grep $INSTANCE_NAME | awk '{print $2}'`
echo "one instance on deleting state !"
nova reset-state --active $INSTANCE_ID_TO_DEL
nova delete $INSTANCE_ID_TO_DEL
fi
# Use the --active to force the instance back into an active state instead of an error state
echo "try to force delete ..."
nova reset-state --active $INSTANCE_ID
nova force-delete $INSTANCE_ID
# try to stop instance and then delete
nova list | grep deleting
if [ $? -eq 0 ]; then
INSTANCE_ID_TO_DEL=`nova list --all-tenants=1 | grep $INSTANCE_NAME | awk '{print $2}'`
nova reset-state --active $INSTANCE_ID_TO_DEL
nova stop $INSTANCE_ID_TO_DEL
nova delete $INSTANCE_ID_TO_DEL
fi
# CHECK instances in error state
nova list --all-tenants=1 | grep
if [ $? -eq 0 ]; then
echo "check instances in error state"
INSTANCE_NAME_IN_ERROR=`nova list --all-tenants=1 | grep deleting | awk '{print $4}'`
echo "$INSTANCE_NAME_IN_ERROR is in error state"
fi
# THE END
echo ""
exit 0