Skip to content

Instantly share code, notes, and snippets.

@keymon
Last active September 22, 2015 09:53
Show Gist options
  • Select an option

  • Save keymon/f4b8aad0e5ccca93e243 to your computer and use it in GitHub Desktop.

Select an option

Save keymon/f4b8aad0e5ccca93e243 to your computer and use it in GitHub Desktop.

IaaS start/stop and other useful scripts

Useful scripts to manage VMs on a IaaS: AWS, GCE, etc... Stop all vms, start them, etc..

aws_stop_vms_by_vpc.sh

Choose between the existing VPCs in a given region and stop all the VMs running there:

aws_stop_vms_by_vpc.sh [region]

Requirements: aws-cli and credentials in environment variables

gce_stop_vms_by_network.sh

Choose between existing networks and stop all the VMs running there

Requirements: gcloud configured with the desired account.

#!/bin/bash
#
# Select one VPC in a given region and stop all the VMs on it.
#
#
get_vpcs() {
local region=$1; shift
aws --region $region ec2 describe-vpcs
}
extract_vpcs_ids() {
jq -r '.Vpcs | map(.VpcId)'
}
extract_vpcs_ids_and_tags() {
jq -r '.Vpcs | map( [.VpcId, (.Tags | map("\(.Key)=\(.Value)") | join(";")) ] | join (", ") ) | flatten | join ("\n")'
}
ec2_describe_instances_by_vpcid() {
local region=$1; shift
local vpcid=$1; shift
local state=$1; shift
aws --region $region \
ec2 describe-instances \
--filters \
Name=vpc-id,Values=$vpcid \
${state:+Name=instance-state-name,Values=$state}
}
extract_vm_ids() {
jq -r '.Reservations | map(.Instances | map(.InstanceId)) | flatten | join ("\n")'
}
extract_vm_ids_and_tags() {
jq -r '.Reservations | map(.Instances | map( [.InstanceId, (.Tags | map("\(.Key)=\(.Value)") | join(";")) ] | join (", ") ) ) | flatten | join ("\n")'
}
stop_instances() {
local region=$1; shift
aws --region $region \
ec2 stop-instances \
--instance-ids $@ | \
jq -r '.StoppingInstances | map("\(.InstanceId): \(.PreviousState.Name) => \(.CurrentState.Name)") | join("\n")'
}
region=${1:-eu-west-1}
# Chose one VPC
OLD_IFS=$IFS; IFS=$'\n'
select vpc in $(get_vpcs $region | extract_vpcs_ids_and_tags); do
selected_vpc=${vpc%%,*}
[ "$selected_vpc" ] && break
done
IFS=$OLD_IFS
instances_description=$(ec2_describe_instances_by_vpcid $region $selected_vpc running)
cat <<EOF
About to stop these VMs:
$(echo $instances_description | extract_vm_ids_and_tags)
EOF
read -p "Are you sure? [y/N] " -n 1 -r; echo
[[ ! $REPLY =~ ^[Yy]$ ]] && exit 1
stop_instances $region $(echo $instances_description | extract_vm_ids)
#!/bin/bash
gcloud_list_instances_json() {
gcloud compute instances list --format json
}
gcloud_stop_instances() {
gcloud compute instances stop $1 --zone $2
}
extract_networks_and_num_vms() {
jq -r '. | map(.networkInterfaces | map(.network) | join(",")) | join("\n")' | sort | uniq -c
}
extract_instances_by_network() {
network_name=$1; shift
jq -r ". |
map(select( .networkInterfaces |
map(select(.network == \"$network_name\")) | length >=1)) |
map(\"\\(.name) \\(.zone) \\(.networkInterfaces | map(.network) | join(\",\"))\") | join(\"\n\")"
}
instances_json=$(gcloud_list_instances_json)
echo "Chose one network to stop all vms (#vms network_name):"
OLD_IFS=$IFS; IFS=$'\n'
select network in $(echo $instances_json | extract_networks_and_num_vms); do
selected_network=${network##* }
[ "$selected_network" ] && break
done
IFS=$OLD_IFS
cat <<EOF
About to stop these VMs:
$(echo $instances_json | extract_instances_by_network $selected_network)
EOF
read -p "Are you sure? [y/N] " -n 1 -r; echo
[[ ! $REPLY =~ ^[Yy]$ ]] && exit 1
OLD_IFS=$IFS; IFS=$'\n'
for line in $(echo $instances_json | extract_instances_by_network $selected_network | awk '{print $1 " " $2}'); do
IFS=$OLD_IFS
gcloud_stop_instances $line
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment