From 26926f7268f67576124778091f8663618ffc93e9 Mon Sep 17 00:00:00 2001 From: Noah Meyerhans Date: Thu, 6 Apr 2023 21:37:42 -0700 Subject: [PATCH] Add --quiet command line option to suppress printing metadata keys This lets us extract individual fields from IMDS in a raw form that can be consumed directly by callers without requiring them to strip off the keys themselves. --- ec2-metadata | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/ec2-metadata b/ec2-metadata index 64f3c1f..0b3eb29 100755 --- a/ec2-metadata +++ b/ec2-metadata @@ -44,6 +44,7 @@ Options: METADATA_BASEURL="http://169.254.169.254" METADATA_TOKEN_PATH="latest/api/token" +QUIET="" function set_imds_token() { @@ -67,7 +68,7 @@ function get_meta() #print standard metric function print_normal_metric() { metric_path=$2 - echo -n $1": " + [ -z "$QUIET" ] && echo -n $1": " RESPONSE=$(get_meta ${metric_path}) if [ -n "${RESPONSE}" ]; then echo "$RESPONSE" @@ -79,11 +80,12 @@ function print_normal_metric() { #print block-device-mapping function print_block-device-mapping() { - echo 'block-device-mapping: ' + [ -z "$QUIET" ] && echo 'block-device-mapping: ' x=$(get_meta meta-data/block-device-mapping/) if [ -n "${x}" ]; then for i in $x; do - echo -e '\t' $i: "$(get_meta meta-data/block-device-mapping/$i)" + [ -z "$QUIET" ] && echo -ne '\t' "$i: " + echo "$(get_meta meta-data/block-device-mapping/$i)" done else echo not available @@ -93,17 +95,17 @@ function print_block-device-mapping() #print public-keys function print_public-keys() { - echo 'public-keys: ' + [ -z "$QUIET" ] && echo 'public-keys: ' x=$(get_meta meta-data/public-keys/) if [ -n "${x}" ]; then for i in $x; do index=$(echo $i|cut -d = -f 1) keyname=$(echo $i|cut -d = -f 2) - echo keyname:$keyname - echo index:$index + [ -z "$QUIET" ] && echo keyname:$keyname + [ -z "$QUIET" ] && echo index:$index format=$(get_meta meta-data/public-keys/$index/) - echo format:$format - echo 'key:(begins from next line)' + [ -z "$QUIET" ] && echo format:$format + [ -z "$QUIET" ] && echo 'key:(begins from next line)' echo "$(get_meta meta-data/public-keys/$index/$format)" done else @@ -114,11 +116,12 @@ function print_public-keys() #print tags function print_tags() { - echo 'tags: ' + [ -z "$QUIET" ] && echo 'tags: ' x=$(get_meta meta-data/tags/instance/) if [ -n "${x}" ]; then for i in $x; do - echo -e '\t' $i: "$(get_meta meta-data/tags/instance/$i)" + echo -n -e '\t' "$i: " + echo "$(get_meta meta-data/tags/instance/$i)" done else echo not available @@ -163,7 +166,7 @@ shortopts=almnbithokzPcpvuresdg longopts=(ami-id ami-launch-index ami-manifest-path ancestor-ami-ids block-device-mapping instance-id instance-type local-hostname local-ipv4 kernel-id availability-zone partition product-codes public-hostname public-ipv4 public-keys ramdisk-id - reservation-id security-groups user-data tags help all) + reservation-id security-groups user-data tags help all quiet) oldIFS="$IFS" IFS=, @@ -183,6 +186,9 @@ while true; do print_help ; shift exit 0 ;; + --quiet) + QUIET=1 ; shift + ;; --) shift ; break ;;