forked from arainho/openstack-manage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recover-instance
executable file
·112 lines (83 loc) · 2.1 KB
/
recover-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
#!/bin/bash
# script to check and recover instances state
# VARs
INSTANCE_NAME=""
INSTANCE_ID=""
INSTANCE_IPv4=""
INSTANCE_NAME_TO_RECOVER=""
INSTANCE_ID_TO_RECOVER=""
INSTANCE_NAME_IN_ERROR=""
PORT_ID=""
EMPTY_PORTS="false"
ANSWER=""
ANSWER_RECOVER=""
Status=""
PowerState=""
TaskState=""
# GET name
echo 'enter instance name'
read INSTANCE_NAME
# CHECK if exists
nova list | grep $INSTANCE_NAME
# > /dev/null 2>&1
# GET instance id
INSTANCE_ID=`nova list --all-tenants=1 | grep $INSTANCE_NAME | awk '{print $2}'`
# GET ipv4
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"
# RECOVER INSTANCE
echo ""
echo "recover instance $INSTANCE_NAME, id=$INSTANCE_ID ? ( yes/no )"
read ANSWER_RECOVER
# recover instance if "yes"
if [[ $ANSWER_RECOVER == "yes" ]]; then
echo "reset-state $INSTANCE_ID"
nova reset-state $INSTANCE_ID
nova reset-state --active $INSTANCE_ID
echo "stop instance $INSTANCE_ID"
nova stop $INSTANCE_ID
echo "stop instance $INSTANCE_ID"
nova start $INSTANCE_ID
else
echo ""
echo "exit"
exit 0
fi
# CHECK IF instance is UP
Status="ACTIVE"
PowerState="running"
TaskState="-"
nova list | grep $INSTANCE_NAME | grep $Status | grep $PowerState | grep $TaskState
if [ $? -eq 0 ]; then
echo "instance UP :-)"
fi
if [ $? -eq 1 ]; then
echo "Not UP ... or Other State!!!"
fi
# CHECK instances in some error state
PowerState="Shutdown"
#TaskState="deleting"
nova list --all-tenants=1 | grep ERROR
if [ $? -eq 0 ]; then
echo "check instances in error state"
INSTANCE_NAME_IN_ERROR=$(nova list --all-tenants=1 | grep $INSTANCE_ID | awk '{print $4}')
for instance in $INSTANCE_NAME_IN_ERROR; do
echo "$INSTANCE_NAME_IN_ERROR is in error state"
done
fi
# THE END
echo ""
exit 0