diff --git a/src/aws/dependencies/utilitiyFunction.sh b/src/aws/dependencies/utilitiyFunction.sh index 702f4a77..2a003533 100644 --- a/src/aws/dependencies/utilitiyFunction.sh +++ b/src/aws/dependencies/utilitiyFunction.sh @@ -43,3 +43,9 @@ function makePortArray { echo "${portArray[@]}" } + +function getState(){ + local instanceId=$1 + local state=$(aws ec2 describe-instances --instance-ids $instanceId | jq '.Reservations[].Instances[].State.Name') + echo $state +} diff --git a/src/aws/down.sh b/src/aws/down.sh index ce8dcac0..31a4f0c5 100755 --- a/src/aws/down.sh +++ b/src/aws/down.sh @@ -15,8 +15,8 @@ function down(){ instanceId=$(getValueByAttribute $balloonName instanceId) if [ "$instanceId" = "null" ]; then - echo "$balloonName is already deleted" - exit 1 + echo "$balloonName does not exist" + exit 0 fi keyName=$(getValueByAttribute $balloonName key) diff --git a/src/aws/start.sh b/src/aws/start.sh index c9036ce5..76cec03f 100644 --- a/src/aws/start.sh +++ b/src/aws/start.sh @@ -14,12 +14,24 @@ function start(){ instanceId=$(getValueByAttribute $balloonName instanceId) if [ "$instanceId" = "null" ]; then - echo "$balloonName is already deleted" + echo "$balloonName does not exist" exit 1 fi oldPublicIp=$(getValueByAttribute $balloonName publicIp) + state=$(getState $instanceId) + + if [ "$state" == "\"running\"" ]; then + echo "The instance is already running" + exit 0 + fi + + if [ "$state" == "\"stopping\"" ]; then + echo "The instance is stopping" + exit 1 + fi + aws ec2 start-instances --instance-ids $instanceId echo "get the new ip address. The procedure might take time for a while" diff --git a/src/aws/stop.sh b/src/aws/stop.sh index 96ca37ac..240e2bd3 100644 --- a/src/aws/stop.sh +++ b/src/aws/stop.sh @@ -13,10 +13,16 @@ function stop(){ instanceId=$(getValueByAttribute $balloonName instanceId) if [ "$instanceId" = "null" ]; then - echo "$balloonName is already deleted" + echo "$balloonName does not exist" exit 1 fi + state=$(getState $instanceId) + if [[ "$state" == "\"stopped\"" || "$state" == "\"stopping\"" ]]; then + echo "The instance is already stopped" + exit 0 + fi + groupName=$(getValueByAttribute $balloonName groupName) storePortArrayString $groupName tcp $balloonName storePortArrayString $groupName udp $balloonName