Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. force=0
  4.  
  5. function getInstanceIdByEnvTag {
  6. export instance_ids=$(aws ec2 describe-instances --filter Name=tag:Environment,Values=${environment} | jq '.Reservations[].Instances[].InstanceId' | tr -d '"')
  7.  
  8. echo "The following instances with Tag:${environment} will be stopped"
  9. for instance in ${instance_ids[@]};
  10. do
  11. echo ${instance}
  12. done
  13. }
  14.  
  15. function execInstancesByInstanceID {
  16. echo "executing: aws ec2 stop-instances --instance-ids ${instance_ids[@]}" |tr -s '\n' ' '
  17.  
  18. for instance in ${instance_ids[*]};
  19. do
  20. echo "Executing $1 on instance: ${instance}"
  21. aws ec2 $1-instances --instance-ids ${instance}
  22. done
  23. }
  24.  
  25. while getopts ":e:sf" opt; do
  26. case $opt in
  27. e)
  28. environment=$OPTARG
  29. ;;
  30. x)
  31. getInstanceIdByEnvTag;
  32. execInstancesByInstanceID "stop";
  33. ;;
  34. s)
  35. echo "starting nodes"
  36. getInstanceIdByEnvTag;
  37. execInstancesByInstanceID "start";
  38. ;;
  39. f)
  40. force=1
  41. ;;
  42. \?)
  43. echo "Invalid option: -$OPTARG" >&2
  44. exit 1
  45. ;;
  46. :)
  47. echo "Option -$OPTARG requires an argument." >&2
  48. exit 1
  49. ;;
  50. esac
  51. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement