From 5296be32ed682c78a9ba0eedab9fa1ffb815e5c9 Mon Sep 17 00:00:00 2001 From: Jerome Petazzoni Date: Wed, 3 May 2017 11:26:47 -0700 Subject: [PATCH] Handle untagged resources --- prepare-vms/scripts/aws.sh | 24 +++++++----------------- prepare-vms/scripts/cli.sh | 11 +---------- 2 files changed, 8 insertions(+), 27 deletions(-) diff --git a/prepare-vms/scripts/aws.sh b/prepare-vms/scripts/aws.sh index be68b881..452e7821 100755 --- a/prepare-vms/scripts/aws.sh +++ b/prepare-vms/scripts/aws.sh @@ -4,22 +4,12 @@ source scripts/cli.sh aws_display_tags(){ # Print all "Name" tags in our region with their instance count - echo "[#] [Status] [Tag]" | awk '{ printf " %7s %8s %10s \n", $1, $2, $3}' - aws ec2 describe-instances --filter "Name=tag:Name,Values=[*]" \ - --query "Reservations[*].Instances[*].[{Tags:Tags[0].Value,State:State.Name}]" \ - | awk '{ printf " %-13s %-10s %-1s\n", $1, $2, $3}' \ - | uniq -c \ - | sort -k 3 -} - -aws_display_tokens(){ - # Print all tokens in our region with their instance count - echo "[#] [Token] [Tag]" | awk '{ printf " %7s %12s %30s\n", $1, $2, $3}' - # --query 'Volumes[*].{ID:VolumeId,AZ:AvailabilityZone,Size:Size}' - aws ec2 describe-instances --output text \ - --query 'Reservations[*].Instances[*].{ClientToken:ClientToken,Tags:Tags[0].Value}' \ - | awk '{ printf " %7s %12s %50s\n", $1, $2, $3}' \ - | sort \ + echo "[#] [Status] [Token] [Tag]" \ + | awk '{ printf " %7s %-12s %-25s %-25s\n", $1, $2, $3, $4}' + aws ec2 describe-instances \ + --query "Reservations[*].Instances[*].[State.Name,ClientToken,Tags[0].Value]" \ + | tr -d "\r" \ + | awk '{ printf " %-12s %-25s %-25s\n", $1, $2, $3}' \ | uniq -c \ | sort -k 3 } @@ -70,7 +60,7 @@ aws_get_instance_ids_by_filter() { FILTER=$1 aws ec2 describe-instances --filters $FILTER \ --query Reservations[*].Instances[*].InstanceId \ - --output text | tr "\t" "\n" + --output text | tr "\t" "\n" | tr -d "\r" } diff --git a/prepare-vms/scripts/cli.sh b/prepare-vms/scripts/cli.sh index 8283c73d..34dd599c 100755 --- a/prepare-vms/scripts/cli.sh +++ b/prepare-vms/scripts/cli.sh @@ -10,21 +10,12 @@ die () { need_tag(){ TAG=$1 if [ -z "$TAG" ]; then - echo "Please specify a tag. Here's the list: " + echo "Please specify a tag or token. Here's the list: " aws_display_tags die fi } -need_token(){ - TOKEN=$1 - if [ -z "$TOKEN" ]; then - echo "Please specify a token. Here's the list: " - aws_display_tokens - die - fi -} - need_ips_file() { IPS_FILE=$1 if [ -z "$IPS_FILE" ]; then