diff options
author | Herbert Duarte <herbertd@google.com> | 2014-06-02 13:34:48 -0700 |
---|---|---|
committer | Herbert Duarte <herbertd@google.com> | 2014-06-02 13:34:48 -0700 |
commit | 4ea8499fe23bf22296640332a228577fa71f4d05 (patch) | |
tree | a43fe1735897c26a3af3b6c9a5a9bceb1fb2f114 | |
parent | bdaae8640c4a11cfcb42e853bef7ebd5ae932e90 (diff) | |
parent | 9db2b21c10e5ba907aab258dc98ae3a253c5752d (diff) | |
download | google-compute-image-packages-4ea8499fe23bf22296640332a228577fa71f4d05.tar.gz |
Merge pull request #70 from jeremyje/master
Update get_metadata_value to use v1 metadata server url.
-rwxr-xr-x | google-startup-scripts/usr/share/google/get_metadata_value | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/google-startup-scripts/usr/share/google/get_metadata_value b/google-startup-scripts/usr/share/google/get_metadata_value index 14a80cd..99ebfa6 100755 --- a/google-startup-scripts/usr/share/google/get_metadata_value +++ b/google-startup-scripts/usr/share/google/get_metadata_value @@ -16,20 +16,45 @@ # Get a metadata value from the metadata server. declare -r VARNAME=$1 -declare -r MDS=http://169.254.169.254/0.1/meta-data +declare -r MDS_PREFIX=http://169.254.169.254/computeMetadata/v1 declare -r MDS_TRIES=${MDS_TRIES:-100} +function metadata_get_url_code() { + return $(curl "${1}" -H "Metadata-Flavor: Google" -s \ + -w "%{http_code}" -o /dev/null) +} + +function print_metadata_value() { + local readonly response=$(curl "${1}" -H "Metadata-Flavor: Google" -s) + local readonly return_code=$? + # If the command completed successfully, print the metadata value to stdout. + if [[ ${return_code} == 0 ]]; then + printf "${response}" + fi + return ${return_code} +} + +function print_metadata_value_if_exists() { + local readonly url=$1 + metadata_get_url_code ${url} + http_code=$? + # Test the instance metadata value. + if test ${http_code} -eq 200; then + print_metadata_value ${url} + return_code=$? + fi +} + function get_metadata_value() { local readonly varname=$1 - local readonly tmpfile=$(mktemp) - curl -f ${MDS}/${varname} > ${tmpfile} 2>/dev/null - local return_code=$? - if [[ ${return_code} == 0 ]]; then - cat ${tmpfile} - else - echo "curl for ${varname} returned ${return_code}" > /dev/console + # Print the instance metadata value. + print_metadata_value_if_exists ${MDS_PREFIX}/instance/${varname} + return_code=$? + # If the instance doesn't have the value, try the project. + if [[ ${return_code} != 0 ]]; then + print_metadata_value_if_exists ${MDS_PREFIX}/project/${varname} + return_code=$? fi - rm -f ${tmpfile} return ${return_code} } |