-
Notifications
You must be signed in to change notification settings - Fork 0
/
stopstart_dikapedia.sh
executable file
·91 lines (67 loc) · 2.18 KB
/
stopstart_dikapedia.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
#!/bin/bash
#
#
# Author: Ardika Sulistija
#
# Description: This script checks the status of Dikapedia instances and prompts you if you want to stop/start/reboot the instance.
#
# 07/03/2022
check_state(){
instance_state=$(aws ec2 describe-instances --instance-ids $instance_id --query 'Reservations[].Instances[].State[].Name[]' --output=text)
echo "Instance state: $instance_state"
}
check_status_checks(){
instance_status=$(aws ec2 describe-instance-status --instance-ids $instance_id --query 'InstanceStatuses[].InstanceStatus[].Status[]' --output=text)
echo "Instance Status: $instance_status"
system_status=$(aws ec2 describe-instance-status --instance-ids $instance_id --query 'InstanceStatuses[].SystemStatus[].Status[]' --output=text)
echo "System Status: $system_status"
}
start_action(){
echo "Starting instance now..."
aws ec2 start-instances --instance-ids $instance_id --output=text
echo "Instance Started!"
}
stop_action(){
echo "Stopping instance now..."
aws ec2 stop-instances --instance-ids $instance_id --output=text
echo "Instance stopped!"
}
reboot_action(){
echo "Rebooting instance now..."
aws ec2 reboot-instances --instance-ids $instance_id --output=text
echo "Instance rebooted!"
}
reboot_stop_exit(){
read -p "Do you want to (r)eboot or (s)top the instance? Press (x) to exit: " answer
if [[ $answer == "s" ]]; then
stop_action
elif [[ $answer == "r" ]]; then
reboot_action
elif [[ $answer == "x" ]]; then
echo "Good-bye!"
exit
fi
}
instance_id=i-0dcecd45293806bb7
echo "Checking the status of instance $instance_id..."
check_state
if [[ $instance_state == "stopped" ]]; then
read -p "Do you want to START the instance? (y/n): " start_instance
if [[ $start_instance == "y" ]]; then
start_action
elif [[ $start_instance == "n" ]]; then
echo "Good-bye!"
fi
elif [[ $instance_state == "running" ]]; then
check_status_checks
if [[ $instance_status == "ok" ]]; then
echo "Instance is healthy."
reboot_stop_exit
elif [[ $instance_status == "initializing" ]]; then
echo "Instance is still initializing..."
reboot_stop_exit
elif [[ $instance_status == "impaired" ]]; then
read -p "Instance is failing 1/2."
reboot_stop_exit
fi
fi