Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- force=0
- function getInstanceIdByEnvTag {
- export instance_ids=$(aws ec2 describe-instances --filter Name=tag:Environment,Values=${environment} | jq '.Reservations[].Instances[].InstanceId' | tr -d '"')
- echo "The following instances with Tag:${environment} will be stopped"
- for instance in ${instance_ids[@]};
- do
- echo ${instance}
- done
- }
- function execInstancesByInstanceID {
- echo "executing: aws ec2 stop-instances --instance-ids ${instance_ids[@]}" |tr -s '\n' ' '
- for instance in ${instance_ids[*]};
- do
- echo "Executing $1 on instance: ${instance}"
- aws ec2 $1-instances --instance-ids ${instance}
- done
- }
- while getopts ":e:sf" opt; do
- case $opt in
- e)
- environment=$OPTARG
- ;;
- x)
- getInstanceIdByEnvTag;
- execInstancesByInstanceID "stop";
- ;;
- s)
- echo "starting nodes"
- getInstanceIdByEnvTag;
- execInstancesByInstanceID "start";
- ;;
- f)
- force=1
- ;;
- \?)
- echo "Invalid option: -$OPTARG" >&2
- exit 1
- ;;
- :)
- echo "Option -$OPTARG requires an argument." >&2
- exit 1
- ;;
- esac
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement