Skip to content

Commit

Permalink
Add --quiet command line option to suppress printing metadata keys
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Noah Meyerhans committed Apr 7, 2023
1 parent b09801b commit 26926f7
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions ec2-metadata
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Options:

METADATA_BASEURL="http://169.254.169.254"
METADATA_TOKEN_PATH="latest/api/token"
QUIET=""

function set_imds_token()
{
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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=,
Expand All @@ -183,6 +186,9 @@ while true; do
print_help ; shift
exit 0
;;
--quiet)
QUIET=1 ; shift
;;
--)
shift ; break
;;
Expand Down

0 comments on commit 26926f7

Please sign in to comment.